/*
	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.sineInOut,
			width: 250,
			height: 250,
			animateCaption: true
		}, options || {});
		 var map;
		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());
		
		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: 500, fps:100}).hide(),
			resize: this.center.effects({duration: this.options.resizeDuration, transition: this.options.resizeTransition, onComplete: nextEffect}),
			maplayer: this.maplayer.effect('opacity', {duration: 500, onComplete: nextEffect}),
			bottom: this.bottom.effect('margin-top', {duration: 400, 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';
		var company=decodeURI(this.link.substring(this.link.indexOf('cname=')+6));		
		var mySplitResult = company.split("&");

		var logo=decodeURI(this.link.substring(this.link.indexOf('logo=')+5));		
		var mySplitResult1 = logo.split("&");

		var web=decodeURI(this.link.substring(this.link.indexOf('web=')+4));		
		var mySplitResult2 = web.split("&");
		

		var p1=decodeURI(this.link.substring(this.link.indexOf('q=')+2));		

		this.showAddress(mySplitResult[0],p1,mySplitResult1[0],mySplitResult2[0]);
		this.nextEffect();
		return false;
	},

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

			this.caption.setHTML("<a href=\""+this.link+"\" target=\"_blank\"> Community Pioneers Google-Map </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.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);
		this.map.clearOverlays();
	},
	
	showAddress: function(comname,address,logo,web){
		this.geocoder.getLatLng(
		address,
		function(point){
			if(point){
				//map = new GMap2(document.getElementById("map"));
				var newmap=this.map;
				this.map.setCenter(point, 15);				
				var marker = new GMarker(point);
				if(web==" ")
				{
					webadd="Not Available";
				}
				else
				{
					//webadd=web;
					webadd='To know more visit us at <a href="http://'+web+'" style="color:#606060" target="_blank">'+web+'</a>';
				}
				
				if(comname=="Amsterdam News")
				{var discount="* 10% discount on all purchases.";}
				else if(comname=="City Electric and Communications")
				{var discount="* 10% discount on all purchases.";}
				else if(comname=="E-Harlem")
				{var discount="* 10% discount on any Ads place with E-Harlem.";}
				else if(comname=="Katrina Florist")
				{var discount="* 10% discount on all purchases.";}
				else if(comname=="Faison Fire House")
				{var discount="* 10% discount on ticket prices of any event.";}
				else if(comname=="Two Fifth Lounge")
				{var discount="* 10% discount on all purchases.";}
				else if(comname=="Settepani")
				{var discount="* 10% discount on all purchases.";}
				else if(comname=="Mobay")
				{var discount="* 10% discount on the bill.";}
				else if(comname=="Hue-Man Bookstore and Cafe")
				{var discount="* 10% discount on all purchases.";}
				else if(comname=="Nector")
				{var discount="* 10% discount on all purchases.";}
				else if(comname=="Harlem Vintage")
				{var discount="* 10% discount on all purchases.";}
				else if(comname=="The Brownstone")
				{var discount="* Free tote bag on purchases over $500<BR>*  Free make-over on purchase of Vera Moore's cosmetics.";}
				else if(comname=="Apollo")
				{var discount="* 10% discount on all Apollo Logo Merchandise.";}
				else if(comname=="Harlemlanes")
				{var discount="<span style='font:normal 12px Arial;'>* One free bowl (Not applicable on Fridays or Saturday after 9pm)<BR>* 10% discount at the Harlem Lanes Sports bar (Not applicable with other promotions).</span>";}
				else if(comname=="Cafe One")
				{var discount="* 10% discount on the bill.";}
				else if(comname=="Tonnie's Minis")
				{var discount="* 10% discount on all purchases.";}
				else if(comname=="Sabree")
				{var discount="* 10% discount off service, percentage off does not include retainer fee.";}
				else if(comname=="Subway")
				{var discount="* 10% discount on the bill.";}
				else if(comname=="ImageNation")
				{var discount="* 10% discount on all events.";}			
				else if(comname=="Harlem Underground")
				{var discount="* 10% discount on all purchases.";}
				else if(comname=="H and M Art Gallery")
				{var discount="* 20% discount on all purchases.";}
				else if(comname=="The Back Room")
				{var discount="* 5% discount on any combo service provided at The Back Room.";}
				else if(comname=="IMAN Cosmetics")
				{var discount="* 10% discount on all purchases.";}
				else if(comname=="Bikram Yoga East Harlem")
				{var discount="* 15% discount with purchase of $100 or more.";}
				else if(comname=="Gospel Uptown")
				{var discount="* Free Wine or Beer with Entree.";}
				else if(comname=="GSquaredNewYork")
				{var discount="* 10% discount on all purchases.";}
				else if(comname=="Harlem News Group Inc")
				{var discount="* Not applicable.";}
				else if(comname=="Spaha Cafe")
				{var discount="* 10% discount on all purchases.";}
				else if(comname=="IMENA")
				{var discount="* Become an IMENA QUEEN and take 20% off your first month of services!.";}
				else if(comname=="1209 Enterprises,LLC")
				{var discount="* 10% off services.";}
				else if(comname=="Turning Heads Salon and Day Spa")
				{var discount="* 10% off Salon and Spa Services.";}					
				else if(comname=="Aloft")
				{var discount="* $159.00 per night.";}		
				else if(comname=="KT Design Solutions LLC")
				{var discount="* Not applicable.";}	
				else if(comname=="Verizon Wireless")
				{var discount="* 25% off accessories.";}			

				else 
				{var discount="* Not applicable.";}


				GEvent.addListener(marker, 'click', function(){ 
				marker.openExtInfoWindow(
				  newmap,
				   "simple_example_window",
				  "<div style='display:block;width:500px;margin-left:10px;margin-top:10px;'><div style='font:bold 13px Arial;float:left;margin-right:10px;margin-left:5px;width:95px;'><IMG SRC='images/"+logo+"' BORDER='0' ALT='Community Pioneers:'></div><div style='font:bold 13px Arial;float:left;width:350px;color:#0F7F8D'>"+comname+"</div><div style='float:left;width:360px;margin-top:10px;margin-bottom:10px;margin-right:10px;border-bottom:1px solid #BCBDB7;height:2px;'></div><div style='float:left;width:270px;'><div style='float:left;width:270px;font:normal 13px Arial;color:#606060'>Customer Benefits:</div><div style='float:left;width:370px;font:normal 13px Arial;color:#606060'>"+discount+"</div></div><div style='float:left;width:350px;margin-top:10px;margin-left:110px;margin-right:10px;font:normal 13px Arial;color:#606060'>"+address+"</div><div style='float:left;width:460px;margin-top:10px;margin-left:10px;margin-right:10px;border-bottom:1px solid #BCBDB7;height:2px;'></div><div style='float:left;width:460px;margin-top:10px;margin-left:10px;margin-right:10px;'><div style='font:bold 13px Arial;float:left;margin-right:10px;width:100px;'><IMG SRC='images/button.png' BORDER='0' ALT='PowerofOne'></div><div style='font:bold 13px Arial;float:left;width:350px;font:normal 13px Arial;color:#606060'>"+webadd+"</div></div></div>",
				  {beakOffset: 3}
				); 				
			  });

				
				this.map.addOverlay(marker);			
						
				marker.openExtInfoWindow(
				  newmap,
				  "simple_example_window",
				  "<div style='display:block;width:500px;margin-left:10px;margin-top:10px;'><div style='font:bold 13px Arial;float:left;margin-right:10px;margin-left:5px;width:95px;'><IMG SRC='images/"+logo+"' BORDER='0' ALT='Community Pioneers:'></div><div style='font:bold 13px Arial;float:left;width:350px;color:#0F7F8D'>"+comname+"</div><div style='float:left;width:360px;height:2px;margin-top:10px;margin-bottom:10px;margin-right:10px;border-bottom:1px solid #BCBDB7;'></div><div style='float:left;width:270px;'><div style='float:left;width:270px;font:normal 13px Arial;color:#606060'>Customer Benefits:</div><div style='float:left;width:370px;font:normal 13px Arial;color:#606060'>"+discount+"</div></div><div style='float:left;width:350px;margin-top:10px;margin-left:110px;margin-right:10px;font:normal 13px Arial;color:#606060'>"+address+"</div><div style='float:left;width:460px;margin-top:10px;margin-left:10px;margin-right:10px;border-bottom:1px solid #BCBDB7;height:2px;'></div><div style='float:left;width:460px;margin-top:10px;margin-left:10px;margin-right:10px;'><div style='font:bold 13px Arial;float:left;margin-right:10px;width:100px;'><IMG SRC='images/button.png' BORDER='0' ALT='PowerofOne'></div><div style='font:bold 13px Arial;float:left;width:350px;font:normal 13px Arial;color:#606060'>"+webadd+"</div></div></div>",
				  {beakOffset: 3}
				); 				
			}
			else
			{
				this.close();
				alert('Diese Adresse konnte nicht gefunden werden');
			}
		}.bind(this));
	}
};
window.addEvent('domready', GMapsOverlay.init.bind(GMapsOverlay));

