var xmlHttp;
var currentPage=0;
var currentElement;
var contentCache;

function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}

function preloadDimmer() {
	pic1= new Image(); 
	pic1.src="../images/dimmer1.gif"; 
}
//--------------------------------------------------------------------------------------------------------------------
//Show/Hide Dimmer.
//--------------------------------------------------------------------------------------------------------------------
function showHideDimmer() {
	var dimmer = $('dimmer');
	//var stupidIE6Select = $('msgColor');
	var pageHeight = $('pageContainer').offsetHeight;
	var pageWidth = $('pageContainer').offsetWidth;
	var mediaHeight = $('mediaWindowAnchor').offsetHeight+10;
	var windowDim = getWindowSize();
	var newHeight;
	
	if(pageHeight<mediaHeight) {
		newHeight = mediaHeight+20;
	} else if(windowDim[1]>pageHeight) {
		newHeight = windowDim[1];
	} else {
		newHeight = pageHeight;
	}
	
	if(windowDim[1]>pageHeight) {
		newHeight = windowDim[1];
	}
	
	if (dimmer.style.display!="block") {
		//stupidIE6Select.style.visibility="hidden";
		dimmer.style.height = (newHeight-142)+"px"; 
		dimmer.style.width = pageWidth+"px"; 
		dimmer.style.display="block";
		$('loadingBox').style.display = "block";
		$('partyOverlay').style.display="none";
		positionMediaWindow('loadingBox');
	} else {
		dimmer.style.display="none";
		$('partyOverlay').style.display="block";
		//stupidIE6Select.style.visibility="visible";
	}
}
//--------------------------------------------------------------------------------------------------------------------
//RESET DIMMER.
//--------------------------------------------------------------------------------------------------------------------
function resetDimmer() {
	var dimmer = $('dimmer');
	//var stupidIE6Select = $('msgColor');
	var pageHeight = $('pageContainer').offsetHeight;
	var pageWidth = $('pageContainer').offsetWidth;
	var mediaHeight = $('mediaWindow').offsetHeight+10;
	var windowDim = getWindowSize();
	
	if(pageHeight<mediaHeight) {
		newHeight = mediaHeight+20;
	} else if(windowDim[1]>pageHeight) {
		newHeight = windowDim[1];
	} else {
		newHeight = pageHeight;
	}
	
	if(windowDim[1]>pageHeight) {
		newHeight = windowDim[1];
	}
	
	if (dimmer.style.display=="block") {
		dimmer.style.height = (newHeight-142)+"px"; 
		dimmer.style.width = pageWidth+"px"; 
	}
}

function catchClick(type) {
	xmlHttp=GetXmlHttpObject();
	
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}

	var url="/includes/clicks.php?type="+type+"&time="+Math.random()*99999;
	/*xmlHttp.onreadystatechange=function() {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
			messageReturnedID=xmlHttp.responseText;
		}
	}*/
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)	
}
//--------------------------------------------------------------------------------------------------------------------
//
//--------------------------------------------------------------------------------------------------------------------
function popWindow(title) {
	var newWindow = new mediaWindow(title,'../birthday/'+currentPage+'.html','','ajax');
	
	newWindow.create();
	
	showHideDimmer();
}

function mediaWindow(title,content,footer,contentType,scaleType) {
	var windowBody = $('mediaWindowAnchor');
	
	this.id = 'mediaWindow';
	this.title = title;
	this.contentIn = content;
	this.footer = '';
	this.contentType = contentType;
	this.scaleType = "set";
	
	this.width = '500';
	this.height = '500';
	
	if(contentType=='html') {
		this.content = content;
		this.padding="style='padding:5px'";
	} else if(contentType=='ajax'){
		this.content = getAjaxContent(content);
		this.padding="style='padding:0px'";
	}
	
	
		
	
	this.create = function () {
		
		var newdiv = document.createElement('div');
		var divIdName = this.id;
		
		newdiv.id = divIdName;       //IE
		newdiv.className = "mediaWindowFrame";   //IE
		
		newdiv.setAttribute('id',divIdName);  //All the rest
		newdiv.setAttribute('class','mediaWindowFrame');  //All the rest
		
		newdiv.innerHTML = '<div class="mediaWindowClose" onclick="closeForm(&quot;'+divIdName+'&quot;)"></div>';
		newdiv.innerHTML += '<div id="box" class="mediaWindowHeader" style="left:0px; top:0px;" >'+this.title+'</div>';
		newdiv.innerHTML += '<div id="windowContent" class="mediaWindowContent" '+this.padding+' >'+this.content+'</div>';
		newdiv.innerHTML += '<div id="windowFooter" class="mediaWindowFooter">'+this.footer+'</div>';
		windowBody.appendChild(newdiv);
		
		var createdFrame = $(divIdName);
		
		/*if(this.width)
			$('windowContent').style.width = this.width+"px";
		if(this.height)
			$('windowContent').style.height = (this.height-44)+"px";*/
		//resizeMediaWindow(this.width, this.height, this.scaleType);
		currentElement = createdFrame;
		//currentElement.style.display = "block";
		//$('mediaWindow').style.width="500px";

		//positionMediaWindow()
		
		
		
		
		windowBody.style.border="solid 1px #FFFFFF";
		//positionMediaWindow();
		
		
		
	}
}
function closeForm(id) {
	$('mediaWindowAnchor').removeChild($(id));
	$('mediaWindowAnchor').style.border="none";
	currentPage=0;
	showHideDimmer();
}

