﻿// JScript File

var splitter = null;
var startdroppos = 0;
var splitterstartleftpos = 0;
var sidebar = null;
var sidebarwidth = 0;
var content;
var contentmarginleft = 0;
var calendarTableId = "";

var contentContainerWidth = 0;

var browserVersion;
var inputSideBarWidth;
var inputSideBarExpandFlag;
var splitterDraggingFlag = false;
var startDraggingX;
var startDraggingY;



//detect browser version
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "IE",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

function InitLayout()
{
    inputSideBarExpandFlag = document.getElementById(inputSideBarExpandFlagID);
    inputSideBarWidth = document.getElementById(inputSideBarWidthID);
    sidebar = document.getElementById(sideBarID);
	content = document.getElementById("content");
	splitter = document.getElementById(splitterBarID);
    var width = GetPixelWidth(sidebar);
    sidebar.style.marginLeft = String(-width -10) + "px";
    $get(divPageMainPanelID).style.borderLeftWidth = width + 10 + "px";
    browserVersion = BrowserDetect.version;
   
   
    //document.getElementById("navtabs").style.marginLeft = width + 10 + "px";
    document.getElementById("hometab").style.width = width + 10 + "px";
    ResetMainPanelMinWidth(width);
    //AlignContentHeight();
    
    
}

function AlignContentHeight()
{
    var height;
    if(sidebar.offsetHeight < content.offsetHeight)
    {
        height = content.offsetHeight;
    }
    else
    {
        height = sidebar.offsetHeight;
    }
    
    if(height < 500)
    {
        height = 500;
    }
    sidebar.style.pixelHeight = height;
    content.style.pixelHeight = height;
}



function ResetMainPanelMinWidth(sidebarWidth)
{
    var width;

    
    width = sidebarWidth + contentContainerWidth + 10;
    if( width < 900)
        width = 900;
    
    var bodyWidth = 900;
    
    if( document.documentElement && document.documentElement.clientWidth ) 
    {
        bodyWidth = document.documentElement.clientWidth;
    }else if( document.body &&  document.body.clientWidth ) 
    {
        bodyWidth = document.body.clientWidth;
    }
    
    if( width < bodyWidth )
    {
        width = bodyWidth;
    }
    
    
    
    
    //divMainPanel.style.minWidth = minWidth;
    
    var divbannercontainer = document.getElementById(divBannerContainerID);
    var divMainPanel = document.getElementById(divPageMainPanelID);
    var divFooterPanel = document.getElementById("footerPanel");
    
    divbannercontainer.style.width = width + "px";
    divFooterPanel.style.width = width + "px";
    divMainPanel.style.width = width - sidebarWidth - 10 + "px";
    
    
    content.style.width = width - sidebarWidth - 10 - 30 + "px";

    var calendarTable = document.getElementById(calendarTableId);     
    if(calendarTable != null)
    {
        if(calendarTable.offsetWidth != GetPixelWidth(content))
        {
            width = calendarTable.offsetWidth + sidebarWidth + 10 + 30;
            divbannercontainer.style.width = width + "px";
            divMainPanel.style.width = width - sidebarWidth - 10 + "px";
            
            
            content.style.width = width - sidebarWidth - 10 - 30 + "px";
           
        }
    }
    
}


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 null;
}

function setCookie( c_name, value, expiredays)
{
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name+ "=" +escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}

function GetPixelWidth(bar)
{
    return Number(bar.style.width.substring(0, bar.style.width.length - 2))
}

function ResizeSideBar(movedistance)
{
    if(sidebarwidth + movedistance >= 200 && sidebarwidth + movedistance <= 400)
    {
        inputSideBarWidth.value = sidebarwidth + movedistance;
        sidebar.style.width = sidebarwidth + movedistance + "px";
        var width =  sidebarwidth + movedistance;
        sidebar.style.marginLeft = - width - 10 + "px"; 
        $get(divPageMainPanelID).style.borderLeftWidth = width + 10 + "px";
        if(browserVersion < 7)
        {
            //document.getElementById("navtabs").style.marginLeft = width + 12 + "px";
            document.getElementById("hometab").style.width = width + 12 + "px";
        }
        else
        {
            //document.getElementById("navtabs").style.marginLeft = width + 10 + "px";
            document.getElementById("hometab").style.width = width + 10 + "px";
        }
        setCookie("SideBarWidthCookieName", new String(width), 1000);
        ResetMainPanelMinWidth(sidebarwidth + movedistance);
    }
}

function getStyle(element,styleProp)
{
	if (element.currentStyle)
		var y = element.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(element,null).getPropertyValue(styleProp);
	return y;
}

