// 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/map5.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="http://storageking.com.au/style/nzmap2.php"></script><div id="mapoutline"><div id="mapele"></div></div><div id="maplinks"></div><p><!-- spacer --></p><div id="sfo" style="margin-left: 10px"><form name="searchform" action="nzmap.php" autocomplete="off"><div><label for="suburb">Suburb: </label><input type="text" name="s" id="suburb" /></label> <input type="submit" value="Search" /></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.linksele = document.getElementById('maplinks');

	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 msk = new google.maps.MarkerImage(
			"style/mskmarker.png",
			new google.maps.Size(22,33),
			new google.maps.Point(0,0),
			new google.maps.Point(11,33)
	);
	var msks = new google.maps.MarkerImage(
			"style/mskmarkers.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;

	var cz=1;
	this.m=[];
	this.ml=[];
	var y = 0;
	for (x = 0; x < ct; x ++) {
		if (typeof(s[x]) != 'number') {
			var pos=new google.maps.LatLng(s[x][0],s[x][1])
			this.m[y] = new google.maps.Marker({
				position:pos,
				map:this.map,
				clickable:true,
				icon:sk,
				shadow:sks,
				title:"Storage King "+s[x][2]+(s[x][4]?" - Phone: "+s[x][4]:'')
			});
			this.m[y].fn=s[x][3];
			this.m[y].start=cz;
			this.m[y].end=20;
			google.maps.event.addListener(this.m[y], "click", function() {
				var p = obj.map.getCenter();
				document.location.href="#"+p.lat()+"+"+p.lng()+"+"+obj.map.getZoom();
				document.location.href=this.fn;
			});
			this.ml[y] = [pos,s[x][2],s[x][3],s[x][4],s[x][5]];
			y++;
		} else {
			cz = s[x];
		}
	}
	ct=g.length;
	for (x = 0; x < ct; x ++) {
		if (typeof(g[x]) != 'number') {
			this.m[y] = new google.maps.Marker({
				position:new google.maps.LatLng(g[x][0],g[x][1]),
				map:this.map,
				clickable:true,
				icon:msk,
				shadow:msks,
				title:g[x][2]+" stores at this location"
			});
			this.m[y].start=cs;
			this.m[y].end=ce;
			google.maps.event.addListener(this.m[y], "click", function() {
				obj.map.setCenter(this.getPosition());
				obj.map.setZoom(this.end+1);
			});
			y++;
		} else {
			cs = g[x];
			x++;
			ce = g[x];
		}
	}
	this.updateLinks();
	google.maps.event.addListener(this.map, "bounds_changed", function() {
		obj.updateLinks();
	});
	this.updateMarkers();
	google.maps.event.addListener(this.map, "zoom_changed", function() {
		obj.updateMarkers();
	});
}
gm.prototype.setCenter = function(lat,lng,zoo) {
	var loc = new google.maps.LatLng(lat,lng);
	this.map.setZoom(zoo);
	this.map.setCenter(loc);
}
gm.prototype.updateMarkers=function() {
	var cz = this.map.getZoom();
	for(x=0,ct=this.m.length;x<ct;x++) {
		this.m[x].setVisible(this.m[x].start<=cz && this.m[x].end>=cz);
	}
}
gm.prototype.updateLinks=function() {
	var ct,x,b = this.map.getBounds();
	if(typeof(b) == 'undefined') {
		return;
	}
	var n=0,out='',outa=[];
	for(x=0,ct=this.ml.length;x<ct && n < 7;x++) {
		if (b.contains(this.ml[x][0])) {
			outa[n]=this.ml[x];
			n++;
		}
	}
	outa.sort(function(a,b) {
		var ta=a[1].toLowerCase(),tb=b[1].toLowerCase();
		if (ta<tb) {
			return -1;
		}
		if (ta == tb) {
			return 0;
		}
		return 1;
	});
	for (x=0;x < n; x++) {
		out+='<li><a href="'+outa[x][2]+'">'+outa[x][1]+'</a><br/>'+(outa[x][3]?'<b>Phone:</b> '+outa[x][3]+'<br/>':'')+outa[x][4]+'</li>';
	}
	if (n > 0) {
		out = '<ul>'+out+'</ul>';
	}
	if (n == 7) {
		out = '<p><!-- spacer --></p><div>Zoom in to your area for store information</div>';
	}
	this.linksele.innerHTML=out;
}

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", "nzmap.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.documentElement?document.documentElement.scrollTop: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", "nzmap.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);

