/***********************************************************************
 *                                                                     *
 * Copyright (C) 2003-2009 iWebGate Technology Ltd & Charlie Gargett   *
 * Author: Charlie Gargett                                             *
 *                                                                     *
 ***********************************************************************
 *                                                                     *
 * This software  product and related  source code is  licenced by the *
 * owner and copyright holder,  iWebGate Technology Ltd, Perth Western *
 * Australia 6000, AUSTRALIA.                                          *
 *                                                                     *
 * All intellectual property rights  in this software product and user *
 * documentation are owned by  iWebGate Technology Ltd,  PERTH Western *
 * Australia  6000,   AUSTRALIA,   and  are  protected  by  applicable *
 * Australian Copyright laws and international treaty provisions.  The *
 * owner retains all rights not expressly granted.                     *
 *                                                                     *
 * All javascript source code exposed by this and related websites via *
 * direct  HTML  embedding,  in-direct  linking  through  reference to *
 * external  source  files  or  by  any  other  means  of  exposure of *
 * javascript  routines made by this software product may  be used for *
 * the  explicit purpose of research into  the development  of similar *
 * routines but may not be copied, modified, disassembled, decompiled, *
 * reverse  engineered, transfered as  is or as a copy,  modification, *
 * merged portion,  in whole or in part, except as expressly  provided *
 * for by the owner or copyright holder in writing.                    *
 *                                                                     *
 ***********************************************************************/

function iwgMenu() {
    this.marry = marry;
    this.menu_init = menu_init;
    this.show_menu = show_menu;
    this.sched_hide_menu = sched_hide_menu;
    this.set_hilight_color = set_hilight_color;
    this.set_menu_speed = set_menu_speed;

    var menus = []
    var hilight_color = "#555555"
    var speed = 10
    var hovering = null
    var hide_delay = 100

// Public functions
    function marry(oHeadT, oMenuT, options) {
        var oHead = document.getElementById(oHeadT)
        var oMenu = document.getElementById(oMenuT)
        var m = new Object()
        
        anchor = options['anchor']
        direction = options['direction']
        mparent = options['mparent']
        offsetx = parseInt(options['offsetx'])
        offsety = parseInt(options['offsety'])
        if(isNaN(offsetx)) offsetx = 0
        if(isNaN(offsety)) offsety = 0

        if(!anchor || anchor == '') anchor = 'bl'
        if(!direction || direction == '') direction = 'down'

        m.pos = new Object()
        m.dim = new Object()
        m.menu = null
        m.head = null
        m.hover = false
        m.direc = direction
        m.anchor = anchor
        m.offsetx = offsetx
        m.offsety = offsety

        m.dim.w = parseInt(oMenu.style.width)
        m.dim.h = parseInt(oMenu.style.height)
        m.menu = oMenu
        m.head = oHead

        if (mparent) {
            mparent = document.getElementById(mparent)
            if (mparent) m.menu.mparent = mparent
        }

        addListener(m.menu,'mouseover',function() {m.menu.hover = true;hovering = m;})
        addListener(m.menu,'mouseout',function() {m.menu.hover = false;hovering = null;})
        addListener(m.head,'mouseover',function() {show_menu(m)})
        addListener(m.head,'mouseout',function() {m.menu.hover = false;hovering = null})
        m.menu.style.cursor = "default"

        assign_hilight_listener(m.menu)

        menus[menus.length] = m
    }

    function assign_hilight_listener(menu) {
        for (var i = 0; i < menu.childNodes.length; i++) {
            cn = menu.childNodes[i]
            if (cn.nodeName == 'DIV') {
//                if (cn.getAttribute && cn.getAttribute('nohilight')) continue;
//                if (!cn.style) continue;

                if (!(cn.getAttribute && cn.getAttribute('nohilight'))) {
                    cn.style.cursor = "pointer"
                    addListener(cn,'mouseover',function(event) {menuitem_hilight(event)})
                    addListener(cn,'mouseout',function(event) {menuitem_hilight(event)})
                }
                assign_hilight_listener(cn)
            }
        }
    }

    function menu_init() {
        for (var i = 0; i < menus.length; i++) {
            m = menus[i]

            pos = findPagePosition(m.head)
            m.pos.x = parseInt(pos.x)
            m.pos.y = parseInt(pos.y)

            m.menu.style.display = 'none'
        }
        window.setTimeout(sched_hide_menu, 100)
    }

    function show_menu(m) {
        var pos = findPagePosition(m.head)

        m.pos.x = parseInt(pos.x)
        m.pos.y = parseInt(pos.y)
        switch(m.anchor) {
            case 'tl':
                m.pos.x += m.offsetx
                m.pos.y += m.offsety
                break;
            case 'bl':
                m.pos.x += m.offsetx
                m.pos.y += parseInt((m.head.offsetHeight)-m.offsety)
                break;
            case 'tr':
                m.pos.x += parseInt((m.head.offsetWidth)-m.offsetx)
                m.pos.y += m.offsety
                break;
            case 'br':
                m.pos.x += parseInt((m.head.offsetWidth)-m.offsetx)
                m.pos.y += parseInt((m.head.offsetHeight)-m.offsety)
                break;
        }

        var an = new Anime()
        var options = {
	        'direction':m.direc,
	        'speed':speed,
	        'delay':10,
	        'screenx':m.pos.x,
	        'screeny':m.pos.y,
	        'origwidth':m.dim.w,
	        'origheight':m.dim.h
        }

        // TODO: Find out why menu track hilighting does not work...
        if (m.menu.mparent) {
            if (hovering) hovering.head.style.background = hilight_color
        }
        m.menu.hover = true
        if (m.menu.style.display == 'none' && !m.menu.working)
            an.rollout(m.menu, options)
    }
    
    function sched_hide_menu() {
        for (var i = 0; i < menus.length; i++) {
            m = menus[i]
            if (!m.menu.hover) {
                if (!isMenuParent(m.menu))
                    hide_menu(m)
            }
        }
        window.setTimeout(sched_hide_menu, hide_delay)
    }
    
    function isMenuParent(m) {
        if (hovering) {
	        if (!hovering.menu.mparent) return false
	        if (m.id == hovering.menu.mparent.id) return true
	    }
        return false
    }

    function set_hilight_color(color) {
        hilight_color = color
    }

    function set_menu_speed(s) {
        speed = s
    }

// Private functions
	function hide_menu(m) {
	    var an = new Anime()
        var direc = "up"

        switch(m.direc) {
            case 'up':
                direc = "down"
                break
            case 'down':
                direc = "up"
                break;
            case 'left':
                direc = "right"
                break;
            case 'right':
                direc = "left"
                break;
        }

	    var options = {
		    'direction':direc,
		    'speed':speed,
		    'delay':10
	    }
	    if (m.menu.style.display != 'none' && !m.menu.working)
	       an.rollin(m.menu, options)
	}

    function menuitem_hilight(e) {
        if (navigator.appVersion.indexOf('MSIE') != -1) {
            var sObj = event.srcElement
            if (sObj.style.background != hilight_color) sObj.style.background = hilight_color
            else sObj.style.background = ''
        } else {
            var sObj = e.target
	        if (sObj.style.background == '') sObj.style.background = hilight_color
	        else sObj.style.background = ''
        }

    }
}

