var IE = document.all?true:false;

// If NS -- that is, !IE -- then set up for mouse capture
//if (!IE) document.captureEvents(Event.MOUSEMOVE);

// Set-up to use getMouseXY function onMouseMove
//document.onmousemove = getMouseXY;
if (!IE) { document.addEventListener("mousemove",getMouseXY, false); }
if (IE) { document.attachEvent("onmousemove", getMouseXY); }

// Temporary variables to hold mouse x-y pos.s
var tempX = 0;
var tempY = 0;

// Main function to retrieve mouse x-y pos.s

function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft;
    tempY = event.clientY + document.body.scrollTop;
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX;
    tempY = e.pageY;
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  
  // show the position values in the form named Show
  // in the text fields named MouseX and MouseY
  //document.Show.MouseX.value = tempX;
  //document.Show.MouseY.value = tempY;
  
  return true;
}

var ajaxRequest;  

function ajaxFunction(){
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	
	ajaxRequest.onreadystatechange = function(){
	if(ajaxRequest.readyState == 4){
			document.getElementById("pop_up").innerHTML = ajaxRequest.responseText;			
		}
	}

}

function getPos(obj) {
	//var obj = document.getElementById(elName);
	var cl = ct = 0;
	if (obj.offsetParent) {
		do {
			cl += obj.offsetLeft;
			ct += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [cl, ct];
}

function getPopup(code) { 
	ajaxFunction();
	var strHtml = "<div style='text-align: center;'><img src='images/loader.gif' /></div>";
	document.getElementById("pop_up").innerHTML = strHtml;	
	ajaxRequest.open("GET", "page_preview/get_popup.php?code=" + code, true);
	ajaxRequest.send(null); 
	
}

function showPopup(code, obj) {
	var popup = document.getElementById("pop_up");
	var pos = getPos(obj);
	
	if (popup.y == 0) {	//if page was reloaded initiate the popup y position to current menu item
		popup.y = pos[1];
		popup.style.top = pos[1] + "px";
	}
	
	popup.anim = true;
	
	popup.newY = pos[1];
	popup.fade_dir = 100;
	popup.style.left =  pos[0] + 180 + "px";
	//popup.style.top = pos[1] + "px";
	popup.style.display = "block";
	getPopup(code);
	clearInterval(popup.t);
	popup.t = setInterval("fader('pop_up')", 50);
	clearInterval(popup.t_mover);
	popup.t_mover = setInterval("move('pop_up')", 50);
}

function movePopup() {
	var popup = document.getElementById("pop_up");
	popup.style.display = "block";
}

function hidePopup() {
	var popup = document.getElementById("pop_up");
//	popup.fade_dir = 0;
	popup.style.display = "none";
//	clearInterval(popup.t);
//	popup.t = setInterval("fader('pop_up')", 50);
}

function fader(id) {
	var obj = document.getElementById(id);
	var op = 0;
	if (obj.op != null) op = obj.op;
	op += (obj.fade_dir - op) / 5;
	if (op < 0) op = 0;
	if (op > 100) op = 100;
	obj.op = op;
	obj.style.opacity = op * 0.01;
	obj.style.filter = "alpha(opacity=" + op + ")";		
	if (op < 5) { 
		obj.style.display = "none";
	} else {
		obj.style.display = "block";
	}
	
	if (op < 5	|| op > 95) clearInterval(obj.t);
}

function move(id) { 
	var obj = document.getElementById(id);
	var y = 0;
	if (obj.y != null) y = obj.y;
	y += (obj.newY - y) / 7;
	obj.y = y;
	obj.style.top = y + "px";
	if (Math.abs(obj.newY - obj.y) < 5) clearInterval(obj.t_mover);
}

function init() { 
	var popup = document.getElementById("pop_up");
	popup.op = 0;
	popup.y = 0;
}

init();

function popupPic(pic,w,h) { 
	ajaxFunction();
	var strHtml = "<div style='text-align: center;'><img src='images/loader.gif' /></div>";
	var popup = document.getElementById("pop_up");
	showdeadcenterdiv(w,h,'pop_up');
	popup.style.width = w + "px";
	popup.style.height = h + "px";
	popup.style.display = "block";	
	document.getElementById("pop_up").innerHTML = strHtml;	
	ajaxRequest.open("GET", "getPic.php?f=" + pic, true);
	ajaxRequest.send(null); 	
}

