var tempImg1 = new Image();
var tempImg2 = new Image();

tempImg1.src = 'images/buttons/make_a_reservation_on.png';
tempImg2.src = 'images/buttons/submit_on.png';

var requests = new Array();

function postData() {
	// setup the POST data
	var detailForm = document.getElementById('contactForm');
	var formElements = detailForm.elements;
	var data = '';
	
	for(i = 0; i < formElements.length; i++) {
		if(formElements[i].disabled)
			continue;
	
		if(formElements[i].name != "kIgnore") {
			if(i)
				data += "&";
			
			if(formElements[i].options) {
				var valueAppend = "";
			
				for(k = 0; k < formElements[i].length; k++) {
					if(formElements[i].options[k].selected) {
						if(valueAppend != "") {
							valueAppend += ",";
						}
						
						valueAppend += formElements[i].options[k].value;
					}
				}
				
				data += escape(formElements[i].name) + "=" + escape(valueAppend);
			} else {
				data += escape(formElements[i].name) + "=" + escape(formElements[i].value);
			}
		}
	}

	// send the request
	sendRequest("contact", null, data, "processContact();");
}

function sendRequest(whichOne, queryString, postData, callback) {
	requests[whichOne] = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
    
    if(requests[whichOne]) {
        requests[whichOne].onreadystatechange = function() {processRequest(whichOne, callback);};		
		// setup to handle GET or a POST, based on the data
		requests[whichOne].open((postData == null ? "GET" : "POST"), "lib/ajax_contact.php?"+(postData == null ? "" : "iepost=yes&")+(queryString != null ? queryString : ""), true);

		if (postData && postData.length > 0) {			
			requests[whichOne].setRequestHeader("Content-Type", "application/x-www-form-urlencoded");		
		}
		requests[whichOne].send(postData);
    }
}

function processRequest(whichOne, callback) {
	if(requests[whichOne].readyState == 4 && requests[whichOne].status == 200) {
		eval(callback);
    }
}

function processContact() {
	var mediaSlip = document.getElementById('mediaSlip');
	
	mediaSlip.innerHTML = requests['contact'].responseText;
}

function hover(who, baseClass, what) { 
	var current; 
	eval('current = who.className.match(/(Sel)?'+baseClass+'(Hover)?/);'); 
	current = current[0]; 
	if(what && current && current.search(/Hover$/) == -1) {
		eval('who.className = who.className.replace(/'+current+'/, "'+current+'Hover");'); 
	} else if(!what && current && current.search(/Hover$/) != -1) {
		eval('who.className = who.className.replace(/'+current+'/, "'+current.replace(/Hover$/, '')+'");'); 
	}
}

// preload the hover state of the close button
var closeHover = new Image();
closeHover.src = 'images/close_hover.png';

var mediaBackgroundColor = "#9c9c63";

function showMediaSlip(width, height) {
	var modalShade = document.getElementById('modalShade');
	var mediaSlipTable = document.getElementById('mediaSlipTable');
	var mediaSlip = document.getElementById('mediaSlip');
	var mediaSlipContainer = document.getElementById('mediaSlipContainer');
	var printStyle = document.getElementById('printStyle');
	var mediaSlipScale = document.getElementById('mediaSlipScale');
	var isAbsPosition = modalShade.currentStyle && modalShade.currentStyle['position'] == 'absolute';
	var origWidth = width;
	var origHeight = height;
	var data;

	// check the dimensions - if this would be wider or taller than the window, scale it down appropriately
	var currentWidth = window.innerWidth ? window.innerWidth : (document.documentElement ? document.documentElement.clientWidth : document.body.clientWidth);
	var currentHeight = window.innerHeight ? window.innerHeight : (document.documentElement ? document.documentElement.clientHeight : document.body.clientHeight);
	// HERE'S LOGIC TO MAKE THIS WORK FOR TRANSITIONAL DOCTYPES, IN CASE YOU NEED THAT...
	//var currentWidth = window.innerWidth ? window.innerWidth : ((document.documentElement && document.documentElement.clientWidth > 0) ? document.documentElement.clientWidth : document.body.clientWidth);
	//var currentHeight = window.innerHeight ? window.innerHeight : ((document.documentElement && document.documentElement.clientHeight > 0) ? document.documentElement.clientHeight : document.body.clientHeight);
	if (width > (currentWidth-80)) {
		// restrain the width, and scale the height accordingly
		var targetWidth = (currentWidth-80);
		var targetHeight = Math.floor( (targetWidth*(height))/(width) );

		width = targetWidth; 
		height = targetHeight;
	}
	if (height > (currentHeight-60)) {
		// restrain the height, and scale the width accordingly
		var targetHeight = (currentHeight-60);
		var targetWidth = Math.floor( (targetHeight*(width))/(height) );
		
		width = targetWidth; 
		height = targetHeight;
	}

	// Reset the orig width/height
	mediaSlip.origWidth = origWidth;
	mediaSlip.origHeight = origHeight;

	// adjust the dimensions to accomodate the media
	mediaSlip.style.width = width+'px';
	mediaSlip.style.height = height+'px';
	
	// center the popup within the window, counting on the "currentWidth" and "currentHeight" we read in before
	var verticalPosition = isAbsPosition ? (window.scrollY ? window.scrollY : document.documentElement.scrollTop) : 0;
	// ...vertically center it...
	verticalPosition = (currentHeight/2)-((height+40)/2)+verticalPosition;
	if(verticalPosition < 0) {
		verticalPosition = 0;
	}
	mediaSlipTable.style.marginTop = verticalPosition+'px';
	
	// and size the modal shader so that it takes up the entire content area, if it needs to
	if(isAbsPosition) {
		// get the total height of the content (which includes the area you have to scroll to)...
		var contentHeight = (document.documentElement.scrollHeight > document.body.scrollHeight) ? document.documentElement.scrollHeight : document.body.scrollHeight;	
		var contentWidth= (document.documentElement.scrollWidth > document.body.scrollWidth) ? document.documentElement.scrollWidth : document.body.scrollWidth;
		
		modalShade.style.height = contentHeight+'px';
		modalShade.style.width = contentWidth+'px';
	}
	
	// Setup the printing
	if(printStyle) {
		printStyle.hrefSave = printStyle.href;
		printStyle.href = 'lib/print_popup.css';
	}
	
	sendRequest('contact', null, null, 'processContact();');
	
	// finally, show the slip and the modal shade backdrop
	modalShade.style.display = 'block';
	mediaSlipContainer.style.display = 'block';	
}

