﻿//''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
//' Parts of this file are Microsoft Content Management Server - Sample Code
//'
//' This sample code is provided "AS IS" with no warranties, and confers no rights.
//' You assume all risk for your use. (c) 2002 Microsoft Corporation. All rights reserved.
//'
//''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

// Declare variables

var dragDiv = null;

var dragOn = 0
var dragX = 0;
var dragY = 0;

var dragInit = 0;

var zMax = 0;

// Function to handle the dragging on the div element

function initDrag()
{
	if (document.layers) 
	    document.captureEvents(Event.MOUSEMOVE|Event.MOUSEDOWN|Event.MOUSEUP);
	    
	document.onmousemove = dragf;
	document.onmousedown = dragf;
	document.onmouseup = dragf;

	dragDiv = null;
	dragInit=1;

	if (document.getElementsByTagName) 
	   zMax = document.getElementsByTagName("DIV").length;
	else
	    if (document.all)
	        zMax = document.body.all.tags("DIV").length;
	    else
	        if (document.layers) zMax = document.layers.length;
}

// Function to calculate where the element is dragged

function dragf(arg)
{
	ev = arg?arg:event;
	
	if (dragDiv && ev.type=="mousedown")
	{
		dragOn = 1;
		
		dragX=(ev.pageX?ev.pageX:ev.clientX)-parseInt(dragDiv.style.left);
		dragY=(ev.pageY?ev.pageY:ev.clientY)-parseInt(dragDiv.style.top);
		
		dragDiv.style.zIndex = zMax++;

		return false;
	}

	if (ev.type=="mouseup")
	{
		dragOn = 0;
		
		if (dragDiv)
		{
		    SetCookie("consoleID", dragDiv.id, "10080", "/", "", "");
		    SetCookie("consoleTop", dragDiv.style.top + "", "10080", "/", "", "");
		    SetCookie("consoleLeft", dragDiv.style.left + "", "10080", "/", "", "");
		}	
	}
	
	if (dragDiv && ev.type=="mousemove" && dragOn)
	{
		dragDiv.style.left = (ev.pageX?ev.pageX:ev.clientX)-dragX;
		dragDiv.style.top = (ev.pageY?ev.pageY:ev.clientY)-dragY;

		return false;
	}
	
	if (ev.type=="mouseout")
	{
		if (!dragOn)
		    dragDiv = null;
	}
}

// Function to finalize the drag event

function drag(div)
{
	if (!dragInit)
	    initDrag();
	    
	if (!dragOn)
	{
		dragDiv = document.getElementById?document.getElementById(div): 
		document.all?document.all[div]:document.layers?document.layers[div]:null;
		
		if (document.layers)
		    dragDiv.style = dragDiv;
		    
		dragDiv.onmouseout = dragf;
	}
}

// Function to show or hide the div element

function OpenCloseDiv(divName)
{
	if (divName.style.display == "none")
		divName.style.display="block";
	else
		divName.style.display="none";
}	

// Cookie Handling

AddOnLoadEvent(RestorePosition);

function RestorePosition()
{
    var consoleID = GetCookie("consoleID");
    if (consoleID)
    {
        var consoleDiv = document.getElementById?document.getElementById(consoleID):null;
        if (consoleDiv)
        {
            var consoleLeft = GetCookie("consoleLeft");
            var consoleTop = GetCookie("consoleTop");
            if ((consoleLeft) && (consoleTop))
            {
                consoleDiv.style.left = consoleLeft;
                consoleDiv.style.top = consoleTop;
            }
        }
    }
}

