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

var searchpage = false;
if (/map\.php/.test(document.location.href)) {
	document.write('<script language="Javascript" type="text/javascript" src="http://www.google.com/jsapi?key=ABQIAAAABXjDmcG2M5Zenej8CV4OHhSAx8ZB80p9CJORS-8ZpCFiGwB8vRTiqwPRv2HMBNwJ_LCJRp0uV4lWYQ"></script>');
	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;
		google.load("maps", "2");
		google.setOnLoadCallback(function() {
			obj.loaded();
		});
	}
}
gm.prototype.loaded = function() {
	var mapele = document.getElementById('mapele');
	this.layout = mapele.parentNode;
	this.map = new google.maps.Map2(mapele,{logoPassive:true});

	mapele.onmousewheel = function(e) {
		if (!e) e = window.event;
		if (e.preventDefault) e.preventDefault();
		e.returnValue = false;
		return false;
	}
	google.maps.Event.addDomListener(mapele, "DOMMouseScroll", mapele.onmousewheel);

	var bm = new google.maps.Icon(G_DEFAULT_ICON);
	bm.image="bmarker.png";

	var fm = new google.maps.Icon(G_DEFAULT_ICON);
	fm.image="fmarker.png";

	var largeright = new google.maps.LargeMapControl();
	largeright.getDefaultPosition = function() {
		return new google.maps.ControlPosition(G_ANCHOR_TOP_RIGHT, new google.maps.Size(5, 50));
	}

	this.map.addControl(largeright);
	this.map.addControl(new google.maps.MapTypeControl());

	this.map.setCenter(new google.maps.LatLng(this.lat,this.lng), this.zoom, G_NORMAL_MAP);

	this.map.addMapType(G_PHYSICAL_MAP);
	this.map.enableContinuousZoom();
	this.map.enableScrollWheelZoom();

	var man = new google.maps.MarkerManager(this.map,{borderPadding:100});

	var markers=new Array(),x,ct=s.length,m;
	for (x = 0; x < ct; x ++) {
		if (typeof(s[x]) == 'number') {
			man.addMarkers(markers,s[x]);
			markers = new Array();
		} else {
			if(typeof(s[x][4])!='undefined'){
				m=new google.maps.Marker(new google.maps.LatLng(s[x][0],s[x][1]),{clickable:true,icon:fm});
			}else{
				m=new google.maps.Marker(new google.maps.LatLng(s[x][0],s[x][1]),{clickable:true,icon:bm});
			}
			m.fn=s[x][3];
			if(typeof(s[x][4])!='undefined'){
				m.nb_tooltip = "<span></span>"+s[x][2]+"<br/>This pub has "+(s[x][5]==1?'a special offer':s[x][5]+'special offers')+" available!";
				m.nb_bub = "<b>"+s[x][2]+"</b><br/>This pub has <a href=\"javascript:p=gmi.map.getCenter(); document.location.href=&quot;#&quot;+p.lat()+&quot;+&quot;+p.lng()+&quot;+&quot;+gmi.map.getZoom(); document.location.href='/freebies.php?pub="+s[x][4]+"'\">"+(s[x][5]==1?'a special offer':s[x][5]+'special offers')+"</a> available!<br/><a href=\"javascript:p=gmi.map.getCenter(); document.location.href=&quot;#&quot;+p.lat()+&quot;+&quot;+p.lng()+&quot;+&quot;+gmi.map.getZoom(); document.location.href='"+s[x][3]+"'\">Click here to visit their website.</a>";
				google.maps.Event.addListener(m, "click", function() {
					this.openInfoWindow(this.nb_bub,{maxWidth:200,maxHeight:50});
				});
			}else{
				m.nb_tooltip = s[x][2];
				google.maps.Event.addListener(m, "click", function() {
					var p = gmi.map.getCenter();
					document.location.href="#"+p.lat()+"+"+p.lng()+"+"+gmi.map.getZoom();
					document.location.href=this.fn;
				});
			}
			google.maps.Event.addListener(m, "mouseover", this.showtooltip);
			google.maps.Event.addListener(m, "mouseout", this.hidetooltip);
			markers[markers.length] = m;
		}
	}
	ct = g.length;
	for (x = 0; x < ct; x ++) {
		if (typeof(g[x]) == 'number') {
			man.addMarkers(markers,g[x],g[x+1]);
			x++;
			markers = new Array();
		} else {
			m = new GMarker(new google.maps.LatLng(g[x][0],g[x][1]), {clickable:true,icon:G_DEFAULT_ICON});
			m.nb_tooltip = g[x][2]+" pubs at this location";
			m.bindInfoWindowHtml("<div>There are "+g[x][2]+" pubs at this location. Please zoom in closer to view these pubs. You can zoom in by clicking on the + sign at right or rolling your mouse wheel.</div>",{maxWidth:200});
			google.maps.Event.addListener(m, "mouseover", this.showtooltip);
			google.maps.Event.addListener(m, "mouseout", this.hidetooltip);
			markers[markers.length] = m;
		}
	}
	man.refresh();
}
gm.prototype.showtooltip = function() {
	var obj=gmi;
	var point = obj.map.fromLatLngToContainerPixel(this.getLatLng());
	if (!obj.tooltipdiv) {
		obj.tooltipdiv = document.createElement('div');
		obj.tooltipdiv.className = 'tooltipdiv';
		obj.layout.appendChild(obj.tooltipdiv);
		obj.tooltipreset = true;
		obj.tooltipresettimer = false;
	}
	obj.tooltipdiv.x = point.x;
	obj.tooltipdiv.style.left = '0';
	obj.tooltipdiv.style.top = (point.y+5)+'px';
	if (obj.tooltiptimer) {
		clearTimeout(obj.tooltiptimer);
	}
	obj.tooltipdiv.innerHTML = this.nb_tooltip;
	if (obj.tooltipreset) {
		obj.tooltiptimer = setTimeout(function() {
			obj.tooltiptimer = false;
			obj.tooltiptimeout();
		}, 500)
	} else {
		if (obj.tooltipresettimer) {
			clearTimeout(obj.tooltipresettimer);
			obj.tooltipresettimer = false;
		}
		obj.tooltiptimer = setTimeout(function() {
			obj.tooltiptimer = false;
			obj.tooltiptimeout();
		}, 30)
	}
}
gm.prototype.tooltiptimeout = function(obj) {
	this.tooltipdiv.style.left = (this.tooltipdiv.x - Math.floor(this.tooltipdiv.offsetWidth / 2)) + 'px';
	this.tooltipdiv.style.visibility="visible";
	this.tooltipreset=false;
}
gm.prototype.hidetooltip = function() {
	var obj=gmi;
	if (obj.tooltiptimer) {
		clearTimeout(obj.tooltiptimer);
		obj.tooltiptimer = false;
	}
	if (obj.tooltipresettimer) {
		clearTimeout(obj.tooltipresettimer);
	}
	obj.tooltipresettimer = setTimeout(function() {
		obj.tooltipresettimeout();
	}, 100);
	obj.tooltipdiv.style.visibility="hidden";
}
gm.prototype.tooltipresettimeout = function() {
	this.tooltipresettimer = false;
	this.tooltipreset = true;
}

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() {
		return false;
	},
	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.parentNode.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.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);
