$(function(){
	var gmarker = [],
		html = [],
		to_html = [],
		from_html = [];

	// AJAX-ify all domain links
	$('a[href^="?page="], a[href="/"]').live('click', function(event) {
		var page = $(this).attr('href')|| '?page=partners';

		// Set hash
		document.location.hash = page;

		// Format page
		page = page === '/' ? 'home' : page.replace(/.+=/i, '');
		$(document.body).attr('id', page);

		$.ajax({
			url: 'content/' + (page || 'home') + '.htm',
			success: function(data) {
				var html = $('<div>' + data + '</div>'),
					panel1 = $('.panel1', html),
					panel2 = $('.panel2', html);

				// Reset css
				$('.panel1, .panel2').css({ backgroundColor: '', position: '' })

				// Destroy google map if present
				if(window.carritMap) {
					window.carritMap.destroy();
				}

				// Update panel contents
				if(panel1.length) {
					$('.panel1').flip({
						direction:'rl',
						content: panel1,
						speed: 350
					})
				}
				if(panel2.length) {
					$('.panel2').flip({
						direction:'rl',
						content: panel2,
						speed: 350,
						onEnd: function() {
							init(page);
						}
					})
				}

			}
		})

		return false;
	})

	// Setup more details links
	$('.flip-forward').live('click', function() {
		var container = $(this).parents('.panel-content'),
			newContent = container.next().html();

		container.parent().flip({
			direction:'rl',
			content: newContent,
			speed: 350
		});

		return false;
	});
	$('.flip-back').live('click', function() {
		$(this).parents('.panel').revertFlip();
		return false;
	});

	function setupLocation() {
		window.carritMap = new googleMap( $('.panel2')[0] );
	}

	function googleMap(target) {
		this.target = target;
		this.html = {
			from: '',
			to: ''
		};
		this.marker = null;
		this.map = null;

		$.extend(this, {
			init: function() {
				// Setup document unload event
				document.unload = this.destroy;

				// Display the map, with some controls and set the initial location
				this.map = new GMap2(this.target);
				this.map.addControl(new GSmallZoomControl3D());
				this.map.addControl(new GOverviewMapControl());
				this.map.addControl(new GHierarchicalMapTypeControl());
				this.map.setCenter(new GLatLng(51.520572,-0.143442), 16);
				this.map.setMapType(G_NORMAL_MAP);

				// Set up markers with info windows
				var point = new GLatLng(51.519572,-0.143442);
				this.createMarker(point, '<p class="maptext"><img src="http://www.carritt.co.uk/img/carritt-logo-smallmap.gif" alt="Carrit & Co"></p><p>1 Duchess Street,<br>London W1W 6AN</p>')
				this.map.addOverlay(this.marker);

				GEvent.trigger(this.marker, 'click');
			},

			// A function to create the marker and set up the event window
			createMarker: function(point,  prefix) {
				var self = this;
				this.marker = new GMarker(point);

				// The info window version with the "to here" form open
				this.html.to = prefix + '<br>Directions: <b>To here</b> - <a href="javascript:window.carritMap.marker.openInfoWindowHtml(window.carritMap.html.from);">From here</a>' +
					'<br>Start address:<form action="http://maps.google.com/maps" method="get" target="_blank">' +
					'<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br>' +
					'<INPUT value="Get Directions" TYPE="SUBMIT">' +
					'<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() + '"/>';

				// The info window version with the "to here" form open
				this.html.from = prefix + '<br>Directions: <a href="javascript:window.carritMap.marker.openInfoWindowHtml(window.carritMap.html.to);">To here</a> - <b>From here</b>' +
					'<br>End address:<form action="http://maps.google.com/maps" method="get"" target="_blank">' +
					'<input type="text" SIZE=40 MAXLENGTH=40 name="daddr" id="daddr" value="" /><br>' +
					'<INPUT value="Get Directions" TYPE="SUBMIT">' +
					'<input type="hidden" name="saddr" value="' + point.lat() + ',' + point.lng() + '"/>';

				// The inactive version of the direction info
				var html = prefix + '<br>Directions: <a href="javascript:window.carritMap.marker.openInfoWindowHtml(window.carritMap.html.to);">To here</a> - '+
					'<a href="javascript:javascript:window.carritMap.marker.openInfoWindowHtml(window.carritMap.html.from);">From here</a>';

				GEvent.addListener(this.marker, 'click', function() {
					self.marker.openInfoWindowHtml(html);
				});
			},

			destroy: function() {
				GUnload();
			}
		})

		if(!GBrowserIsCompatible()) {
			return false;
		}
		else {
			this.init();
		}
	}

	function init(page) {
		switch(page) {
			case 'location':
				setupLocation();
			break;
		}
	}

	// Initialize page
	init( $(document.body).attr('id') );
})