/*
Copyright (c) 2006, 
WebWM Sourceforge Developers
Jorge Eduardo Cardona <jorgeecardona at users.sourceforge.net>
Jorge Eduardo Torres <torrmal at users.sourceforge.net>
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    * Neither the name of the WebWM project nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
var IE = document.all?true:false;
var NS = !IE;

/* Data for the DragDrop */
var	DragDrop_State =0;

var	DragDrop_BaseX;
var	DragDrop_BaseY;

var	DragDrop_DeltaX;
var	DragDrop_DeltaY;

var	DragDrop_Id;
var	Object_Id;
var	Resize_Id;

var	DX;
var	DY;
var	MouseXPos;
var	MouseYPos;

var	CornerX;
var	CornerY;

var 	CloseButton=false;

var	LastDropped;
var	LastCounter=1;
/****************************/


document.onmousedown = DocOnMouseDown;
function DocOnMouseDown(ev)
{
	if(NS)
		event=ev;
		

	switch(DragDrop_State)
	{
		case 0:
		{
			DragDrop_BaseX=event.screenX;
			DragDrop_BaseY=event.screenY;
			DragDrop_State = 1;
			DragDropDown(event);
			return false;
			
			break;
		}
		default:
		{
			DragDrop_State = 0;
			break;
		}
	}
	

}

document.onmousemove = DocOnMouseMove;
function DocOnMouseMove(ev)
{	
	if(NS)
		event=ev;
	
	MouseXPos=event.screenX;
	MouseYPos=parseInt(event.screenY)-parseInt(document.body.scrollTop);

	switch(DragDrop_State)
	{
		case 1:
		{
			DragDrop_DeltaX=event.screenX-DragDrop_BaseX;
			DragDrop_DeltaY=event.screenY-DragDrop_BaseY;
			
			DragDropMove(event);
			DragDropResize(event);
			return false;
			break;
		}
		default:
		{
			DragDrop_State = 0;
			DragDrop_Id=0;
			break;	
		}
	}	
}

document.onmouseup = DocOnMouseUp;
function DocOnMouseUp(ev)
{	
	if(NS)
		event=ev;
		

	switch(DragDrop_State)
	{
		case 1:
		{
			
			DragDrop_State = 0;
			DragDropUp(event);
			
			DragDrop_Id=0;
			break;
		}
	}
}



function SetObjectID(ObID)
{
	Object_Id=ObID;
}

function DragDropDown(event)
{
	if(!(boxID=Object_Id)) return;
	box=document.getElementById(boxID);
	if(!box) return;
	DX=parseInt(box.style.left);
	DY=parseInt(box.style.top);
}

function DragDropMove(event)
{
	if(!(boxID=Object_Id)) return;
	box=document.getElementById(boxID);
	if(!box) return;
	box.style.top = DY + DragDrop_DeltaY;
	box.style.left = DX +DragDrop_DeltaX ;
	
}

function DragDropResize(event)
{
	var box,boxID,temp;
	if(!(boxID=Resize_Id)) return;
	boxID="_Resize";
	box=document.getElementById(boxID);
	if(!box) return;
	box.style.width =((temp=(CornerX+DragDrop_DeltaX))<60)?box.style.width:temp;
	box.style.height =((temp=(CornerY+DragDrop_DeltaY))<30)?box.style.height:temp;

	
}

function DragDropUp(event)
{
	SetObjectID(null);
	
	WindowMoveDrop(WindowDivId);
	ResizeDrop(Resize_Id);
	Resize_Id=null;

}

function ResizeDrop(obID){
	var  box;
	boxID="_Resize";
	box=document.getElementById(boxID);
	if(!box) return;
	SetWindowSize(obID,box.style.width,box.style.height);
	box.style.visibility="hidden";
	
}

function Detection()
{
	if(document.all)
	{
		alert('Internet Explorer');
	}
	else
	{
		alert('Mozilla Firefox');		
	}
}


