var carousel_interval;
var infowindow;
var station_coords = new Array();

station_coords.push({name: "Bond Street", zoom: 17, coord: new google.maps.LatLng(51.514325,-0.149678)});
station_coords.push({name: "Covent Garden", zoom: 16, coord: new google.maps.LatLng(51.513153,-0.124481)});
station_coords.push({name: "Leicester Square", zoom: 16, coord: new google.maps.LatLng(51.511357,-0.128459)});
station_coords.push({name: "Oxford Circus", zoom: 16, coord: new google.maps.LatLng(51.515213,-0.141889)});
station_coords.push({name: "Paddington", zoom: 17, coord: new google.maps.LatLng(51.516575,-0.175658)});
station_coords.push({name: "Piccadilly", zoom: 16, coord: new google.maps.LatLng(51.509911,-0.133767)});

function googleTranslateElementInit() 
{
  new google.translate.TranslateElement({
    pageLanguage: 'en',
    layout: google.translate.TranslateElement.InlineLayout.HORIZONTAL
  }, 'google_translate_element');

}

function preload(arrayOfImages) 
{
    $(arrayOfImages).each(function(){
        $('<img/>')[0].src = this;
    });
}

function scrollCarousel()
{
	$("#carouselbox").data("selected", $("#carouselbox").data("selected") + 1);
	if($("#carouselbox").data("selected") >= carousel_array.length) $("#carouselbox").data("selected", 0);
	$("#carouselbox").css("background-image", "url('" + webroot + "img/carousels/backgrounds/" +  carousel_array[$("#carouselbox").data("selected")].id + ".png')");
	$("#carouseltitle").attr("src", webroot + "img/carousels/titles/" + carousel_array[$("#carouselbox").data("selected")].id + ".png");
	$("#carousellink").text(carousel_array[$("#carouselbox").data("selected")].title + "»");
	$("#carousellink").attr("href", carousel_array[$("#carouselbox").data("selected")].url);
}

function resetMap()
{
	if(map)
	{
		map.panTo(new google.maps.LatLng(51.511001,-0.145637));
		map.setZoom(13);
	}
}

function initialiseCarousel()
{
	if($("#carouselbox").length == 1)
	{
		if (typeof(carousel_array) != "undefined")
		{
			if(carousel_array != null)
			{
				$("#carouselbox").data("selected", 0);
				carousel_interval = setInterval(scrollCarousel, 5000);
			}
		}
	}
}

function initialiseMap()
{
	if($("#mapcanvas").length == 1)
	{
		var mapCentre = new google.maps.LatLng(51.511001,-0.145637);
		var mapOptions = { zoom: 13, minZoom: 13, maxZoom: 19, center: mapCentre, mapTypeId: google.maps.MapTypeId.ROADMAP, mapTypeControl: false};
		map = new google.maps.Map(document.getElementById("mapcanvas"), mapOptions);
		google.maps.event.addListener(map, 'bounds_changed', function() { 
																			var bounds = map.getBounds();
																			$(".locationbox").addClass("hidden");
																			$(".locationbox").removeClass("left");
																			$(".locationbox").removeClass("right");
																			var counter = 0;
																			for(var i=0;  i<map_markers.length; i++)
																			{
																				if(bounds.contains(new google.maps.LatLng(map_markers[i].Location.latitude, map_markers[i].Location.longitude)))
																				{
																					$("#locationbox" + map_markers[i].Location.id).removeClass("hidden");
																					if(counter++%2 == 0) $("#locationbox" + map_markers[i].Location.id).addClass("left");
																					else $("#locationbox" + map_markers[i].Location.id).addClass("right");
																						
																				}
																			} 
																		});
		if(typeof(map_markers) != "undefined")
		{
			if(map_markers != null)
			{
				for(var i=0; i<map_markers.length; i++)
				{

					var marker = new google.maps.Marker( {icon: webroot + 'img/icon_mapmarker.png',position: new google.maps.LatLng(map_markers[i].Location.latitude, map_markers[i].Location.longitude), map: map, title: map_markers[i].Location.title} );
					marker.set("title", map_markers[i].Location.title);	
					marker.set("address", map_markers[i].Location.address);
					marker.set("id", map_markers[i].Location.id);
					google.maps.event.addListener(marker, 'click', function() { if(infowindow) infowindow.close(); infowindow = new google.maps.InfoWindow( { content: "<div class=\"mapinfobox\"><h3>" + this.get("title") + "</h3><img src=\"" + webroot + "img/locations/" + this.get("id") + ".png\" /><p>" + this.get("address") + "</p></div>" } ); infowindow.open(map, this) });
				}
			}
		}
		
		if($(".mapresetlink").length == 1) 
		{
			$(".mapresetlink").click(resetMap);
		}
		
		if($(".maplist").length == 1)
		{
			$(".maplist").change(function() 
								 {
								 	if($(this).val() != "")
								 	{
								 		if(typeof(station_coords) != undefined)
								 		{
								 			if($(this).val() <= station_coords.length)
								 			{
								 				map.setZoom(station_coords[$(this).val()].zoom);
									 			map.panTo(station_coords[$(this).val()].coord);
									 		}
								 		}
								 	}
								 });
		}
	}
}