function setPage() {
	currentPage++;
	if (currentPage > 13) {
		currentPage = 0;	
	}
}

function showVideo() {
	parentPos = findPos($('windowContent'));
	
		
	$('vidBox').style.top = parentPos[0];
	$('vidBox').style.left = parentPos[1];
	$('vidBox').style.display = 'block';	
}

function hideVideo() {
	$('vidBox').style.display = 'none';	
}

function findPos(obj) {
	var curleft = 0;
	var curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function getAjaxContent(url){
	var xmlHttp000 = GetXmlHttpObject();
	var result;
	
	/*if($('windowContent')) {
		$('windowContent').innerHTML = "<img src='images/loading.gif' align='middle' />Loading...";	
	}*/
	
	setPage();
	xmlHttp000.onreadystatechange=function(){
		if (xmlHttp000.readyState == 4 && (xmlHttp000.status==200 || window.location.href.indexOf("http")==-1)) {
			$('windowContent').innerHTML = "<div style='background-color:#999999;width:500px;clear:both;float:right;cursor:pointer;text-align:right;margin-bottom:5px;font-weight:bold;font-size:10pt;color:#FFFFFF;' onclick='getAjaxContent(&quot;../birthday/"+(currentPage)+".html&quot;);catchClick(&quot;moreClicked&quot;)'>More -->&nbsp;&nbsp;</div>"+xmlHttp000.responseText;
			
			$('windowContent').style.width=500+"px";
			
			currentElement.style.display = "block";
			$('loadingBox').style.display = "none";
			positionMediaWindow();
		}
	}

	var noCache=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
	
	xmlHttp000.open('GET', url+noCache, true)
	xmlHttp000.send(null)
}

function resizeMediaWindow(width, height, scaleType) {
	var mediaWindow = $('mediaWindow');
	var mediaContentWindow = $('windowContent');
	var pageWidth = $('pageContainer').offsetWidth;
	var pageHeight = $('pageContainer').offsetHeight;
	
	if(scaleType == "max") {
		mediaWindow.style.width=(pageWidth-20)+"px";
		mediaWindow.style.height=(pageHeight-20)+"px";
		
		positionMediaWindow();
	} else if(scaleType == "set") {
		mediaContentWindow.style.width=width+"px";
		mediaContentWindow.style.height=mediaWindow.offsetHeight+"px";	
		
		positionMediaWindow();
	}
}

function positionMediaWindow(win) {
	var mediaWindow;
	
	if(win) {
		mediaWindow = $(win);
	} else {
		mediaWindow = $('mediaWindow');
	}
	
	var windowDims = getWindowSize();
	var pageScroll = getPageScroll();
	var newWidth, newHeight;
	
	var mediaTop = 152;//142; //pageScroll[1]+(windowDims[1]/2)-(mediaWindow.offsetHeight/2);
	var mediaLeft = (windowDims[0]/2)-(mediaWindow.offsetWidth/2);
	
	if(mediaTop<5) {
		mediaTop = 5;
	}
	if(mediaLeft<5) {
		mediaLeft = 5;
	}
	
	if(win) {
		$(win).style.left=mediaLeft+"px";	
		$(win).style.top=mediaTop+"px";
	} else {
		$('mediaWindowAnchor').style.left=mediaLeft+"px";
		$('mediaWindowAnchor').style.top=mediaTop+"px";
	}
}

//--------------------------------------------------------------------------------------------------------------------
//DEFINE "AJAX" objXMLHttp object
//--------------------------------------------------------------------------------------------------------------------
function GetXmlHttpObject() { 
	var objXMLHttp=null
	if (window.XMLHttpRequest) {
		objXMLHttp=new XMLHttpRequest()
	} else if (window.ActiveXObject) {
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
} 
//--------------------------------------------------------------------------------------------------------------------
//GET WINDOW DIMENSIONS
//--------------------------------------------------------------------------------------------------------------------
function getWindowSize() {
	var windowWidth = 0, windowHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		windowWidth = window.innerWidth;
		windowHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	return [windowWidth, windowHeight];
}
//--------------------------------------------------------------------------------------------------------------------
//GET WINDOW SCROLL OFFSET
//--------------------------------------------------------------------------------------------------------------------
function getPageScroll(){

	var xScroll,yScroll;

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

	return [xScroll,yScroll];
}
//--------------------------------------------------------------------------------------------------------------------
//PAUSE
//--------------------------------------------------------------------------------------------------------------------
function pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}
