// JavaScript Geoscape Util MenuBar
// Anand J.
// Geoscape
// March 2009

var Geoscape = Geoscape ? Geoscape : {
    GIS : {}
};

Geoscape.GIS.Util = Geoscape.GIS.Util ? Geoscape.GIS.Util : function()
{
    var Anim = 0;
    
    var AlphaAcc = 0;
    
    var BlindsDivID = "Blinds";
    
    var ModalDivID = "ModalDialog";
    
    var ModalDivOKID = "ModalOkButton";
    
    var ModalBtnDiv = "ModalBtnDiv";
    
    var ModalDivHTML = "<table class='Wnd' style='width: auto'><tr><td class='WndLTC'></td><td class='WndT'></td><td class='WndRTC'></td></tr><tr class='WndHdr'><td class='WndL'></td><td class='WndMain'><div class='Title'><span id='ModalDlgTitle'></span></div></td><td class='WndR'></td></tr><tr class='WndBody'><td class='WndL'></td><td class='WndMain'><div id='ModalDlgContent'></div><input id='" + ModalDivOKID + "' style='margin-top:20px; float:right' type='button' class='Button' value='Ok' /></td><td class='WndR'></td></tr><tr><td class='WndLBC'></td><td class='WndB'></td><td class='WndRBC'></td></tr></table>";
    
    function hideOther(frame)
    {
        var otherFrame = frame.parentNode.firstChild;
        
        while (null != otherFrame)
        {
            if ((frame != otherFrame) && (null != otherFrame.style))
            { 
                otherFrame.style.visibility = "hidden";
                otherFrame.style.display = "none";
            }
            
            otherFrame = otherFrame.nextSibling;
        }
    }
    
    function getCookie(name)
    {
        var cookies = document.cookie;
        
        if (cookies.indexOf(name) != -1)
        {
            var startpos = cookies.indexOf(name) + name.length + 1;
            
            var endpos = cookies.indexOf(";", startpos);

	    if (startpos == endpos) return null;
            
            if (endpos < 0) endpos = cookies.length;
            
            return cookies.substring(startpos, endpos);
        }
        
        else return null;
    }

    function fadeIn(obj, alpha)
    {
        alpha = alpha + Math.round(AlphaAcc);
        
        AlphaAcc = AlphaAcc + 0.5;
        
        if (alpha > 70) return;
        
        obj.style.filter = "alpha(opacity=" + alpha + ");";
        
        var alphaFrac = alpha / 100.0;
        
        obj.style.opacity = alphaFrac.toString();
        
        if (Anim != 0) setTimeout(function(){fadeIn(obj, alpha);}, 15);
    }
    
    function animate(obj, x, y, xD, yD, m, dx, dy, id, callBack)
    {
        flag = true;
        
        if (x)
        {
            x = x + dx;
            y = m * (x - xD) + yD;           
        }
        
        else
        {
            y = y + dy;
            x = (y - yD) / m + xD;
        }
        
        obj.style.left = x + "px";
        obj.style.top = y + "px";
        
        if (x)
        {
            if (dx > 0)
            {
                if (x < xD) setTimeout(function(){animate(obj, x, null, xD, yD, m, dx+1, dy, id, callBack); id = x = xD = yD = m = dx = dy = callBack = null;}, 15);
                
                else flag = false;
            }
            
            else
            {
                if (x > xD) setTimeout(function(){animate(obj, x, null, xD, yD, m, dx-1, dy, id, callBack); id = x = xD = yD = m = dx = dy = callBack = null;}, 15);
                
                else flag = false;
            }
        }
        
        else if (y)
        {
            if (dy > 0)
            {
                if (y < yD) setTimeout(function(){animate(obj, null, y, xD, yD, m, dx, dy+1, id, callBack); id = y = xD = yD = m = dx = dy = callBack = null;}, 15);
                
                else flag = false;
            }
            
            else
            {
                if (y > yD) setTimeout(function(){animate(obj, null, y, xD, yD, m, dx, dy-1, id, callBack); id = y = xD = yD = m = dx = dy = callBack = null;}, 15);
                
                else flag = false;
            }
        }
        
        if (!flag)
        {
            document.body.removeChild(obj);
            if (callBack) callBack(id);
        }
    }

    var public =
    {
        GetCurrentUser:function()
        {
            var usr = getCookie("gis_usr");
            return (usr) ? usr.toLowerCase() : null;
        },
        
        GetURLParam:function(name)
        {
            name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
            var regexS = "[\\?&]"+name+"=([^&#]*)";
            var regex = new RegExp( regexS );
            var results = regex.exec( window.location.href );
            if (results == null) return "";
            else return results[1];
        },
        
        LoadFrame:function(frameID, url, deactivateOther, forceRefresh)
        {
            var frame = document.getElementById(frameID);
            
            if (null != frame)
            {
		Geoscape.GIS.Util.StatusBar.Clear();

                if (deactivateOther) Geoscape.GIS.Util.MenuBar.DeActivate();
                
                hideOther(frame);
                
                var frameUrl = frame.src;

                if (frameUrl.replace(/\?/,":").match(url.replace(/\?/,":") + "$") != url.replace(/\?/,":") || (forceRefresh))
                {
                    Geoscape.GIS.Util.StatusBar.Init();
		            //frame.contentWindow.document.write("<html><head></head><body style=\"background-Color:#0065a4\"></body></html>");
                    frame.src = url;
                }

		frame.style.visibility = "visible";
		frame.style.display = "inline";
            }
        },
        
        AdjustFrameHeight:function(frameContainerID)
        {
            var frameContainer = document.getElementById(frameContainerID);
            
            if (frameContainer)
            {
                dy = document.documentElement.clientHeight - 86;
                
                if (dy < 0) dy = 0;
                
                frameContainer.style.height = dy + "px";
            }
        },
        
        StartBlinds:function()
        {
            blinds = document.getElementById(BlindsDivID);
            
            if (!blinds)
            {
                blinds = document.createElement("div");
                blinds.setAttribute("id", BlindsDivID);
                blinds.className = "BlindScreen";
                document.body.appendChild(blinds);
            }
            
            if (Anim != 1)
            {
                blinds.style.visibility = "visible";
                blinds.style.cursor = "wait";
                Anim = 1;
                AlphaAcc = 1;
                fadeIn(blinds, 0);
            }
        },
        
        StopBlinds:function()
        {
            Anim = 0;
            
            blinds = document.getElementById(BlindsDivID);
            
            if (blinds)
            {                
                blinds.style.visibility = "hidden";
		blinds.style.filter = "alpha(opacity=0)";
		blinds.style.opacity = "0";
                blinds.style.cursor = "default";
            }
        },
        
        ShowModalDialog:function(dialogTitle, innerHTML, animBlinds, okButtonHidden, btnHTML)
        {
            modalDlg = document.getElementById(ModalDivID);
            
            if (!modalDlg)
            {
                modalDlg = document.createElement("div");
                modalDlg.innerHTML = ModalDivHTML;
                modalDlg.setAttribute("id", ModalDivID);
                modalDlg.className = "ModalDialog";
                document.body.appendChild(modalDlg);
                document.getElementById(ModalDivOKID).onclick = function()
                {
                    Geoscape.GIS.Util.StopBlinds();
                    Geoscape.GIS.Util.HideModalDialog();
                }
            }
            
            var mdc = document.getElementById("ModalDlgContent");
            
            document.getElementById("ModalDlgTitle").innerHTML = dialogTitle;
            mdc.innerHTML = innerHTML;
            
            okButton = document.getElementById(ModalDivOKID);
            
            if (okButtonHidden) okButton.style.display = "none";
            
            else okButton.style.display = "inline";
                        
            var btnsNode = document.getElementById(ModalBtnDiv);
            
            if (btnHTML)
            {
                if (!btnsNode)
                {
                    btnsNode = document.createElement("div");
                    btnsNode.setAttribute("id", ModalBtnDiv);
                    mdc.parentNode.appendChild(btnsNode);
                }
                btnsNode.innerHTML = btnHTML;
            }
            
            else if ((btnsNode) && (mdc.parentNode.lastChild == btnsNode)) mdc.parentNode.removeChild(btnsNode);

	        if (Geoscape.GIS.Lang) okButton.value = Geoscape.GIS.Lang.GetErrorString("OK");
	        
	        mdc.className = "";
	        mdc.style.height = "";
	        var maxH = 0.6 * document.documentElement.clientHeight;	        
	    
	        if (mdc.offsetHeight > maxH)
	        {
	            mdc.className = "RestrictDlg";
	            mdc.style.height = maxH + "px";
	        }
	        
            var y = (document.documentElement.clientHeight - modalDlg.offsetHeight) / 3;
            var x = (document.documentElement.clientWidth - modalDlg.offsetWidth) / 2;

            if (y < 0) y = 0;
            if (x < 0) x = 0;
                
            modalDlg.style.top = y + "px";
            modalDlg.style.left = x + "px";

            modalDlg.style.visibility = "visible";
            
            if (!okButtonHidden) okButton.focus();

            if (animBlinds) Geoscape.GIS.Util.StartBlinds();
        },
        
        HideModalDialog:function()
        {
            modalDlg = document.getElementById(ModalDivID);
            
            if (modalDlg)
            {
                document.body.removeChild(modalDlg);
            }
            
            Geoscape.GIS.Util.StopBlinds();
        },

	ModalDialogVisible:function()
	{
            modalDlg = document.getElementById(ModalDivID);

	    if ((modalDlg) && (modalDlg.style.visibility == "visible")) return true;

	    return false;		
	},

	    GetIEVersion:function()
        {
            var rv = -1;
            if (navigator.appName == 'Microsoft Internet Explorer')
            {
                var ua = navigator.userAgent;
                var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
                if (re.exec(ua) != null) rv = parseFloat( RegExp.$1 );
            }
            return rv;
        },
        
        GetX:function(obj)
        {
            var x = 0;
            while ((null != obj) && (obj != document.body))
            {
                x += obj.offsetLeft;
                obj = obj.parentNode;
            }
        
            return x;
        },
    
        GetY:function(obj)
        {
            var y = 0;
            while ((null != obj) && (obj != document.body))
            {
                y += obj.offsetTop;
                obj = obj.parentNode;
            }
        
            return y;
        },

        HighlightSelection:function(obj, className, overRide)
        {
            if (overRide) obj.className = "OverrideRow";
            else obj.className = className;
        },
        
        AnimT:function(obj, x1, y1, x2, y2, id, callBack)
        {
            obj.style.position = "absolute";            
            obj.style.left = x1 + "px";
            obj.style.top = y1 + "px";
            
            document.body.appendChild(obj);
            
            var m = (y2 - y1) / (x2 - x1);
            
            dy = (y2 > y1) ? 1 : -1;
            
            dx = (x2 > x1) ? 1 : -1;
            
            if (Math.abs(m) < 1) animate(obj, x1, null, x2, y2, m, dx, dy, id, callBack);
            
            else animate(obj, null, y1, x2, y2, m, dx, dy, id, callBack);
        }
    }
    return public;
}();