function setVisibleImageContainer()
{
	$(".imagesbox").addClass("hidden");
	$("#imagesbox" + $(".imageswrapper").data("selectedindex")).removeClass("hidden");
}

function setVisibleVideoContainer()
{
	$(".videosbox").addClass("hidden");
	$("#videosbox" + $(".videoswrapper").data("selectedindex")).removeClass("hidden");
}

function initialiseGallery()
{
	if($(".imageswrapper").length == 1)
	{
		$(".imageswrapper").data("selectedindex", 0);
		if($("#previousimages").length == 1)
			$("#previousimages").click(function() {
														var selectedIndex = $(".imageswrapper").data("selectedindex");
														if(selectedIndex > 0)
														{
															$(".imageswrapper").data("selectedindex", selectedIndex - 1);
															setVisibleImageContainer();
														}
													} );
		if($("#nextimages").length == 1)
			$("#nextimages").click(function() {
													var selectedIndex = $(".imageswrapper").data("selectedindex");
													if(selectedIndex < $(".imagesbox").length -1)
													{
														$(".imageswrapper").data("selectedindex", selectedIndex + 1);
														setVisibleImageContainer();
													}
												} );		
	}
	if($(".videoswrapper").length == 1)
	{
		$(".videoswrapper").data("selectedindex", 0);
		if($("#previousvideos").length == 1)
			$("#previousvideos").click(function() {
														var selectedIndex = $(".videoswrapper").data("selectedindex");
														if(selectedIndex > 0)
														{
															$(".videoswrapper").data("selectedindex", selectedIndex - 1);
															setVisibleVideoContainer();
														}
													} );
		if($("#nextvideos").length == 1)
			$("#nextvideos").click(function() {
													var selectedIndex = $(".videoswrapper").data("selectedindex");
													if(selectedIndex < $(".videosbox").length -1)
													{
														$(".videoswrapper").data("selectedindex", selectedIndex + 1);
														setVisibleVideoContainer();
													}
												} );		
	}	

	$("a.galleryimagelink").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + '</span>';
		}
	});

	$("a.galleryimagelink").click(function(){
												$(".imageswrapper").data("selectedindex", $(this).parent(".imagesbox").first().attr("id").replace("imagesbox", ""));
												setVisibleImageContainer();
											});

	$("a.galleryvideolink").click(function(){
												$(".videoswrapper").data("selectedindex", $(this).parent(".videosbox").first().attr("id").replace("videosbox", ""));
												setVisibleVideoContainer();
											});

//	$("a.galleryvideolink").click(function() {alert('hello'); } );

	$("a.galleryvideolink").each(function() {
												$(this).fancybox({
												'transitionIn'		: 'none',
												'transitionOut'		: 'none',
												'titlePosition' 	: 'over',
												'swf'				: {flashvars: "file=" + $(this).attr("src")},
												'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) { return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + '</span>';}
																})
											});	

	$("a.galleryimagelink.selectedgallery").first().click();
	$("a.galleryvideolink.selectedgallery").first().click();
}

$(document).ready(function() {	
	if (typeof(preload_images) != "undefined")	
		if(preload_images != null)
			preload(preload_images);
	
	$(".toggleinput").focus(function() {
											if($(this).val() == $(this).attr("alt"))
												$(this).val("");
										});
	$(".toggleinput").blur(function() {
											if($(this).val() == "")
												$(this).val($(this).attr("alt"));
										});
	
	
	initialiseCarousel();
	initialiseMap();	
	initialiseGallery();
});

