/*
	GMapsOverlay v1.0
	by André Fiedler (http://www.visualdrugs.net) - GNU license.
*/

var GMapsOverlay = {

	init: function(options) {
		
		this.options = Object.extend({
			resizeDuration: 400,
			resizeTransition: Fx.Transitions.Sine.easeInOut,
			width: 250,
			height: 250,
			animateCaption: true
		}, options || {});
		
		if(!GBrowserIsCompatible()) return false;
		
		this.geocoder = new GClientGeocoder();

		this.anchors = [];
		$each(document.links, function(el){
			if (el.rel && el.rel.test(/^gmap/i)){
				el.onclick = this.click.pass(el, this);
				this.anchors.push(el);
			}
		}, this);
		this.eventKeyDown = this.keyboardListener.bindAsEventListener(this);
		this.eventPosition = this.position.bind(this);

		this.overlay = new Element('div').setProperty('id', 'gmOverlay').injectInside(document.body);

		this.center = new Element('div').setProperty('id', 'gmCenter').setStyles({ 
			 width: this.options.width+'px', 
			 height: this.options.height+'px', 
			 marginLeft: '-'+(this.options.width/2)+'px'
		 }).injectInside(document.body);
		this.maplayer = new Element('div').setProperty('id', 'gmMap').injectInside(this.center);
		
		this.map = new GMap2(this.maplayer);
		this.map.addControl(new GLargeMapControl());
		this.map.addControl(new GMapTypeControl());
		
		this.bottomContainer = new Element('div').setProperty('id', 'gmBottomContainer').setStyle('display', 'none').injectInside(document.body);
		this.bottom = new Element('div').setProperty('id', 'gmBottom').injectInside(this.bottomContainer);
		this.closelink = new Element('a').setProperties({id: 'gmCloseLink', href: '#'}).injectInside(this.bottom);
		this.caption = new Element('div').setProperty('id', 'gmCaption').injectInside(this.bottom);
		new Element('div').setStyle('clear', 'both').injectInside(this.bottom);
		
		this.closelink.onclick = this.overlay.onclick = this.close.bind(this);
		
		this.center.setStyle('display','none');
		
		var nextEffect = this.nextEffect.bind(this);
		this.fx = {
			overlay: new Fx.Tween(this.overlay, { property: 'opacity', duration: 500}).set(0),
			resize: new Fx.Morph(this.center, $extend({duration: this.options.resizeDuration, onComplete: nextEffect}, this.options.resizeTransition ? {transition: this.options.resizeTransition} : {})),
			maplayer: new Fx.Tween(this.maplayer, { property: 'opacity', duration: 500, onComplete: nextEffect}).set(0),
			bottom: new Fx.Tween(this.bottom, { property: 'margin-top', duration: 400, onComplete: nextEffect}).set(0)
		};
		
	},

	click: function(link){
		return this.show(link.href);
	},

	show: function(link){
		this.link = link;
		this.position();
		this.setup(true);
		this.top = window.getScrollTop() + (window.getHeight() / 15);
		this.center.setStyles({ 'top': this.top+'px', 'display' : 'auto'});
		this.fx.overlay.start(0,0.8);
		return this.changeLink();
	},

	position: function(){
		this.overlay.setStyles({top: window.getScrollTop()+'px', height: window.getHeight()+'px'});
	},

	setup: function(open){
		var elements = $A(document.getElementsByTagName('object'));
		if (window.ie) elements.extend(document.getElementsByTagName('select'));
		elements.each(function(el){ el.style.visibility = open ? 'hidden' : 'visible'; });
		var fn = open ? 'addEvent' : 'removeEvent';
		window[fn]('scroll', this.eventPosition)[fn]('resize', this.eventPosition);
		document[fn]('keydown', this.eventKeyDown);
		this.step = 0;
	},

	keyboardListener: function(event){
		this.close();
	},

	changeLink: function(){
		this.step = 1;
		this.bottomContainer.setStyle('display','none');
		this.center.addClass('gmLoading');
		this.showAddress(decodeURI(this.link.substring(this.link.indexOf('ie=')+2)));
		this.nextEffect();
		return false;
	},

	nextEffect: function(){
		switch (this.step++){
		case 1:
			this.center.removeClass('gmLoading');
			this.center.setStyle('display','');
			this.caption.set("html","<a href=\""+this.link+"\" target=\"_blank\">View on Google Maps</a>");
			if (this.center.clientHeight != this.maplayer.offsetHeight){
				this.fx.resize.start({height: this.maplayer.offsetHeight, width: this.maplayer.offsetWidth, marginLeft: -this.maplayer.offsetWidth/2});
				break;
			}
			this.step++;
		case 2:
			this.bottomContainer.setStyles({top: (this.top + this.center.clientHeight)+'px', height: '0px', marginLeft: this.center.getStyle("margin-left"), width: this.center.clientWidth+'px', display: ''});
			this.fx.maplayer.start(1);
			
			break;
		case 3:
			if (this.options.animateCaption){
				this.fx.bottom.set(-this.bottom.offsetHeight);
				this.bottomContainer.setStyle("height",'');
				this.fx.bottom.start(0);
				break;
			}
			this.bottomContainer.setStyle("height",'');
		default:
			this.step = 0;
			break;
		}
	},

	close: function() {
		if (this.step < 0) return;
		this.step = -1;
		for (var f in this.fx) this.fx[f].cancel();
		this.center.setStyle("display",'none');
		this.bottomContainer.setStyle("display",'none')
		this.fx.overlay.chain(this.setup.pass(false, this)).start(0);
		this.fx.maplayer.start(0);
		this.fx.resize.start({width: this.options.width+'px', height: this.options.height+'px', marginLeft: '-'+(this.options.width/2)+'px' });
		return false;
	},
	
	showAddress: function(address){
		var params = this.parseGmapLink("http://www.google.co.uk/?ie"+address);
		var point = new GLatLng(params["lat"],params["long"]);
		this.map.setCenter(point, 13,G_NORMAL_MAP);
		this.gm_addMarker(point);
	},
	
	gm_addMarker: function(point){
		var gm_marker = new GMarker(point);
		this.map.addOverlay(gm_marker);
	},
	
	parseGmapLink: function (url){
		var parts = url.split("?");
		if(parts.length<2)return new Array();
		var params = parts[1].split("&");
		var gMapParams=new Array();
		for(i = 0 ; i < params.length ; i++){
			keyValue=params[i].split("=");
			if(keyValue[0]!="ll"){
				gMapParams[keyValue[0]]=keyValue[1];
			}
			else{
				var latlong=keyValue[1].split(",");
				gMapParams["lat"]=latlong[0];
				gMapParams["long"]=latlong[1];
			}
		}
		return gMapParams;
	}
	
};
window.addEvent('domready', GMapsOverlay.init.bind(GMapsOverlay));