//when page loads start standard functions
function initialize() {
	//replace external links with open in new window
	externalLinks();
}

window.onload = initialize;


function $import(src){
	var scriptElem = document.createElement('script');
  scriptElem.setAttribute('src',src);
	scriptElem.setAttribute('type','text/javascript');
	document.getElementsByTagName('head')[0].appendChild(scriptElem);
}


// import with a random query parameter to avoid caching
function $importNoCache(src){
	var ms = new Date().getTime().toString();
	var seed = "?" + ms; 
	$import(src + seed);
}


function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors .length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
			anchor.target = "_blank";
			anchor.title = (anchor.title != "") ? anchor.title+" (opens in a new window) ": "opens in a new window";
			anchor.className = (anchor.className != '') ? anchor.className+' external' : 'external';
//			anchor.onclick = 'javascript: pageTracker._trackPageview (\'/outgoing/'+anchor.href+'\');';
			var track = anchor.href;
			track = track.replace('http://','');
			anchor.onclick = function() { 
				pageTracker._trackPageview('/outgoing/'+track);
			}
		}
	}
}


function prepareInput(id,value)
{
	if(document.getElementById(id).value==value) document.getElementById(id).value='';
}

function searchArea(areaCode)
{
	var url = "/local-market.htm?areas="+areaCode;
	window.location = url;
}


function loadMap(mapPhoto)
{
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map-area"));
		map.addControl(new GSmallMapControl());
		var point = new GLatLng($("#lat").val(), $("#lng").val());

		if(mapPhoto=="photo")
		{
			map.setMapType(G_SATELLITE_MAP);
			map.setCenter(point, (15));
		} else {
			map.setCenter(point, (14));
		}

		var marker = new GMarker(point, getIcon());
		map.addOverlay(marker);

		if(mapPhoto=="map")
		{
			GEvent.addListener(marker, "click", function() {
				map.setCenter(new GLatLng(point), (map.getZoom()+1))
			});
		}

	}
}

function getIcon()
{
	var icon = new GIcon();
	icon.image = "/images/mapicon.png";
	icon.shadow = "/images/mapicon_shadow.png";
	icon.iconSize = new GSize(25, 32);
	icon.shadowSize = new GSize(49, 38);
	icon.iconAnchor = new GPoint(0, 0);

	return icon;
}


var currentDisplay = "gallery";
var currentNav = "pd-photos";
function showContent(nav,show)
{
	if(show!=currentDisplay)
	{
		$("#"+show).css("display","block");
		$("#"+currentDisplay).css("display","none");
		currentDisplay = show;
	}

	$("#"+nav).addClass("current");
	$("#"+currentNav).removeClass("current");
	currentNav = nav;
}



//jquery functions
$(document).ready(function() {
	initialize();

	$("#pd-photos").click(function(){
		showContent($(this).attr("id"),$(this).attr("rel"));
	});

	$("#pd-video").click(function(){
		var temp = "";
		var videoId = $("#property-video-area").attr("rel");
		var width = 500;
		var height = 288;

		temp +=  "\n<div class=\"vzaar_media_player\">";
			temp +=  "\n<object id=\"video\" width=\""+width+"\" height=\""+height+"\" type=\"application/x-shockwave-flash\" data=\"http://view.vzaar.com/"+videoId+".flashplayer\">";
				temp +=  "\n<param name=\"movie\" value=\"http://view.vzaar.com/"+videoId+".flashplayer\">";
				temp +=  "\n<param name=\"allowScriptAccess\" value=\"always\">";
				temp +=  "\n<param name=\"allowFullScreen\" value=\"true\">";
				temp +=  "\n<param name=\"wmode\" value=\"transparent\">";
				temp +=  "\n<param name=\"flashvars\" value=\"\">";
				temp +=  "\n<embed src=\"http://view.vzaar.com/"+videoId+".flashplayer\" type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\""+width+"\" height=\""+height+"\" allowScriptAccess=\"always\" allowFullScreen=\"true\" flashvars=\"\"></embed>";
				temp +=  "\n<video width=\""+(width-14)+"\" height=\""+(height-14)+"\" src=\"http://view.vzaar.com/"+videoId+".mobile\" poster=\"http://view.vzaar.com/"+videoId+".image\" controls></video>";
			temp +=  "\n</object>";
		temp +=  "\n</div>";

		$("#property-video-area").html(temp);
	});

	
	$("#pd-location-map").click(function(){
		showContent($(this).attr("id"),$(this).attr("rel"));
		//loadMap("map");
	});

	$("#pd-aerial-photo").click(function(){
		showContent($(this).attr("id"),$(this).attr("rel"));
		//loadMap("photo");
	});

	$("#pd-video").click(function(){
		showContent($(this).attr("id"),$(this).attr("rel"));
	});

	$("#main_content_area").corner("8px");

	$("#banner_area").corner("8px tl tr");

	$("#price_links").corner("8px bl br");

	var myDate = new Date();
	$("#year").html("&copy; Shields and Rutland " + myDate.getFullYear());

	$("#search").corner("4px");

	$("#paging").corner("4px");

	$(".return-to-search").corner("4px");

	$(".list_property .photo-area .photo-container").corner("5px");

	$("#property #gallery #photo-area, #property #property-map-area, #property #property-photo-map-area, #property #property-video-area").corner("5px");

	if ($("#homepage #map").length > 0 ) {
		$("#homepage #map").flash(
			{	src: "/flash/guernsey.swf",
				width: 360,
				height: 298
			},
			{ version: 8 }
		);
	}

	$("a.lightbox").lightBox()

});
