// not needed for api v3
//var google_maps_key = 'ABQIAAAARZjfVLdgh_BBZJi6-biOEhQq8e6wg6ggAF2reMLErOqGZhI-WxS5LBELi340zNwKRV5slxblwrCCvQ';

function OpenWindow(theURL,winName) { //v2.0
  window.open(theURL,winName,"fullscreen=1,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0");
  return false;
}

function toggleImageCheckbox(name) {
	var input = document.getElementsByName(name).item(0);
	var box = document.getElementById(name+"_checkbox");
	var cursrc = box.getAttribute("src");
	var newsrc = cursrc.replace("_checked.png", '');
	newsrc = newsrc.replace('.png', '');
	var val = 'yes';
	if(box.src.indexOf("checked") > -1) {
		newsrc += '.png';
		val = 'no';
	}
	else {
		newsrc += '_checked.png';
	}
	box.src = newsrc;
	input.value = val;
}

function toggleAdvancedSearch() {
	var adv = document.getElementById("search_advanced");
	var method = document.getElementsByName("search_method").item(0);
	var img = document.getElementById("search_poweredby");
	if(adv.style.display == 'none' || adv.style.display == '') {
		method.value = 'advanced';	
		img.src = "/images/poweredby_blue_nograd.png";
		$('#advanced_search_link').html('&#9658; Basic Search');
	}
	else {
		$('#advanced_search_link').html('&#9658; Advanced Search');
		method.value = 'normal';
		img.src = "/images/poweredby_blue.png";
	}
	
	$("#search_advanced").toggle("slow");
}

window.onload = function() {
	if(document.getElementById("search_poweredby")) {
		var preload = document.createElement("img");
		preload.setAttribute("src", "/images/poweredby_blue_nograd.png");
		var preload2 = document.createElement("img");
		preload2.setAttribute("src", "/images/poweredby_blue.png");
	}
	var textinputs = document.getElementsByTagName("input");
	for(var i = 0; i < textinputs.length; i++) {
		if(textinputs[i].type.toLowerCase() == 'text') {
			textinputs[i].onfocus = textInputFocus;
		}
	}
}

function textInputFocus(e) {
	if(window.event)
	{
		var target = window.event.srcElement;
	}else{
		target = e.target;
	}
	ignore_values = String(ignore_values);
	if(ignore_values.indexOf(target.value) != -1)
	{
		target.value = '';
	}
}

function contactAgent(agent_id) {
	var url = unescape('/contact.php%3Fwidth%3D485%26height%3D300%26action%3Dmessage%26agent%3D')+agent_id;
	tb_show('Contact Agent', url, false);
}

function submitContactForm() {
	var theform = document.getElementById("contact_form");
	var data = new Object();
	var value;
	for(var i = 0; i < theform.elements.length; i++) {
		if(true || theform.elements[i].nodeName.toLowerCase() == 'input') {
			value = theform.elements[i].value;
		}
		else value = theform.elements[i].innerHTML;
		data[theform.elements[i].name] = value;
	}
	var url = unescape('/contact.php%3Fwidth%3D485%26height%3D300%26action%3Dmessage');
	$.post(url, data, function(response, status) {
		if(response == 'ok') tb_remove();
		else {
			$("#TB_ajaxContent").html(response);
		}
	});
	return false;
}

var Maps = {
	map: undefined,
	geocoder: undefined,
	marker: undefined,
	info: undefined,
	address: undefined,
	showMapTab: function(address) {
		$("#property_photos").hide();
		this.geoCodeAndShowMap(address);
		$("#property_map").show();
	},
	geoCodeAndShowMap: function(address) {
		this.address = address;
		this.geocoder = new google.maps.Geocoder(); 
		this.geocoder.geocode( { address: address }, function(results, geostatus) {
			if (geostatus == google.maps.GeocoderStatus.OK && results.length) {
				// You should always check that a result was returned, as it is
				// possible to return an empty results object.
				if (geostatus != google.maps.GeocoderStatus.ZERO_RESULTS) {
					Maps.showMap(results[0].geometry.location);	
				}
			}
			 else {
				alert("Address lookup was unsuccessful due to: " + geostatus);
			}
		});
	},
	showMap: function(geoloc) {
		if(this.map == undefined) {
			var mapOpts = {
				zoom: 18,
				center: geoloc,
				mapTypeId: google.maps.MapTypeId.HYBRID
			}
			this.map = new google.maps.Map(document.getElementById("property_map"), mapOpts);
			this.marker = new google.maps.Marker({
				position: geoloc,
				map: this.map,
				icon: new google.maps.MarkerImage("/images/icon_house_28.png", new google.maps.Size(28,28,'px','px')),
				clickable: true
			});
/*
			this.info = new google.maps.InfoWindow({
				content: 'test',
				pixelOffset: new google.maps.Size(4,4,'px','px')
			});
*/
			google.maps.event.addListener(this.marker, 'click', function() {
				//Maps.info.open(Maps.map, Maps.marker);
				window.open(Maps.mapLink(Maps.address));
			});
		}
	},
	mapLink: function(address) {
		return 'http://maps.google.com/maps?f=q&source=s_q&hl=en&t=h&q='+encodeURIComponent(address);
	}
}
function showPhotosTab() {
	$("#property_map").hide();
	$("#property_photos").show();
}

function changeSearchTab(agent_id, connect_only, cpath) {
	var newform = document.createElement("form");
	newform.setAttribute("method", "post");
	newform.setAttribute("action", "/"+cpath+"/search");
	var in_agent_id = document.createElement("input");
	in_agent_id.setAttribute("name", "agent_id");
	in_agent_id.value = agent_id;
	var in_connect_only = document.createElement("input");
	in_connect_only.setAttribute("name", "connect_only");
	in_connect_only.value = connect_only;
	var in_method = document.createElement("input");
	in_method.setAttribute("name", "search_method");
	in_method.value = (connect_only=='yes')?"normal":"local";
	var in_vtour_only = document.createElement("input");
	in_vtour_only.setAttribute("name", "vtour_only");
	in_vtour_only.value = (agent_id=='')?'yes':'no';
	newform.appendChild(in_method);
	newform.appendChild(in_agent_id);
	newform.appendChild(in_connect_only);
	newform.appendChild(in_vtour_only);
	newform.style.visibility = "hidden";
	document.body.appendChild(newform);
	newform.submit();
//$('#agent_id_input').val('<?=$agent_id;?>');$('#connect_only_input').val('yes'); document.search_form.submit();
}


