// This file is Copyright (C) Daniel James 2007,2008
// All rights reserved.
// Phone: 1800 888 981
// Email: info@netbreeze.com.au
//
var menu,pages;
function initanim(d,bg,w,h) {
	this.anim=$('#anim');
	this.div=$('<div class="pane"></div>').appendTo(this.anim);
	this.w=w;
	this.bg=$('<div class="bg" style="background-image:url(/anim.nb?i='+bg+'); width:'+(w+724)+'px"></div>').appendTo(this.div);
	this.resetbg();
	var x,y,ct;
	this.cars=[];
	y=0;
	for(x=0,ct=d.length;x<ct;x+=3) {
		this.cars[y]=car(this,d[x],d[x+1],d[x+2]);
		y++;
	}
	this.car=0;
	this.resetcar();
}
initanim.prototype={
resetbg:function() {
	var o=this;
	this.div.attr('scrollLeft',0);
	this.div.animate({scrollLeft:o.w},o.w*30,'linear',function() {
		o.resetbg();
	});
},
resetcar:function() {
	this.cars[this.car].play();

	this.car++;
	if(this.car>=this.cars.length) this.car = 0;
}
}

function car(p,i,d,s) {
	if (d==1) {
		return new leftcar(p,i,s);
	}
	return new rightcar(p,i,s);
}

function leftcar(p,i,s) {
	this.init(p,i,s);
}

leftcar.prototype={
init:function(p,i,s) {
	var o=this;
	this.p=p;
	this.driving=false;
	this.waiting=false;
	this.state=false;
	this.img=new Image();
	this.img.onload=function() {
		o.loaded();
	}
	this.img.src='/anim.nb?i='+i;
	this.s=6;
},
loaded:function() {
	var o=this;
	this.div=$('<div class="car"></div>').css({
		width:o.img.width,
		height:o.img.height
	});
	if (/MSIE 6/.test(navigator.userAgent)) {
		this.div.css('filter',"progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='scale', src='"+o.img.src+"')");
	} else {
		this.div.css('background-image','url('+o.img.src+')');
	}
	this.div.appendTo(this.p.anim);
	this.w=this.img.width;
	this.state=true;
	if(this.waiting) {
		this.run();
	}
},
reset:function() {
	this.div.css('left',724);
},
getfinish:function() {
	return -this.w;
},
run:function() {
	this.driving=true;
	this.reset();
	var o=this;
	this.div.animate({
		left:this.getfinish()
	},(724+this.w)*this.s,'linear',function() {
		o.driving=false;
		o.p.resetcar();
	});
},
play:function() {
	if (this.state) {
		this.run();
	} else {
		this.waiting = true;
	}
}
}

function rightcar(p,i,s) {
	this.init(p,i,s);
}
rightcar.prototype=new leftcar;
rightcar.prototype.reset=function() {
	this.div.css('left',-this.w);
}
rightcar.prototype.getfinish=function() {
	return 724;
}

