/**
 *
 * @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() < 18) {
				GoogleMap.setZoom(18);
			}
			GoogleMap.setCenter(new google.maps.LatLng(locale.point.lat, locale.point.lng));
		});	
	}					
});

var locales = [
	{
		point: {
			lat: 47.613947,
			lng: -122.319177
		},
		name: 'Cha Cha',
		street: '1013 E Pike St',
		city: 'Seattle',
		state: 'WA',
		zip: '98122',
		address: '1013 E Pike St, Seattle, WA 98122‎',
		link: 'http://www.chachalounge.com',
		title: 'Sunday July 11. 6pm - Close. 21+',
		details: 'Hosts: Vera co-founder Kate Becker<br />(Seattle Theatre Group)<br />DJ Derek Fudesco<br />(Murder City Devils / Cave Singers<br />/ Pretty Girls Make Graves)'
	},
	
	{
		point: {
			lat: 47.555964,
			lng: -122.270047
		},
		coffee: true,
		name: 'Cafe Vita: Seward Park',
		street: '5028 Wilson Ave S',
		city: 'Seattle',
		state: 'WA',
		zip: '98118',
		address: '5028 Wilson Ave S, Seattle, WA 98118',
		link: 'http://www.caffevita.com/index.php?page=seward_park',
		title: 'Monday July 12. 6am - 7pm.',
		details: ''		
	},
	{
		point: {
			lat: 47.666061,
			lng: -122.383222
		},			
		name: "Hattie's Hat",
		street: '2311 NW Market St',
		city: 'Seattle',
		state: 'WA',
		zip: '98107',			
		address: '2311 NW Market St, Seattle, WA 98107',
		link: 'http://www.hattieshat.com/',
		title: 'Monday July 12. 6pm - Close. 21+',
		details: 'Hosts: Ben London and Michael Stephens<br />(Recording Academy/GRAMMYs)<br /> Starr Harris (Vera)<br /> Brite Futures'
	},
	{
		point: {
			lat: 47.6590705,
			lng: -122.3501444
		},
		coffee: true,
		name: 'Cafe Vita: Fremont',
		street: '4301 Fremont Ave N',
		city: 'Seattle',
		state: 'WA',
		zip: '98103',
		address: '4301 Fremont Ave N, Seattle, WA 98103',
		link: 'http://www.caffevita.com/index.php?page=fremont',
		title: 'Tuesday July 12. 6am - 8pm.',
		details: ''		
	},
	{
		point: {
			lat: 47.562464,
			lng: -122.386924
		},				
		name: 'West 5 Lounge',
		street: '4539 California Ave SW',
		city: 'Seattle',
		state: 'WA',
		zip: '98116',			
		address: '4539 California Ave SW, Seattle, WA 98116',
		link: 'http://www.westfive.com',
		title: 'Tuesday July 13. 6pm - Close. 21+',
		details: 'Hosts: Kate Jackson and<br>Megan Jasper (Sub Pop)'
	},
	{
		point: {
			lat: 47.6012515,
			lng: -122.3295364
		},
		coffee: true,
		name: 'Cafe Vita: Pioneer Square',
		street: '125 Prefontaine Pl S',
		city: 'Seattle',
		state: 'WA',
		zip: '98104',
		address: '125 Prefontaine Pl S, Seattle, WA 98104',
		link: 'http://www.caffevita.com/index.php?page=pioneer_square',
		title: 'Wednesday July 14. 6am - 7pm.',
		details: ''		
	},
	{
		point: {
			lat: 47.608130,
			lng: -122.299724
		},				
		name: 'Twilight Exit',
		street: '2514 E Cherry St',
		city: 'Seattle',
		state: 'WA',
		zip: '98122',			
		address: '2514 E Cherry St, Seattle, WA',
		link: 'http://www.thestranger.com/seattle/Location?location=24755',
		title: 'Wednesday July 14. 6pm - Close. 21+',
		details: 'Hosts: Josh Powell (Vera Program Director)<br />Crescentia (Twilight)<br />Rahwa Habte (Hidmo/Vera Board)<br />Travis Maisch (Vera)'
	},
	{
		point: {
			lat: 47.6267571,
			lng: -122.3477185
		},
		coffee: true,
		name: 'Cafe Vita: Queen Anne',
		street: '813 5th Ave N',
		city: 'Seattle',
		state: 'WA',
		zip: '98109',
		address: '813 5th Ave N, Seattle, WA 98109',
		link: 'http://www.caffevita.com/index.php?page=queen_anne',
		title: 'Thursday July 15. 6am - 8pm.',
		details: ''		
	},
	{
		point: {
			lat: 47.615115,
			lng: -122.323199
		},				
		name: "Linda's Tavern",
		street: '707 E Pine St',
		city: 'Seattle',
		state: 'WA',
		zip: '98122',			
		address: '707 E Pine St, Seattle, WA 98122',
		link: 'http://www.thestranger.com/seattle/Location?location=24347',
		title: 'Thursday July 15. 6pm - Close. 21+',
		details: 'Hosts: Shannon Stewart<br />and James Keblas (Vera co-founders)'
	},
	{
		point: {
			lat: 47.614054,
			lng: -122.31905
		},
		coffee: true,
		name: 'Cafe Vita: Capitol Hill',
		street: '813 5th Ave N',
		city: 'Seattle',
		state: 'WA',
		zip: '98122',
		address: '1005 East Pike St, Seattle, WA 98122',
		link: 'http://www.caffevita.com/index.php?page=capitol_hill',
		title: 'Friday July 16. 6am - 11pm.<br />Saturday July 17. Wrap-up party 6-8pm.',
		details: ''		
	},	
	{
		point: {
			lat: 47.625544,
			lng: -122.352646
		},				
		name: 'Solo Bar',
		street: '200 Roy St',
		city: 'Seattle',
		state: 'WA',
		zip: '98109',			
		address: '200 Roy St, Seattle, WA 98109',
		link: 'http://www.solo-bar.com/',
		title: 'Friday July 16. 6pm - Close. 21+',
		details: 'Hosts: Frank Nieto (230 Publicity)<br />Three Imaginary Girls<br />Guest DJs!'
	},		
	{
		point: {
			lat: 47.6181399,
			lng: -122.347253
		},				
		name: '5 Point Cafe',
		street: '415 Cedar St',
		city: 'Seattle',
		state: 'WA',
		zip: '98121',			
		address: '415 Cedar St, Seattle, WA 98121',
		link: 'http://www.the5pointcafe.com',
		title: 'Friday July 16. 6pm - Close. 21+',
		details: 'Hosts: KEXP DJs Rachel Ratner <br />and Sharlese'
	},
	{
		point: {
			lat: 47.613938,
			lng: -122.320399
		},				
		name: "Neumo's",
		street: '925 E Pike St',
		city: 'Seattle',
		state: 'WA',
		zip: '98122',			
		address: '925 E Pike St, Seattle, WA‎ 98122',
		link: 'http://neumos.com/',
		title: 'Saturday July 17. All ages, bar w/ ID',
		details: 'Grand Finale event w/ Brite Futures<br />Doors at 8pm.<br />Tickets at Moe Bar and <a href="https://www.etix.com/ticket/online/performanceSearch.jsp?performance_id=1276653&amp;cobrand=neumos" rel="external">etix.com</a><br />'
	}		
														
];

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

		$$('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');
});