function OnMouseDown1()
{
	box = document.getElementById(100);
	box.style.left=200;
	alert('Event OnMouseDown');
}


function ShowObjectxy(ObjectID,x,y)
{
	if(!ObjectID) return false;
	box=document.getElementById(ObjectID);
	box.style.left=(x)?x:box.style.left;
	box.style.top=(y)?y+document.body.scrollTop:box.style.top;
	box.style.visibility="visible";	
	
}

function ShowObject(ObjectID)
{
	if(!ObjectID) return false;
	box=document.getElementById(ObjectID);
	
	box.style.visibility="visible";	
	
}

function ShowObjectX(ObjectID)
{
	var box;
	if(!ObjectID) return false;
	box=document.getElementById(ObjectID);
	ChangeActive(ObjectID);

	box.style.visibility="visible";
	box.style.position="absolute";

	
}

function HideObject(ObjectID)
{	
	var box1;
	box1=document.getElementById(ObjectID);
	box1.style.visibility="hidden";
	box1.trigged=false;
	
}

function CollapseObject(obID)
{
	var box1,box2,box3;
	if(!obID) return;
	
	box1=document.getElementById(obID);
	box1.style.visibility="collapse";
	box1.trigged=false;
}

function ChangeActive(obID){
	var box1,box2,box3;
	if(!obID) return;
	
	box1=document.getElementById(obID);
	box3=document.getElementById(obID+"_Move");
	box2=document.getElementById("_Protection");
	box2.style.position="relative";
	if(LastDropped!=obID){
		LastCounter++;
		box1.stylevisibility="hidden";
		
		box2.style.zIndex=LastCounter+2;
		box3.style.zIndex=LastCounter+3;
		box1.style.zIndex=LastCounter;
		box1.stylevisibility="visible";
		box2.stylevisibility="visible";
	}
}

function WindowMoveDrag(obID)
{
	var box1;
	var box2;
	var box3;
	var index1;

	if(!obID) return;
	ChangeActive(obID);

	box3=document.getElementById("_Protection");
	box1=document.getElementById(obID+"_Move");
	box2=document.getElementById(obID+"_Frame");
	box4=document.getElementById(obID+"_Body");
	//alert('ja');
	
	box3.style.left=-10;
	box3.style.top=-10;
	box3.style.width=1800;
	box3.style.height=1600;
	box3.style.position="absolute";
	box3.style.visibility="visible";
	if(IE) return SetObjectID(obID);
	HideObject(obID);
	//box2=document.getElementById("_Frame");
	box1.style.width=parseInt(box2.style.width);
	box1.style.height=parseInt(box2.style.height);	
	box1.style.position="relative";
	
	box1.style.visibility="visible";
	box1.style.left=2;//box2.style.left;
	box1.style.top=-2;
	
	box1.style.position="absolute";
	
	SetObjectID(obID+"_Move");
	WindowDivId=obID;

}


function WindowMoveDrop(obID)
{
	var box1;
	var box2;
	var x,y;
	WindowDivId=null;
	SetObjectID(null);
	HideObject("_Protection");
	box3=document.getElementById("_Protection");
	box3.style.zIndex=0;
	if(!obID) return;
	
	LastDropped=obID;
	if(IE) return SetObjectID(null);
	if(CloseButton==false)ShowObject(obID);
	else CloseButton=false;
	box1=document.getElementById(obID+"_Move");
	box2=document.getElementById(obID);
	box2.style.left=parseInt(box2.style.left)+parseInt(box1.style.left);
	box2.style.top=parseInt(box2.style.top)+parseInt(box1.style.top);
	box1.style.visibility="hidden";
}

function WindowMoveOut(obID)
{
	DragDropMove(null);
}

