/*
	Intergalvision java fun
*/
	
var htmlinner;
var req;
var objCalendar
//Ajax stuff
function startDate(month,year) {
        var url = "_calendar.php?m="+month+"&y="+year;
        if(window.XMLHttpRequest) {
                req = new XMLHttpRequest();
        } else if(window.ActiveXObject) {
                req = new ActiveXObject("Microsoft.XMLHTTP");
        }
        req.open("GET", url, true);
        req.onreadystatechange = callbackdate;
        req.send(null);
}

function callbackdate() {
        if(req.readyState == 4) {
                if(req.status == 200) {
                        response = req.responseText;
                        document.getElementById("calendar").innerHTML = response;
                } else {
                        alert("There was a problem retrieving the data:\n" + req.statusText);
                }
        }
}

function callbackdate2() {
        if(req.readyState == 4) {
			if(req.status == 200) {
					htmlinner = req.responseText;
			}
        }
}

function updateDate(year,month,day) {
	
	document.getElementById("date").value = day+"/"+month+"/"+year;
	document.getElementById("datedisplay").innerHTML = day+"/"+month+"/"+year;
	
	hideCalbox();
	
}


//
// getPageScroll()
// Returns array with x,y page scroll values.
//
function getPageScroll(){

	var yScroll;

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

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



//
// getPageSize()
// Returns array with page width, height and window width, height
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		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;
	if (self.innerHeight) {	// all except Explorer
		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;
	}

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


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

//
// pause(numberMillis)
// Pauses code execution for specified time. Uses busy code, not good.
//
function pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}

function showCalbox(objLink)
{
	// prep objects
	var objOverlay = document.getElementById('overlay');
	var objcalendar = document.getElementById('calendar');
	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	// center calendar if it exists
	if (objcalendar) {
		var hight = 295;
		var width = 241;
		//alert((arrayPageScroll[1] + ((arrayPageSize[3] - 35 - hight) / 2) + 'px'));
		objcalendar.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - hight) / 2) + 'px');
		objcalendar.style.left = (((arrayPageSize[0] - 20 - width) / 2) + 'px');
		objcalendar.style.display = 'block';
		objcalendar.style.position = 'absolute';
		objcalendar.style.zIndex = '120';
	}

	// set height of Overlay to take up whole page and show
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.display = 'block';
	
	document.getElementById("town2").style.display = 'none';
	document.getElementById("size").style.display = 'none';
	document.getElementById("rooms").style.display = 'none';
	document.getElementById("twoweek").style.display = 'none';
	document.getElementById("break").style.display = 'none';
	document.getElementById("oneweek").style.display = 'none';
	document.getElementById("all").style.display = 'none';
	document.getElementById("pub").style.display = 'none';
	document.getElementById("beach").style.display = 'none';
	document.getElementById("waterside").style.display = 'none';
	document.getElementById("pets").style.display = 'none';
	document.getElementById("shmokin").style.display = 'none';
	document.getElementById("pub").style.display = 'none';
	document.getElementById("shop").style.display = 'none';
	document.getElementById("village").style.display = 'none';
	document.getElementById("garden").style.display = 'none';
	document.getElementById("pool").style.display = 'none';
	document.getElementById("fire").style.display = 'none';
	document.getElementById("town").style.display = 'none';
	document.getElementById("propname").style.display = 'none';
	document.getElementById("ref").style.display = 'none';
	
}


function hideCalbox()
{
	// get objects
	objOverlay = document.getElementById('overlay');
	var objcalendar = document.getElementById('calendar');

	// hide lightbox and overlay
	objOverlay.style.display = 'none';
	objcalendar.style.display = 'none';
	document.getElementById("town2").style.display = 'block';
	document.getElementById("size").style.display = 'block';
	document.getElementById("rooms").style.display = 'block';
	document.getElementById("twoweek").style.display = 'block';
	document.getElementById("break").style.display = 'block';
	document.getElementById("oneweek").style.display = 'block';
	document.getElementById("all").style.display = 'block';
	document.getElementById("pub").style.display = 'block';
	document.getElementById("beach").style.display = 'block';
	document.getElementById("waterside").style.display = 'block';
	document.getElementById("pets").style.display = 'block';
	document.getElementById("shmokin").style.display = 'block';
	document.getElementById("pub").style.display = 'block';
	document.getElementById("shop").style.display = 'block';
	document.getElementById("village").style.display = 'block';
	document.getElementById("garden").style.display = 'block';
	document.getElementById("pool").style.display = 'block';
	document.getElementById("fire").style.display = 'block';
	document.getElementById("town").style.display = 'block';
	document.getElementById("propname").style.display = 'block';
	document.getElementById("ref").style.display = 'block';
}

function initCalbox()
{
	
	if (!document.getElementsByTagName){ return; }
	var anchors = document.getElementsByTagName("a");

	// loop through all anchor tags
	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];

		if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "calpop")){
			anchor.onclick = function () {showCalbox(this); return false;}
		}
	}
	
	var objBody = document.getElementsByTagName("body").item(0);
	
	// create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlay');
	//objOverlay.onclick = function () {hideCalbox(); return false;}
	objOverlay.style.display = 'none';
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '110';
 	objOverlay.style.width = '100%';
	objOverlay.innerHTML = "&nbsp;";
	objBody.insertBefore(objOverlay, objBody.firstChild);
	
	//crate the calendar div
	var objCalendar = document.createElement("div");
	objCalendar.setAttribute('id','calendar');
	objCalendar.setAttribute('class','calandar');
	objCalendar.style.display = 'none';
	objOverlay.appendChild(objCalendar);
	
	startDate(null,null);

}

function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}

}


addLoadEvent(initCalbox);	// run initCalbox onLoad
