/***********************************************************************
 *                                                                     *
 * 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

// Public functions
    function marry(oHeadT, oMenuT, offset, direction) {
        var oHead = document.getElementById(oHeadT)
        var oMenu = document.getElementById(oMenuT)
        var m = new Object()

        m.pos = new Object()
        m.dim = new Object()
        m.menu = null
        m.head = null
        m.hover = false
        m.direc = direction
        m.offset = offset

        m.dim.w = parseInt(oMenu.style.width)
        m.dim.h = parseInt(oMenu.style.height)
        m.menu = oMenu
        m.head = oHead

        m.menu.onmouseover = function() {m.menu.hover = true}
        m.menu.onmouseout = function() {m.menu.hover = false}
        m.head.onmouseover = function() {show_menu(m)}
        m.head.onmouseout = function() {m.menu.hover = false}
        m.menu.style.cursor = "default"
        m.menu.count = 0

        for (var i = 0; i < m.menu.childNodes.length; i++) {
            cn = m.menu.childNodes[i]
            if (cn.nodeName == 'DIV') {
                cn.style.cursor = "pointer"
                cn.onmouseover = function(event) {menuitem_hilight(event)}
                cn.onmouseout = function(event) {menuitem_hilight(event)}
                m.menu.count++
            }
        }

        menus[menus.length] = m
    }

    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.head.offsetHeight)-1)

            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.offset) {
            case 'tl':
                break;
            case 'bl':
                m.pos.y += parseInt((m.head.offsetHeight)-1)
                break;
            case 'tr':
                m.pos.x += parseInt((m.head.offsetWidth)-1)
                break;
            case 'br':
                m.pos.x += parseInt((m.head.offsetWidth)-1)
                m.pos.y += parseInt((m.head.offsetHeight)-1)
                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
        }

        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) hide_menu(m)
        }
        window.setTimeout(sched_hide_menu, 100)
    }

    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 = ''
        }

    }
}
