
function createMarker(point,html,marker_id,price,icon) {
	var marker = "";
	if(icon!=""){
		marker = new GMarker(point, {icon:icon, title:price});
	}else{
		marker = new GMarker(point);
	}
	GEvent.addListener(marker, "click", function() {
		show_street(marker);
		marker.openInfoWindowHtml(html);
	});
	gmarkers[marker_id] = marker;
	return marker;
}

function myclick(i) {
	GEvent.trigger(gmarkers[i], "click");
}

function initialize_pano(lat, lon) {
	document.getElementById('pano').style.display = "";
	document.getElementById('pano').style.height= "200px";
	document.getElementById('pano').style.width = "320px";

	panoClient = new GStreetviewClient();      
	
	// street icon
	var baseIcon = new GIcon();
	baseIcon.iconSize=new GSize(32,32);
	baseIcon.shadowSize=new GSize(56,32);
	baseIcon.iconAnchor=new GPoint(16,16);
	baseIcon.infoWindowAnchor=new GPoint(16,0);
	var the_view = new GIcon(baseIcon, "http://maps.google.com/mapfiles/kml/pal3/icon52.png", null, null);
	var point = new GLatLng(lat,lon);
	houseMarker = new GMarker(point,{icon:the_view, draggable: true, title:"Street View", bouncy:false});
	map.addOverlay(houseMarker); 
	GEvent.addListener(houseMarker, "dragend", function() {
		map.closeInfoWindow();
		panoClient.getNearestPanorama(houseMarker.getLatLng(), showPanoData);
	});
	
	myPano = new GStreetviewPanorama(document.getElementById("pano"));
	panoClient.getNearestPanorama(houseMarker.getLatLng(), showPanoData);
	
	GEvent.addListener(myPano, "initialized", handleInitialized); 
}