function initmenu() { // Popup menu
	if (document.getElementById) {
		var menu = document.getElementById('menu');
		if (!menu) {
			return;
		}
		var toplevel = menu.childNodes[0].childNodes[0].childNodes[0].childNodes;
		var x;
		for (x = 0; x < toplevel.length; x ++) {
			var curele = toplevel[x];
			if (curele.childNodes[0].tagName == 'A') {
				this.inithover(curele);
			}
		}
		this.currentmenu = false;
		this.timer = false;
	}
}
initmenu.prototype.inithover = function(td) {
	var ele = td.childNodes[0];
	ele.img = ele;
	ele.baseclassname = ele.className;
	ele.onmouseover = this.mouseover;
	ele.onmouseout = this.mouseout;
	var abs = this.abs(td.childNodes[0]);
	if ((td.childNodes[1] != null) && (td.childNodes[1].tagName == 'UL')) {
		td.childNodes[1].style.left = (abs[1] - 1) + 'px';
		td.childNodes[1].style.top = (abs[0] + ele.offsetHeight) + 'px';
		td.childNodes[1].style.width = (ele.offsetWidth - 2) + 'px';
		td.childNodes[1].menuwidth = ele.offsetWidth - 6;
		td.childNodes[1].initted = false;
		td.childNodes[1].isvisible = false;
		td.childNodes[1].menuparent = false;
		td.childNodes[1].menubutton = ele;
		td.childNodes[1].img = ele;
	}
}
initmenu.prototype.close = function() {
	menu.closeuntil(false);
}
initmenu.prototype.closesoon = function() {
	this.timer = setTimeout(this.close,800);
}
initmenu.prototype.norush = function() {
	clearTimeout(this.timer);
}
initmenu.prototype.mouseover = function() {
	if (this.img) {
		this.img.className=this.img.baseclassname+' hover';
	}
	var ul = this.parentNode.childNodes[1];
	if ((ul != null) && (ul.tagName == 'UL')) {
		if (!ul.isvisible) {
			ul.isvisible = true;
			if (!ul.initted) {
				ul.initted = true;
				var x,y;
				for (x = 0; x < ul.childNodes.length; x ++) {
					y = ul.childNodes[x].childNodes[0];
					if (y.tagName == 'A') {
						y.onmouseover = menu.mouseover;
						y.onmouseout = menu.mouseout;
						if (navigator.userAgent.indexOf("MSIE 5") != -1) { // oldie
							y.style.width = (ul.menuwidth + 20) + 'px';
						} else {
							y.style.width = ul.menuwidth + 'px';
						}
						y.img = false;
						var li = ul.childNodes[x];
						var abs = menu.abs(li);
						if ((li.childNodes[1] != null) && (li.childNodes[1].tagName == 'UL')) {
							li.childNodes[1].style.width = '100px';
							li.childNodes[1].menuwidth = 80;
							li.childNodes[1].style.marginLeft = (ul.offsetWidth) + 'px';
							li.childNodes[1].style.marginTop = li.offsetTop + 'px';
							li.childNodes[1].initted = false;
							li.childNodes[1].isvisible = false;
							li.childNodes[1].menuparent = ul;
							li.childNodes[1].menubutton = false;
						}
					}
				}
			}
			ul.style.visibility = 'visible';
			menu.closeuntil(ul.menuparent);
			menu.currentmenu = ul;
		}
	} else {
		menu.closeuntil(this.parentNode.parentNode);
	}
	menu.norush();
}
initmenu.prototype.closeuntil = function(ele) {
	while (this.currentmenu && (this.currentmenu != ele)) {
		if (this.currentmenu.menubutton) {
			this.currentmenu.menubutton.className=this.currentmenu.menubutton.baseclassname;
		}
		this.currentmenu.style.visibility = 'hidden';
		this.currentmenu.isvisible = false;
		this.currentmenu = this.currentmenu.menuparent;
	}
}
initmenu.prototype.mouseout = function() {
	var ul = this.parentNode.childNodes[1];
	if (this.img) {
		if ((ul == null) || (ul.tagName != 'UL') || (!ul.isvisible)) {
			this.img.className=this.img.baseclassname;
		}
	}
	menu.closesoon();
}
initmenu.prototype.abs = function(n) {
	var t = 0,l = 0;
	while (n && (n.style.position != 'relative') && (n.id != 'layout')) {
		t += n.offsetTop; l += n.offsetLeft; n = n.offsetParent;
	}
	return [t,l];
}
function initpage() { // Dynamic paging
	if (document.getElementById) {
		this.content = document.getElementById('content');
		if (!this.content) {
			return;
		}
		var hr = this.content.getElementsByTagName('hr')
		if (hr.length) {
			this.pages = new Array();
			var h1 = this.content.getElementsByTagName('h1');
			if (h1[0] != null) {
				this.heading = h1[0].innerHTML;
			} else {
				this.heading = false;
			}
			this.pages = this.content.innerHTML.split(/<HR[ ]*\/?>/i);
			this.current = -1;
			this.navele = false;
			this.showpage(0);
		}
	}
}
initpage.prototype.setupnav = function() {
	if (this.navele == false) {
		this.navele = document.createElement('div');
		this.navele.id = 'nav';
		var foot = document.getElementById('footer');
		foot.appendChild(this.navele);
	}
	var nav = '';
	if (this.current > 0) {
		nav+= '<span class="left"><a href="javascript:pages.showpage('+(this.current - 1)+')"><span>◄</span> back</a></span>';
	}
	if (this.current < (this.pages.length - 1)) {
		nav+= '<span class="right"><a href="javascript:pages.showpage('+(this.current + 1)+')">next <span>►</span></a></span>';
	}
	this.navele.innerHTML = nav;
}
initpage.prototype.showpage = function(num) {
	if (this.current >= 0) {
		window.document.location.href='#';
	}
	this.current = num;
	this.content.innerHTML = (this.heading && num?'<h1>'+this.heading+'</h1><div>&nbsp;</div>':'')+this.pages[num];
	this.setupnav()
}
function initsimulator(id) {
	if (document.getElementById) {
		var obj = this;
		this.embed = id;
		this.out = document.getElementById('bighead');
		this.select = document.sim['nb[s]'];
		this.select.onchange = function() {
			var s = this.options[this.selectedIndex].value;
			var img = new Image();
			img.width='580';
			img.height='380';
			img.src = "/simulator.nb?nb%5Bop%5D=image&nb%5Bs%5D="+s+"&nb%5Bid%5D="+obj.embed;
			obj.out.replaceChild(img,obj.out.childNodes[0]);
		}
	}
}

