/**
 * The custom JavaScript used for the added functionality.
 *
 * @author Dan Hensby <dan@betterbrief.co.uk>
 * @author Josh Holloway <josh@betterbrief.co.uk>
 * @version 1.1
 * @copyright 2010 Better Brief LLP
 *
 */
var $map;
//Load jQuery
google.load("jquery", "1.4");

function initMap() {
	if ($map.length) {

		var mapElement = $map[0],
			//england
			center = new google.maps.LatLng(54.53383250794428, -3.97705078125),
			myOptions = {
				zoom: 6,
				center: center,
				mapTypeId: google.maps.MapTypeId.HYBRID,
				backgroundColor: '#fff',
				streetViewControl: true
			},
			map = new google.maps.Map(mapElement, myOptions),
			lastInfoWindow = null;

		jQuery.getJSON(
			'MapData.json',
			null,
			function(data,status,XMLHttpRequest) {
				if (status == 'success' && data) {
					jQuery.each(data,function(i,point) {
						var myLatLng = new google.maps.LatLng(point.Lat,point.Lon),
							marker = new google.maps.Marker({
								position: myLatLng,
								map: map,
								title: point.Name
							}),
							infoWindow = new google.maps.InfoWindow({
								content: point.Content
							});

						google.maps.event.addListener(marker,'click',function() {
							if (!infoWindow.isOpen) {
								if (lastInfoWindow) {
									lastInfoWindow.isOpen = false;
									lastInfoWindow.close();
								}
								infoWindow.isOpen = true;
								infoWindow.open(map,marker);
								lastInfoWindow = infoWindow;
							}
							else {
								lastInfoWindow = null;
								infoWindow.isOpen = false;
								infoWindow.close();
							}
						});
					});
				}
			}
		);

		mapElement.style.backgroundImage = 'none';
	}
}

function loadMap() {
	google.load("maps","3",{
		callback: initMap,
		"other_params" : "sensor=true&region=GB"
	});
}

// effectively 'on document ready'
google.setOnLoadCallback(function() {



		//$.noConflict();
		jQuery(document).ready(function($) {

			$('#SetPassword-_Password,#SetPassword-_ConfirmPassword').val('');

			$map = $('#GMap');
			if ($map.length) {
				loadMap();
			}

		var buynow = $('div.makeConn a');
		if (buynow.length) {
			buynow.overlay({
				onBeforeLoad: function() {
				this.getOverlay().addClass('buyNow');
					// Trigger original beforeLoad and then extend it below:
					buynow.trigger('onBeforeLoad');
					$(buynow.attr('rel') + ' iframe').show();
					var overlay = $('<div id="BackgroundOverlay" style="display:none;background:black;height:'+$(document).height()+'px;width:100%;position:absolute;top:0;left:0;z-index:9001"></div>').css('opacity', '0.6');
					overlay.bind('click.close', function(ev) {
						if(ev.target === this) {
							$('a.close').trigger('click');
						}
					});
					$(document.body).prepend(overlay);
					overlay.fadeIn();
					$(window).bind('resize.overlay', function() { overlay.height($(this).height()); });
				},
				onClose: function() {
					this.getOverlay().removeClass('buyNow');
					buynow.trigger('onClose');
					$(buynow.attr('rel') + ' iframe').hide();
					$('#BackgroundOverlay').fadeOut(function() {
						$(this).remove();
					});
				}
			});
		}

		$.featureList(
			$("#tabs li a"),
			$("#output li"), {
				start_item	:	1
			}
		);

		/*

		// Alternative


		$('#tabs li a').featureList({
		output			:	'#output li',
		start_item		:	1
		});

		*/


		// Expand Panel
		$("#open").click(function(){
			$("div#panel").slideDown("slow");
		});

		// Collapse Panel
		$("#close").click(function(){
			$("div#panel").slideUp("slow");
		});

		// Switch buttons from "Log In | Register" to "Close Panel" on click
		$("#toggle a").click(function () {
			$("#toggle a").toggle();
		});

	});

});

