/**
 *
 * @author Kris H.
 */

/**
 * Open links in new window
 **/ 
var ExternalLinks = Class.create({
	initialize: function(sElements) {
		var els = $$(sElements);	
		for (var i = 0; i < els.length; i++) {
			els[i].writeAttribute('target', '_blank');
		}
	}
});

var SectionSlider = Class.create({
	initialize: function(sTriggers) {
		var triggers = $$(sTriggers);
		SectionSlider.triggers = triggers;
		
		for(var i = 0; i < triggers.length; i++) {
			triggers[i].href = '#/' + triggers[i].rel;
		}
		if(typeof SWFAddress == 'object') {
			SWFAddress.addEventListener(SWFAddressEvent.CHANGE, this.slideTo);
		}
		else {
			return false;
		}
		
		triggers.invoke('observe', 'click', function(e) {
			e.stop();
			var el = e.element();
			var target = el.rel;
			if(typeof SWFAddress == 'object') {
				SWFAddress.setValue(target);
			}	
		});

	},
	
	slideTo: function() {
		var path = SWFAddress.getPathNames();
		SectionSlider.triggers.invoke('removeClassName', 'on');
		var target = 'about';
		
		if(path.length > 0) {	
			target = path[0];
		}
				
		var navTarget = 'nav-' + target;
		if($(navTarget)) {
			$(navTarget).addClassName('on');
		}
		if($(target)) {
			var left = $(target).offsetTop;
			var top = $(target).positionedOffset()[1];
	        new Effect.Morph($('inner-content'), {
	            style: 'top: -' + top + 'px',
	            duration: 0.5
	        });
		}
	}
	
	
	
});

var activewindow;
var GoogleMap = Class.create({
	initialize: function(el, locales, options){
	  	if ($(el)) {
	  	
	  		this.options = Object.extend({
				imagePath: '_img/marker.png',
				coffeeImagePath: '_img/coffee-marker.png',
	  			template: '#{title}<br /><strong>#{name}</strong><br />#{street}<br />#{city}, #{state} #{zip}'
	  		}, options || {});
      		
      		this.gmarkers = new Array();
      		
			// @TODO: make this less rigid
			this.mapOptions = {
				zoom: 15,
			    center: new google.maps.LatLng(47.667477, -122.385078),
			 	navigationControl: true,
				navigationControlOptions: { style: google.maps.NavigationControlStyle.ZOOM_PAN },
			    scaleControl: true,
				mapTypeControl: false,
				mapTypeId: google.maps.MapTypeId.TERRAIN
			};

      		var map = new google.maps.Map($(el), this.mapOptions); 
 			GoogleMap = map;

			this.gBounds = new google.maps.LatLngBounds();
			
			for (var i = 0; i < locales.length; i++) {
			    var locale = locales[i];
				this.addToMap(locale);
		  	}
      	}
			
	},
	
	addToMap: function(locale) {
		
		var template = new Template(this.options.template);
		if(locale.coffee == true) {
			var markerImage = window.rootVirtual + this.options.coffeeImagePath;
		}
		else {
			var markerImage = window.rootVirtual + this.options.imagePath;
		}
		
		var myLatLng = new google.maps.LatLng(locale.point.lat, locale.point.lng);
	    var marker = new google.maps.Marker({
	        position: myLatLng,
	        map: GoogleMap,
	        icon: markerImage
	    });
		
		// extend the point to the others and tell google maps to fit it all within one window
		this.gBounds.extend(myLatLng);
		GoogleMap.fitBounds(this.gBounds);
		
		// push all markers into the gmarkers array - so we can reference it from outside the class
		this.gmarkers.push(marker);
		
		var infowindow = new google.maps.InfoWindow({
		    content: template.evaluate(locale)
		});
		
		google.maps.event.addListener(marker, 'click', function(e) {
			
			if(activewindow) {
				activewindow.close();
			}

			infowindow.open(GoogleMap, marker);
			activewindow = infowindow;
			
			if (GoogleMap.getZoom() < 16) {
				GoogleMap.setZoom(16);
			}
			GoogleMap.setCenter(new google.maps.LatLng(locale.point.lat, locale.point.lng));
			GoogleMap.panBy(0, -100);
		});	
	}
});

