//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 prepareSearchBox ()
{
	document.getElementById('q').style.width = '200px';
	document.getElementById('search-area').style.width = '300px';
	if(document.getElementById('q').value=='enter keywords') document.getElementById('q').value='';
}

function resetSearchBox ()
{
	document.getElementById('search-area').style.width = '200px';
	document.getElementById('q').style.width = '100px';
}



function updateMainImage(arrayId)
{
	rotatePicture = false;
	document.getElementById('main-large-picture').src = availablePhotos[arrayId];
}


var num = 1;
var rotatePicture = true;
var runOnce = false;

function autoRotatePhotos()
{
	setInterval("update()", 5000);
}

function update()
{
	if (availablePhotos.length==1)
	{
		rotatePicture = false;
	}

	if (rotatePicture)
	{
		document.getElementById('main-large-picture').src = availablePhotos[num];
	}

	if (runOnce)
	{
		rotatePicture = false;
	}


	num++;

	if (num == availablePhotos.length)
	{
		num = 0;
		runOnce = true;
	}
}


function searchByYearMonth(url)
{
	var fullUrl = url + '?yearMonth=' + document.getElementById('yearMonth').value;
	if (document.getElementById('filterBuildingId'))
	{
		fullUrl = fullUrl + '&filterBuildingId=' + document.getElementById('filterBuildingId').value;
	}
	window.location =  fullUrl;
}


function filterByBuildingId(url)
{
	window.location = url + '&filterBuildingId=' + document.getElementById('filterBuildingId').value;
}




function loadMap() {
	if (document.getElementById('map'))
	{
		if (document.getElementById('latitude') && document.getElementById('longitude'))
		{
			if (GBrowserIsCompatible()) {
				var icon = new GIcon();
				icon.image = "/images/mappointer.png";
				icon.shadow = "/images/mappointershadow.png";
				icon.iconSize = new GSize(29, 36);
				icon.shadowSize = new GSize(60, 36);
				icon.iconAnchor = new GPoint(14, 36);
				icon.infoWindowAnchor = new GPoint(37, 41);
				
				
				var map = new GMap2(document.getElementById("map"));
				map.addControl(new GSmallMapControl());
				map.setCenter(new GLatLng(document.getElementById('latitude').value, document.getElementById('longitude').value), 13);

				var marker = new GMarker(map.getCenter(), icon);
				map.addOverlay(marker);


				GEvent.addListener(marker, "click", function() {
					map.setCenter(new GLatLng(document.getElementById('latitude').value,  document.getElementById('longitude').value), (map.getZoom()+1))
				});
			}
		}
	}
}

var map;
var xAdjustment = 5;
var yAdjustment = 50;

function loadMainMap() {
	if (document.getElementById('main-map'))
	{
		if (GBrowserIsCompatible()) {
			map = new GMap2(document.getElementById("main-map"));
			map.addControl(new GSmallMapControl());
			map.setCenter(new GLatLng(52.63038006359, 1.295099258422), 15);

			GEvent.addListener(map, "zoomend", function() {
				map.clearOverlays();
				drawIcons();
			});

			drawIcons();
/*
			GEvent.addListener(map, "dragend", function() {
				alert(map.getCenter());
			});
*/

		}
	}
}


function drawIcons()
{
	var zoomLevel = map.getZoom();
	var iconSize = 20;
//	alert('zoomLevel:' + zoomLevel);
	if (zoomLevel<15)
	{
		iconSize = 20;
		yAdjustment = 50;
	} else if (zoomLevel==15) {
		iconSize = 41;
		yAdjustment = 60;
	} else if (zoomLevel==16) {
		iconSize = 65;
		yAdjustment = 70;
	} else if (zoomLevel>16) {
		iconSize = 91;
		yAdjustment = 85;
	}
	
	
	for (i=0; i<buildings.length; i++)
	{
		createMarker(i,iconSize)

//		var marker2 = new GMarker(point);
//		map.addOverlay(marker2);
	}
}


function createMarker(arrayId,iconSize)
{

	var icon = new GIcon();

	icon.image = "/images/map-icons/"+buildings[arrayId][0]+".png";
	//icon.shadow = "/images/mappointershadow.png";
	icon.iconSize = new GSize(iconSize, iconSize);
	//icon.shadowSize = new GSize(60, 36);
	icon.iconAnchor = new GPoint((iconSize/2), (iconSize/2));
	icon.infoWindowAnchor = new GPoint(37, 41);
	
	var point = new GLatLng(buildings[arrayId][1], buildings[arrayId][2])
	var marker = new GMarker(point, icon);
	marker.value = arrayId;

	GEvent.addListener(marker, "click", function() {
		window.location=buildings[arrayId][4];
	});

	GEvent.addListener(marker, "mouseover", function() {
		var gPoint = map.fromLatLngToDivPixel(point);
		var gPoint2 = map.fromLatLngToDivPixel(map.getCenter());
		displayLabel (gPoint.x+(250-gPoint2.x),((gPoint.y+(310-gPoint2.y))*1),arrayId);
	});

	GEvent.addListener(marker, "mouseout", function() {
		hideLabel();
	});

	map.addOverlay(marker);

}


function displayLabel(x,y,arrayId)
{
	var xposition = x + xAdjustment;
	var yposition = y - yAdjustment;
	document.getElementById('map-label').style.left = xposition;
	document.getElementById('map-label').style.top = yposition;
	document.getElementById('map-label').style.visibility = 'visible';
	document.getElementById('map-label').innerHTML = buildings[arrayId][3];
}


function hideLabel()
{
	document.getElementById('map-label').style.visibility = 'hidden';
}

function resetMap()
{
	map.setCenter(new GLatLng(52.63038006359, 1.295099258422), 15);
}

function prepareName()
{
	if(document.getElementById('name').value=='your name') document.getElementById('name').value='';
}

function prepareEmail()
{
	if(document.getElementById('email').value=='your email address') document.getElementById('email').value='';
}

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

	$("#main-pictures").show()
	$('ul#main-pictures').innerfade({
		speed: 1000,
		timeout: 8000,
		type: 'sequence',
		containerheight: '150px'
	});

	// Reset Font Size
	var originalFontSize = $('html').css('font-size');
		$(".resetFont").click(function(){
		$('html').css('font-size', originalFontSize);
		$.cookie('cookieFontSize', '', { expires: -1 });
	});
	
	// Increase Font Size
	$(".increaseFont").click(function(){
		var currentFontSize = $('html').css('font-size');
		var currentFontSizeNum = parseFloat(currentFontSize, 10);
		var newFontSize = currentFontSizeNum*1.2;
		$('html').css('font-size', newFontSize);
		$.cookie('cookieFontSize', newFontSize, { expires: 7 });
		return false;
	});
	
	// Decrease Font Size
	$(".decreaseFont").click(function(){
		var currentFontSize = $('html').css('font-size');
		var currentFontSizeNum = parseFloat(currentFontSize, 10);
		var newFontSize = currentFontSizeNum*0.8;
		$('html').css('font-size', newFontSize);
		$.cookie('cookieFontSize', newFontSize, { expires: 7 });
		return false;
	});

	//cookie code
	if($.cookie('cookieFontSize')!='')
	{
		$('html').css('font-size', $.cookie('cookieFontSize'));
	}

	$('#main-links > li').bind('mouseover', mmul_open);
	$('#main-links > li').bind('mouseout',  mmul_timer);

	//load buildings map
	loadMap();

	//load the main map
	loadMainMap();

});