/*
	GMapsOverlay v1.0
	by André Fiedler  - GNU license.
*/

var GMapsOverlay = {

	init: function(options){
		this.options = Object.extend({
			resizeDuration: 200,
			resizeTransition: Fx.Transitions.sineInOut,
			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(/^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.bottomContainer = new Element('div').setProperty('id', 'gmBottomContainer').setStyle('display', 'none').injectInside(document.body);
		this.bottom = new Element('div').setProperty('id', 'gmBottom').injectInside(this.bottomContainer);
		new Element('a').setProperties({id: 'gmCloseLink', href: '#'}).injectInside(this.bottom).onclick = this.overlay.onclick = this.close.bind(this);
		this.caption = new Element('div').setProperty('id', 'gmCaption').injectInside(this.bottom);
		new Element('div').setStyle('clear', 'both').injectInside(this.bottom);
		
		this.center.style.display = 'none';
		
		var nextEffect = this.nextEffect.bind(this);
		this.fx = {
			overlay: this.overlay.effect('opacity', {duration: 200, fps:100}).hide(),
			resize: this.center.effects({duration: this.options.resizeDuration, transition: this.options.resizeTransition, onComplete: nextEffect}),
			maplayer: this.maplayer.effect('opacity', {duration: 200, onComplete: nextEffect}),
			bottom: this.bottom.effect('margin-top', {duration: 200, 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.fx.overlay.start(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' : ''; });
		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.style.display = 'none';
		this.fx.maplayer.hide();
		this.center.className = 'gmLoading';
		//this.showAddress(decodeURI(this.link.substring(this.link.indexOf('q=')+2)));
		if (this.link.indexOf('loc=') > 0) { loc = decodeURI(this.link.substring(this.link.indexOf('loc=')+4)) } else {loc = ""}
		this.showAddress(loc);
		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.bottomContainer.setStyles({top: (this.top + this.center.clientHeight)+'px', height: '0px', marginLeft: this.center.style.marginLeft, 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.style.height = '';
				this.fx.bottom.start(0);
				break;
			}
			this.bottomContainer.style.height = '';
		case 4:
			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 = this.bottomContainer.style.display = 'none';
		this.fx.overlay.chain(this.setup.pass(false, this)).start(0);
		return false;
	},
	

showAddress: function(loc){
      // arrays to hold copies of the markers and html used by the side_bar
      // because the function closure trick doesnt work there
      var gmarkers = [];
      var htmls = [];
      var i = 0;
      
      // === Create an associative array of GIcons() ===
      var gicons = [];
      gicons["center"] = new GIcon(G_DEFAULT_ICON);
      gicons["mobile"] = new GIcon(G_DEFAULT_ICON);
      gicons["3rdparty"]  = new GIcon(G_DEFAULT_ICON);

      // A function to create the marker and set up the event window
      function createMarker(point,name,html,icontype) {
        // === create a marker with the requested icon ===
        var marker = new GMarker(point, gicons[icontype]);
        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(html);
        });
        // save the info we need to use later for the side_bar
        gmarkers[i] = marker;
        htmls[i] = html;
        i++;
        return marker;
      }
    
	    // create the map
			this.map = new GMap2(this.maplayer);
			this.map.setUIToDefault();
			//this.map.addControl(new GMapTypeControl(),'G_ANCHOR_BOTTOM_RIGHT');
			//this.map.addControl(new GLargeMapControl());
			this.map.setCenter(new GLatLng( 40,-95), 3);

      // Read the data from gmap-data.xml
     	var request = GXmlHttp.create();
      request.open("GET", "gmap-data.xml", true);
      request.onreadystatechange = function() {
        if (request.readyState == 4) {
          var xmlDoc = GXml.parse(request.responseText);
          // obtain the array of markers and loop through it
          var markers = xmlDoc.documentElement.getElementsByTagName("marker");
          for (var i = 0; i < markers.length; i++) {        	
            // obtain the attribues of each marker
            var lat = parseFloat(markers[i].getAttribute("lat"));
            var lng = parseFloat(markers[i].getAttribute("lng"));
            var point = new GLatLng(lat,lng);
            if (markers[i].getAttribute("name")){ var htmlpre = "<b>"+markers[i].getAttribute("name")+"</b><br>" } else { var htmlpre = ""}
            var html = htmlpre+markers[i].getAttribute("html");
            var label = markers[i].getAttribute("label");
            // === read the icontype attribute ===
            var icontype = markers[i].getAttribute("icontype");
            // === create the marker, passing the icontype string ===
            var marker = createMarker(point,label,html,icontype);
            GMapsOverlay.map.addOverlay(marker);
						if (loc) {
							if (loc == label) {
								GMapsOverlay.map.setCenter(new GLatLng( lat,lng), 13);
								gmarkers[i].openInfoWindowHtml(htmls[i]);
								var address = markers[i].getAttribute("html");
								var address = address.replace(/<br>/g, ",");
								GMapsOverlay.caption.setHTML("<a href=\"http://maps.google.com/maps?q="+address+"\" target=\"_blank\">Go to Google Maps Site</a>");
							}
						}
          }
        }
      }
      request.send(null);
    }
};
window.addEvent('domready', GMapsOverlay.init.bind(GMapsOverlay));