function CollapsedTaxonomy(divId, icon,cookieName,url1,url2)
{
    var div = document.getElementById(divId);
    if(div.style.display == "none")
    {
        div.style.display = "";
        icon.src = url1;
        setCookie(cookieName, "false", 365);
    }
    else
    {
        div.style.display = "none";
        setCookie(cookieName, "true", 365);
        icon.src = url2;
    }
}

function donothing()
{
}

// JScript File
function ShowStreamsDiv(flg ,x ,y)
{
    var divStreams;
    divStreams = document.getElementById(divStreamsId);
    if(flg)
    {
        divStreams.style.display = "";
        var borderLeft;
        borderLeft = 0;
        if($get(divPageMainPanelID).style.borderLeftWidth)
        {
            borderLeft = Number($get(divPageMainPanelID).style.borderLeftWidth.substring(0, $get(divPageMainPanelID).style.borderLeftWidth.length - 2))
        }
        var left;
        left = x;    

        var bodyWidth = 900;  
        if( document.documentElement && document.documentElement.clientWidth ) 
        {
            bodyWidth = document.documentElement.clientWidth;
        }else if( document.body &&  document.body.clientWidth ) 
        {
            bodyWidth = document.body.clientWidth;
        }
        
        if(left + divStreams.offsetWidth > bodyWidth + 10)
        {
            left = bodyWidth + 10 - divStreams.offsetWidth ;
        }

        left = left + GetScrollLeft(divStreams.offsetParent) - GetOffsetLeft(divStreams.offsetParent) - 20 - borderLeft;
        divStreams.style.left = left + "px";
        divStreams.style.top = y +  GetScrollTop(divStreams.offsetParent) - GetOffsetTop(divStreams.offsetParent) - 10 + "px";
    }
    else
    {
        divStreams.style.display = "none";
    }
}



function ShowAdvancedStreamsDiv(flg ,x ,y)
{
    var divStreams;
    divStreams = document.getElementById("divAdvancedPopupStreams");
    if(flg)
    {
        divStreams.style.display = "";
        var borderLeft;
        borderLeft = 0;
        if($get(divPageMainPanelID).style.borderLeftWidth)
        {
            borderLeft = Number($get(divPageMainPanelID).style.borderLeftWidth.substring(0, $get(divPageMainPanelID).style.borderLeftWidth.length - 2))
        }
        var left;
        left = x;    

        var bodyWidth = 900;  
        if( document.documentElement && document.documentElement.clientWidth ) 
        {
            bodyWidth = document.documentElement.clientWidth;
        }else if( document.body &&  document.body.clientWidth ) 
        {
            bodyWidth = document.body.clientWidth;
        }
        
        if(left + divStreams.offsetWidth > bodyWidth + 10)
        {
            left = bodyWidth + 10 - divStreams.offsetWidth ;
        }

        left = left + GetScrollLeft(divStreams.offsetParent) - GetOffsetLeft(divStreams.offsetParent) - 20 - borderLeft;
        divStreams.style.left = left + "px";
        divStreams.style.top = y +  GetScrollTop(divStreams.offsetParent) - GetOffsetTop(divStreams.offsetParent) - 10 + "px";
    }
    else
    {
        divStreams.style.display = "none";
    }
}



function ShowStreamsMenu(id, flg ,x ,y)
{
    var divStreams;
    divStreams = document.getElementById(id);
    if(flg)
    {
        divStreams.style.display = "block";
        
        var left, top;
        left = x;
        top = y; 
        
        var divMainPanel = document.getElementById(divPageMainPanelID);
        var borderLeft;
        borderLeft = 0;
        if(divMainPanel.style.borderLeftWidth)
        {
            borderLeft = Number(divMainPanel.style.borderLeftWidth.substring(0, divMainPanel.style.borderLeftWidth.length - 2))
        }

        left = left + document.documentElement.scrollLeft - GetOffsetLeft(divStreams.offsetParent) - 20 - borderLeft;
        divStreams.style.left = left + "px";
        
        top -= GetOffsetTop(divStreams.offsetParent) + 10 - document.documentElement.scrollTop;
        divStreams.style.top = top + "px";
    }
    else
    {
        divStreams.style.display = "none";
    }
}



// JScript File
function selectAllCheckList(checked, checklistId)
{
    //enumerate all check box
    var chkTable = document.getElementById(checklistId);
    if (chkTable == null)
    {
        return false;
    }
    for(var i = 0; i < chkTable.rows.length;i++)
    {
        var row = chkTable.rows[i];
        for(var j = 0;j < row.cells.length;j++)
        {
            var cell = row.cells[j];
            for (var k =0;k < cell.children.length;k++)
            {
                var control = cell.children[k];
                if (control.type == "checkbox")
                {
                    control.checked = checked;
                }
            } 
        }
    }
    return false;
}

