﻿
var oShape;
var oLnkSel;
var oShapeLink;
var iFinalLeft = 0;
var oTimer = null;
var oCurrentMenuItem = null;
var oDelay = null;
var iDirection = 0;
var iMaxLeft = (window.screen.availWidth - 110);
var iMinLeft = getCookie('left');


function Highlight(oMenuItem) {
    oCurrentMenuItem = oMenuItem;
    
    var aoMargins = oCurrentMenuItem.getElementsByTagName("span");
    aoMargins[0].className = "HPMenuBoxSelectedLeft";
    aoMargins[1].className = "HPMenuBoxSelectedRight";
    
    var aoLinks = oCurrentMenuItem.getElementsByTagName("a");
    aoLinks[0].className = "HPMenuBoxSelected";
}

function UnHighlight(oMenuItem) {
    var aoMargins = oMenuItem.getElementsByTagName("span");
    aoMargins[0].className = "HPMenuBoxMarginLeft";
    aoMargins[1].className = "HPMenuBoxMarginRight";
    
    var aoLinks = oMenuItem.getElementsByTagName("a");
    aoLinks[0].className = "HPMenuBoxItem";
    
    //oCurrentMenuItem = null;
    
}

function DoMenuItemOnMouseDown(oMenuItem) {
    SetMenuShapeOnLoad();
    if(oCurrentMenuItem != null) {
        UnHighlight(oCurrentMenuItem);
    }
    oCurrentMenuItem = oMenuItem;
    SetMenuShape(oMenuItem);
    
    //save position
    //deleteCookie('left');
    setCookie('left', getPositionLeft(oMenuItem), 1);
}

function SetMenuShapeOnLoad() {
    oShape = document.getElementById("lnkMenuShape");
    oLnkSel = document.getElementById("linkMenuSelected");
    oCurrentMenuItem = oLnkSel;
    
    if(iMinLeft == 0)
        iMinLeft = getPositionLeft(document.getElementById("ulHPMenuLinks"));
    
    if(iMinLeft == 0)
        iMinLeft = 309;
        
    if(iMaxLeft == 0) 
        iMaxLeft = iMinLeft + 700;
        
    oShapeLink = document.getElementById("lnkMenuShapeAnchor");
    var iShapeLeft = getPositionLeft(oLnkSel);
    oShape.style.left = iShapeLeft + "px";
}

function SetMenuShape(oHoverShape) {

    oShape = document.getElementById("lnkMenuShape");
    
    /*if(oHoverShape == null) {
        oLnkSel = document.getElementById("linkMenuSelected");
    }
    else {*/
        oLnkSel = oHoverShape;
    //}
    if(iMinLeft == 0)
        iMinLeft = getPositionLeft(document.getElementById("ulHPMenuLinks"));

    
    if(iMinLeft == 0)
        iMinLeft = 309;
        
    if(iMaxLeft == 0) 
        iMaxLeft = iMinLeft + 700;
        
    oShapeLink = document.getElementById("lnkMenuShapeAnchor");
    iFinalLeft = getPositionLeft(oLnkSel);

    if(iFinalLeft == 0) {
        iFinalLeft = 309;
    }
    
    if(iFinalLeft < parseInt(oShape.style.left)) {
        iDirection = 1;
        //oShape.style.left = iMaxLeft + "px";
        oShape.style.display = "inline";
    }
    else {
        iDirection = 0;
        //oShape.style.left = iMinLeft + "px";
        oShape.style.display = "inline";
    }

    MoveMenuShape();
    
}


function MoveMenuShape() {

    var iLeft = parseInt(oShape.style.left);
    
    if(iDirection == 0) {
        if(iLeft < iFinalLeft) {
            if(iFinalLeft - iLeft <= 50) {
                oShape.style.left = (iLeft + 4) + "px";
            }
            else {
                oShape.style.left = (iLeft + 8) + "px";
            }
            oTimer = setTimeout(MoveMenuShape, 1);
        }
        else {
            Highlight(oCurrentMenuItem);
            oShape.style.display = "none";
            clearTimeout(oTimer);
            oTimer = null;
            window.location.href = oCurrentMenuItem.getElementsByTagName("a")[0].getAttribute("hrefToGo");
        }
    }
    else if(iDirection == 1) {
        if(iLeft > iFinalLeft) {
            if(iLeft - iFinalLeft <= 50) {
                oShape.style.left = (iLeft - 4) + "px";
            }
            else {
                oShape.style.left = (iLeft - 8) + "px";
            }
            oTimer = setTimeout(MoveMenuShape, 1);
        }
        else {
            Highlight(oCurrentMenuItem);
            oShape.style.display = "none";
            clearTimeout(oTimer);
            oTimer = null;
            window.location.href = oCurrentMenuItem.getElementsByTagName("a")[0].getAttribute("hrefToGo");
        }
    }
    
}

function getPositionLeft(This){
    var el = This;
    var pL = 0;
    while(el){
        pL+=el.offsetLeft;
        el=el.offsetParent;
    }
    return pL;
}


    
function setCookie(c_name,value,expiredays)
{
    deleteCookie(c_name);
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+
    ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name)
{
    if (document.cookie.length>0)
      {
      c_start=document.cookie.indexOf(c_name + "=");
      if (c_start!=-1)
        {
        c_start=c_start + c_name.length+1;
        c_end=document.cookie.indexOf(";",c_start);
        if (c_end==-1) c_end=document.cookie.length;
        return unescape(document.cookie.substring(c_start,c_end));
        }
      }
    return '';
}

function deleteCookie(c_name)
{
    document.cookie=c_name+ "=;expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
