// This file is Copyright (C) 2008 Daniel James All rights reserved.
// P 1800 888 981
// E daniel@netbreeze.com.au

var gmi;
var searchpage = false;
if (/map\.php/.test(document.location.href)) {
	document.write('<link rel="stylesheet" href="style/map.css" content-type="text/css" /><script language="Javascript" type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script><script language="Javascript" type="text/javascript" src="style/map2.php"></script><div id="mapoutline"><div id="mapele"></div></div><div id="sfo" style="margin-left: 10px"><form name="searchform" action="map2.php" autocomplete="off"><div><label for="suburb">Postcode or Suburb: </label><input type="text" name="s" id="suburb" /> <input type="submit" value="FIND NOW" /></div></form></div>');
	searchpage = true;
}

function gm(lat,lng,zoom) {
	if (typeof(google) != 'undefined') {
		this.tooltipdiv = false;
		if (/#[-0-9+.]+$/.test(document.location.href)) {
			var s = document.location.href;
			s = s.substr(s.indexOf('#')+1);
			var a = s.split('+');
			this.lat = Number(a[0]);
			this.lng = Number(a[1]);
			this.zoom = Number(a[2]);
		} else {
			this.lat = lat;
			this.lng = lng;
			this.zoom = zoom;
		}
		var obj=this;
		window.onload=function() {
			obj.loaded();
		}
	}
}
gm.prototype.loaded = function() {
	var mapele = document.getElementById('mapele');
	this.layout = mapele.parentNode;

	this.map = new google.maps.Map(mapele,{
		center:new google.maps.LatLng(this.lat,this.lng),
		zoom:this.zoom,
		mapTypeId:google.maps.MapTypeId.ROADMAP
	});

	var sk = new google.maps.MarkerImage(
			"style/skmarker.png",
			new google.maps.Size(22,33),
			new google.maps.Point(0,0),
			new google.maps.Point(11,33)
	);
	var sks = new google.maps.MarkerImage(
			"style/skmarkers.png",
			new google.maps.Size(45,33),
			new google.maps.Point(0,0),
			new google.maps.Point(11,33)
	);

	var x,ct=s.length,m,obj=this;
	for (x = 0; x < ct; x ++) {
		if (typeof(s[x]) != 'number') {
			m = new google.maps.Marker({
				position:new google.maps.LatLng(s[x][0],s[x][1]),
				map:this.map,
				clickable:true,
				icon:sk,
				shadow:sks,
				title:"Storage King "+s[x][2]+(s[x][4]?"\nPhone: "+s[x][4]:'')
			});
			m.fn=s[x][3];
			google.maps.event.addListener(m, "click", function() {
				var p = obj.map.getCenter();
				document.location.href="#"+p.lat()+"+"+p.lng()+"+"+obj.map.getZoom();
				document.location.href=this.fn;
			});
		}
	}
}
gm.prototype.setCenter = function(lat,lng,zoo) {
	var loc = new google.maps.LatLng(lat,lng);
	this.map.setZoom(zoo);
	this.map.setCenter(loc);
}

