var minWidth = 0;
var ideUA = false;
var dragging = false;

function windowLoaded(evt) {
    // prevent IE text selection while dragging!!!
    // Little-known trick!
    document.body.ondrag = function () { return !dragging; };
    document.body.onselectstart = function () { return !dragging; };
}

// CHange the color and image of the splitter bar
function HighlightSplitterBar(strObjName, intOnOff)
{       
    var sliderBar = document.getElementById(strObjName);
	
    if (intOnOff == 0)
    {
        sliderBar.style.backgroundImage ='url(../images/lib_grippy1.gif)';
    }
    else
    {
        sliderBar.style.backgroundImage ='url(../images/lib_grippy.gif)';        
    }
   return;
}

//  Main 
//	All global variables written by server control
function DoControlSetup()
{
	minWidth=0;
	this.name = "DoControlSetup";
	
   	if (browser == "Netscape")
	{	    
        document.addEventListener("onkeypress",KeyPressOpenCloseToc,true);
    }
    document.onkeypress = KeyPressOpenCloseToc;

    FetchResizableAreaCells();
    return;
}

//  Layout the <div> elements based on the client side renderable area
function FetchResizableAreaCells()
{
    winWidth = parseInt(document.body.clientWidth);
    oContainerCell = document.getElementById(sContainerCell);
    oNavCell = document.getElementById(sNavCell);
    oSplitterCell = document.getElementById(sSplitterCell);
    oContentCell = document.getElementById(sContentCell);
    oSplitterCell.style.width = "5px";

    if (GetTocPanelCookie(cookieName) != -1)
    {
        sSplitterDefault = GetTocPanelCookie(cookieName);
    }

	if (oNavCell.style.width != sSplitterDefault)
	{
	    oNavCell.style.width = sSplitterDefault;
	    oSplitterCell.style.left = sSplitterDefault;
	    oSplitterCell.style.width = "5px";
	    oContentCell.style.left = parseInt(oSplitterCell.style.width) + parseInt(sSplitterDefault) + px;
	}    

    if (winWidth - parseInt(oContentCell.style.left) < minWidth)
    {
    	var newWidth = winWidth-605;
	    if (newWidth < 0)
	    {
    		newWidth = parseInt(oNavCell.style.width);
	    }

	    oNavCell.style.width = newWidth + px;
        oSplitterCell.style.left = oNavCell.style.width;
        oSplitterCell.style.width = "5px";
        oContentCell.style.left = parseInt(oSplitterCell.style.width) + newWidth  + px;
	}
    
    ResizeContentArea();
    return;
}


//  Attach Event Handlers
function SelectSplitter()
{	// Required for Mozilla 1.0, Netscape 7.2
oContainerCell.className = "OverlayShow";
	if (browser == "Netscape")
	{	//	NS4: document.captureEvents(Event.MOUSEMOVE);
        oContainerCell.addEventListener("onmousemove",ResizePanel,true);
        oContainerCell.addEventListener("onmouseup",ReleaseSplitter,true);
	}
	//	IE

//		document.onmousemove = ResizePanel;
//		document.onmouseup = ReleaseSplitter;

	oContainerCell.onmousemove = ResizePanel;
	oContainerCell.onmouseup = ReleaseSplitter;
	dragging = true;
	return;
}

//	Changed to OnMouseMove functionality
function ResizePanel(e)
{
// oSplitterCell
	var contentSizeLimit;

	if (browser == "Netscape")
	{
		e.cancelBubble = true;
		splitterX = e.pageX;
    }
	else
	{
	    window.event.cancelBubble = true;
		if (window.event.clientX - 4 < 0)
		{
		    splitterX = 0;
		}
		else
		{
		    splitterX = window.event.clientX - 4;
		}
	}

  window.status = splitterX
  sSplitterCookieX =splitterX
  
      //  alert("sSplitterCookieX = " + sSplitterCookieX);
    if (parseInt(sSplitterCookieX) == 0)
    {
       DisableTocPanel(true);
    }
    else
    {
       DisableTocPanel(false);       
    }  

//    oNavCell.style.width = parseInt(sSplitterCookieX) + px;
    oSplitterCell.style.left = parseInt(sSplitterCookieX) + px;
//    oContentCell.style.left = sSplitterCookieX + parseInt(oSplitterCell.style.width) + px;
//    ResizeContentArea();

	return;
}

//	Changed to OnMouseUp functionality
function ReleaseSplitter()
{
    oContainerCell.className = "OverlayHidden";

    //  alert("sSplitterCookieX = " + sSplitterCookieX);
    if (parseInt(sSplitterCookieX) < 1)
    {
       DisableTocPanel(true);
    }
    else
    {
       DisableTocPanel(false);       
    } 
    sTmpNavCellWidth = parseInt(sSplitterCookieX) + px;
    oNavCell.style.width = parseInt(sSplitterCookieX) + px;
    oSplitterCell.style.left = parseInt(sSplitterCookieX) + px;
    oContentCell.style.left = sSplitterCookieX + parseInt(oSplitterCell.style.width) + px;
    ResizeContentArea();
	SetTocPanelCookie(cookieName,oNavCell.style.width, days);
	oContainerCell.onmousemove = null;
	oContainerCell.onmouseup = null;
	dragging = false;
	return;
}