var locales = [
	{
		point: {
			lat: 47.61288,
			lng: -122.31890
		},
		name: 'Auto Battery',
		street: '1009 E Union',
		city: 'Seattle',
		state: 'WA',
		zip: '98122',
		link: 'http://www.autobatterybar.com/',
		title: 'Sunday June 26. 6pm - Close. 21+',
		details: ''
	},
	{
		point: {
			lat: 47.613947,
			lng: -122.319177
		},
		name: 'Cha Cha',
		street: '1013 E Pike St',
		city: 'Seattle',
		state: 'WA',
		zip: '98122',
		link: 'http://www.chachalounge.com',
		title: 'Monday June 27. 6pm - Close. 21+',
		details: ''
	},
	{
		point: {
			lat: 47.625544,
			lng: -122.352646
		},
		name: 'Solo Bar',
		street: '200 Roy St',
		city: 'Seattle',
		state: 'WA',
		zip: '98109',			
		link: 'http://www.solo-bar.com/',
		title: 'Monday June 27. 6pm - Close. 21+',
		details: ''
	},
	{
		point: {
			lat: 47.562464,
			lng: -122.386924
		},				
		name: 'West 5 Lounge',
		street: '4539 California Ave SW',
		city: 'Seattle',
		state: 'WA',
		zip: '98116',			
		link: 'http://www.westfive.com',
		title: 'Tuesday June 28. 6pm - Close. 21+',
		details: ''
	},
	{
		point: {
			lat: 47.61312,
			lng: -122.31591
		},				
		name: 'Pony',
		street: '1221 East Madison Street',
		city: 'Seattle',
		state: 'WA',
		zip: '98122',			
		link: 'http://www.ponyseattle.com',
		title: 'Tuesday June 28. 6pm - Close. 21+',
		details: ''
	},	
	{
		point: {
			lat: 47.608130,
			lng: -122.299724
		},				
		name: 'Twilight Exit',
		street: '2514 E Cherry St',
		city: 'Seattle',
		state: 'WA',
		zip: '98122',
		link: 'http://www.thestranger.com/seattle/Location?location=24755',
		title: 'Wednesday June 29. 6pm - Close. 21+',
		details: ''
	},
	{
		point: {
			lat: 47.615115,
			lng: -122.323199
		},				
		name: "Linda's Tavern",
		street: '707 E Pine St',
		city: 'Seattle',
		state: 'WA',
		zip: '98122',			
		link: 'http://www.thestranger.com/seattle/Location?location=24347',
		title: 'Thursday June 30. 6pm - Close. 21+',
		details: ''
	},
	{
		point: {
			lat: 47.61783,
			lng: -122.32447
		},				
		name: "Redwood",
		street: '514 E Howell Street',
		city: 'Seattle',
		state: 'WA',
		zip: '98122',			
		link: 'http://www.thestranger.com/seattle/redwood/Location?oid=178252',
		title: 'Friday July 1. 6pm - Close. 21+',
		details: ''
	},
	{
		point: {
			lat: 47.66819,
			lng: -122.38584
		},				
		name: "Sunset Tavern",
		street: '5433 Ballard Ave NW',
		city: 'Seattle',
		state: 'WA',
		zip: '98107',			
		link: 'http://sunsettavern.com',
		title: 'Friday July 1. 6pm - Close. 21+',
		details: ''
	},
	{
		point: {
			lat: 47.666061,
			lng: -122.383222
		},			
		name: "Hattie's Hat",
		street: '5231 Ballard Ave NW',
		city: 'Seattle',
		state: 'WA',
		zip: '98107',			
		link: 'http://www.hattieshat.com/',
		title: 'Saturday July 2. 6pm - Close. 21+',
		details: ''
	},
	{
		point: {
			lat: 47.6181399,
			lng: -122.347253
		},				
		name: '5 Point Cafe',
		street: '415 Cedar St',
		city: 'Seattle',
		state: 'WA',
		zip: '98121',			
		link: 'http://www.the5pointcafe.com',
		title: 'Saturday July 2. 6pm - Close. 21+',
		details: ''
	}
];

document.observe("googlemap:load", function(event) {
	if(google) {
		var mapit = new GoogleMap('map-canvas', locales, {template: '<div><p>#{title}<br /><strong>#{name}</strong><br />#{street}<br />#{city}, #{state} #{zip}<br /><a href="#{link}" target="_blank">Web site</a></p></div>'});

		$$('a.map-view').invoke('observe', 'click', function(e) {
			e.stop();
			var el = e.findElement('a');
			var id = el.readAttribute('href').split("-").pop();
			google.maps.event.trigger(mapit.gmarkers[id], 'click');
		});	
	}

});

document.observe('dom:loaded', function() {
	document.fire('googlemap:load');
	var externals = new ExternalLinks('a[rel=external]');
	//var sectionexpander = new SectionSlider('nav a');
});