function httpreq() {
	var req = false;
	if(window.XMLHttpRequest) {
		try { req = new XMLHttpRequest(); } catch(e) { req = false; }
	} else if(window.ActiveXObject) {
		try { req = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {
			try { req = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) { req = false; }
		}
	}
	return req;
}
var search;
function searchclass() {
	if (this.form = document.searchform) {
		if (searchpage) {
			var obj=this;
			this.form.onsubmit=function() {
				return obj.submithandle();
			}
		}
		if (document.getElementById) {
			this.timeout=false;
			this.input = this.form.s;
			this.input.show=false;
			this.input.popup=false;
			this.input.oldtext=this.input.value;
			this.input.onkeypress=this.updateinput;
			this.input.onkeydown=this.scrollpopup;
			this.input.onkeyup=this.checkclear;
			this.input.onblur=this.hidesearchpopup;
			this.input.updatehighlight = this.updatehighlight;
			this.input.updatepopup = this.updatepopup;
			this.initpopup();
		}
	}
}
searchclass.prototype = {
	submithandle:function() {
		var value = this.form.s.value;
		var req = httpreq();
		if (req) {
			req.onreadystatechange = function() {
				search.form.s.value='';
				search.form.s.focus();
				if (req.readyState == 4) {
					var ret = req.responseText;
					if (ret == 'false') {
						alert('Sorry, the suburb or postcode you entered was not found. Please try again');
					} else {
						var a = eval(ret);
						gmi.setCenter(a[0],a[1],a[2]);
					}
				}
			}
			req.open("GET", "map.php?f="+value, true);
			req.send(null);
			return false;
		}
		return true;
	},
	hidesearchpopup:function() {
		this.popup.style.visibility = 'hidden';
	},
	checkclear:function(e) {
		if (document.all) e = window.event;
		
		if (this.popup.style.visibility == 'visible') {
			if ((e.keyCode != 38) && (e.keyCode != 40)) {
				setTimeout("search.input.updatepopup()", 100);
			}
		}
	},
	updateinput:function(e) {
		if (document.all || e.charCode) {
			if (search.timeout) {
				clearTimeout(search.timeout);
			}
			search.timeout = setTimeout("search.input.updatepopup()", 100);
		}
	},
	lefttotal:function(ele) {
		if (ele.offsetParent) {
			return ele.offsetLeft + this.lefttotal(ele.offsetParent);
		}
		return ele.offsetLeft;
	},
	toptotal:function(ele) {
		if (ele.offsetParent) {
			return ele.offsetTop + this.toptotal(ele.offsetParent);
		}
		return ele.offsetTop;
	},
	initpopup:function() {
		this.input.popup = document.createElement('div');
		this.input.popup.style.width = (this.input.offsetWidth - 4) + 'px';
		this.input.popup.style.left = this.lefttotal(this.input) + 'px';
		this.input.popup.style.visibility = 'hidden';
		this.input.popup.className = 'inputpopup';
		this.input.popup.input = this.input;
		this.input.popup.onmousemove = this.checkmouseupdate;
		document.body.appendChild(this.input.popup);
	},
	checkmouseupdate:function(e) {
		if (document.all) e = window.event;
		var y = e.pageY?e.pageY:e.clientY + document.body.scrollTop;
		var newtest = Math.floor((y - this.offsetTop - 2) / 18) + 1;
		if ((newtest > this.input.scrollcount) || (newtest < 1)) {
			return;
		}
		if (newtest != this.input.scrollindex) {
			this.input.scrollindex = newtest;
			this.input.updatehighlight();
		}
	},
	updatehighlight:function() {
		var divs = this.popup.getElementsByTagName('div');
		if (this.oldhighlight != null) {
			this.oldhighlight.style.background = 'buttonface';
			this.oldhighlight.style.color = 'buttontext';
		}
		divs[this.scrollindex - 1].style.background='highlight';
		divs[this.scrollindex - 1].style.color='highlighttext';
		this.oldhighlight = divs[this.scrollindex - 1];
		this.value = divs[this.scrollindex - 1].innerHTML;
	},
	scrollpopup:function(e) {
		if (document.all) e = window.event;

		if (this.popup.style.visibility == 'visible') {
			if (e.keyCode == 38) {
				this.scrollindex -= 1;
				if (this.scrollindex < 1) {
					this.scrollindex = this.scrollcount;
				}
				this.updatehighlight();
			} else if (e.keyCode == 40) {
				this.scrollindex += 1;
				if (this.scrollindex > (this.scrollcount)) {
					this.scrollindex = 1;
				}
				this.updatehighlight();
			} else if (e.keyCode == 9) {
				this.popup.style.visibility = 'hidden';
			} else if ((e.keyCode == 37) || (e.keyCode == 39)) {
				return;
			} else {
				if (search.timeout) {
					clearTimeout(search.timeout);
				}
				search.timeout = setTimeout("search.input.updatepopup()", 100);
				return;
			}
		}
	},
	updatepopup:function() {
		search.timeout = false;
		if (this.value.length >= 2) {
			if (this.value == this.oldtext) {
				if (this.show) {
					this.popup.style.visibility = 'visible';
				}
			} else {
				this.oldtext = this.value;
				var req = httpreq();
				var input = this;

				if (req) {
					req.onreadystatechange = function() {
						if (req.readyState == 4) {
							var ret = req.responseText;
							if (ret == 'false') {
								input.popup.style.visibility = 'hidden';
								input.show = false;
							} else {
								input.show = true;
								var arr = eval(ret);
								var x;
								var div;
								input.scrollindex = 0;
								input.scrollcount = arr.length;
								input.popup.innerHTML='';

								for (x = 0; x < arr.length; x ++) {
									div = document.createElement('div');
									div.innerHTML = arr[x];
									input.popup.appendChild(div);
									if (div.offsetWidth > input.offsetWidth) {
										div.setAttribute("title", arr[x]);
									}
								}

								input.lasthighlight = null;
								input.popup.style.top = (search.toptotal(input) - input.popup.offsetHeight) + 'px';
								input.popup.style.visibility = 'visible';
								input.oldhighlight=null;
							}
						}
					}
					req.open("GET", "map.php?e="+this.value, true);
					req.send(null);
				}
			}
		} else {
			this.popup.style.visibility = 'hidden';
		}
	}
}

function init() {
	search = new searchclass();
}
domloaded=function(ie) {
	var f,u,d=document;
	if (ie) {
		var t=d.createElement('d:r');
		f=function() {
			try {
				t.doScroll('left');
				t=null;
				init();
			} catch(e) {
				setTimeout(f,10);
			}
		}
		f();
	} else {
		u=navigator.userAgent;
		if(/webkit|safari|khtml/i.test(u)) {
			f=function() {
				if (/loaded|complete/.test(d.readyState)) {
					init();
				} else {
					setTimeout(f,10);
				}
			}
			f();
		} else {
			if(/(?!.*compatible)^mozilla|opera/i.test(u)) {
				d.addEventListener("DOMContentLoaded",init,false);
			} else {
				window.onload=init;
			}
		}
	}
}
domloaded(/*@cc_on!@*/0);

