//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='';
}


//jquery functions
$(document).ready(function() {
	initialize();
	$(".home_page_banner").each( function() {
		$this = $(this);
		$this.wrap("<div id=\"banner_home_page\" style=\"width:"+$this.attr("width")+"px; height:"+$this.attr("height")+"px; background-image: url("+$this.attr("src")+");\"></div>");
		$this.hide();
	});

	$(".standard-page-banner").each( function() {
		$this = $(this);
//		$this.wrap("<div id=\"banner_standard_page\" style=\"width:"+$this.attr("width")+"px; height:"+$this.attr("height")+"px; background-image: url("+$this.attr("src")+");\"></div>");
//		$this.hide();
	});

	$("#banner_home_page").corner("tl bl 6px");
	$("#banner_standard_page").corner("tr br 6px");


	$(".thumbnail a").mouseover(function(){
		$(".main_picture_area img").attr("src",$(this).attr("rel"));
		$(".main_picture_area img").attr("title",$(this).attr("title"));
		$("#banner_title").html($(this).attr("title"));
	});

	$(".thumbnail img").mouseover(function(){
		$(this).animate({opacity: 0.6}, 200);
	});

	$(".thumbnail img").mouseout(function(){
		$(this).animate({opacity: 1}, 200);
	});

	var currentTime = new Date();
	$("#year").html("&copy; "+currentTime.getFullYear()+" JB Riney & Co Ltd");


	if ( $("#contact_map").length > 0 ) {
		loadContactMap();
	}

	$(".extra_content_item a .image_wrap img, .case_study_left a img, .case_study_image a img").mouseover(function(){
		$(this).animate({opacity: 0.6}, 200);
	});

	$(".extra_content_item a .image_wrap img, .case_study_left a img, .case_study_image a img").mouseout(function(){
		$(this).animate({opacity: 1}, 200);
	});

	if ( $(".standard_page #content_text").length > 0 ) {
		var el = $(".standard_page #content_text");
		el.html(el.html().replace(/J B Riney &amp; Co Ltd/ig, "<span class=\"jb_highlight\">J B Riney &amp; Co Ltd</span>"));
	}
});

setInterval("settime()", 1000);
function settime () {
	var curtime = new Date();
	var curhour = curtime.getHours();
	var curmin = curtime.getMinutes();
	var cursec = curtime.getSeconds();
	var time = "";
	var month = curtime.getMonth() + 1
	var day = curtime.getDate()
	var year = curtime.getFullYear()

	if(curhour == 0) curhour = 12;
	time = (curhour > 12 ? curhour - 12 : curhour) + ":" +
		(curmin < 10 ? "0" : "") + curmin + ":" +
		(cursec < 10 ? "0" : "") + cursec + " " +
		(curhour > 12 ? "PM" : "AM");
	$("#query").html(day + "/" + month + "/" + year + " " + time);
}



function loadContactMap()
{
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("contact_map"));
		map.addControl(new GSmallMapControl());

		var point = new GLatLng(51.53435, -0.02133);
		var myHtml = "<b>Head Office</b><br/>J B Riney &amp; Co Ltd, 455 Wick Lane, Bow, London, E3 2TB";
		createContactMarket(point,myHtml);
		var point = new GLatLng(51.56135, 0.02133);
		map.setCenter(point, (11));

		var point = new GLatLng(51.49603, 0.10274);
		var myHtml = "<b>Riney Area Office (South London)</b><br/>18 - 20 Purland Road, Thamesmead, London, SE28 0AT";
		createContactMarket(point,myHtml);

		var point = new GLatLng(51.57432, -0.03755);
		var myHtml = "<b>Riney Area Office (North London) and Sign Manufacturing Plant</b><br/>Low Hall Depot, Argall Avenue, Leyton, London, E10 7AS";
		createContactMarket(point,myHtml);

	}
}

function createContactMarket(point,myHtml)
{
	var marker = new GMarker(point, getIcon());
	GEvent.addListener(marker, "click", function() {
		map.openInfoWindowHtml(point, myHtml);
	});

	map.addOverlay(marker);

}



function displayCaseStudy()
{
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallMapControl());

		var point = new GLatLng(locationPoints['lat'], locationPoints['lng']);
		createMarker(point)
		map.setCenter(point, (13));
	}
}

function createMarker(point)
{
	var marker = new GMarker(point, getIcon());

	GEvent.addListener(marker, "click", function() {
		window.location='/case-studies.php?caseStudyId=' + locationPoints['caseStudyId'] + '#case-study-anchor';
	});

	map.addOverlay(marker);
}

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

	return icon;
}


