jQuery(document).ready(function() {

	var site = (''+window.location).substr(0, (''+window.location).indexOf('/', 9)+1);
	
	var title = 'Festival Contentpourien 2011';
	var text = 'Le festival Contentpourien revient pour sa troisième édition afin de vous proposer toujours plus de diversité musicale et artistique!';
	
	jQuery('#identity').after('' +
		'<div id="share">' +
			'<div>' +
				'<a target="_blank" id="share-twitter" href="http://twitter.com/share?url='+site+'&text='+encodeURIComponent(title)+'"><span>Twitter</span></a>' +
				'<a target="_blank" id="share-facebook" href="http://www.facebook.com/sharer.php?u='+site+'&t='+encodeURIComponent(title)+'"><span>Facebook</span></a>' +
			'</div>' +
		'</div>');
	jQuery('#page-artiste').each(function() {
		jQuery('#main h1', this).before('' +
			'<div class="share">' +
				'<a target="_blank" href="http://www.facebook.com/sharer.php?u='+(window.location)+'&t='+(document.title)+'" id="share-facebook-artiste"><span>Facebook</span></a>' +
				'<a target="_blank" href="http://twitter.com/share?url='+(window.location)+'&text='+(document.title)+'" id="share-twitter-artiste"><span>Twitter</span></a>' +
			'</div>');
	});

	
	swfobject.embedSWF("/view/reserver.swf", 'button-reservez', "107", "95", "9.0.0", false, {}, {menu:"false", quality:"high",wmode:'transparent'}, {});
});

if (jQuery('body').is('#page-artiste')) {
	
	var locations = jQuery('#main .infos .locations');
	var dates = jQuery('#main .infos .dates');
	var as = jQuery('#dateselector a').click(function() {
		
		var self = jQuery(this);
		var pos = jQuery.inArray(this, as);
		if (pos === -1)
			return ;
		as.removeClass('selected');
		self.addClass('selected');
		locations.removeClass('selected');
		dates.removeClass('selected');
		jQuery(locations[pos]).addClass('selected');
		jQuery(dates[pos]).addClass('selected');
	});
}

if (jQuery('body').is('#page-lieux-acces')) {
	google.load('maps', '3', {other_params: "sensor=false"});
	google.load('search', '1');
	google.setOnLoadCallback(function() {
	
		var locations = jQuery('#main .location');
		var map = null;
		var geocoder = null;
		var marker = null;
		var cache = {};
		var infowindow = null;
		
		function	cached_geocode(address, ondone) {
			if (typeof cache[address] != 'undefined')
				return ondone(cache[address]);
			if (!geocoder)
				geocoder = new google.maps.Geocoder();
			geocoder.geocode({address: address}, function(results, status) {
				if (status != google.maps.GeocoderStatus.OK) {
					cache[address] = false;
					console.warn("Geocode was not successful for the following reason: " + status);
					ondone(cache[address]);
					return ;
				}
				cache[address] = results[0];;
				
				ondone(cache[address]);
			});
		};
		
		function	scroll_map_to(address) {
			
			cached_geocode(address, function(point) {
				var zoom = 12;
				if (point === false) {
					point = {geometry: {location: new google.maps.LatLng(48.9804062, 1.6720311000000265)}};
					zoom = 10;
				}
				
				if (!map)
					map = new google.maps.Map(jQuery('#map')[0], {
						zoom: 12,
						center: point.geometry.location,
						mapTypeId: google.maps.MapTypeId.ROADMAP
					});
				
				if (marker)
					marker.setMap(null);
				marker = new google.maps.Marker({
					map: map,
					position: point.geometry.location
				});
				if (!infowindow)
					infowindow = new google.maps.InfoWindow();
				infowindow.setContent(('<div style="width:280px; height:100px">' +
						'<div class="address">{address}</div>'+
						'<div class="infobox">' +
							'Itinéraire <b>vers ce lieu </b> :<br />' +
							'Entrez votre Lieu de Départ <i>(format &laquo; rue, cp, ville &raquo;)</i>'+
							'<form action="http://maps.google.com/maps" method="get" target="_blank">' +
								'<input type="text" maxlength="50" name="saddr" id="saddr" value="" style="width:98%"><br />' +
								'<input value="Envoyer" type="submit" style="float: right;">' +
								'<input type="hidden" name="daddr" value="{latitude},{longitude}">' +
							'</form>' +
						'</div>'+
					'</div>').replace(/{address}/g, point.formatted_address || address).replace(/{latitude}/g, point.geometry.location.lat()).replace(/{longitude}/g, point.geometry.location.lng()));
				infowindow.open(map, marker);
				
				map.setZoom(zoom);
				setTimeout(function() {
					map.panTo(point.geometry.location);
				}, 100);
			});
		};
		function	update_hash() {
			var hash = (''+window.location).split('#')[1];
			if (hash) {
				var loc = locations.filter('#bloc-'+hash);
				if (loc.length) {
					locations.hide();
					loc.show();
					scroll_map_to(loc.find('input[name=address]').val());
					return ;
				}
			}
			locations.filter(':not(:first)').hide();
			locations.first().show();
			scroll_map_to(locations.first().find('input[name=address]').val());
		};
		
		update_hash();
		var prev = ''+window.location;
		setInterval(function() {
			if (''+window.location === prev)
				return ;
			prev = ''+window.location;
			update_hash();
		}, 100);
		
	});
}