Geoscape.GIS.Util.ProgressBar = Geoscape.GIS.Util.ProgressBar ? Geoscape.GIS.Util.ProgressBar : function()
{
    var public =
    {
        Show:function(v, block)
        {
	    if (!v) v = Geoscape.GIS.Lang.GetErrorString("4");

            Geoscape.GIS.Util.ShowModalDialog("Geoscape", "<br/><img src=\"../../images/common/progress1.gif\"/><br/><br/>" + v + "<span id='prgrss'>&nbsp;</span>", (block) ? true : false, true);
        },
        
        Hide:function()
        {
            Geoscape.GIS.Util.HideModalDialog();
        },
        
        Value:function(v)
        {
            document.getElementById("prgrss").innerHTML = v;
        }
    }
    
    return public;
}();

Geoscape.GIS.Util.Disclaimer = Geoscape.GIS.Util.Disclaimer ? Geoscape.GIS.Util.Disclaimer : function()
{
	function show(msg, noAccept)
	{
	    var html = "<div id='disclaimer'>" + msg + "</div>";
	    var htmlBtn = "<div style='margin-top:20px; float:right'><input type='button' class='Button' value='"+Geoscape.GIS.Lang.GetErrorString("ACCEPT")+"' onclick='Geoscape.GIS.Util.Disclaimer.Accept();' />&nbsp;&nbsp;<input type='button' class='Button' value='"+Geoscape.GIS.Lang.GetErrorString("CANCEL")+"' onclick='Geoscape.GIS.Util.HideModalDialog();' /></div>";
	    if (noAccept) Geoscape.GIS.Util.ShowModalDialog(Geoscape.GIS.Lang.GetErrorString("TERMS"), html, true);

	    else Geoscape.GIS.Util.ShowModalDialog(Geoscape.GIS.Lang.GetErrorString("EULA"), html, true, true, htmlBtn);
	}

	var public =
	{
	    Accept:function()
	    {
	    	        Geoscape.GIS.Auth.login({
                                        username : document.getElementById("username").value, 
                                        passwd : document.getElementById("passwd").value,
                                        referrer : document.referrer,
                                        licaccept : true
                                    },
                                    {
                                        redirectUrl : "/aapages/index.htm",
                                        errorBoxId : "errormsg"
                                    });
	    },
	    
		Fetch:function(noAccept)
		{
			var lang = "en-GB";
			if ((parent) && (parent.Geoscape)) lang = parent.Geoscape.GIS.Lang.LangBar.CurrentLang();
			else if (Geoscape.GIS.Lang) lang = Geoscape.GIS.Lang.LangBar.CurrentLang();

			if (!lang) lang = "en-GB";

			Geoscape.GIS.Core.getText("http://gis.geoscape.eu/docs/eula" + lang + ".htm", "", function(obj) { show(obj, noAccept); noAccept = null; });
		}

	}
	
	return public;
	
}();

