// Global style sheet (c) Multimap.com

// Add Load Event
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

// scalebar zoom
function scalebar(scale) {
	// loop through scales dropdown and set scale before submitting
	document.map.zm.value = 1;
	scales = document.map.scale;
	for (i=0; i < scales.length; i++) {
		if (scale == scales.options[i].value) {
			scales.options[i].selected = true;
			break;
		}
	}
	document.map.submit();
}

function showAerial() {}

// panning bars mouseover
function rollover(imgObj, imgId, imgId2) {
	if (document.getElementById) {
		imgSrc = "/images/ps/pan/" + imgId + "_over.gif";
		imgObj.src = imgSrc;
		if (imgId2) {
			id = "pan" + imgId2.toUpperCase();
			imgObj = document.getElementById(id);
			imgSrc = "/images/ps/pan/" + imgId2 + "_over.gif";
			imgObj.src = imgSrc;
		}
	}
}
function rollout(imgObj, imgId, imgId2) {
	if (document.getElementById) {
		imgSrc = "/images/ps/pan/" + imgId + ".gif";
		imgObj.src = imgSrc;
		if (imgId2) {
			id = "pan" + imgId2.toUpperCase();
			imgObj = document.getElementById(id);
			imgSrc = "/images/ps/pan/" + imgId2 + ".gif";
			imgObj.src = imgSrc;
		}
	}
}

// local info combo box (partially disabled)

function getOption(kw,keypressed) {
	myselect = document.localinfo.local
	myinput = document.localinfo.kw
	if (keypressed != 8 && keypressed != 46 && keypressed != 13) {
		for(i=0; i<myselect.options.length; i++){
		 	option = myselect.options[i].text
			pattern = "^" + kw
			re = new RegExp(pattern,"i")
			if (re.test(option)) {
				myselect.options[i].selected = true
				/*myinput.value = option
				selectionStart = kw.length
				selectionEnd = option.length
				selectionText = option.substring(selectionStart,selectionEnd)
				if (myinput.setSelectionRange) 	{myinput.setSelectionRange(selectionStart,selectionEnd)}
				if (myinput.createTextRange && selectionText) {
					var r = myinput.createTextRange();
					r.findText(selectionText);
					r.select();
				}*/
				break
			} else {
				myselect.options[0].selected = true
			}
		}
	}
}

function setText(myoption) {
	document.localinfo.kw.value = document.localinfo.local.options[myoption].text
}

// change status bar for Google ads
function ss(w){window.status='Go to '+w;return true;}
function cs(){window.status='';}

function google_ad_request_done(google_ads) {
  var i;
  document.writeln('<dl>');
  for(i = 0; i < google_ads.length; ++i) {
    document.writeln('<dt><a href=' + google_ads[i].url + ' onmouseover="return ss(\'' + google_ads[i].visible_url + '\')" onmouseout="cs()">' + google_ads[i].line1 + '</a></dt>');
	document.writeln('<dd><a href=' + google_ads[i].url + ' onmouseover="return ss(\'' + google_ads[i].visible_url + '\')" onmouseout="cs()">' + google_ads[i].line2 + ' ' + google_ads[i].line3 + '</a> ');
	document.writeln('<span><a href=' + google_ads[i].url + ' onmouseover="return ss(\'' + google_ads[i].visible_url + '\')" onmouseout="cs()">' + google_ads[i].visible_url + '</a></span></dd>');
  }
  document.writeln('</dl>');
}

