﻿// 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 = 560;

var browserVersion;
var inputSideBarWidth;
var inputSideBarExpandFlag;
var splitterDraggingFlag = false;
var startDraggingX;
var startDraggingY;




function CalcSidebarWidth(originalWidth) {
    if (originalWidth < 200)
        originalWidth = 200;
    if (isFixedWidth == true) {
        if (originalWidth + 10 + contentContainerWidth > totalWidth) {
            if (totalWidth - 10 - contentContainerWidth >= 200) {
                return totalWidth - 10 - contentContainerWidth;
            }
            else
                return 200;
        }
    }
    return originalWidth;
}

function InitLayout()
{
    browserVersion = BrowserDetect.version;
    inputSideBarExpandFlag = document.getElementById(inputSideBarExpandFlagID);
    inputSideBarWidth = document.getElementById(inputSideBarWidthID);
    sidebar = document.getElementById(sideBarID);
	content = document.getElementById("content");
	splitter = document.getElementById(splitterBarID);
	var orignalwidth = GetPixelWidth(sidebar);
	var width = CalcSidebarWidth(orignalwidth);
	SetSideBarWidth(width, false);
    ResetMainPanelMinWidth(width);
}

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;


    if (isFixedWidth == true)
        width = totalWidth;
    else 
    {
        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(divFooterPanelID);
    
    
    var contentWidth = width - sidebarWidth - 10 - 30;

    content.style.width = contentWidth + "px";

    var calendarTable = document.getElementById(calendarTableId);     
    if(calendarTable != null)
    {
        if (calendarTable.offsetWidth != contentWidth) {
            contentWidth = calendarTable.offsetWidth;
            content.style.width = contentWidth + "px";
            sidebarWidth = width - contentWidth - 10 - 30;
            sidebarWidth = CalcSidebarWidth(sidebarWidth);
            SetSideBarWidth(sidebarWidth, false);
        }
    }

    width = contentWidth + sidebarWidth + 10 + 30;
    divbannercontainer.style.width = width + "px";
    divFooterPanel.style.width = width + "px";
    divMainPanel.style.width = contentWidth + 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 SetSideBarWidth(sidebarwidth, saveFlag) {

    sidebar.style.width = sidebarwidth + "px";
    sidebar.style.marginLeft = -sidebarwidth - 10 + "px";
    $get(divPageMainPanelID).style.borderLeftWidth = sidebarwidth + 10 + "px";
    if (browserVersion < 7) {
        //document.getElementById("navtabs").style.marginLeft = width + 12 + "px";
        document.getElementById("hometab").style.width = sidebarwidth + 12 + "px";
    }
    else {
        //document.getElementById("navtabs").style.marginLeft = width + 10 + "px";
        document.getElementById("hometab").style.width = sidebarwidth + 10 + "px";
    }

    if (!(typeof saveFlag == 'boolean' && saveFlag == false)) {
        inputSideBarWidth.value = sidebarwidth;
        setCookie("SideBarWidthCookieName", new String(sidebarwidth), 1000);
    }
}

function ResizeSideBar(movedistance)
{
    var width = sidebarwidth + movedistance;
    if (width >= 200) {
        if ((width <= 400 && isFixedWidth == false) || (width <= totalWidth - 10 - contentContainerWidth && isFixedWidth == true)) {
            SetSideBarWidth(width);
            ResetMainPanelMinWidth(width);
        }
    }
}

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()
{
}


function ShowPopupStreams(divStreamsID, flg, e) {
    var divStreams = document.getElementById(divStreamsID);
    if (flg) {
        divStreams.style.display = "block";

        var left, top;
        left = e.clientX;
        top = e.clientY;
        var parentPos = __getElementPos(divStreams.offsetParent);

        var parentBorderSize = __getBorderWidth(divStreams.offsetParent);

        left = left + document.documentElement.scrollLeft - (parentPos.x + parentBorderSize.left) - 20;
        top = top + document.documentElement.scrollTop - (parentPos.y + parentBorderSize.top) - 10;

        divStreams.style.left = left + "px";
        divStreams.style.top = top + "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);
        width = 200;
        SetSideBarWidth(width, true);
        splitter.style.display = "block"
        imgArrow.src = imgArrow.src.replace("Images/arrow-l.gif", "Images/arrow-r.gif");
        imgArrow.alt = expandCategoryButtonString;
        imgArrow.title = expandCategoryButtonString;
    }
    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");
        imgArrow.alt = collapseCategoryButtonString;
        imgArrow.title = collapseCategoryButtonString;
    }

    width = CalcSidebarWidth(width);
    SetSideBarWidth(width, false);
    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
        }
    }
}

