var Helper = {
	preInit : function(){
		document.write = this.write;
		Maps.preInit();
	},
	init : function(){
		this.hiddenSection = document.getElementById('hidden-section').getElementsByTagName('div')[0];
		if(this.hiddenSection){
			this.writeContainer = this.hiddenSection.appendChild(document.createElement('div'));
		}
		if(Maps.available()){Maps.init();}
	},
	getElement : function(e){
		var t;
		if (!e){e = window.event;}
		if (e.target){t = e.target;}
		else if (e.srcElement){t = e.srcElement;}
		if (t.nodeType != 1){t = t.parentNode;}
		return t;
	},
	XmlHttp : [
		function () {return new XMLHttpRequest();},
		function () {return new ActiveXObject('Msxml2.XMLHTTP');},
		function () {return new ActiveXObject('Msxml3.XMLHTTP');},
		function () {return new ActiveXObject('Microsoft.XMLHTTP');}
	],
	sendRequest : function (url, callback, getData, postData, req){
		if(!req){
			req = Helper.createXmlHttpObject();
			if(!req){return;}
		}
		var getString = '';
		var postString;
		if(getData){
			for(var name in getData){
				getString += encodeURIComponent(name.toString()) + '=' + encodeURIComponent(getData[name].toString()) + '&';
			}
			var index = url.indexOf('?');
			if(index < 0){url += '?' + getString;}
			if(index > -1){
				if(index == url.length - 1){url += getString;}
				else{url += '&' + getString;}
			}
		}
		if(postData){
			postString = '';
			for(var name in postData){
				postString += encodeURIComponent(name.toString()) + '=' + encodeURIComponent(postData[name].toString()) + '&';
			}
		}
		var method = (postData) ? 'POST' : 'GET';
		req.open(method, url, true);
		if(postData){req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');}
		req.onreadystatechange = function () {
			if(req.readyState != 4){return;}
			if(req.status != 200 && req.status != 304){return;}
			callback(req);
		};
		if(req.readyState == 4){return;}
		req.send(postString);
	},
	createXmlHttpObject : function(){
		var xmlhttp = false;
		for(var i = 0; i < Helper.XmlHttp.length; i++){
			try{ xmlhttp = Helper.XmlHttp[i](); }
			catch(e){ continue; }
			break;
		}
		return xmlhttp;
	},
	createCookie : function(name,value,days,hours,minutes,seconds){
		var expires;
		if(!days){days = 0;}
		if(!hours){hours = 0;}
		if(!minutes){minutes = 0;}
		if(!seconds){seconds = 0;}
		if (days || hours || minutes || seconds) {
			var date = new Date();
			date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000) + (hours * 60 * 60 * 1000) + (minutes * 60 * 1000) + (seconds * 1000));
			expires = '; expires=' + date.toGMTString();
		}
		else{expires = '';}
		document.cookie = name + '=' + value + expires + '; path=/';
	},
	readCookie : function(name){
		var nameEQ = name + '=';
		var ca = document.cookie.split(';');
		for(var i = 0; i < ca.length; i++) {
			var c = ca[i];
			while (c.charAt(0) === ' '){c = c.substring(1,c.length);}
			if (c.indexOf(nameEQ) === 0){return c.substring(nameEQ.length,c.length);}
		}
		return null;
	},
	eraseCookie : function(name){
		Helper.createCookie(name, '', -1);
	},
	addLoad : function(func){
		var oldload = window.onload;
		window.onload = function(){
			if (oldload){oldload();}
			func();
		};
	},
	addUnload : function(func){
		var oldunload = window.onunload;
		window.onunload = function(){
			if (oldunload){oldunload();}
			func();
		};
	},
	addScript : function(options){
		var script = document.createElement('script');
		script.type = 'text/javascript';
		script.async = !options.callback && !options.scriptText;
		if(options.scriptText){
			script.text = options.scriptText;
		} else {
			script.src = options.location;
		}
		var scripts = document.getElementsByTagName('script');
		var prevScript = scripts[scripts.length - 1];
		prevScript.parentNode.insertBefore(script, prevScript);
		if(options.callback){
			Helper.callbacks = Helper.callbacks || [];
			Helper.callbacks.push(options);
			Helper.waitSyncScript();
		}
	},
	waitSyncScript : function(){
		for(var i = Helper.callbacks.length - 1; i > -1; i--){
			if(Helper.callbacks[i].callback()){
				Helper.callbacks.splice(i, 1);
				i = Helper.callbacks.length - 1;
			}
		}
		if(Helper.callbacks.length > 0){
			setTimeout(Helper.waitSyncScript, 50);
		}
	},
	write : function(text){
		Helper.writeContent = Helper.writeContent || '';
		Helper.writeContent += text;
		if(Helper.writeContainer){
			Helper.writeContainer.innerHTML += Helper.writeContent;
			Helper.writeContent = '';
			Helper.writeContainer = Helper.hiddenSection.appendChild(document.createElement('div'));
		}
	},
	scrollToElement : function(elm){
		var x = 0;
		var y = 0;
		while(elm !== null){
			x += elm.offsetLeft;
			y += elm.offsetTop;
			elm = elm.offsetParent;
		}
		window.scrollTo(x, y);
	},
	getClassRegex : function(className){
		var regex = new RegExp('(^| )' + className + '( |$)');
		return regex;
	},
	hasClass : function(elm, className){
		return (Helper.getClassRegex(className).exec(elm.className) != null);
	},
	toggleClass : function(elm, className){
		if(Helper.getClassRegex(className).exec(elm.className) != null){
			Helper.removeClass(elm, className);
		} else {
			Helper.addClass(elm, className);
		}
	},
	removeClass : function(elm, className){
		var regex = Helper.getClassRegex(className);
		var match = regex.exec(elm.className);
		if(match != null){
			elm.className = elm.className.replace(regex, (match.length > className.length + 1) ? ' ' : '');
		}
	},
	addClass : function(elm, className){
		var regex = Helper.getClassRegex(className);
		var match = regex.exec(elm.className);
		if(match == null){
			elm.className += ' ' + className;
		}
	}
};