function searchclass() {
	if (this.form = document.searchform) {
		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 obj=this;
		var value = this.form.s.value;
		if(window.gmi.httpreq===false){
			window.gmi.httpreq=window.gmi.inithttpreq();
		}else if(window.gmi.httpreq){
			window.gmi.httpreq.abort();
		}
		var req = window.gmi.httpreq;
		if (req) {
			req.onreadystatechange = function() {
				obj.form.s.value='';
				obj.form.s.focus();
				if (req.readyState == 4) {
					var ret=req.responseText;
					var a=eval(ret);
					if(a){
						if(a[0]==0){
							window.gmi.map.setCenter(new google.maps.LatLng(a[1],a[2]),a[3]);
						}if(a[0]==1){
							value=value.replace(/ +$/,'');
							window.gmi.findandshow(value,a[1],a[2]);
						}
					}else{
						alert("Sorry. I was unable to find '"+value+"'. Please try again.");
					}
				}
			}
			req.open("GET", "/billboard_locations.nb?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(function(){
					window._search.input.updatepopup();
				}, 100);
			}
		}
	},
	updateinput:function(e) {
		if (document.all || e.charCode) {
			if (window._search.timeout) {
				clearTimeout(window._search.timeout);
			}
			window._search.timeout=setTimeout(function(){
				window._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 (window._search.timeout) {
					clearTimeout(window._search.timeout);
				}
				window._search.timeout=setTimeout(function(){
					window._search.input.updatepopup();
				},100);
				return;
			}
		}
	},
	updatepopup:function() {
		window._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;
				if(window.gmi.httpreq===false){
					window.gmi.httpreq=window.gmi.inithttpreq();
				}else if(window.gmi.httpreq){
					window.gmi.httpreq.abort();
				}
				var req = window.gmi.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=(window._search.toptotal(input)-input.popup.offsetHeight)+'px';
								input.popup.style.visibility='visible';
								input.oldhighlight=null;
							}
						}
					}
					req.open("GET","/billboard_locations.nb?e="+this.value, true);
					req.send(null);
				}
			}
		} else {
			this.popup.style.visibility='hidden';
		}
	}
}

