

/*

	####   POP UP WINDOW HANDLING

*/

// move window

var mouseX = 0;
var mouseY = 0;
var lastMouseX = null;
var lastMouseY = null;
var dim;
var width_number;
var height_number;

var lastURL;

function enableMoveListener()
{
	var divObj = document.getElementById('upDiv');
	addEvent( divObj, "mousemove", mouseMoving);
}

function disableMoveListener()
{
	var divObj = document.getElementById('upDiv');
	removeEvent( divObj, "mousemove", mouseMoving);
	
	lastMouseX = null;
	lastMouseY = null;
}

function mouseMoving(e)
{
	if (!e) e = window.event;
	
	mouseX = e.clientX;
	mouseY = e.clientY;
	
	if(lastMouseX != null)
	{
		
		
		var deltaX = mouseX - lastMouseX;
		var deltaY = mouseY - lastMouseY;
		
		var divObj = document.getElementById('popup');
		
		var left_number = parseInt(divObj.style.left.replace(/^(\d+)(px)$/, "$1"));
		var top_number = parseInt(divObj.style.top.replace(/^(\d+)(px)$/, "$1"));
		
		
		var left_String = (left_number+parseInt(deltaX)) + "px";
		var top_String = (top_number+parseInt(deltaY)) + "px";
		
		if(mouseX > 0 )//&& (((left_number + width_number < dim[0]-40) && deltaX >0) ||  deltaX <0)
		{
			divObj.style.left = left_String;
		}
		else
		{
			disableMoveListener();
		}
		if(mouseY > 0 )//&& (top_number + height_number < dim[1]-20)
		{
			divObj.style.top = top_String;
		}
		else
		{
			disableMoveListener();
		}
		//alert(left_String +", "+  top_String +", "+ top_number +", "+deltaY);
		
	}
	
	lastMouseX = mouseX;
	lastMouseY = mouseY;
	
}


function addEvent( obj, type, fn )
{
   if (obj.addEventListener) {
      obj.addEventListener( type, fn, false );
   } else if (obj.attachEvent) {
     
      obj.attachEvent( "on"+type, fn );
   }
   
   return obj;
}

function removeEvent( obj, type, fn )
{
  
  if (obj.removeEventListener) {
      obj.removeEventListener( type, fn, false );
   } else if (obj.detachEvent) {
      obj.detachEvent( "on"+type, fn );// problem with IE
   }
}


// opening and closing


var lastwidth;
var lastheight;
var lastheightOffset;
var lastcaptionID;
var lastcaption;
var lastURL;
var limiter = 0;




function openPicWindow(myUrl,captionID) 
{ 
	lastURL = myUrl;
	lastcaptionID = captionID;
	
	myUrl = encodeURIComponent(myUrl);
	var url = "y-cms/system2_curr_exhib/frontendfiles/ajaxGetPicDimension.php?picurl="+myUrl;
	
	// Browser Weiche
	
	var httpRequest = false; 
	if(window.XMLHttpRequest)  // Mozilla, Safari,... 
		httpRequest = new XMLHttpRequest(); 
	else if(window.ActiveXObject) 
	{ // IE 
		try 
		{ 
			httpRequest = new ActiveXObject("Msxml2.XMLHTTP"); 
		} 
		catch (e)
		{ 
			try 
			{ 
				   httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); 
			} 
			catch (e) {} 
		} 
	} 
   if(!httpRequest) return false; 
   
   // Handling response
   
	httpRequest.onreadystatechange = function() 
	{ 
		if(httpRequest.readyState == 4) 
		{ 
			if(httpRequest.status == 200) handleHttpResponse(httpRequest.responseText); 
			else 
			return false; 

		} 
		else 
		return false; 
	} 
	httpRequest.open('get', url, true); 
	httpRequest.send(null); 
	
	return true;
}