//	ONKEYPRESS:
//	CHECK WHICH KEYS HAVE BEEN PRESSED
//	IF A MATCH IS FOUND, SHOW/HIDE TOC PANEL
function KeyPressOpenCloseToc(e)
{
    if (browser == "Netscape")
	{	//     //   alert(e.which + "\n" + e.target.tagName);
		if (e.which == 116)
		{
		    if (e.target.tagName.toLowerCase() != "input" && e.target.tagName.toLowerCase() != "textarea" )
		    {
		        OpenClosePanel();
            }
			e.cancelBubble = true;
		}
	}
	else if(window.event.keyCode == 116)
	{   
		if (window.event.srcElement.tagName.toLowerCase() != "textarea" && window.event.srcElement.tagName.toLowerCase() != "input")
		{
		    OpenClosePanel();
		}
		window.event.cancelBubble = true;
	}
	return;
}

//  This function enables/disabled the nodes in the TOC, 
//  depending on the state/size of the TOC panel.
function DisableTocPanel(boolEnabled)
{
    var tocPanel = document.getElementById(TocPaneNameId);
    if(tocPanel.disabled == "undefined")
    {
        tocPanel.disabled = boolEnabled;
    }
    
    if(tocPanel.style.display == "none" && boolEnabled == true)
    {
        boolEnabled == false;
    }  
   
    if (boolEnabled == true )
    {
        tocPanel.disabled = true;
        tocPanel.style.display = "none";
    }
    else
    {
        tocPanel.disabled = false;
        tocPanel.style.display = "inline";
    }
     if(tocOpenCollapseHandler!=null  && typeof(tocOpenCollapseHandler)=="function")
     {
        tocOpenCollapseHandler(boolEnabled);
     };
    
    return; 
}


//  Simple Hide/Show Functionality
//  Need to add cookie setting
//  Add CSS ClassName change for ContentPanel
function OpenClosePanel()
{
    //  alert(oNavCell.style.width + "\n" + sTmpNavCellWidth);
    if (oNavCell.style.width != "0px")
    {
        sTmpNavCellWidth = oNavCell.style.width;
        oNavCell.style.width = "0px";
        oContentCell.style.left = parseInt(oSplitterCell.style.width) + px;
        oSplitterCell.style.left = "0px";
        DisableTocPanel(true);
    }
    else if ( (oNavCell.style.width == "0px" && sTmpNavCellWidth == undefined) ||  (oNavCell.style.width == "0px" && sTmpNavCellWidth == "0px"))
    {
        oNavCell.style.width = "250px";
        oSplitterCell.style.left = "250px";
        sTmpNavCellWidth = oNavCell.style.width;
        oContentCell.style.left = parseInt(oNavCell.style.width) + parseInt(oSplitterCell.style.width) + px;
        DisableTocPanel(false); 
    }
    else
    {
        oNavCell.style.width = sTmpNavCellWidth;
        oSplitterCell.style.left = sTmpNavCellWidth;
        oContentCell.style.left = parseInt(oNavCell.style.width) + parseInt(oSplitterCell.style.width) + px;
        DisableTocPanel(false);
    }

    if (winWidth - parseInt(oContentCell.style.left) < minWidth)
    {
	    var newWidth = winWidth - minWidth - 5;
	    if (newWidth < 0)
	    {
            if (parseInt(oNavCell.style.width) > 76)
            {
		        newWidth=76;
            } 
	        else
	        {
		        newWidth = parseInt(oNavCell.style.width);
            } 
	    }
	    oNavCell.style.width = newWidth + px;
        oSplitterCell.style.left = oNavCell.style.width;
        oSplitterCell.style.width = "5px";
        oContentCell.style.left = parseInt(oSplitterCell.style.width) + newWidth  + px;
    }

    SetTocPanelCookie(cookieName,oNavCell.style.width, days);
    ResizeContentArea();
    return;
}



//  Change the size of the Content container
function ResizeContentArea()
{
    if (winWidth - parseInt(oContentCell.style.left) < minWidth)
    { 
        oContentCell.style.width = minWidth + px;
    } 
    else
    { 
	    oContentCell.style.width = winWidth - parseInt(oContentCell.style.left)  + px;
    } 
    return;
}

/*
------------------------------------------
Cookies and Misc Functions
-------------------------------------
*/
// Set cookie on each event
// Function to set the Toc panel size
// Browser must support cookies and script
function SetTocPanelCookie(sName, sValue, days)
{
    if (window.navigator.cookieEnabled == true)
    {
	    var expires = ";";
	    if (days > 0)
	    {
            var cookieDate = new Date();
		    cookieDate.setTime(cookieDate.getTime()+(days*24*360000));
            expires = "; expires=" + cookieDate.toGMTString();
	    }
        var CookieInfo = sName + "=" + escape(sValue) + "; expires=-1" + "; path=/"; 
        document.cookie = CookieInfo;

        var CookieInfo = sName + "=" + escape(sValue) + expires + "; path=/"; 
        document.cookie = CookieInfo;
   }
   return;
}

// Function to get the Toc panel size
// Browser must support cookies and script
// Special for VS, by default do not show the TOC on first instance,
// and support the cookie state on subsquent pages
function GetTocPanelCookie(sName)
{    
    var currentTocWidth = -1;
	var allCookie = String(document.cookie);
	var pos = allCookie.indexOf(sName);
    //  alert(window.history.length);
    
    if (document.referrer == "" && ideUA == true && window.history.length == 0)
	{  
	    SetTocPanelCookie(cookieName, "0px", days);
	    return currentTocWidth;
    }
    else
    {     
	    if (pos != -1 )
	    {
            var tocValue = allCookie.split("; ");

            for (i = 0 ; i < tocValue.length; i++)
            {
	            var cookieValue = tocValue[i].split("=");
	            if (sName == cookieValue[0])
	            {
		            currentTocWidth = cookieValue[1];
		            break;
	            }
            }
	    }
	}
	return currentTocWidth;
}