function hideMediaSlip() {
	var modalShade = document.getElementById('modalShade');
	var mediaSlip = document.getElementById('mediaSlip');
	var mediaSlipContainer = document.getElementById('mediaSlipContainer');
	var printStyle = document.getElementById('printStyle');

	// fix back the printing
	if(printStyle) {
		printStyle.href = printStyle.hrefSave ? printStyle.hrefSave : 'lib/print.css';
	}

	// hide it 
	mediaSlipContainer.style.display = 'none';
	modalShade.style.display = 'none';
}

function mediaSlipResize() {
	var mediaSlipContainer = document.getElementById('mediaSlipContainer');

	if(mediaSlipContainer && mediaSlipContainer.style.display == 'block') {
		var modalShade = document.getElementById('modalShade');
		var mediaSlip = document.getElementById('mediaSlip');
		var mediaSlipTable = document.getElementById('mediaSlipTable');
		var mediaSlipImg = document.getElementById('mediaSlipImg');
		var mediaSlipScale = document.getElementById('mediaSlipScale');
		var isAbsPosition = modalShade.currentStyle && modalShade.currentStyle['position'] == 'absolute';
		var width = mediaSlip.origWidth;
		var height = mediaSlip.origHeight;
		
		// check the dimensions - if this would be wider or taller than the window, scale it down appropriately
		var currentWidth = window.innerWidth ? window.innerWidth : (document.documentElement ? document.documentElement.clientWidth : document.body.clientWidth);
		var currentHeight = window.innerHeight ? window.innerHeight : (document.documentElement ? document.documentElement.clientHeight : document.body.clientHeight);
		// HERE'S LOGIC TO MAKE THIS WORK FOR TRANSITIONAL DOCTYPES, IN CASE YOU NEED THAT...
		//var currentWidth = window.innerWidth ? window.innerWidth : ((document.documentElement && document.documentElement.clientWidth > 0) ? document.documentElement.clientWidth : document.body.clientWidth);
		//var currentHeight = window.innerHeight ? window.innerHeight : ((document.documentElement && document.documentElement.clientHeight > 0) ? document.documentElement.clientHeight : document.body.clientHeight);
		if (width > (currentWidth-80)) {
			// restrain the width, and scale the height accordingly
			var targetWidth = (currentWidth-80);
			var targetHeight = Math.floor( (targetWidth*(height))/(width) );
	
			width = targetWidth; 
			height = targetHeight;
		}
		if (height > (currentHeight-60)) {
			// restrain the height, and scale the width accordingly
			var targetHeight = (currentHeight-60);
			var targetWidth = Math.floor( (targetHeight*(width))/(height) );
			
			width = targetWidth; 
			height = targetHeight;
		}
		
		if(mediaSlipImg) {
			mediaSlipImg.style.width = width + 'px';
			mediaSlipImg.style.height = height + 'px';
			var scale = Math.floor(width/mediaSlip.origWidth*100);
			mediaSlipScale.innerHTML = (scale == 100 ? '' : 'Scale: '+scale+'%');
		} else {
			if(width < mediaSlip.origWidth) {
				width = mediaSlip.origWidth;
			}
			if(height < mediaSlip.origHeight) {
				height = mediaSlip.origHeight;
			}
		}
		
		// adjust the dimensions to accomodate the media
		mediaSlip.style.width = width+'px';
		mediaSlip.style.height = height+'px';
		
		// center the popup within the window, counting on the "currentWidth" and "currentHeight" we read in before
		var verticalPosition = isAbsPosition ? (window.scrollY ? window.scrollY : document.documentElement.scrollTop) : 0;
		// ...vertically center it...
		verticalPosition = (currentHeight/2)-((height+40)/2)+verticalPosition;
		if(verticalPosition < 0) {
			verticalPosition = 0;
		}
		mediaSlipTable.style.marginTop = verticalPosition+'px';

		// and size the modal shader so that it takes up the entire content area, if it needs to
		if(isAbsPosition) {
			// get the total height of the content (which includes the area you have to scroll to)...
			var contentHeight = (document.documentElement.scrollHeight > document.body.scrollHeight) ? document.documentElement.scrollHeight : document.body.scrollHeight;	
			var contentWidth= (document.documentElement.scrollWidth > document.body.scrollWidth) ? document.documentElement.scrollWidth : document.body.scrollWidth;
			
			modalShade.style.height = contentHeight+'px';
			modalShade.style.width = contentWidth+'px';
		}
	}
}

function mediaSlipScroll() {
	var mediaSlipContainer = document.getElementById('mediaSlipContainer');

	if(mediaSlipContainer && mediaSlipContainer.style.display == 'block') {
		var modalShade = document.getElementById('modalShade');
		var isAbsPosition = modalShade.currentStyle && modalShade.currentStyle['position'] == 'absolute';
		
		if(isAbsPosition) {
			mediaSlipResize();
		}
	}
}

window.onresize = mediaSlipResize;
window.onscroll = mediaSlipScroll;