function handleHttpResponse(content)
{
	//disableMoveListener();// crash in IE !
	
	if(content == "false") return false;
	
	w = parseInt(content.replace(/^(\d+)-(\d+)$/, "$1"));
	h = parseInt(content.replace(/^(\d+)-(\d+)$/, "$2"));
	
	lastwidth = w;
	lastheight = h;
	 
	 
	 var pic_caption = document.getElementById(lastcaptionID) ? document.getElementById(lastcaptionID).innerHTML : "&nbsp;";
	 lastcaption = pic_caption;
	 
	 var heightOffset = (pic_caption.match(/<br/i))? 45 : 25; // <BR in IE6
	 lastheightOffset = heightOffset;
	
	 
	//w += 40;	// by try & error
	h += heightOffset;
	
	// for shadow: 18 for padding 20 for borders 4
	
	var cssOffset = 42;
	
	h += cssOffset;
	w += cssOffset;
	
	
		
	var divObj;
	
	if(document.getElementById)
	{
		var divObjContent = document.getElementById('popup_content');
		divObjContent.innerHTML = "";
		
		divObj = document.getElementById('popup');
		
		var offset =  get_PageOffset();
		var offsetX = offset[0] + 10;
		var offsetY = offset[1] + 10;
				
		divObj.style.top = offsetY+"px";
		divObj.style.left = offsetX+"px";
		divObj.style.width = w+"px";
		divObj.style.height = h+"px"; 
		
		divObj.style.visibility = "hidden";
		
		
		// fill popup
		
		// pic handling
	
		var picObj = new Image();
		picObj.src = lastURL;
			
		var innerHTMLContent_upDiv = "<div id='upDiv' style='z-index:201;position:absolute;top:0;left:0;padding:10px;margin:0;border:solid 2px #dddddd;background-color:#333;width:"+lastwidth+"px;height:"+(lastheight+lastheightOffset)+"px;'>";
		innerHTMLContent_upDiv += "<table border='0' cellpadding='0' border='0' cellspacing='0' style='width:100%;margin:0;padding:0'><tr>";
		innerHTMLContent_upDiv += "<td colspan='2' id='pic_cell' style='background-image:url("+picObj.src+");background-repeat:no-repeat;";
		innerHTMLContent_upDiv += "text-align:center;vertical-align:middle;height:"+(lastheight+6)+"px'><img src='y-cms/Includes/dummy.gif' width='"+picObj.width+"' height='"+picObj.height+"' /></td></tr>";
		
		innerHTMLContent_upDiv += "<tr><td style='vertical-align:middle;text-align:left;'><span id='popup_caption' class='thumbs_text'>"+lastcaption+"</span></td>";
		innerHTMLContent_upDiv += "<td style='vertical-align:bottom;text-align:right;'><a href='javascript:void null;' onclick='javascript:close_popup();'>";
		innerHTMLContent_upDiv += "<img src='images/closebutton.gif' width='18' height='17' alt='' border='0' style='vertical-align:bottom;' /></a></td></tr>";
		innerHTMLContent_upDiv += "</table>";
		innerHTMLContent_upDiv += "</div>";
		
		//innerHTMLContent_upDiv += "<div style='z-index:202;position:absolute;top:" + (picObj.height + 12)+"px;left:12px;width:"+picObj.width+"px;height:"+lastheightOffset+"px;'>";
		//innerHTMLContent_upDiv += "<img src='y-cms/Includes/dummy.gif' width='"+picObj.width+"' height='"+lastheightOffset+"' /></div>";
		 
		var innerHTMLContent_shadow = "<table border='0' cellpadding='0' border='0' cellspacing='0' style='margin:0;padding:0;'>";
		innerHTMLContent_shadow += "<tr><td style='width:"+(lastwidth+24)+"px;height:"+(lastheight+24+lastheightOffset)+"px;'><img src='y-cms/Includes/dummy.gif' width='1' height='1' /></td>";
		innerHTMLContent_shadow +=	"<td class='popup_td' style='width:18px;background:url(y-cms/Includes/shadow_18x1200.png) no-repeat;'><img src='y-cms/Includes/dummy.gif' width='1' height='1' /></td></tr>";
		innerHTMLContent_shadow +=	"<tr><td class='popup_td' style='height:18px;background-image:url(y-cms/Includes/shadow_1600x18.png);background-repeat:no-repeat;'><img src='y-cms/Includes/dummy.gif' width='1' height='1' /></td>";
		innerHTMLContent_shadow +=	"<td class='popup_td' style='background:url(y-cms/Includes/shadow_18x18.png) no-repeat;'><img src='y-cms/Includes/dummy.gif' width='1' height='1' /></td></tr>";
		innerHTMLContent_shadow +=	"</table>";
		
		document.getElementById('popup_content').innerHTML = innerHTMLContent_shadow + innerHTMLContent_upDiv;
		
		showLoadingAnimation(picObj);
		
		var divObj = document.getElementById('popup');
		if(divObj.style.visibility = "hidden") divObj.style.visibility = "visible";
		
		var upper_divObj = document.getElementById('upDiv');
		
		addEvent( upper_divObj, "mousedown", enableMoveListener);
		addEvent( upper_divObj, "mouseup", disableMoveListener);
		addEvent( upper_divObj, "mouseout", disableMoveListener);
		
		
	}
	else return false;
	
	
	
}

function close_popup()
{
	if(document.getElementById)
	{
		var divObjContent = document.getElementById('popup_content');
		divObjContent.innerHTML = "";
		var divObj = document.getElementById('popup');
		divObj.style.visibility = "hidden";
		
	}
}




function showLoadingAnimation(picObj)
{
	if(picObj.complete)
	{
		document.getElementById("pic_cell").innerHTML = "&nbsp;";
		document.getElementById("pic_cell").style.textAlign = "right";
		document.getElementById("pic_cell").style.verticalAlign = "bottom";
	}
	else
	{
		
		
		if(limiter < 600) 
		{
			if(limiter % 8 > 1) document.getElementById("pic_cell").innerHTML = "<span class='load'>Loading ...</span>";
			else document.getElementById("pic_cell").innerHTML = "<span class='load'>Loading &nbsp; &nbsp;</span>";
			
			window.setTimeout(function () { showLoadingAnimation(picObj); }, 250);
		}
		else if(limiter >= 600 && limiter < 610) 
		{
		 	document.getElementById("pic_cell").innerHTML = "<span class='load'>Loading timed out!</span>";
		 	window.setTimeout(function () { showLoadingAnimation(picObj); }, 250);
		 	
		}
		else document.getElementById("pic_cell").innerHTML = "&nbsp;";
		
		limiter++;
	}
}

	

function get_PageOffset()
{
	var x,y;
	if (self.pageYOffset) // all except Explorer
	{
		x = self.pageXOffset;
		y = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}
	
	var dim = new Array(x,y);
	return dim;
}	

function get_PageDim()
{
	var w,h;
	
	if (self.innerWidth) // all except Explorer
	{
		w = self.innerWidth;
		h = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		w = document.documentElement.clientWidth;
		h = document.documentElement.clientHeight;
	}
	else if (document.body) // all other Explorers
	{
		w = document.body.clientWidth ;
		h = document.body.clientHeight;
	}
	
	var dim = new Array(w,h);
	return dim;
}