Geoscape.GIS.Util.StatusBar = Geoscape.GIS.Util.StatusBar ? Geoscape.GIS.Util.StatusBar : function()
{
    var StatusBarArray = null;
    
    var public =
    {
        AddItem:function(module, htmlString)
        {
            if (module != Geoscape.GIS.Util.MenuBar.CurrentMod()) return;
            
            if (!StatusBarArray) StatusBarArray = new Array();
            
            if (!StatusBarArray[module]) StatusBarArray[module] = "<div class=\"Item\">" + htmlString + "</div>";
            
            else StatusBarArray[module] += "<div class=\"Item\">" + htmlString + "</div>";
            
            Geoscape.GIS.Util.StatusBar.Show(module);
        },
        
        Clear:function()
        {
            document.getElementById('statusBar').innerHTML = "";
        },
        
        Show:function(module)
        {
            if ((StatusBarArray) && (StatusBarArray[module])) document.getElementById('statusBar').innerHTML = StatusBarArray[module];
        },
        
        HasMod:function(module)
        {
            if ((StatusBarArray) && (StatusBarArray[module])) return true;
            
            return false;
        },

	DelMod:function(module)
	{
	    if ((StatusBarArray) && (StatusBarArray[module])) StatusBarArray[module] = null;
	},

        Init:function()
        {
            Geoscape.GIS.Util.StatusBar.Clear();
            StatusBarArray = null;            
        }
    }

    return public;
}();

