zindex = 100;


function bring_window_to_front(elementWindow)
{
	zindex++;
	elementWindow.style.zIndex = zindex;
}

function getClientWidth()
{
    return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}

function getClientHeight()
{
    return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
}

function beginDrag(elementToDrag, event){
	bring_window_to_front(elementToDrag);
	
	var deltaX = event.clientX - parseInt(elementToDrag.style.left);
	var deltaY = event.clientY - parseInt(elementToDrag.style.top);
    if (elementToDrag.style.top < 5) {exit;}
	if (document.addEventListener){
		document.addEventListener("mousemove", moveHandler, true);
		document.addEventListener("mouseup", upHandler, true);
	}
	else if (document.attachEvent){
		document.attachEvent("onmousemove", moveHandler);
		document.attachEvent("onmouseup", upHandler);
	}
	else {
		var oldmovehandler = document.onmousemove;
		var olduphandler = document.onmouseup;
		document.onmousemove = moveHandler;
		document.onmouseup = upHandler;
	}
	if (event.stopPropagation) event.stopPropagation();
	else event.cancelBubble = true;
	if (event.preventDefault) event.preventDefault();
	else event.returnValue = false;
	function moveHandler(e){
		if (!e) e = window.event;
      //   window.console.log(window.clientWidth);
       if ((e.clientY - deltaY) < 5 ) {return;}
       if ((e.clientY - deltaY) > getClientHeight()-100) {return;}
       if ((e.clientX - deltaX) > getClientWidth()-400) {return;}
       if ((e.clientX - deltaX) < 1) {return;}        
		elementToDrag.style.left = (e.clientX - deltaX) + "px";
		elementToDrag.style.top = (e.clientY - deltaY) + "px";
		setcookie(elementToDrag.id + 'left', (e.clientX - deltaX));
		setcookie(elementToDrag.id + 'top', (e.clientY - deltaY));
		if (e.stopPropagation) e.stopPropagation();
		else e.cancelBubble = true;
	}
	function upHandler(e){
		if (!e) e = window.event;
		if (document.removeEventListener){
			document.removeEventListener("mouseup", upHandler, true);
			document.removeEventListener("mousemove", moveHandler, true);
		}
		else if (document.detachEvent){
			document.detachEvent("onmouseup", upHandler);
			document.detachEvent("onmousemove", moveHandler);
		}
		else {
			document.onmouseup = olduphandler;
			document.onmousemove = oldmovehandler;
		}
		if (e.stopPropagation) e.stopPropagation();
		else e.cancelBubble = true;
	}
}

function hideWindow(floatWindow)
{
	htmlstr ='<a href="javascript:void(0)" onclick="closeWindow(\'' + floatWindow + '\')"><img src="images/close_button.png" alt="close" /></a>&nbsp;<a href="#" onclick="showWindow(\'' + floatWindow + '\')"><img src="images/show_button.gif" alt="show"/></a>';
	$('#' + floatWindow + ' .pop_up_content_400').hide();
	$('#' + floatWindow + ' .pop_up_content_500').hide();
	$('#' + floatWindow + ' .pop_up_content_600').hide();
	$('#' + floatWindow + ' .buttonsgroup').html(htmlstr);
}

function showWindow(floatWindow)
{	
	html ='<a href="javascript:void(0)" onclick="closeWindow(\'' + floatWindow + '\')"><img src="images/close_button.png" alt="close" /></a>&nbsp;<a href="#" onclick="hideWindow(\'' + floatWindow + '\')"><img src="images/hide_button.png" alt="hide"/></a>';
	$('#' + floatWindow + ' .pop_up_content_400').show();
	$('#' + floatWindow + ' .pop_up_content_500').show();
	$('#' + floatWindow + ' .pop_up_content_600').show();
	$('#' + floatWindow + ' .buttonsgroup').html(html);
}

function closeWindow(floatWindow, linkID)
{
    $('#' + floatWindow).hide();
    if (linkID == undefined)
    {
        var rWnd = /^window(\d+)$/i;
        var matches = rWnd.exec(floatWindow);
        if (matches)
        {
            linkID = matches[1];
        }
    }
    $('#lnk' + linkID).removeClass('slc');
    $('#lnk' + linkID).addClass('menunormal');
	setcookie(floatWindow + '_show', '0');
}