function show_map(that, longitude, latitude){
	if(document.getElementById('map').style.display == "none"){
		resize('100','%');
	}

	var subd = document.getElementById(that.id).getElementsByTagName('div');
	var lon = longitude;
	var lat = latitude;
	var address = "";
	var content = "";
	var price = "";
	var the_price = "";
	
	
	var added = "n";
	
	var display_num = "";
	var category = "";
	var num_rooms = "";
	
	
	var total = subd.length;
	for(i=0; i < total; i++){
		//alert(subd[i].id);
		//alert(subd[i].className);
		//alert(subd[i].innerHTML);
		if(subd[i].className == 'added'){
			if(subd[i].innerHTML == ""){
				added = "n";
			}else{
				added = "y";
			}
		}else if(subd[i].className == 'address'){
			address = subd[i].innerHTML;
		}else if(subd[i].className == 'the_price'){
			price = subd[i].innerHTML;
			price = price.toLowerCase();
			var price_array = price.split(/<h2>(.*)<\/h2>/);
			if(price_array[1]){ 
				the_price = price_array[1];
			}
		}else if(subd[i].className == 'details'){
			content = "<br/>"+subd[i].innerHTML;

			content = content.replace(/<img src\=\'\/images\/bed.gif\'\/>/g,"");
			content = content.replace(/<img src\=\'\/images\/bath.gif\'\/>/g,"");
			content = content.replace(/<img src\=\'\/images\/car.gif\'\/>/g,"");
			content = content.replace(/<img src\=\"\/images\/bed.gif\">/g,"");
			content = content.replace(/<img src\=\"\/images\/bath.gif\">/g,"");
			content = content.replace(/<img src\=\"\/images\/car.gif\">/g,"");
			content = content.replace(/<IMG src\=\"\/images\/bed.gif\">/g,"");
			content = content.replace(/<IMG src\=\"\/images\/bath.gif\">/g,"");
			content = content.replace(/<IMG src\=\"\/images\/car.gif\">/g,"");

		}else if(subd[i].className == 'category'){
			var category_string = subd[i].innerHTML;
			var category_array = category_string.match(/Category: (.*)/);

			if(category_array[1]){ 
				category = category_array[1];
			}
		}else if(subd[i].className == 'bedrooms'){
			var num_rooms_string = subd[i].innerHTML;
			num_rooms = num_rooms_string.match(/\d/);
		}else if(subd[i].className == 'links'){
		}
		

	}
	content = content.replace(/<div class=\"links\">/g,"<div class=\"links\"><div class='sv' style=\"color:#fff; background-color:#222;\">Price Last Found Active</div>");
	content = content.replace(/<DIV class\=links>/g,"<div class=\"links\"><div class='sv' style=\"color:#fff; background-color:#222;\">Price Last Found Active</div>");

	var baseIcon = new GIcon();
	baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
	baseIcon.iconSize = new GSize(20, 34);
	baseIcon.shadowSize = new GSize(37, 34);
	baseIcon.iconAnchor = new GPoint(9, 34);
	baseIcon.infoWindowAnchor = new GPoint(9, 2);
	baseIcon.infoShadowAnchor = new GPoint(18, 25);

	if(category.toLowerCase() == "house") 						{ if (num_rooms >= "1" && num_rooms <= "9"){ display_num = new GIcon(baseIcon, "http://www.oldlistings.com.au/mapsicons/iconb"+ num_rooms +".png", null, baseIcon.shadow);	}else{ display_num = new GIcon(baseIcon, "http://www.oldlistings.com.au/mapsicons/iconb.png", null, baseIcon.shadow);}
	}else if(category.toLowerCase() == "apartment") 			{ if (num_rooms >= "1" && num_rooms <= "9"){ display_num = new GIcon(baseIcon, "http://www.oldlistings.com.au/mapsicons/icong"+ num_rooms +".png", null, baseIcon.shadow);	}else{ display_num = new GIcon(baseIcon, "http://www.oldlistings.com.au/mapsicons/icong.png", null, baseIcon.shadow);}
	}else if(category.toLowerCase() == "unit") 					{ if (num_rooms >= "1" && num_rooms <= "9"){ display_num = new GIcon(baseIcon, "http://www.oldlistings.com.au/mapsicons/icon1"+ num_rooms +".png", null, baseIcon.shadow);	}else{ display_num = new GIcon(baseIcon, "http://www.oldlistings.com.au/mapsicons/icon1.png", null, baseIcon.shadow);}
	}else if(category.toLowerCase() == "townhouse") 			{ if (num_rooms >= "1" && num_rooms <= "9"){ display_num = new GIcon(baseIcon, "http://www.oldlistings.com.au/mapsicons/icon2"+ num_rooms +".png", null, baseIcon.shadow);	}else{ display_num = new GIcon(baseIcon, "http://www.oldlistings.com.au/mapsicons/icon2.png", null, baseIcon.shadow);}
	}else if(category.toLowerCase() == "land") 					{ if (num_rooms >= "1" && num_rooms <= "9"){ display_num = new GIcon(baseIcon, "http://www.oldlistings.com.au/mapsicons/icon3"+ num_rooms +".png", null, baseIcon.shadow);	}else{ display_num = new GIcon(baseIcon, "http://www.oldlistings.com.au/mapsicons/icon3.png", null, baseIcon.shadow);}
	}else if(category.toLowerCase() == "villa") 				{ if (num_rooms >= "1" && num_rooms <= "9"){ display_num = new GIcon(baseIcon, "http://www.oldlistings.com.au/mapsicons/icon4"+ num_rooms +".png", null, baseIcon.shadow);	}else{ display_num = new GIcon(baseIcon, "http://www.oldlistings.com.au/mapsicons/icon4.png", null, baseIcon.shadow);}
	}else if(category.toLowerCase() == "unit-apmt") 			{ if (num_rooms >= "1" && num_rooms <= "9"){ display_num = new GIcon(baseIcon, "http://www.oldlistings.com.au/mapsicons/icon5"+ num_rooms +".png", null, baseIcon.shadow);	}else{ display_num = new GIcon(baseIcon, "http://www.oldlistings.com.au/mapsicons/icon5.png", null, baseIcon.shadow);}
	}else if(category.toLowerCase() == "acreage-semi-rural")	{ if (num_rooms >= "1" && num_rooms <= "9"){ display_num = new GIcon(baseIcon, "http://www.oldlistings.com.au/mapsicons/icon6"+ num_rooms +".png", null, baseIcon.shadow);	}else{ display_num = new GIcon(baseIcon, "http://www.oldlistings.com.au/mapsicons/icon6.png", null, baseIcon.shadow);}
	}else if(category.toLowerCase() == "duplex-semi-detached")	{ if (num_rooms >= "1" && num_rooms <= "9"){ display_num = new GIcon(baseIcon, "http://www.oldlistings.com.au/mapsicons/icon7"+ num_rooms +".png", null, baseIcon.shadow);	}else{ display_num = new GIcon(baseIcon, "http://www.oldlistings.com.au/mapsicons/icon7.png", null, baseIcon.shadow);}
	}else if(category.toLowerCase() == "rural") 				{ if (num_rooms >= "1" && num_rooms <= "9"){ display_num = new GIcon(baseIcon, "http://www.oldlistings.com.au/mapsicons/icon8"+ num_rooms +".png", null, baseIcon.shadow);	}else{ display_num = new GIcon(baseIcon, "http://www.oldlistings.com.au/mapsicons/icon8.png", null, baseIcon.shadow);}
	}else 														{ if (num_rooms >= "1" && num_rooms <= "9"){ display_num = new GIcon(baseIcon, "http://www.oldlistings.com.au/mapsicons/icon9"+ num_rooms +".png", null, baseIcon.shadow);	}else{ display_num = new GIcon(baseIcon, "http://www.oldlistings.com.au/mapsicons/icon9.png", null, baseIcon.shadow);}
	}
	var search_text = address;
	search_text = search_text.replace(/<h2>/g,"");
	search_text = search_text.replace(/<\/h2>/g,"");
	search_text = search_text.replace(/<H2>/g,"");
	search_text = search_text.replace(/<\/H2>/g,"");
	search_text = search_text.replace(/&amp;/g," ");
	
	//search_text = search_text.replace(/\//g," ");
	var search_box = "<div class='mapdata'>";
	search_box += get_google_search(search_text);
	search_box += "</div>";
	
	if(lon != "" && lat != "" && added == "n"){
		if((typeof myPano) == 'undefined'){
			initialize_pano(lat, lon);
		}
		var point = new GLatLng(lat,lon);
		var marker = createMarker(point,'<div id="map_marker">'+address+price+content+'</div>'+search_box,that.id,the_price,display_num);
		map.addOverlay(marker);
		subd[i - 1].innerHTML = "added";
		GEvent.trigger(gmarkers[that.id], "click");
		//map.setCenter(point);
	}else{
		myclick(that.id);
	}
	if(document.getElementById('map').style.display != "none"){
		document.getElementById('boxcontent').innerHTML = "<input type='button' onclick=\"resize('10','px')\" value=\"Hide Map\" />";
		if(document.getElementById('pano').style.display != "none"){
			if ((typeof myPano) != 'undefined'){
				document.getElementById('boxcontent').innerHTML += "<input type='button' onclick=\"hide_street_view()\" value=\"Hide Street View\" />";
			}
		}else{
			if ((typeof myPano) != 'undefined'){
				document.getElementById('boxcontent').innerHTML += "<input type='button' onclick=\"show_street_view()\" value=\"Show Street View\" />";
			}
		}
	}
	map.checkResize();
}

function resize(size,type){

	var height = get_page_height();
	var pano_height = 200;

	if(type == "px"){
		document.getElementById('map').style.display = "none";
		document.getElementById('pano').style.display = "none";
		document.getElementById('box').style.height=size+'px';
	}else{
		var map_height = 0;

		map_height = height - 30 - pano_height;
		document.getElementById('pano').style.display = "";
		document.getElementById('pano').style.height=pano_height+'px';

		document.getElementById('map').style.display = "";
		document.getElementById('box').style.height=height+'px';
		document.getElementById('map').style.height=map_height+'px';
	}

	if(document.getElementById('map').style.display != "none"){
		document.getElementById('boxcontent').innerHTML = "<input type='button' onclick=\"resize('10','px')\" value=\"Hide Map\" />";
		if(document.getElementById('pano').style.display != "none"){
			if((typeof myPano) != 'undefined'){
				document.getElementById('boxcontent').innerHTML += "<input type='button' onclick=\"hide_street_view()\" value=\"Hide Street View\" />";
			}
		}else{
			if((typeof myPano) != 'undefined'){
				document.getElementById('boxcontent').innerHTML += "<input type='button' onclick=\"show_street_view()\" value=\"Show Street View\" />";
			}
		}
	}else{
		document.getElementById('boxcontent').innerHTML = "<input type='button' onclick=\"resize('100','%')\" value=\"Show Map\" />";
	}

	document.getElementById('rightad').innerHTML = "";
	map.checkResize();
	
}
function hide_street_view(){
	if((typeof myPano) != 'undefined'){
		myPano.hide();
	}
	document.getElementById('pano').style.display = "none";
	
	var height = get_page_height();
	
	var map_height = height - 30;
	document.getElementById('map').style.height=map_height+'px';
	document.getElementById('boxcontent').innerHTML = "<input type='button' onclick=\"resize('10','px')\" value=\"Hide Map\" />";
	map.checkResize();
	
	document.getElementById('boxcontent').innerHTML = "<input type='button' onclick=\"resize('10','px')\" value=\"Hide Map\" />";
	if(document.getElementById('pano').style.display != "none"){
		document.getElementById('boxcontent').innerHTML += "<input type='button' onclick=\"hide_street_view()\" value=\"Hide Street View\" />";
	}else{
		if((typeof myPano) != 'undefined'){
			document.getElementById('boxcontent').innerHTML += "<input type='button' onclick=\"show_street_view()\" value=\"Show Street View\" />";
		}
	}
}

function show_street_view(){

	var height = get_page_height();
	var pano_height = 200;
		
	var map_height = height - 30 - pano_height;
	document.getElementById('pano').style.display = "";
	document.getElementById('pano').style.height=pano_height+'px';
	document.getElementById('pano').style.width = "320px";
	document.getElementById('map').style.height=map_height+'px';

	//document.getElementById('boxcontent').innerHTML = "<input type='button' onclick=\"resize('10','px')\" value=\"Hide Map\" />";
	if((typeof myPano) != 'undefined'){
		myPano.show();
	}
	map.checkResize();
	
	document.getElementById('boxcontent').innerHTML = "<input type='button' onclick=\"resize('10','px')\" value=\"Hide Map\" />";
	if(document.getElementById('pano').style.display != "none"){
		document.getElementById('boxcontent').innerHTML += "<input type='button' onclick=\"hide_street_view()\" value=\"Hide Street View\" />";
	}else{
		if((typeof myPano) == 'undefined'){
			document.getElementById('boxcontent').innerHTML += "<input type='button' onclick=\"show_street_view()\" value=\"Show Street View\" />";
		}
	}

}

function get_page_height(){
	var height = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		height = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		height = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		height = document.body.clientHeight;
	}else{
		height = 300;
	}
	return height;
}

/* *********************************************** */
function handleInitialized(location) {
	houseMarker.setLatLng(new GLatLng(location.latlng.lat(), location.latlng.lng()))
	return;	
}


function show_street(marker){
	houseMarker.setLatLng(new GLatLng(marker.getLatLng().lat(), marker.getLatLng().lng()))
	panoClient.getNearestPanorama(houseMarker.getLatLng(), showPanoData);

}
	
function showPanoData(panoData) {
	if (panoData.code != 200) {
		//GLog.write('showPanoData: Server rejected with code: ' + panoData.code);
		panoerrors += panoData.code;
		return;
	}
	var angle = computeAngle(houseMarker.getLatLng(), panoData.location.latlng);
	myPano.setLocationAndPOV(panoData.location.latlng, {yaw: angle});
}

function computeAngle(endLatLng, startLatLng) {
  var DEGREE_PER_RADIAN = 57.2957795;
  var RADIAN_PER_DEGREE = 0.017453;

  var dlat = endLatLng.lat() - startLatLng.lat();
  var dlng = endLatLng.lng() - startLatLng.lng();
  // We multiply dlng with cos(endLat), since the two points are very closeby,
  // so we assume their cos values are approximately equal.
  var yaw = Math.atan2(dlng * Math.cos(endLatLng.lat() * RADIAN_PER_DEGREE), dlat) * DEGREE_PER_RADIAN;
  return wrapAngle(yaw);
}

function wrapAngle(angle) {
	if (angle >= 360) {
		angle -= 360;
	} else if (angle < 0) {
		angle += 360;
	}
	return angle;
};
/* *********************************************** */

function get_google_search(search){
	var html = "";
	html += "<div class='cse-branding-bottom' style='background-color:#FFFFFF;color:#000000'>\n";
	html += "  <div class='cse-branding-form'>\n";
	if (navigator.appVersion.indexOf("MSIE") != -1){
		//alert('IE');
		html += "    <form action='http://www.google.com/cse' id='cse-search-box' >\n";
	}else{
		html += "    <form action='http://www.google.com/cse' id='cse-search-box' target='_blank'>\n";
	}
	html += "      <div>\n";
	html += "        <input type='hidden' name='cx' value='000244904622550717011:eai3sribsaq' />\n";
	html += "        <input type='hidden' name='ie' value='UTF-8' />\n";
	html += "        <input type='text' name='q' size='20' value='"+ search + "'/>\n";
	html += "        <input type='submit' name='sa' value='Search' />\n";
	html += "      </div>\n";
	html += "    </form>\n";
	html += "  </div>\n";
	html += "  <div class='cse-branding-logo'>\n";
	html += "    <img src='http://www.google.com/images/poweredby_transparent/poweredby_FFFFFF.gif' alt='Google' />\n";
	html += "  </div>\n";
	html += "  <div class='cse-branding-text'>\n";
	html += "    Custom Search\n";
	html += "  </div>\n";
	html += "</div>\n";
	
	return html;
}
