var isIE = (navigator.userAgent.indexOf("MSIE")!=-1);

function showProjectPopup(projectid, pageurl, aerial) {
	if (!aerial) var aerial = 0;
	
	var popupBg = document.getElementById("popupbg");
	if (!popupBg) {
		//first use of this popup, create it
		var  popupBg= document.createElement("div");
		popupBg.id = "popupbg";
		popupBg.style.height = document.body.clientHeight+"px";
		popupBg.onclick = function () {
			closeProjectPopup();
		}
		document.body.appendChild(popupBg);
		
		var popupDiv = document.createElement("div");
		popupDiv.id = "popupdiv";
		document.body.appendChild(popupDiv);
		
		var popupDivHeader = document.createElement("div");
		popupDivHeader.id = "popupdivheader";
		popupDiv.appendChild(popupDivHeader);
		
		var popupDivClose = document.createElement("div");
		popupDivClose.id = "popupdivclose";
		popupDivClose.onclick = function () {
			closeProjectPopup();
		}
		popupDivHeader.appendChild(popupDivClose);
		
		var popupDivContent = document.createElement("div");
		popupDivContent.id = "popupdivcontent";
		popupDiv.appendChild(popupDivContent);
		
		var popupBg = document.getElementById("popupbg");
	}
	var popupDiv = document.getElementById("popupdiv");
	
	popupBg.style.display = "block";
	popupDiv.style.display = "block";
	
	window.onresize = function() {
		arrangePopupDiv();
	}
	window.onscroll = function() {
//		scroll(0,0);
		arrangePopupDiv();
	}
	
	Request.send("index.php?p=project_detail&id="+projectid+"&ajaxcall=1"+(aerial ? "&aerial=true" : ""), "GET", populateProject);
}

function closeProjectPopup() {
	var popupBg = document.getElementById("popupbg");
	if (popupBg) {
		popupBg.style.display = "none";
	}
	
	var popupDiv = document.getElementById("popupdiv");
	if (popupDiv) {
		popupDiv.style.display = "none";
	}

	var popupDivContent = document.getElementById("popupdivcontent");
	if (popupDivContent) {
		popupDivContent.innerHTML = "";
	}
	
	window.onresize = function() {
	}
	window.onscroll = function() {
	}
}

function populateProject(response) {
	var popupDivContent = document.getElementById("popupdivcontent");
	if (popupDivContent) {
		var parts = response.responseText.split("||||");
		var thecontent = parts[0];
		thecontent = thecontent.replace(/SetPicture/g, "SetPictureMain")
		thecontent = thecontent.replace(/Next/g, "NextMain")
		thecontent = thecontent.replace(/Prev/g, "PrevMain")
		popupDivContent.innerHTML = thecontent;
		
		current = parseInt(parts[1]);
		mypics = new Array();
		mypics = parts[2].split("|||");
	}

	arrangePopupDiv();
	
}

function arrangePopupDiv() {
	var popupDiv = document.getElementById("popupdiv");
	
	popupDiv.style.left = ((windowWidth()/2)-(popupDiv.clientWidth/2)-8+getScrollX())+"px";
	popupDiv.style.top = ((windowHeight()/2)-(popupDiv.clientHeight/2)+getScrollY())+"px";
}

function windowWidth() {
	var myWidth = 0;

	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
	}

	return myWidth;
}

function windowHeight() {
	var myHeight = 0;

	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myHeight = document.body.clientHeight;
	}

	return myHeight;
}


function getScrollX() {
	if (isIE) {
		var myScrollX = document.body.scrollLeft;
//		var myScrollX = document.body.scrollLeft ? document.body.scrollLeft : document.body.parentNode.scrollLeft;
	} else {
		var myScrollX = window.scrollX;
	}

	return myScrollX;
}

function getScrollY() {
//	alert(document.body.parentNode.scrollTop+" - "+document.body.scrollTop);
	if (isIE) {
		var myScrollY = document.body.parentNode.scrollTop ? document.body.parentNode.scrollTop : document.body.scrollTop;
	} else {
		var myScrollY = window.scrollY;
	}

	return myScrollY;
}

