function px2int(t) {
	return parseInt(t.replace("px", ""));
}


var map;
 
function initialize() {
	// Load Map
	map = new google.maps.Map(document.getElementById('google-map'), {
		zoom: 15,
		center: new google.maps.LatLng(51.51231, -0.12156),
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		mapTypeControl: false,
		scaleControl: true,
		streetViewControl: false
	});
 
	// Add Marker
	marker = new google.maps.Marker({
		position: new google.maps.LatLng(51.51231, -0.12156),
		map: map,
		icon: 'img/map-icon.png',
		title: 'Tuttons'
	});
}
 
jQuery(function($){
	// 360 TOUR
	
	$("#tour-button").click(function() {
		$("#tour").show();
		$("#transition-images-wrapper").hide();
		$("#google-map").hide();
		$("#form-slider").hide();
	});


	//Contact page - Google Map
	if($('body').attr('id') == 'contact') {
		$('#google-map').show();
		initialize();
		$("#transition-images-wrapper").hide();
		$("#tour").hide();
		$("#form-slider").hide();
	}

	// GOOGLE MAP
	
	$("#google-map-button").click(function() {
		$("#google-map").show();
		initialize();
		$("#transition-images-wrapper").hide();
		$("#tour").hide();
		$("#form-slider").hide();
	});


	
	// FORMs SLIDER

	var forms_slider_top = px2int($("#form-slider").css("top"));

	$("#join-us-button, #book-table-button, #private-event-enquiry-button").click(function(e) {
		e.stopPropagation();

		// hide all forms and show the correct one
		var form2show = $(this).attr("id").replace("-button", "") + "-form";
		$("#join-us-form, #book-table-form, #private-event-enquiry-form").removeClass("on").hide();
		$("#"+form2show).addClass("on").show();

		// save current position of slider
		var cur_pos = px2int($("#form-slider").css("top"));

		if (cur_pos == forms_slider_top) {
		// the slider is hidden
			var goupto = $(this).attr("title"); // attribute title="" holds the max top value
			$('#form-slider').stop().animate({top: goupto}, 1000);
		} else {
		// the slider is visible
			if ($(this).hasClass("form-tab-on")) {
				// clicked already open tab
				$('#form-slider').stop().animate({top: forms_slider_top}, 1000);
			} else {
				// clicked a different tab
				var goupto = $(this).attr("title"); // attribute title="" holds the max top value
				$('#form-slider').stop().animate({top: goupto}, 1000);
			}	
		}

		// hide all tabs and show the correct one
		$("#join-us-button, #book-table-button, #private-event-enquiry-button").removeClass("form-tab-on");
		$(this).addClass("form-tab-on");
	});
	
	


	// OFFERs SLIDER
	
	var offers_slider_bottom = px2int($("#offers-slider").css("bottom"));
	
	$("#offers-slider .title").click(function(e) {
		//don't pop up - just go to special offers page
		return;
		e.stopPropagation();
		var cur_pos = px2int($("#offers-slider").css("bottom"));
		if (cur_pos < 0) {
			$("#offers-slider").stop().animate({bottom: 0}, 1000);
		} else {
			$("#offers-slider").stop().animate({bottom: offers_slider_bottom}, 1000);
		}
	});
	
	$("#offers-slider .offer").not(":first").hide();
	
	$("#offers-slider .next").click(function() {
		scroll_offer(1);
	});
	
	$("#offers-slider .prev").click(function() {
		scroll_offer(-1);
	});
	
	
	function scroll_offer(dir) {
		var visible_index = 0;
		$("#offers-slider .offer").each(function(i) {
			if ($(this).is(":visible")) {
				visible_index = i;
			}
		});
		
		$("#offers-slider .offer").eq(visible_index).fadeOut(500, function() {
			visible_index += dir;
			if (visible_index < 0) {
				visible_index = $("#offers-slider .offer").size() - 1;
			} else if (visible_index >= $("#offers-slider .offer").size()) {
				visible_index = 0;
			}
			
			$("#offers-slider .offer").eq(visible_index).fadeIn(500);
		});
		
		
		
	}
	
	
	
	
	
	/* Hide all popups when clicked outside */
	
	$("#form-slider, #offers-slider").click(function(e) {
		e.stopPropagation();
	})
	
	$("html").click(function(e) {
		$("#form-slider").stop().animate({top: forms_slider_top}, 1000);
		//$("#offers-slider").stop().animate({bottom: offers_slider_bottom}, 1000);
	});
	
	
	
	
	
	// MENUs
	
	$(".menu_table").not(":first").hide();
	
	
	var menu_subnav = [];
	$(".menu_table").each(function() {
		menu_subnav.push($(this).find(".menu_category_cell").text());
	});
	
	for (i = 0; i < menu_subnav.length; i++) {
		if (i == (menu_subnav.length - 1)) {
			$("#menu-subnav").append('<div class="submenu" title="' + i + '">' + menu_subnav[i] + '</div>');
		} else {
			$("#menu-subnav").append('<div class="submenu" title="' + i + '">' + menu_subnav[i] + ' | </div>');
		}
	}
	
	$("#menu-subnav .submenu").live("click", function() {
		var s = px2int($(this).attr("title"));
		$(".menu_table").hide();
		$(".menu_table").eq(s).show();
	});
	

	$('a.bookofferbtn').fancybox({
		'type':'iframe',
		'hideOnContentClick':true,
		'width':325,
		'height':375,
		'scrolling':'no'
	});
})

