var ml_map;
var houseMarker = new Array();
var ml_lastMarker;

var ml_baseIcon = new GIcon();
ml_baseIcon.shadow = "/i/shadow.png";
ml_baseIcon.iconSize = new GSize(20, 34);
ml_baseIcon.shadowSize = new GSize(37, 34);
ml_baseIcon.iconAnchor = new GPoint(9, 34);
ml_baseIcon.infoWindowAnchor = new GPoint(9, 2);
ml_baseIcon.infoShadowAnchor = new GPoint(18, 25);

function $(id) {
	return document.getElementById(id);
}

function toggle(id) {
	var obj = $(id);
	obj.style.display = (obj.style.display != 'none' ? 'none' : '' );
}
function toggleClass(id) {
	var obj = $(id);
	obj.className = (obj.className != 'off' ? 'off' : 'on' );
}

//basically switches back to the form
function refineYourSearch(){
	toggle('results');
	toggle('options');
	ml_map.clearOverlays();
}

function init() {
	ml_map = new GMap2($("map"));
	ml_map.setCenter(new GLatLng(43.47455, -80.53799), 14);
	ml_map.addControl(new GLargeMapControl());
	ml_map.addControl(new GMapTypeControl());
	ml_map.enableDoubleClickZoom();
	resizeMap();
	applyOnClicks();
}

function applyOnClicks(){
	var submitButton = $('submit');
	var refineButton = $('refine');
	var showadvanced = $('showadvanced');
	var hideadvanced = $('hideadvanced');
	
	submitButton.onclick = getPlaces;
	refineButton.onclick = refineYourSearch;
	showadvanced.onclick = toggleAdvancedSearch;
	hideadvanced.onclick = toggleAdvancedSearch;
}

function toggleAdvancedSearch(){
	toggleClass('showadvanced');
	toggleClass('hideadvanced');
	toggleClass('advanced'); 
}

function getPlaces(){
	var start = $('small_value').value;
	var end = $('large_value').value;
	
	if(start == "")	start = 350;
	if(end == "") end = 400;	
	
	if($('advanced').className != "off"){
		var minrooms = $('minrooms').value;
		var utilities = $('utilities').checked;
		var parking = $('parking').checked;
		
		url="xprice_json2.php?start="+start+"&end="+end+"&minrooms="+minrooms+"&utilities="+utilities+"&parking="+parking;	
	}
	else{
		url="xprice_json2.php?start="+start+"&end="+end;	
	}
	
	var request = GXmlHttp.create();

	request.open("GET", url, true);
	request.onreadystatechange = function() {
		
		if(request.readyState == 1)
		{
			$('loading_message').style.display = "block";
		}
		else if(request.readyState == 4)
		{
			//PREP THE WAY FOR THE NEW DATA
			$('options').style.display = "none";
			$('results').style.display = "block";
			$('loading_message').style.display = "none";
			var targ = $('results_list');
			
			ml_baseIcon.image = "/i/marker/step_6.png";
			targ.innerHTML = "";
			
			eval(request.responseText);

			//show how many results
			$('numresults').innerHTML = "("+count+")";
		}
	}
	request.send(null);
}

function cMark(targ,id,add,price,lat,long,util,parking,term,vacancies,totalrooms,dateAvailable,name,contact1,contact2,contact3) {
	
	var point = new GLatLng(parseFloat(lat), parseFloat(long));
	var marker = new GMarker(point, ml_baseIcon);
	
	var contact_html = '<h4>'+add+'</h4><ul><li>'+name+'</li><li>'+contact1+'</li><li>'+contact2+'</li><li>'+contact3+'</li></ul>';
	var detail_html = '<h4>'+add+'</h4><ul><li><b>Price/Month: </b> $'+price+'</li><li><b>Term: </b>'+term+'</li><li><b>Available: </b>'+dateAvailable+'</li><li><b>'+vacancies+'</b> of '+totalrooms+' rooms available</li><li><b>Utilities: </b><img src="/i/u'+util+'.jpg" /></li><li><b>Parking: </b><img src="/i/u'+parking+'.jpg" /></li><li><a href="http://www.hrf.uwaterloo.ca/ochousing2/dresults.asp?rid=' + id + '"><b>More Info</b></a></li></ul>'; 
	var infoTabs = [
					  new GInfoWindowTab("Details", detail_html),
					  new GInfoWindowTab("Contact", contact_html)
					];
	
	GEvent.addListener( marker, "click", function() {
		marker.openInfoWindowTabsHtml(infoTabs);
		if(ml_lastMarker){
			$('x'+ml_lastMarker).className = "";
		}
		$('x'+id).className = "clicked";
		ml_lastMarker = id;
		
	});
	
	//Add to index.
	targ.innerHTML += "<div id=\"x"+id+"\" onclick=\"GEvent.trigger(houseMarker["+id+"],'click')\" >"
					+"<span class='price'>$"+price+"</span>"
					+add
					+"</div>\n";

	houseMarker[id] = marker;
	ml_map.addOverlay(marker);
}

function stripe(id) {
	var even = false;
	
	//Here you can change the colour of the stripes, change the 6 digit hex values below
	//White
	var evenColor = "#ffffff";
	//blueish
	var oddColor = "#F1F5FA";

	var div = document.getElementById(id);
	if (! div) { return; }
	// find all the li elements... 
 	var divs = div.getElementsByTagName("div");
 	// ... and iterate through them
	for (var i = 0; i < divs.length; i++) {
		divs[i].style.backgroundColor = even ? evenColor : oddColor;
		divs[i].style.fontWeight = 'normal';
		even =  ! even;
	}
}

function resizeMap() {
	var height = getWindowSize();
	var map_height = height - 80;
	var result_height = height - 190;
	$("map").style.height = map_height + 'px';
	$('results_list').style.height = result_height + 'px';
}

function getWindowSize()
{
	var height = 0;
	if(self.innerHeight) {
			height = self.innerHeight;
	}else if(document.documentElement && document.documentElement.clientHeight) {
			height = document.documentElement.clientHeight;
	}else {
			height = document.body.clientHeight;
	}
	return height;
}

window.onload = init;
window.onresize = resizeMap;
window.onunload = GUnload;