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

var GMapsOverlay = {

	init: function(options){
		this.options = Object.extend({
			resizeDuration: 100,
			resizeTransition: Fx.Transitions.sineInOut,
			width:600,
			height:400
		}, options || {});
		
		if(!GBrowserIsCompatible()) return false;
		
		this.geocoder = new GClientGeocoder();

		this.anchors = [];
		$each(document.links, function(el){
			if (el.rel && el.rel.test(/^gmapsoverlay/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());
		
		new Element('a').setProperties({id: 'gmCloseLink', href: '#'}).injectInside(this.center).onclick = this.overlay.onclick = this.close.bind(this);
		
		this.center.style.display = 'none';
		
		var nextEffect = this.nextEffect.bind(this);
		this.fx = {
			overlay: this.overlay.effect('opacity', {duration: 400, fps:10}).hide(),
			resize: this.center.effects({duration:this.options.resizeDuration, transition:this.options.resizeTransition, onComplete: nextEffect}),
			maplayer: this.maplayer.effect('opacity', {duration: 30, onComplete: nextEffect})
		};
		
	},

	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: ''});
		this.overlay.style.display = 'block';
		this.fx.overlay.start(0.6);
		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;
		elements.each(function(el){ el.style.visibility = open ? 'hidden' : 'visible'; el.style.display = open ? 'none' : 'block'; });
		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.fx.maplayer.hide();
		this.showAddress(decodeURI(this.link.substring(this.link.indexOf('q=')+2)));
		this.nextEffect();
		return false;
	},

	nextEffect: function(){
		switch (this.step++){
		case 1:
			this.center.className = '';

			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.fx.maplayer.start(1);
			break;
		case 3:
			this.step = 0;
		}
	},

	close: function(){
		if (this.step < 0) return;
		this.step = -1;
		for (var f in this.fx) this.fx[f].stop();
		this.center.style.display  = 'none';
		this.fx.overlay.chain(this.setup.pass(false, this)).start(0);
		return false;
	},
	
	showAddress: function(address){
		this.geocoder.getLatLng(
		address,
		function(point){
			if(point){
				this.map.setCenter(point, 15);
				var marker = new GMarker(point);
				this.map.addOverlay(marker);
				marker.openInfoWindowHtml("<h2 style=color:#313479>Sydney Flying Squadron</h2><br>" + address);
			}
			else
			{
				this.close();
				this.overlay.style.display = 'none';
				alert('Sorry this map is unavailable');
			}
		}.bind(this));
	}
};
window.addEvent('domready', GMapsOverlay.init.bind(GMapsOverlay));