var mouseX;
var mouseY;
var maxheight = 0;
var maxwidth = 0;
var max=1;
var don=false;
var DRAG_lastX=0;
var DRAG_lastY=0;
var DRAG_dragging;
var loaded;

var cl;
var cT = 0;
var cL = 0;
var gapL = 0;
var gapT = 0;


function test(ctr,val1)
{
	loaded=true;
	evalim1(ctr)
	//document.getElementById("genlayeri"+ctr).innerHTML ="<IMG SRC='"+val1+"'>";	
	//document.getElementById("genlayer"+ctr).style.top = (mouseY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop))-50;
	//document.getElementById("genlayer"+ctr).style.left = mouseX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
	//document.getElementById("genlayer"+ctr).style.visibility = 'visible';
}

function evalim1(ctr)
{
	loaded=true;
	var cl2 = "";	
	cl = "layer"+ctr;
	for (var vlp=1;vlp<=max;vlp++)
	{
		cl2 = "layer"+vlp;
		if (vlp==ctr)
		{
			document.getElementById(cl2).style.zIndex =10;			
		}
		else
		{
			document.getElementById(cl2).style.zIndex =1;
		}
	} 
}




// Mouse Capture functions
function DRAG_begindrag(e) 
{      
if (loaded==true)
{
	if (IE)
	{
	      	if (event.button == 1) 
		{       
		   	DRAG_lastX=mouseX;                
			DRAG_lastY=mouseY;      

			cT = parseFloat(document.getElementById(cl).style.top);
			cL = parseFloat(document.getElementById(cl).style.left);
			gapL =DRAG_lastX - cL;
			gapT =DRAG_lastY - cT;

			DRAG_dragging=true; 
			DRAG_drag();  
		}        
		else 
		{                
			/*right mouse here*/                
			//return true;        
		}

	   } else {
		
		if ((don)&&(e.which == 1))
		{       
	 			DRAG_lastX=mouseX;                
				DRAG_lastY=mouseY;       

				cT = parseFloat(document.getElementById(cl).style.top);
				cL = parseFloat(document.getElementById(cl).style.left);
				gapL =DRAG_lastX - cL;
				gapT =DRAG_lastY - cT;
         
				DRAG_dragging=true;   
				DRAG_drag();               
		}        
		else 
		{                
			/*right mouse*/                
			//return true;        
		}
	}
}
}

function DRAG_enddrag(e) 
{     
if (loaded==true)
{   
 	if (IE)
	{
		if (event.button == 1) 
		{       
			don = false
			DRAG_dragging=false;                
		}        
		else {                
			/*right mouse*/                
			//return true;        
		}
	}
	else
	{
		if ((don)&&(e.which == 1))
		{    
		   	document.getElementById(cl).style.top =  DRAG_lastY- gapT;		
			document.getElementById(cl).style.left = DRAG_lastX- gapL;    
			
			don = false;
			DRAG_dragging=false;                			       
		}        
		else {                
		/*right mouse*/                
		//return true;        
		}
	}
}
}


function DRAG_drag(e) 
{    
if (loaded==true)
{    		
	if (IE)
	{
		if (DRAG_dragging) 
		{       
		        document.getElementById(cl).style.top =  DRAG_lastY - gapT;			
			document.getElementById(cl).style.left = DRAG_lastX- gapL;

			DRAG_lastX = mouseX;                
			DRAG_lastY = mouseY;                

			don = true;
		}        	
		else 
		{                
			//return true;        
		}

	}
	else
	{
		if (DRAG_dragging) 
		{                
			document.getElementById(cl).style.top =  DRAG_lastY- gapT;		
			document.getElementById(cl).style.left = DRAG_lastX- gapL;

			DRAG_lastX = mouseX;
			DRAG_lastY = mouseY;
		        don = true;
		}        	
		else 
		{                
			//return true;        
		}
	}
}
}




function mouseXY(e) {

	don = false; 
        
	if (IE)
	   {

		mouseX = event.clientX;			 
	      	mouseY = event.clientY;

		DRAG_lastX = mouseX;                
		DRAG_lastY = mouseY;     


	if (loaded==true)
	{
			if ((mouseY < parseFloat(document.getElementById(cl).style.top) + parseFloat(document.getElementById(cl).style.height))&&(mouseY > parseFloat(document.getElementById(cl).style.top)))
			{
				if ((mouseX < parseFloat(document.getElementById(cl).style.left) + parseFloat(document.getElementById(cl).style.width))&&(mouseX > parseFloat(document.getElementById(cl).style.left)))
				{
					//document.getElementById(cl).style.color = "blue";
					don = true;
				}
				else
				{
					//document.getElementById(cl).style.color = "yellow";
				}
			}
			else
			{
					//document.getElementById(cl).style.color = "yellow";
			}


	        if ((don == true)&&(event.button == 1))
		{	
			 DRAG_drag();   
		}

	      	
	}		 
	      
	   } else {

		mouseX = e.pageX;
	      	mouseY = e.pageY;
		DRAG_lastX = mouseX;                
		DRAG_lastY = mouseY;     

	if (loaded==true)
	{
			if ((mouseY + 10 < parseFloat(document.getElementById(cl).style.top) + parseFloat(document.getElementById(cl).style.height))&&(mouseY - 10 > parseFloat(document.getElementById(cl).style.top)))
			{
				if ((mouseX + 10 < parseFloat(document.getElementById(cl).style.left) + parseFloat(document.getElementById(cl).style.width))&&(mouseX - 10 > parseFloat(document.getElementById(cl).style.left)))
				{
					//document.getElementById(cl).style.color = "blue";
					don = true;
					ired = false;
				}
				else if ((mouseX < parseFloat(document.getElementById(cl).style.left) + parseFloat(document.getElementById(cl).style.width))&&(mouseX > parseFloat(document.getElementById(cl).style.left)))
				{
					//document.getElementById(cl).style.color = "red";
					//don = false;
					ired = true;
				}
				
			}
			else if ((mouseY < parseFloat(document.getElementById(cl).style.top) + parseFloat(document.getElementById(cl).style.height))&&(mouseY > parseFloat(document.getElementById(cl).style.top)))
			{
				
				if ((mouseX - 10 > parseFloat(document.getElementById(cl).style.left) + parseFloat(document.getElementById(cl).style.width))&&(mouseX + 10 < parseFloat(document.getElementById(cl).style.left)))
				{
					//document.getElementById(cl).style.color = "blue";
					don = true;
					ired = false;
				}
				else 
				if ((mouseX < parseFloat(document.getElementById(cl).style.left) + parseFloat(document.getElementById(cl).style.width))&&(mouseX > parseFloat(document.getElementById(cl).style.left)))
				{
					//alert ("!!");
					//document.getElementById(cl).style.color = "red";
					//don = false;
					ired = true;
				}
				
			}
			else
			{
					document.getElementById(cl).style.color = "yellow";
					ired = false;
			}

			if ((don)&&(e.which == 1))
			{
				DRAG_drag();   	 
			}	
	}
	   }
}