function OnBodyResize()
{
    sidebar = document.getElementById(sideBarID);
    var width = GetPixelWidth(sidebar);
    ResetMainPanelMinWidth(width);
}

function ExpandCategoryPanel(e)
{
    var flag = inputSideBarExpandFlag.value;
    var width;
	var imgArrow = document.getElementById(expandCategoryButtonID);
    if(flag == "1")
    {
        inputSideBarExpandFlag.value = "0";
        setCookie("SideBarExpandFlag", "0", 365);
        width = parseInt(inputSideBarWidth.value);
        splitter.style.display = "block"
        imgArrow.src = imgArrow.src.replace("Images/arrow-l.gif", "Images/arrow-r.gif");
    }
    else
    {
        setCookie("SideBarExpandFlag", "1", 365);
        inputSideBarExpandFlag.value = "1";
        width = 400;
        splitter.style.display = "none"
        imgArrow.src = imgArrow.src.replace("Images/arrow-r.gif", "Images/arrow-l.gif");
    }
    

    sidebar.style.width = width + "px";
    sidebar.style.marginLeft = -width - 10 + "px";
    $get(divPageMainPanelID).style.borderLeftWidth  = width + 10 + "px";
    if(browserVersion < 7)
    {
        //document.getElementById("navtabs").style.marginLeft = width + 12 + "px";
        document.getElementById("hometab").style.width = width + 12 + "px";
    }
    else
    {
        //document.getElementById("navtabs").style.marginLeft = width + 10 + "px";
        document.getElementById("hometab").style.width = width + 10 + "px";
    }
    ResetMainPanelMinWidth(width);
}

function pageLoad() {
    if(typeof masterPageLoad == 'function')
        masterPageLoad();
    if(typeof contentPageLoad == 'function')
        contentPageLoad();
}



function OnSplitterMouseDown(e)
{
    
    e = FixEvent(e);
    if(splitterDraggingFlag == false)
    {
        startDraggingX = e.clientX;
        sidebarwidth = GetPixelWidth(sidebar);
        document.onmousemove = OnDocumentMouseMove;
        document.onmouseup = OnDocumentMouseUp;
        splitterDraggingFlag = true;
    }
}

function FixEvent(e)
{
    if (typeof e == 'undefined') e = window.event;
    if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
    if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
    return e;
}

function OnSplitterDragging(e)
{
    if(splitterDraggingFlag == true)
    {
        var movedistance = e.clientX - startDraggingX;
        ResizeSideBar(movedistance);
    }
}

function OnSplitterDraggingEnd(e)
{
    if(splitterDraggingFlag == true)
    {
        splitterDraggingFlag = false;
        document.onmousemove = null;
        document.onmouseup = null;
        document.onmouseenter = null;
    }
}

function OnDocumentMouseMove(e)
{
    e = FixEvent(e);
    OnSplitterDragging(e);
}

function OnDocumentMouseUp(e)
{
    e = FixEvent(e);
    OnSplitterDraggingEnd(e);
}


function containsDOM (container, containee) 
{
    var isParent = false;
    do {
        if ((isParent = container == containee))
          break;
        containee = containee.parentNode;
    }
    while (containee != null);
    return isParent;
}

function checkMouseEnter (element, evt) {
    if (element.contains && evt.fromElement) {
        return !element.contains(evt.fromElement);
    }
    else if (evt.relatedTarget) {
        return !containsDOM(element, evt.relatedTarget);
    }
}

function checkMouseLeave (element, evt) {
    if (element.contains && evt.toElement) {
        return !element.contains(evt.toElement);
    }
    else if (evt.relatedTarget) {
        return !containsDOM(element, evt.relatedTarget);
    }
}


function setPopupParent(popupId)
{
    theForm.appendChild($get(popupId + "_foregroundElement"));
    theForm.appendChild($get(popupId + "_backgroundElement"));
}

function doClick(buttonName,e)
{
    //the purpose of this function is to allow the enter key to 
    //point to the correct button to click.
    var key;

     if(window.event)
          key = window.event.keyCode;     //IE
     else
          key = e.which;     //firefox

    if (key == 13)
    {
        //Get the button the user wants to have clicked
        var btn = document.getElementById(buttonName);
        if (btn != null)
        { //If we find the button click it
            btn.click();
            event.keyCode = 0
        }
    }
}