function initspanes() {
	$('#sleft,#sright,#isright').each(function() {
		var d=$(this);
		d.css('overflow','hidden');
		if (this.scrollHeight>255) {
			var up=$('<div id="'+this.id+'up" class="sout sno">▲</div>').bind('click',function() {
					if (d.attr('scrollTop')<=198) {
						$(this).addClass('sno');
					}
					$(this.down).removeClass('sno');
				if(d.attr('scrollTop')>0) {
					d.stop(true,true).animate({
						'scrollTop':(d.attr('scrollTop')>=198?d.attr('scrollTop')-198:0)
					},200);
				}
			}).bind('mouseover',function(){
				$(this).addClass('sover').removeClass('sout');
			}).bind('mouseout',function(){
				$(this).removeClass('sover').addClass('sout');
			}).appendTo('#content');
			var down=$('<div id="'+this.id+'down" class="sout">▼</div>').bind('click',function() {
				var max=d.attr('scrollHeight')-255;
				if(d.attr('scrollTop')<max) {
					if (d.attr('scrollTop')>=max-198) {
						$(this).addClass('sno');
					}
					$(this.up).removeClass('sno');
					d.stop(true,true).animate({
						'scrollTop':(d.attr('scrollTop')<=max-198?d.attr('scrollTop')+198:max)
					},200);
				}
			}).bind('mouseover',function(){
				$(this).addClass('sover').removeClass('sout');
			}).bind('mouseout',function(){
				$(this).removeClass('sover').addClass('sout');
			}).appendTo('#content');
			down.get(0).up=up.get(0);
			up.get(0).down=down.get(0);
		}
	});
}
function showcase(i){
	this.timeout=false;
	this.initted=false;
	this.start(i);
}
showcase.prototype={
step:function(){
	var obj=this;
	this.image++;
	if(this.image>=this.images.length){
		this.image=0;
	}
	this.next=this.frame;
	this.frame=1-this.frame;
	this.frames[this.frame].css('z-index',33);
	this.frames[this.next].css('z-index',32);
	this.frames[this.frame].empty().append(this.images[this.image]);
	this.frames[this.frame].fadeIn(2000,function() { obj.frames[obj.next].hide(); });
	this.timeout=setTimeout(function(){ obj.step(); },5000);
},
stop:function(){
	if(this.timeout) {
		clearTimeout(this.timeout);
		this.timeout=false;
		this.frames[this.next].hide();
		this.frames[this.frame].hide();
	}
},
start:function(i){
	var x,ct,obj=this;
	this.i=i;
	if (!this.initted) {
		this.initted=true;
		this.frames=[];
		this.frames[0]=$('<div/>');
		this.frames[1]=$('<div/>');
		var l=$('#showcase');
		$.each(this.frames,function(j) {
			this.addClass('flash');
			this.appendTo(l);
		});
	}
	this.images=[];
	for(x=0,ct=i.length;x<ct;x++){
		this.images[x]=new Image();
		this.images[x].src='/showcase.nb?nb%5Bop%5D=image&nb%5Bsize%5D=2&nb%5Bid%5D='+i[x];
	}
	this.image=0;
	this.frame=1;
	this.next=0;
	this.frames[1].css('z-index',33);
	this.frames[0].css('z-index',32);
	this.frames[1].empty().append(this.images[0]);
	this.frames[1].show();
	this.frames[0].hide();
	if(this.images.length>1) {
		this.timeout=setTimeout(function(){ obj.step(); },3000);
	}
}
}
function fixobjects() {
	if (document.getElementById) {
		if (navigator.userAgent.indexOf("MSIE") != -1) { // ieonly
			var obj = document.getElementsByTagName('object');
			var x;
			for (x = 0; x < obj.length; x ++) {
				obj[x].outerHTML = obj[x].outerHTML;
			}
		}
	}
}
fixobjects();
