$(document).ready(function() {

	var browser=0;
	
	//get Microsoft browser version
	if (navigator.appName=="Microsoft Internet Explorer") {
		var browser=parseFloat((new RegExp("MSIE ([0-9]{1,}[.0-9]{0,})")).exec(navigator.userAgent)[1]);
	}

	$("#city").keyup(function () {
	
		document.getElementById("zipcode").value="";
		
		//triger right citylist when changing selectbox;
		if (this.value.length > 2 && browser!=6) {
			var region=document.getElementById("region").value;
			getCities(this.value, region);
		} else {
		// hide suggest box
		document.getElementById("citylist").style.display="none";
		}
		
		
	});
	
	$("#region").change(function () {
		
		//make cities field empty
		document.getElementById("city").value="";
		
		document.getElementById("zipcode").value="";
		
		// hide suggest box
		document.getElementById("citylist").style.display="none";
		
		//triger right citylist when selectbox has region specified
		var city=document.getElementById("city").value;
		
		if (city.length > 2) {
			getCities(city, this.value);
		}
	});
	
	
	
	$("#zipcode").keyup(function () {
		document.getElementById("kmrange").value=0;
		document.getElementById("region").value="";
		document.getElementById("city").value="";
		document.getElementById("citylist").style.display="none";
	});
	
	
	$("#kmrange").change(function () {
		
		//make zipcode field empty
		document.getElementById("zipcode").value="";
	});
	
	$("#maplink").click(function () {
		// delete mapSearch cookie to avoid double cookies
		if ($.cookie("mapSearch")=="true") {
			$.cookie("myCookie", null, {path: "/"});
				$.cookie("mapSearch", "false", { expires: 365, path: '/'});
			} else {
				$.cookie("myCookie", null, {path: "/"});
				$.cookie("mapSearch", "true", { expires: 365, path: '/'});
			}
		doMapLink();
	});
	
	if ($.cookie("mapSearch")=="true") {
		doMapLink();
	}
		
	// check if google map div exists
	if ($("#map_canvas").length) {
		initialize();
	};
	
	// list an overview of images
	if ($("#image-overview").length) {
		getImageOverview($.getQueryString({ ID:"propertyId" }));
	}
	
	$(".advert-link").click(function () {
		countClicks(this.id, this.href)
	});
	
	$('a.mailto').click( function() {
		$(this).attr('href', 'mailto:' + $(this).attr('title').replace(/\[at\]/, '@'));
	} );
})

function countClicks(idValue,urlValue) {
	theId=idValue;
	var idSplit = theId.split("-");
	var adCampagneId = idSplit[1];
	var adTypeId=idSplit[2];
	var theUrl=urlValue;
	$.get("/app/include/do_clicks.cfm", {campagne_id:adCampagneId, clickedurl:theUrl, type_id:adTypeId},
		function(data){
		//alert("Data Loaded: " + data);
		});
}