function google(country, lang, kw, city, local, cat, default_kw, kw_type) {	
	// basic parameters
	google_ad_client = 'ca-multimap_js';
	google_ad_output = 'js';
	google_max_num_ads = 8;
	google_language = lang;
	google_safe = 'high';
	google_adtest = 'off';
	google_encoding = 'latin1';
	google_gl = (country=="GB") ? "UK" : country;
	google_kw_type = (!kw_type) ? "exact" : kw_type;
	
	// get channel
	var ch = new Array();
	ch["rp"]="Properties_Sale";
	ch["rs"]="Rental_Properties";
	ch["accom"]="Hotels";
	ch["eat"]="Restaurants";
	ch["ents"]="Entertainment";
	ch["ind"]="Services";
	ch["www"]="WWW";
	ch["leisure"]="Leisure";
	ch["edu"]="Education";
	ch["auto"]="Motoring";
	ch["health"]="Health";
	ch["Ins"]="Insurance";
	ch["wifi"]="Wifi_Hotspots";
	
	// deal with freelinkers
	local = (local=="" && cat!="") ? cat : local;
	local = (local=="res") ? "eat" : local;
	local = (local=="ind") ? "services" : local;
	
	if (local == "-1") {
		google_ad_channel = 'freetext'
	} else {
		google_ad_channel = (!ch[local]) ? "default" : ch[local];
	}
	
	// check for pan or zoom
	if (location.href.search(/&right\.x|&up\.x|&left\.x|&down\.x|&upleft\.x|&upright\.x|&downleft\.x|&downright\.x|&in\.x|&out\.x|&multimap\.x|&zm=1/) >-1) {
		google_ad_channel = google_ad_channel + "+panzoom";
	}

	// build keywords and select category
	var liForm = document.localinfo;
	if (liForm) {
		var numlocalopts = liForm.local.options.length;
		if (kw == "") {
			if (google_ad_channel.indexOf("default") > -1) {
				cookied_kw = getCookie('default_kw');
				kw = (cookied_kw) ? cookied_kw : default_kw;
			}
			if (google_ad_channel != "freetext") {
				for (var i = 1; i < numlocalopts; i++) {
					c = liForm.local.options[i];
					if (c.value == local) {
					c.selected = true;
					kw = c.text;
					}
				}
			}
		}
	
		var tstamp = new Date();
		var dateInMs = tstamp.getTime();
		dateInMs += 31536000000;
		tstamp.setTime(dateInMs);
		setCookie('default_kw', kw, tstamp, '', 'multimap.com');
		
		google_kw = kw + ',' + city;
		if (google_kw_type == "exact" && kw != "") {
			var regexp = /\.|_/g;
			google_kw = google_kw.replace(regexp,",");
			var regexp = /\s/g;
			google_kw = google_kw.replace(regexp,",");
		} else {
			var regexp = /,/g;
			google_kw = google_kw.replace(regexp," ");
		}
		var regexp = /NULL/g;
		google_kw = google_kw.replace(regexp,"");
		// select dropdown based on freetext
		if (kw == "") {
			liForm.local.options[0].selected = true;
			google_kw_type="broad";
		} else {
			liForm.kw.value = kw;
			var numlocalopts = liForm.local.options.length;
			for (var i = 1; i < numlocalopts; i++) {
				if (liForm.local.options[i].text.toLowerCase().indexOf(kw.toLowerCase())> -1) {
					liForm.local.options[i].selected = true;
					break;
				}
			}
		}
	} else {
		if (kw != '') {
			google_kw = kw;
		} else {
			google_kw = city;
		}
	}
}

// format time for routing
function padout(number) {
	return (number < 10) ? '0' + number : number;
}

// get time for routing
function getTime() {
	r_day = new Date();
	r_now = padout(r_day.getHours()) + ":" + padout(r_day.getMinutes());
	return r_now;
}
	
// check time is valid
function IsValidTime(timeStr, timeNotValid, hourNotValid, minNotValid) {
	var timePat = /^(\d{1,2}):(\d{2})$/;
	var matchArray = timeStr.match(timePat);
	if (matchArray == null) {
		alert(timeNotValid);
		return false;
	}
	hour = matchArray[1];
	minute = matchArray[2];
	if (hour < 0 || hour > 23) {
		alert(hourNotValid);
		return false;
	}
	if (minute<0 || minute > 59) {
		alert (minNotValid);
		return false;
	}
	return true;
}

// next 4 functions copyright 'free' from quirksmode.org. required for aerial photo overlay
function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function getVertScroll() {
	var y;
	if (self.pageYOffset) // all except Explorer
	{
		y = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		y = document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		y = document.body.scrollTop;
	}
	return y;
}

function getHorizScroll() {
	var x;
	if (self.pageYOffset) // all except Explorer
	{
		x = self.pageXOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		x = document.documentElement.scrollLeft;
	}
	else if (document.body) // all other Explorers
	{
		x = document.body.scrollLeft;
	}
	return x;
}

// overlay map into aerial photo
function displayMap(pageX, pageY, clientX, clientY, width, height) {
	if (document.getElementById) {
		var overlay = document.getElementById('overDiv');
		var mapWidth = (width=='')?500:width;
		var mapHeight = (height=='')?300:height;
		var clipWidth = Math.round(mapWidth / 2);
		var clipHeight = Math.round(mapHeight / 2);
		
		var posx = 0;
		var posy = 0;
		
		if (pageX || pageY)
		{
			posx = pageX;
			posy = pageY;
		}
		else if (clientX || clientY)
		{
			posx = clientX + getHorizScroll();
			posy = clientY + getVertScroll();
		}
		
		var clipTop = posy - findPosY(overlay) - Math.round(clipHeight/2);
		var clipLeft = posx - findPosX(overlay) - Math.round(clipWidth/2);		
		var clipBot = clipTop + clipHeight;
		var clipRight = clipLeft + clipWidth;
		var defClip = "rect(" + clipTop +"px "+ clipRight +"px "+ clipBot +"px "+ clipLeft +"px)";

		overlay.style.visibility = "visible";
		overlay.style.display = "block";		
		overlay.style.clip = defClip;
	}
}

