﻿$(document).ready(function () {

	$(".scrollable").scrollable();

	$("a[rel='#image-gallery']").click(function () {
		SetStartPosition($('#image-gallery'));
	});

	$(".image-wrapper .overlayed").click(function () {
		var rel = $(this).attr('rel');
		var gallery = $(this).parents('.ship-holder').find(rel);
		SetStartPosition(gallery);
	});

	$(".overlay .next").click(function () {
		SetCurrent($(this).parent());
	});

	$(".overlay .prev").click(function () {
		SetCurrent($(this).parent());
	});

	function SetStartPosition(slideshow) {
		slideshow.find(".scrollable").width(0);
		var number = slideshow.find(".scrollable").scrollable().getIndex() + 1;
		slideshow.find(".items").children().removeClass("current");
		slideshow.find(".scrollable").scrollable().begin(0);
		var img = $("img:first", slideshow);

		width = ($(window).width() / 2) - (img.width() / 2);
		slideshow.animate({
			'left': width + "px"
		});
	}

	function SetCurrent(slideshow) {
		var number = slideshow.find(".scrollable").scrollable().getIndex() + 1;
		slideshow.find(".items").children().removeClass("current");
		var current = $(".items div:nth-child(" + number + ")", slideshow);
		current.addClass("current");

		width = ($(window).width() / 2) - (current.find('img').width() / 2);

		slideshow.animate({
			'left': width + "px"
		});
		slideshow.find('.scrollable').animate({
			'width': current.find('img').width(),
			'height': current.find('img').height()
		});
	}


	// The toggling of the travel description shown in the travel head
	$('#travel_head .read_more').click(function () {
		$('#travel_head #travel_description').slideToggle('fast');
	});

	var isHotelTabInitiated = false;

	// Setup for the tabs on ResaPage
	$("ul.travel_tabs").tabs("#TravelContent > .pane", {

		// do your own stuff here
		onClick: function (e, index) {

			// "this" is a reference to the Tabs API
			var currentPane = this.getTabs()[index];

			if ($(currentPane).attr('id') == 'accomodationLink') {
				if (!isHotelTabInitiated) {
					$.each(mapArray, function (index) {
						google.maps.event.trigger(mapArray[index].map, 'resize');
						mapArray[index].map.fitBounds(mapArray[index].bounds);
					});
				}
				isHotelTabInitiated = true;
			}
		}
	});

	$('.departures td.book-td a').click(function (e) {
		// If a B-Sessions iframe exists - set it´s src to the booking button´s href
		var bIframe = $('#TravelContent iframe.bookingIframe');
		if (bIframe.length > 0) {
			e.preventDefault();
			bIframe.attr('src', $(this).attr('href'));
			//bIframe.show('fast');
			$('html,body').animate({ scrollTop: bIframe.offset().top }, 'slow');
		}
	});

});
