﻿/// <reference path="jquery-1.3.2-vsdoc2.js" />

var isMasterPage=false;

/*
var counter = 0;
function wDebug(value) {
    counter++;
    var dbg = $("#dbgwin").html();
    $("#dbgwin").html(counter +". "+ value +"<br />"+ dbg);
    dbg = null;
    value=null;
}
*/

// ###################################################################
// ###################################################################
var gMenuArr;

function addArrData(value) {
    if(value != undefined) {
        var addValue = true;
        for(var x=0; x < gMenuArr.length; x++) {
            if(gMenuArr[x] == value) {
                addValue = false;
                break;
            }
        }
        
        if(addValue) {
            gMenuArr[gMenuArr.length] = value;
        }
    }
    
    //wDebug("add - arr length: " + gMenuArr.length);
}

function hasArrData(value) {
    var hasValue = false;
    if(value != undefined) {
        for(var x=0; x < gMenuArr.length; x++) {
            if(gMenuArr[x] == value) {
                hasValue = true;
                break;
            }
        }
    }
    return hasValue;
}

function remArrData(value) {
    if(value != undefined) {
        for(var x=0; x < gMenuArr.length; x++) {
            if(gMenuArr[x] == value) {
                gMenuArr.splice(x,1);
                //wDebug("remove - arr length: " + gMenuArr.length);
                break;
            }
        }
    }
}
// ###################################################################
// ###################################################################

var timer;
var activeId;


function findPosition(oElement) {
    if(typeof(oElement.offsetParent) != 'undefined') {
        for(var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent) {
            posX += oElement.offsetLeft;
            posY += oElement.offsetTop;
        }
        return {left:posX, top:posY};
    }
    else {
        return {left:oElement.x, top:oElement.y};
    }
}


function findWidth(oElement) {
    if( typeof( oElement.offsetParent ) != 'undefined' ) {
        return oElement.offsetWidth;
    }
    else {
        return oElement.width;
    }
}


function sectionLeft() {
    if($("#content_outline").length > 0) {
        return findPosition($("#content_outline")[0]).left;
    }
    else {
        return 0;
    }
}


function getPartialUuid() {
    return(((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
function getNewId() {
    return (getPartialUuid() +"-"+ getPartialUuid() + "-" + getPartialUuid());
}


function getMenuId(obj) {
    var eid = "";
    if($(obj).attr("id")) {
        eid = $(obj).attr("id");
    }
    else {
        eid = getNewId();
        $(obj).attr("id", eid);
        //wDebug("New id: " + eid);
    }
    return eid;
}


function cursorOver() {
    var $liParent = $(this).parent();
    var $submenu = $(this).parent().children('.submenu');
    var eid = getMenuId($submenu);
    
    if(!hasArrData(eid)) {
    
        clearTimeout(timer);
        
        if(activeId != undefined) {
          if(activeId != eid) {
            killMenu(activeId);
          }
        }
        activeId = eid;
        
        var bodyWidth = findWidth(document.body);
        
        var baseLeft = sectionLeft();
        var topmenuLeft = findPosition($(".jTopMenu")[0]).left;
        
        //wDebug("jTopMenu.left:" + topmenuLeft + "; content_outline:" + baseLeft);
        
        
        var elemWidth = findWidth(this);
        var sectionWidth=0;
        if($(".jTopMenu").length > 0) {
            sectionWidth = findWidth($(".jTopMenu")[0]);
        }
        
        var textWidth = findWidth(this);
        var textPos = findPosition(this);
        
        //wDebug("textWidth: " + textWidth + "; textPos.left: " + textPos.left);
        
        
        var leftPos = textPos.left;
        var topPos=0;
        
        topPos = (textPos.top + 20);
        
        var menuWidth=0;
        
        if($("#"+eid).length > 0) {
          menuWidth = $("#"+eid).width();
          //menuWidth = findWidth($("#"+eid)[0]);
        }
        
        //wDebug("text Left: " + leftPos + ", elem width: " + $liParent.width());
        
        var txtItemLeft = leftPos;
        var midArrowPos = (($liParent.children(".hassubmenu").width()/2)-6);
        
        if( (leftPos + menuWidth) > bodyWidth ) {
            var posMarginLeft = 0;
        
            sideWidth = ((bodyWidth - sectionWidth) / 2);
            leftPos = ((sideWidth + sectionWidth) - menuWidth);
        }
        
        midArrowPos = ((txtItemLeft - leftPos) + midArrowPos);
        midArrowPos = midArrowPos + 10; //small alignment fix for arrow
        
        //wDebug("mid arrow pos: " + midArrowPos);
        
        var $arrow = jQuery(".toparrow", $(this).parent().children('.submenu'));
        $arrow.css("margin-left", midArrowPos + "px");
        $arrow=null;
        
        // BIND EVENTS
        $("#"+eid).bind("mouseover", killTimer);
        $("#"+eid).bind("mouseout", cursorOut);
        
        
        leftPos = (leftPos - baseLeft) - 10; // 10 = padding-left
        
        if( (isIE6() || isIE7()) && isMasterPage) {
            topPos = topPos - 85; // 125 Special for IE6
            leftPos = leftPos - 34; // 20
        }
        
        $("#"+eid).css("display","inline").css("left", leftPos + "px").css("top", (topPos) + "px");
        
        //wDebug("#" + eid + " - left:" + leftPos + "px; top:" + topPos +"px;");
        //wDebug("Body: " + bodyWidth + ", MenuWidth: " + menuWidth);
        
        addArrData(eid);
        
        
        bodyWidth=null;
        baseLeft=null;
        elemWidth=null;
        sectionWidth=null;
    }
    else {
        if(activeId == eid) {
            killTimer();
        }
        //wDebug(eid + " - already active in array.");
    }
    
    eid=null;
    $submenu=null;
}


function killTimer() {
    if(timer != undefined) {
        window.clearTimeout(timer);
    }
}


function killMenu(eid) {
    if(hasArrData(eid)) {
        $("#" + eid).css("display", "none");
        remArrData(eid);
    }
    
    eid=null;
}


function cursorOut() {
    var $submenu = $(this).parent().children('.submenu');
    var eid = getMenuId($submenu);
    
    timer = window.setTimeout("killMenu('" + eid + "')",500);
    
    eid=null;
    $submenu=null;
}


function initjTopMenu(ismasterpage) {
    if($(".jTopMenu").length > 0) {
        gMenuArr = new Array();
        
        if(ismasterpage != undefined) {
            if(ismasterpage) {
                isMasterPage = true;
            }
        }
        
        $(".jTopMenu a.hassubmenu").each(function() {
            $(this).bind("mouseover", cursorOver);
            $(this).bind("mouseout", cursorOut);
        });
        
        $(window).bind('resize', function() {
            killMenu(activeId);
        });
    }
}

