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

function GetXmlHttpObject() {
	var xmlHttp=null;
	try { // Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	} catch (e) { // Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

/*Specific Functions */

function switchContact () {
	if(document.getElementById('gi').checked == true) document.getElementById('getquoteform').style.display = 'none';
	else document.getElementById('getquoteform').style.display = 'block';
}

function getArea(id) {
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request")
		return
	}
	var url='areas.php?area='+id;
	xmlHttp.onreadystatechange=populateArea;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function populateArea() {
	if (xmlHttp.readyState==4) {
		var d = new Date();
		var t = d.getTime();
		
		var array = xmlHttp.responseText.split('|||');
		
		document.getElementById('portfolio_image_enc').innerHTML = '<a href="'+array[0]+'" rel="shadowbox[Gallery'+t+']"><img src="'+array[0]+'" alt="" id="portfolio_image" title="Click To View Gallery" /></a>'
		document.getElementById('thumbnails').innerHTML = array[1];
		document.getElementById('area_desc').innerHTML = array[2];
		if(array[4] != '') document.getElementById('testimonials').innerHTML = array[4];
		
		if(array[3]) {
			var linkarray = array[3].split(';;;');
			var links = '';
			for(var i=0;i<linkarray.length;i++) {
				if(i==0) continue;
				links += '<a href="'+linkarray[i]+'" rel="shadowbox[Gallery'+t+']" title=""></a>';
			}
			document.getElementById('hiddenimages').innerHTML = links;
			
			buildImages();
			/*if (myLightWindow) {
				Event.unloadCache();
				myLightWindow._setupLinks();
				myLightWindow._addLightWindowMarkup(false);
			}*/
			Shadowbox.setup();
		}
	}
}

function buildAreas() {
	if(document.getElementById('areas')) {
		var links = document.getElementById('areas').getElementsByTagName('a');
		for(var i=0;i<links.length;i++) {
			links[i].onclick = function(){getArea(this.rel)};
		}
	}
}

function switchImage(src) {
	if(document.getElementById('portfolio_image_enc')) {
		var fullsrc = src.replace(/images\/image_repository\/thumbs/, 'images/image_repository');
		document.getElementById('portfolio_image').src = fullsrc;
		if(document.getElementById('portfolio_image').width > 540) document.getElementById('portfolio_image').width = '540';
	}
}

function buildImages() {
	if(document.getElementById('thumbnails')) {
		var links = document.getElementById('thumbnails').getElementsByTagName('a');
		for(var i=0;i<links.length;i++) {
			var image = links[i].firstChild;
			links[i].onmouseover = function(){switchImage(this.firstChild.src);};
		}
	}
}

/* Window Loader */

addLoadEvent(function() {
	if (document.getElementById('gi')) {
		document.getElementById('gi').onclick = function(){return switchContact();};
		document.getElementById('gq').onclick = function(){return switchContact();};
	}
});
addLoadEvent(buildAreas);
addLoadEvent(buildImages);

