// Set document events
document.onmousedown = MouseDown;
document.onmouseup   = MouseUp;
document.onmousemove = MouseMove;

function GetObjectPosition(obj) 
{
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}
function GetObjectSize(obj)
{
	var w = h = 0;
	if (obj)
	{
	    w = obj.offsetWidth;
	    h = obj.offsetHeight;
	}
	return [w,h];
}


/**************************************************
 * Box movement
 *************************************************/
var movex = 0;
var movey = 0;
var gBox;
function MouseDown(e)
{
	var boxes;
	var elm;
	var coordinate, boxx, boxy;

	if (!e) e = window.event;
	if (e.srcElement) 
		elm = e.srcElement;
	else if (e.target)	   
		elm = e.target;
	
	if (elm.parentNode == null)
		return;
	if (elm.className != 'boxheader' && elm.parentNode.className != 'boxheader')
		return;
	
	while (elm.className != 'box')
	{
		elm = elm.parentNode;
		if (elm == null)
			return;
	}
	gBox = elm;
		
	// Put all other windows below current window
	boxes = document.getElementsByTagName('div');
	for (var i=0; i < boxes.length; i++)
	{
		if (boxes[i].className == 'box')
			boxes[i].style['zIndex'] = 0;
	}
	gBox.style['zIndex'] = 10;
	
	// Set opacity
	if (gBox.currentStyle)
		gBox.style['filter'] = 'alpha(opacity=90)'; 
	else if (gBox.getComputedStyle)
		gBox.style['opacity'] = 0.9;
	
	coordinate = GetObjectPosition(gBox);
	boxx = coordinate[0];
	boxy = coordinate[1];

	movex = e.clientX - boxx;
	movey = e.clientY - boxy;
}
function MouseUp(e)
{
	if (gBox == null)
		return;
		
	if (gBox.currentStyle)
		gBox.style['filter'] = 'alpha(opacity=100)'; 
	else if (gBox.getComputedStyle)
		gBox.style['opacity'] = 1.0;
		
	gBox = null;
}
function MouseMove(e)
{
	if (gBox != null)
	{
		if (!e) e = window.event;
		
		gBox.style['position'] = 'absolute';
		gBox.style['left'] = e.clientX - movex;
		gBox.style['top']  = e.clientY - movey;
	}	
	return false;
}


/**************************************************
 * Box show/hide contents
 *************************************************/
function MinimizeSwitch( arrow_elm, do_switch )
{
	var header, box;
	var divs;

	// Get box from minimize button
	header = arrow_elm.parentNode;
	if (header == null) return false;
	box = header.parentNode;
	if (box == null) return false;
	
	ShowHideBoxContents(box, do_switch);
}
function ShowHideBoxContents(box, do_switch)
{
	var new_style;
	var divs, buttons;
	var minimizebutton
	var elms = new Array();
	var counter = 0;
	
	if (box == null) return;

	// Get elements
	divs = box.getElementsByTagName('div');
	for (var i = 0; i < divs.length; i++)
	{
		if (divs[i].className == 'boxheader')
		{
			// Get minimize button
			buttons = divs[i].getElementsByTagName('a');
			for (var j = 0; j < buttons.length; j++)
			{
				if (buttons[j].className.indexOf('boxarrow') >= 0)
					minimizebutton = buttons[j];
			}
		}
		else
			elms[counter++] = divs[i];
	}
	
	if (elms.length == 0) return false;
	
	// When the button is pressed
	if (do_switch == true)
	{
		// Get current style from object
		new_style = elms[0].style['display'];

		// Switch style
		if (new_style != 'none') new_style = 'none';
		else					 new_style = 'block';

		// Save setting in cookie			
		dk.qbrix.Cookie.Set(elms[0].id, new_style);
	}
	else
	// When called from dialog load
	{
		// Get style from cookie
		new_style = dk.qbrix.Cookie.Get(elms[0].id);
		
		// First time around get from element
		if (new_style == null)
			new_style = elms[0].style['display'];
		
		// Display if not set explicitly
		if (new_style == null)
			new_style = 'block';
	}
	
	// Set new style
	for (var i = 0; i < elms.length; i++)
		elms[i].style['display'] = new_style;

	// Set arrow image
	if (new_style == 'none') minimizebutton.className = 'boxarrow1';
	else					 minimizebutton.className = 'boxarrow2';
}