function WindowRefresh(obID)
{
	
	var box1;
	box1=document.getElementById(obID);
	
	if(box1.style.visibility!="hidden"){
		box1.style.visibility="hidden";
		box1.style.visibility="visible";
		WindowMoveDrag(obID);
		WindowMoveDrop(obID);
	}
}

function CloseWindow(obID){
	CloseButton=true;
	HideObject(obID);
	
}

function SetSize(obID,W,H)
{
	var box;
	box=document.getElementById(obID);
	if(!box) return;

	if(W){	box.style.width=W;
		box.width=W;
	}
	if(H){	box.style.height=H;
		box.height=H;
	}
}

function SetWindowSize(obID,W,H)
{	
	var box;
	box=document.getElementById(obID);
	box.style.state="Max";
	SetSize(obID+"_IFrame",W,(H)?(parseInt(H)-19):H);
	SetSize(obID+"_Body",W,20);//(H)?(parseInt(H)):H);
	SetSize(obID+"_Div_Body",W,(H)?(parseInt(H)-19):H);
	SetSize(obID+"_Top",W,false);
	SetSize(obID+"_Frame",W,H);
	SetSize(obID+"_Move",W,H);
	SetSize(obID+"_IFrameN",W,(H)?(parseInt(H)-19):H);
	SetSize(obID,W,H);
	
	
}

function MinimizeWindow(obID)
{	
	
	var box,box2;
	ChangeActive(obID);
	box=document.getElementById(obID);
	box2=document.getElementById(obID+"_IFrame");
	if((box.style.state=="Max") || !box.style.state){
		box.style.lheight=(box2.style.height)?box2.style.height:box2.height;
		box.style.state="Min";
		SetSize(obID+"_IFrame",false,1);
		SetSize(obID+"_Body",false,1);
		SetSize(obID+"_Div_Body",false,1);
		SetSize(obID+"_Frame",false,19);
		SetSize(obID+"_Move",false,19);
		SetSize(obID+"_IFrameN",false,1);
		SetSize(obID,false,19);
	}
	else {

		box.style.state="Max";
		SetWindowSize(obID,false,parseInt(box.style.lheight)+19);
		
	}

}

function ResizeWindow(obID)
{
	
	var box,box2,box3,box4,boxID;
	if(!(Resize_Id=obID)) return;
	
	boxID="_Resize";

	box=document.getElementById(boxID);
	box2=document.getElementById(obID);
	box3=document.getElementById("_Protection");
	box4=document.getElementById(obID+"_IFrame");
	if(!box || !box2) return;

	box3.style.left=-10;
	box3.style.top=-10;
	box3.style.width=1800;
	box3.style.height=1600;
	box.style.top = box2.style.top;
	box.style.left = box2.style.left;
	
	box.style.zIndex=LastCounter+2;
	box3.style.zIndex=LastCounter+1;

	box3.style.visibility="visible";
	box.style.visibility="visible";
	CornerX=(box.style.width = ((parseInt(box4.width)?parseInt(box4.width):parseInt(box4.style.width))+2));
	CornerY=(box.style.height = (parseInt(box4.height)+19));
}






///////////////////////////////////////////////////////
//THIS MUS BE MOVED TO A NEW FILE, 
//IT WILL BE PART OF THE DESKTOP ENVIRONMENT
//
///////////////////////////////////////////////////////
function LoggedOK()
{
	ShowObjectX('MenuWindow');
	ReloadIframe('MenuWindow');
	//document.all.MenuWindow_IFrameN.reload();//frames['MenuWindow_IFrameN'].document.location.reload();
	HideObject('LoginWindow');
	ReloadIframe('LoginWindow');
	ReloadIframe('LogoutWindow');
	
	
}

function ReloadIframe(ObId)
{
	var iframe = document.getElementById(ObId+'_IFrame');
 	if (!iframe) return false;
 	iframe.src = iframe.src;
}

function ShowNReload(ObID)
{
	ShowObjectX(ObID);
	ReloadIframe(ObID);
	ChangeActive(ObID);
}
