var g_prevWindowWidth = getWindowWidth();
var g_windowWidth;
var g_menuID;
var g_browserID;
var g_orientation;
var g_editcues;
function StringEndsWith(txt,searchtxt,ignoreCase)
{
    txt=txt.trimEnd();
    if(ignoreCase)
    {
        txt=txt.toLowerCase();
        searchtxt=searchtxt.toLowerCase();
    }
    var isTxt = txt.endsWith(searchtxt.toString());
//    if (isTxt === true)
//    {
//        alert("The string \"" + txt + "\" ends with \"" + searchtxt + "\""); 
//    }
//    else
//    {
//       alert("The string \"" + txt + "\" does not end with \"" + searchtxt + "\""); 
//    }
    return isTxt;
}
function CheckElementWidth(menuID, browserID, orientation, editcues) 
{
    if (PageFullyLoaded != true)
    {
        var fn = function(){
            return CheckElementWidth(menuID, browserID, orientation, editcues);
        }
        setTimeout(fn, 100);
    }
    g_windowWidth = getWindowWidth();
    
    menus = document.getElementById(menuID);
    if (menus)
    {
        if(StringEndsWith(menus.className, (orientation + '_NavigationBar_MainMenu'), false))
        {
	        var outerPos = getElementWidth(menus);
            if (outerPos > 0)
            {
                var innerPos = 0;
		        var num = 0;
		        if (g_windowWidth < g_prevWindowWidth)
		        {
		            // Make room for the vscroll bar
		            outerPos = outerPos - 15;
		        }
		        g_prevWindowWidth = g_windowWidth;
		        items = menus.getElementsByTagName('span');
		        var intermediate = 0;
		        for(x=0;x<items.length;x++)
		        {
		            if (StringEndsWith(items[x].className, (orientation + '_NavigationBar_CurrentMainMenuItem'), false))
                    {
                        if (browserID == 'IE')
                        {
		                    innerPos = innerPos + getElementWidth(items[x]);
                        }
                        else
                        {
                            var anchors = items[x].getElementsByTagName('a');
                            for(y=0;y<anchors.length;y++)
                            {
			                    if (StringEndsWith(anchors[y].className, (orientation + '_NavigationBar_MainMenuItemLink'), false))
                                {
    			                    innerPos = innerPos + getElementWidth(anchors[y]);
                                }
                            }
                        }
                    }
                    else if (StringEndsWith(items[x].className, (orientation + '_NavigationBar_MainMenuItem'), false))
                    {
                        if (browserID == 'IE')
                        {
		                    innerPos = innerPos + getElementWidth(items[x]);
                        }
                        else
                        {
                            var anchors = items[x].getElementsByTagName('a');
                            for(y=0;y<anchors.length;y++)
                            {
			                    if (StringEndsWith(anchors[y].className, (orientation + '_NavigationBar_MainMenuItemLink'), false))
                                {
    			                    innerPos = innerPos + getElementWidth(anchors[y]);
                                }
                            }
                        }
                    }
                    else if (StringEndsWith(items[x].className, (orientation + '_NavigationBar_MainMenuSpacer'), false))
                    {
                        intermediate = intermediate + getElementWidth(items[x]);
		                innerPos = innerPos + getElementWidth(items[x]);
		                num = num + 1;
                    }
                    else if (items[x].className == 'NavigationBar_NoMenuSpacer')
                    {
                        num = num + 1;
                    }
                }
                if (num > 0)
                {
                    if (editcues == 'True')
                    {
                        outerPos = outerPos - 2;
                    }
                    var leftOver = outerPos - innerPos;
                    var marginEnds = (leftOver/num)/2;
                    var cummulative = intermediate;
                    items = menus.getElementsByTagName('a');
                    var lastIndex = 0;
                    for(x=0;x<items.length;x++)
		            {
		                var thisIndex = lastIndex;
		                var found = false;
                        if (StringEndsWith(items[x].className, (orientation + '_NavigationBar_MainMenuItemLink'), false))
                        {
                            found = true;
		                    var left;
		                    if (items[x].style.paddingLeft == '')
		                    {
		                        left = 0;
                            }
                            else
                            {
                                left = items[x].style.paddingLeft.split('px')[0];
                            }
                            items[x].style.paddingLeft = (parseInt(left) + parseInt(marginEnds)) + 'px';
                                            			            
                            var right;
                            if (items[x].style.paddingRight == '')
		                    {
		                        right = 0;
                            }
                            else
                            {
                                right = items[x].style.paddingRight.split('px')[0];
                            }
                            items[x].style.paddingRight = (parseInt(right) + parseInt(marginEnds)) + 'px';
                            innerPos = Math.floor(innerPos + (marginEnds * 2));
                            leftOver = outerPos - innerPos;
                            num = num - 1;
                            marginEnds = (leftOver/num)/2;
                            if ((marginEnds * 2) < ((leftOver/num)/2))
                            {
                                marginEnds = marginEnds + 1;
                                innerPos = innerPos + 2;
                            }
                            thisIndex = x;
                        }
                        cummulative = cummulative + getElementWidth(items[x]);
                        if ((cummulative > outerPos) && (outerPos > 0))
                        {
                            //alert('outerPos' + outerPos);
                            //alert('cummulative' + cummulative);
                            //alert('innerPos' + innerPos);
                            
                            if (found)
                            {
                                var right;
                                if (items[x].style.paddingRight == '')
		                        {
		                            right = 0;
                                }
                                else
                                {
                                    right = items[x].style.paddingRight.split('px')[0];
                                }
                                items[x].style.paddingRight = (parseInt(right) - (cummulative-outerPos)) + 'px';
                            }
                        }
                        else
                        {
                            lastIndex = thisIndex;
                        }
                    }
                    if ((cummulative < outerPos) && (outerPos > 0))                
                    {
                        //alert('outerPos' + outerPos);
                        //alert('cummulative' + cummulative);
                        //alert('innerPos' + innerPos);

                        var right;
                        if (items[lastIndex].style.paddingRight == '')
	                    {
	                        right = 0;
                        }
                        else
                        {
                            right = items[lastIndex].style.paddingRight.split('px')[0];
                        }
                        items[lastIndex].style.paddingRight = (parseInt(right) - (cummulative-outerPos)) + 'px';
                    }
                }
            }
            g_menuID = menuID;
            g_browserID = browserID;
            g_orientation = orientation;
            g_editcues = editcues;
        }
    }

//    if (PageFullyLoaded != true)
//    {
//        var fn = function(){
//            return CheckElementWidth(menuID, browserID, orientation, editcues);
//        }
//        setTimeout(fn, 100); 
//    }
};
function getElementWidth(obj)
{
    return obj.offsetWidth;
};
function getElementHeight(obj)
{
    return obj.offsetHeight;
};
var resizeElements = new Array();
function InitAnimation(id,height,width)
{
    for(var nzIndex = 0; nzIndex < resizeElements.length; nzIndex++)
    {
        var tid = resizeElements[nzIndex]["id"];
        if (tid == id)
        {
            return;
        }
    }
    var obj = document.getElementById(id);
    if (obj)
    {
        var newEntry = new Object();
        newEntry["id"] = id;
        newEntry[height] = getElementHeight(obj);
        newEntry[width] = getElementWidth(obj);
        resizeElements.push(newEntry)
        
        obj.style.height = "0px";
    }
};
function ResizeAnimation(id, dim)
{
    for(var nzIndex = 0; nzIndex < resizeElements.length; nzIndex++)
    {
        var tid = resizeElements[nzIndex]["id"];
        if (tid == id)
        {
            return resizeElements[nzIndex][dim];
        }
    }
    return 0;
};
function evaluateNavigationBarWidth()
{
    if (g_windowWidth != getWindowWidth())
    {
        CheckElementWidth(g_menuID, g_browserID, g_orientation, g_editcues) 
    }
};
function getWindowWidth()
{
    var myWidth = 0, myHeight = 0;
    if( typeof( window.innerWidth ) == 'number' ) 
    {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    }
    else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
    {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    }
    else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
    {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    return myWidth;
};
try
{
    if (typeof Sys == 'undefined')
    {
        ;
    }
    else
    {
        if( Sys && Sys.Application ){
           Sys.Application.notifyScriptLoaded();
        }
    } 
}
catch(e)
{
;
}