//shows/hides an id
function showhide(myId,fade) {
	if (document.getElementById) {
		state = document.getElementById(myId).style.display
		if (state == 'block') {
			if (fade) {
				fadeOut(1,myId)
			} else {
				hide(myId)
			}
		}
		else {
			if (fade) {
				document.getElementById(myId).style.opacity = 0.1;
				document.getElementById(myId).style.filter = "alpha(opacity=10)";
			}
			show(myId)
			if (fade) {
				fadeIn(0.1,myId)
			}
		}
		return false;
	}
}

//shows an id
function show(myId) {
	if (document.getElementById) {
		document.getElementById(myId).style.display = 'block'
	}
}

//hides an id
function hide(myId) {
	if (document.getElementById) {
		document.getElementById(myId).style.display = 'none'
	}
}

//show toolbar key when link clicked
function ddb() {
	if (document.getElementById && document.getElementById('keycaption')) {
		document.getElementById('keycaption').onclick = function() {
			showhide('key',true);
			return false;
		}
		document.getElementById('toggleKey').onclick = function() {
			document.getElementById('key').style.opacity = 0.1;
			document.getElementById('key').style.filter = "alpha(opacity=10)";			
			showhide('key',true);
			return false;
		}
	}
}

// fade in
function fadeIn(i,myId) {
	if (i < 1) {
		i = (i+0.000001 > 1)?0.999:i;
		document.getElementById(myId).style.opacity = i;
		MSi = i*100;
		MSi = "alpha(opacity=" + MSi + ")";
		document.getElementById(myId).style.filter = MSi;
		i = i + 0.1;
		setTimeout("fadeIn("+ i + ",'" + myId + "')", 5);
	}	
}

// fade out
function fadeOut(i,myId) {
	if (i > 0.1) {
		i = (i == 1)?0.999:i;
		document.getElementById(myId).style.opacity = i;
		MSi = i*100;
		MSi = "alpha(opacity=" + MSi + ")";
		document.getElementById(myId).style.filter = MSi;
		i = i - 0.1;
		setTimeout("fadeOut("+ i + ",'" + myId + "')", 5);
	} else {
		hide(myId)
	}
}

// Cookie API  v1.0.1
// code (mostly) by Chris Nott (chris[at]dithered[dot]com)


// Write a cookie value
function setCookie(name, value, expires, path, domain, secure) {
   var curCookie = name + '=' + escape(value) + ((expires) ? '; expires=' + expires.toGMTString() : '') + ((path) ? '; path=' + path : '') + ((domain) ? '; domain=' + domain : '') + ((secure) ? '; secure' : '');
   document.cookie = curCookie;
}


// Retrieve a named cookie value
function getCookie(name) {
   var dc = document.cookie;
   
   // find beginning of cookie value in document.cookie
   var prefix = name + "=";
   var begin = dc.indexOf("; " + prefix);
   if (begin == -1) {
      begin = dc.indexOf(prefix);
      if (begin != 0) return null;
   }
   else begin += 2;
   
   // find end of cookie value
   var end = document.cookie.indexOf(";", begin);
   if (end == -1) end = dc.length;
   
   // return cookie value
   return unescape(dc.substring(begin + prefix.length, end));
}


// Delete a named cookie value
function deleteCookie(name, path, domain) {
   var value = getCookie(name);
   if (value != null) document.cookie = name + '=' + ((path) ? '; path=' + path : '') + ((domain) ? '; domain=' + domain : '') + '; expires=Thu, 01-Jan-70 00:00:01 GMT';
   return value;
}

// Test for cookie support
function supportsCookies(rootPath) {
   setCookie('checking_for_cookie_support', 'testing123', '', (rootPath != null ? rootPath : ''));
   if (getCookie('checking_for_cookie_support')) return true;
   else return false;
}

// Job menu
function rollUp() {
	if (!document.getElementById || !document.createElement) return false;
	if (!document.getElementById('jobnav')) return false;
	var objMenu = document.getElementById('jobnav');
	var objNested = objMenu.getElementsByTagName('ul');
		for (var i=0; i<objNested.length; i++) {
			if (objNested[i].className == 'rollup') {
				objNested[i].style.display = 'none';
				objNode = objNested[i].parentNode;
	            strContent = objNode.firstChild.data;
				objAnchor = document.createElement('a');
            	objAnchor.href = '#';
            	objAnchor.onclick = function(){return rollOut(this);}
           		objAnchor.appendChild(document.createTextNode(strContent));
				objNode.replaceChild(objAnchor, objNode.firstChild);
			}
		}
	}

function rollOut(objMenuitem) {
    if (objMenuitem.nextSibling.style.display == 'block')
        objMenuitem.nextSibling.style.display = 'none';
    else
        objMenuitem.nextSibling.style.display = 'block';
    return false;
}


addLoadEvent(ddb);
addLoadEvent(rollUp);