Geoscape.GIS.Util.MenuBar = Geoscape.GIS.Util.MenuBar ? Geoscape.GIS.Util.MenuBar : function()
{
    var currActiveTabIndex = -1;
    
    var menuList = ["Marketscape&trade;", "MDx Reports", "Retail Target", "DirecTarget&reg;", "HomeBase&trade; List", "BizBase&trade; List"];
    
    var menuIDList = ["MS", "MX", "RT", "DT", "CL", "BL"];
    
    var modList = new Array();
    
    var menuContainerID = "";

    var clickCallback = null;
    
    var actCallback = null;

    var closeCallback = null;
    
    function getMenuItemID(index)
    {
        return "menuItem_" + menuContainerID + "_" + index;
    }
    
    function getMenuItemTabID(index)
    {
        return getMenuItemID(index) + "T";
    }
    
    function getMenuItemIndex(menuItemObj)
    {
        var tokenList = menuItemObj.id.split('_');
        
        return parseInt(tokenList[2], 10);
    }
    
    function getFrameID(index)
    {
        return "modFrame" + index;
    }
    
    function getURL(index)
    {
        var url = "";
        
        var frameSrc = document.getElementById(getFrameID(index)).src;
        
        if (frameSrc.length > 0) url = frameSrc;
        
        else switch (index)
        {
            case 0: url = "http://marketscape.geoscape.eu/aapages/ms/default.htm?module=MS";
            break;
            case 1: url = "http://gis.geoscape.eu/MDxReport/MDxDefault4.htm?gkey=EMDS";
            break;
            case 2: url = "rt/default.htm?module=RT";
            break;
            case 3: url = "dt/default.htm?module=DT";
            break;
            case 4: url = "cl/default.htm";
            break;
	    case 5: url = "bl/default.htm";
	    break;
        }
        
        return url;
    }
    
	var public =
	{
	    SetMenu:function(menuListText, menuListID, onClickCallback)
	    {
		    menuList = menuListText;
		    menuIDList = menuListID;
		    clickCallback = onClickCallback;
        },

	    Load:function(containerID, subMenu)
	    {	        
	        var container = document.getElementById(containerID);	        
            
            if (null != container)
            {
                var htmlString = "";
                
                menuContainerID = containerID;
                
                for (var i = 0; i < menuList.length; i++)
                {
                    var menuItemID = getMenuItemID(i);
                    
                    var innerString = "<table id=\"" + menuItemID + "T\" class=\"Normal\"><td class=\"Left\"></td><td class=\"Middle\"";
                    
                    innerString += " id=\"" + menuIDList[i] + "\">" + menuList[i] + "</td><td class=\"Right\"></td></table>";
                    
                    j = menuList.length - i;
                    
                    if (subMenu) htmlString += "<div class=\"SubMenuItem\""; else htmlString += "<div class=\"MenuItem\"";
                    htmlString += " onmouseover=\"Geoscape.GIS.Util.MenuBar.Hover(this, 1);\"";
                    htmlString += " onmouseout=\"Geoscape.GIS.Util.MenuBar.Hover(this, 0);\"";
                    htmlString += " onclick=\"Geoscape.GIS.Util.MenuBar.Activate(this);\"";
                    htmlString += " style=\"z-Index:" + j + "\" id=\"" + menuItemID + "\">" + innerString + "</div>";
                }
                
                container.innerHTML = htmlString;
            }
	    },
	    
	    Hover:function(menuItemObj, highlight)
	    {
	        if (null == menuItemObj) return;
	        
	        if (currActiveTabIndex == getMenuItemIndex(menuItemObj)) return;
	        
	        switch (highlight)
	        {
	            case 0: menuItemObj.firstChild.className = "Normal";
	            break;
	            case 1: menuItemObj.firstChild.className = "Hover";
	            break;
	        }
	    },
	    
	    DeActivate:function()
	    {
	    	if (currActiveTabIndex != -1)
	        {
	            obj = document.getElementById(getMenuItemTabID(currActiveTabIndex));
	            
	            if (null != obj)
	            {
	                obj.className = "Normal";
	                
	                obj.parentNode.style.zIndex = menuList.length - currActiveTabIndex;
	                
	                obj.parentNode.style.cursor = "pointer";

			obj.parentNode.style.top = "0px";
	            }
	        }
	        
	        currActiveTabIndex = -1;
	    },

	    Activate:function(menuItemObj, url, forceRefresh)
	    {
	        if (null == menuItemObj) return;
	        
	        Geoscape.GIS.Util.MenuBar.DeActivate();
	        
	        menuItemObj.firstChild.className = "Active";
	        
	        menuItemObj.style.zIndex = 35;
	        
	        menuItemObj.style.cursor = "default";
	        
	        menuItemObj.style.top = "1px";
	        
	        currActiveTabIndex = getMenuItemIndex(menuItemObj);

		    if (null != clickCallback) clickCallback(menuIDList[currActiveTabIndex]);
	        
	        else
	        {
	            Geoscape.GIS.Util.LoadFrame(getFrameID(currActiveTabIndex), (url) ? url : getURL(currActiveTabIndex), false, forceRefresh);
	            Geoscape.GIS.Util.StatusBar.Show(modList[currActiveTabIndex]);
	            try
	            {
	            if ((actCallback) && (actCallback[modList[currActiveTabIndex]])) actCallback[modList[currActiveTabIndex]](modList[currActiveTabIndex]);
	            }
	            catch (err)
	            {
	            }
	        }
	    },
	    
	    ActivateByID:function(menuID, url, forceRefresh)
	    {
	        if (null == menuID) return;
	        
	        var obj = document.getElementById(menuID);
	        
	        if (null == obj) return;

	        Geoscape.GIS.Util.MenuBar.Activate(obj.parentNode.parentNode.parentNode.parentNode, url, forceRefresh);
	    },
	    
	    GetXPos:function(menuID)
	    {
	    	if (null == menuID) return;
	        
	        var obj = document.getElementById(menuID);
	        
	        if (null == obj) return;
	        
	        return obj.parentNode.parentNode.parentNode.parentNode.offsetLeft;
	    },
	    
	    CurrentMod:function()
	    {
		if (!modList[currActiveTabIndex]) return "home";
	        return modList[currActiveTabIndex];
	    },
	    
	    RegisterMod:function(module, activationCallback)
	    {
	        modList[currActiveTabIndex] = module;
	        
	        if (!actCallback) actCallback = new Array();
	        
	        actCallback[module] = activationCallback;	        
	    },

	    OnClose:function(onCloseCallback)
	    {
		if (!closeCallback) closeCallback = new Array();

		closeCallback[closeCallback.length] = onCloseCallback;
	    },

	    Close:function()
	    {
		if (closeCallback)
		{
			for (var i=0; i < closeCallback.length; i++)
			{
				closeCallback[i](i);
			}
		}

		else logout();
	    },

	    Closed:function(i)
	    {
		closeCallback[i] = null;

		var j = 0;

		for (var i=0; i < closeCallback.length; i++)
		{
			if (!closeCallback[i]) j++;
		}

		if (j == closeCallback.length) logout();
	    }
    }
	
	return public;
}();