window.addEvent('domready', function(){

	// PHOTO CHANGER -----------------------------------------
	var pc = new PhotoChanger('flickr_banners', {
		url: paths.root+'json/flickr_banners'
	});
	pc.addEvents({
		"load": function(total){
			// banners are loaded
		},
		"change": function(banner, index, lastbanner, lastindex){
			// every time a banner changes
			$('flickr_banners').getElement('.info').set('opacity', .7);
			$('flickr_banners').getElement('.title').set('html', banner.title);
			$('flickr_banners').getElement('.author').set('html', 'By: '+banner.author);
		}
	});
	pc.load();
	
	// HEADLINE IS CLICKABLE -----------------------------------------
	$('headline').setStyle('cursor', 'pointer').addEvent('click', function(){
		window.location = 'http://theseamericantimes.com';
	});
	
	// GOOGLE MAP -----------------------------------------
	window.map = new GMap2(document.getElementById('map'));
    map.setCenter(new GLatLng(37.19533058280062, -89.1650390625), 4);
    map.setUIToDefault();
    map.disableScrollWheelZoom();
    
    window.map_shows = {};
    new Request.JSON({url:paths.root+'json/get_shows'})
    .addEvent('success', function(rsp){
    	rsp.each(function(show){
    		if(show.latlng){
    			var location = show.latlng.split(',');
    			show.lat = location[0];
    			show.lng = location[1];
    			show.info = '<div class="google-info-window"><h4>'+show.venue+'</h4><span class="date">'+show.date+', '+show.time+'</span><p>'+show.address+'<br/>'+show.city+', '+show.state+'</p><a href="'+show.googlelink+'" target="_blank">Get Directions</a></div>';
    			show.marker = new GMarker(new GLatLng(show.lat, show.lng));
    			map.addOverlay(show.marker);
    			GEvent.addListener(show.marker, "click", function(){
					show.marker.openInfoWindowHtml(show.info);
				});
				
				map_shows['show_'+show.id] = show;
    		}
    	});
    })
    .send();
    
    
    // VAN LOCATION
	new Request.JSON({url:paths.root+'json/get_van_location'})
	.addEvent('success', function(rsp){
		var icn = new GIcon();
		icn.image = paths.root+"assets/images/icons/van-icon.png";
		icn.shadow = paths.root+"assets/images/icons/van-shadow.png";
		icn.iconSize = new GSize(65, 40);
		icn.shadowSize = new GSize(59, 40);
		icn.iconAnchor = new GPoint(23, 32);
		icn.infoWindowAnchor = new GPoint(17, 0);
		
		// Set up our GMarkerOptions object literal
		var markerOptions = { icon:icn };
		var marker = new GMarker(new GLatLng(rsp.latitude, rsp.longitude), markerOptions);

		map.addOverlay(marker);
		GEvent.addListener(marker, "click", function(){
			marker.openInfoWindowHtml('<div class="google-info-window"><b>We\'re using Twitter to update you on our whereabouts. The van icon on the map will show you our last known location. You can follow our tweets <a href="http://twitter.com/wereinavan" target="_blank">@wereinavan</a>.</b><br/><br/>'+rsp.tweet_text+'<br/></div>');
			//<a href="/home/show_van_route">See where we\'ve been</a>
			
			map.setZoom(9);
			map.panTo(new GLatLng(rsp.latitude, rsp.longitude));
		});
	})
	.send();
	
	
	// Load van route
	
	
	
	
	// Subscribe form
	if($('subscribe-form')){
		
		var form = $('subscribe-form');
		form.get('send').addEvent('success', function(rsp){
			rsp = JSON.decode(rsp);
			if(rsp.status == 'ok'){
				form.reset();
				$('subscribe').set('html', '<h4>Thanks for keeping up with us!</h4>');
			}else{
				form.getElement('.alert').set('html', rsp.msg);
				form.getElement('button').set('disabled', false);
			}
		});
		form.addEvent('submit', function(e){
			e.stop();
			form.getElement('button').set('disabled', true);
			this.send();
		});
	}


});


function showShow(id){
	var show = map_shows['show_'+id];
	map.setZoom(11);
	map.panTo(new GLatLng(show.lat, show.lng));
	map.closeInfoWindow();
	show.marker.openInfoWindowHtml(show.info);
}