var Maps = {
	available : function(){
		if(document.getElementById('map-0')){return true;}
		else{return false;}
	},
	preInit : function(){
		this.mapOverlays = {};
	},
	init : function(){
		var url = 'http://maps.google.com/maps/api/js?sensor=false&callback=Maps.loaded&language=en';
		var siteLang = document.getElementsByTagName('html')[0].lang;
		Helper.addScript({location : url});
	},
	loaded : function(){
		this.geocoder = new google.maps.Geocoder();
		this.maps = [];
		this.mapElements = [];
		var position = 0;
		var mapElement;
		while(mapElement = document.getElementById('map-' + position)){
			this.maps[position] = new Map(mapElement, position);
			this.maps[position].activate();
			position++;
		}
	}
};
var Map = function(element, position){
	this.map = element;
	this.position = position;
	this.cyclePosition = 0;
	this.staticMap = this.map.getElementsByTagName('img')[0];
	this.address = this.map.getElementsByTagName('address')[0].innerHTML;
	this.timeout = 2000;
	this.resetTimeout = 5000;
	this.setLocation = function(results, status){
		if (status == google.maps.GeocoderStatus.OK && results.length > 0) {
			var mapOptions = {
				zoom: 3,
				center: results[0].geometry.location,
				mapTypeId: google.maps.MapTypeId.ROADMAP
			};
			this.staticMap.style.display = 'none';
			this.dynamicMap = new google.maps.Map(this.map, mapOptions);
			var markerOptions = {
				map: this.dynamicMap,
				position: results[0].geometry.location
			};
			/*var marker = new google.maps.Marker(markerOptions);*/
			if (Maps.mapOverlays['map-' + position]) {
				this.overlays = [];
				var count = Maps.mapOverlays['map-' + position].length;
				for (var i = count - 1; i > -1; i--) {
					this.overlays[i] = new google.maps.KmlLayer(window.location.href.split('?')[0].replace('index.php', '') + Maps.mapOverlays['map-' + position][i]);
					this.overlays[i].setMap(this.dynamicMap);
					if(count > 1 && i != this.cyclePosition) {
						this.overlays[i].setMap(null);
					}
				}
				if(count > 1) window.setTimeout("Maps.maps[" + this.position + "].cycle()", this.timeout);
			}
		}
	};
	this.activate = function(){
		var geocodeOptions = {
			'address' : this.address
		};
		var position = this.position;
		Maps.geocoder.geocode(geocodeOptions, function(results, status){
			Maps.maps[position].setLocation(results, status);
		});
	};
	this.cycle = function(){
		if(!this.dynamicMap) return;
		var size = this.overlays.length;
		try{ this.overlays[this.cyclePosition].setMap(null); } catch(err) {
			var test="hi";
		};
		this.cyclePosition = (this.cyclePosition + 1 < size) ? this.cyclePosition + 1 : 0;
		try{ this.overlays[this.cyclePosition].setMap(this.dynamicMap); } catch(err) {
			var test="hi";
		};
		window.setTimeout("Maps.maps[" + this.position + "].cycle()", (this.cyclePosition == size - 1) ? this.resetTimeout : this.timeout);
	};
};
Helper.preInit();

