//ajax popups for portfolio page
//start ajax method
//Check if we are using IE.
	try {
		//If the javascript version is greater than 5.
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		//If not, then use the older active x object.
		try {
			//If we are using IE.
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			//Else we must be using a non-IE browser.
			xmlhttp = false;
		}
	}
	
	//If we are using a non-IE browser, create a JavaScript instance of the object.
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	
function closewin(){
	var theDim = dEI("dim");
	var thePop = dEI("popup");
	theDim.style.display = "none";
	thePop.style.display = "none";
	thePop.style.width = "0px";
	thePop.style.height = "0px";
	theDim.style.width = "0px";
	theDim.style.height = "0px";
}

function getPageScroll(){

	var xScroll, yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}

	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
}

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


function createpage(pid,cid){
	var pageSizeInfo = getPageSize();
	var pageWidth = pageSizeInfo[0];
	var pageHeight = pageSizeInfo[1];
	var pageScrollX = document.viewport.getScrollOffsets().left;
	var pageScrollY = document.viewport.getScrollOffsets().top;
	
	// scrolls only show up after a scroll is made
	if(dEI("dim")){
	var theDim = dEI("dim");
	if(theDim.style.display = "none"){
		theDim.style.display = "block";
	}
	theDim.style.width = pageWidth + pageScrollX + "px";
	theDim.style.height = pageHeight + pageScrollY + "px";
	theDim.style.top = "0px";
	theDim.style.left = "0px";
	theDim.style.position = "absolute";
	}
	if(dEI("popup")){
		var thepopwin = dEI("popup");
		if(thepopwin.style.display = "none"){
		thepopwin.style.display = "block";
	}
		thepopwin.style.width = "648px";
		thepopwin.style.height = "auto";
		var theYPos = ((pageSizeInfo[3] / 2) - (240)) + pageScrollY;
		var theXPos = ((pageSizeInfo[2] / 2) - (648 / 2)) + pageScrollX;
		if(theYPos <= 0){
			theYPos = "20";
		}
		thepopwin.style.top = theYPos + "px";
		thepopwin.style.left = theXPos + "px";
		
	}
	var serverPage = "http://www.newfrontproductions.com/creative/js/galform.php?pid="+ pid+"&cid="+cid+"";
	var obj = dEI("popup");
	xmlhttp.open("GET", serverPage);
	xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				obj.innerHTML = xmlhttp.responseText;
			}
		}
		xmlhttp.send(null);
		
		
	}
	
function dEI(obj){
theObj = document.getElementById(obj);
return theObj;
}

function createElems(){
	if(dEI("dim")){
		// the dim is already on the page
	} else {
			// we know that the dim doesn't exist so let's build it
		var attachNode = document.getElementById("body");
		var dimN = document.createElement("div");
		dimN.id = "dim";
		dimN.name = "dim";
		dimN.style.zIndex = "900";
		dimN.style.background = "#000000";
		// firefox / safari
		dimN.style.opacity = "0.8";
		// ie6 - 8
		dimN.style.filter = "alpha(opacity=80)";
		dimN.style.width = "0px";
		dimN.style.height = "0px";
		dimN.position = "absolute";
		attachNode.appendChild(dimN);
		
	}
	if(dEI("popup")){
		// the popup is already in working order
	} else {
		// let's add the popup to the page via javascript
		var attachNode = document.getElementById("body");
		var popwin = document.createElement("div");
		popwin.id = "popup";
		popwin.name = "popup";
		popwin.style.position = "absolute";
		popwin.style.top = "0px";
		popwin.style.left = "0px";
		popwin.style.width = "0px";
		popwin.style.height = "0px";
		popwin.style.zIndex = "999";
		attachNode.appendChild(popwin);
	}
}

window.onload = function(){
	createElems();
}