$().ready(function(){
	// Concept
	$("#lang-select-en").hide();
	$("#lang-select-en").click(function() {
		$("#concept").animate({
			left: "0"
		}, 1000);
		$(this).hide();
		$("#lang-select-cn").fadeIn();
	});
	$("#lang-select-cn").click(function() {
		$("#concept").animate({
			left: "-1000px"
		}, 1000);
		$(this).hide();
		$("#lang-select-en").fadeIn();
	});
	
	// Restaurant Info
	$("#info-1").hide();
	$("#info-2").hide();
	$("#info-1-bg").hide();
	$("#info-2-bg").hide();
	changeRestaurant(1, true);
		
	// Form Modal Window
	var options = { 
        // target:        $('form'),   // target element(s) to be updated with server response 
        beforeSubmit:  modalFormValidate,  // pre-submit callback 
        success:       modalFormResponse  // post-submit callback 

        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 

        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    };
	function modalFormValidate() {
		return $("form.modalForm").valid();
	}
	function modalFormResponse(responseText, statusText) {
		// alert(responseText);
		$('form.modalForm').html(responseText);
		/*$('form').html("<p><strong>Thank You!</strong></p>" + 
			"<p>Thank you for your reservation. <br />" + 
			"Please note that your reservation is not confirmed. <br />" +
			"Please await a confirmation call from our staff.</p>");*/
	}
	var myOpen = function(hash){
		// Animation
		hash.o.css({'opacity': 0, 'background-color': '#fff'}).show().animate({"opacity": 0.50}, "fast");
		hash.w.css('opacity', 0).show().animate({"opacity": 1});
	};
	var myLoad = function(hash){
		// $('form',hash.w).ajaxForm(function() { 
		// 			$('form',hash.w).html("<p><strong>Thank you for your feedback!</strong></p><p>We will attend to your feedback shortly.</p>");
		// 		});
		// 
		jQuery.validator.messages.required = "";
		$("form.modalForm").validate({
			onkeyup: false,
			errorElement: false
		});
		$('form',hash.w).ajaxForm(options);
	}; 
	$('#formDialog').jqm({ajax:'@href', target: $('#formDialog #form-modal-content'), trigger: 'a.showFormDialog', toTop: true, onShow:myOpen, onLoad:myLoad});
	
	// Menu
	$("#arrow-up").hide();
	
	// Menu Modal Window
	var menuOpen = function(hash){
		// console.log (hash.t.href);
		hash.o.css('opacity', 0).show().animate({"opacity": 0.70}, "fast");
		hash.w.css('opacity', 0).show().animate({"opacity": 1});
		$('#modal-nav').load('inc/modal-nav.inc.php', null, initModalNav);
		page = getPageName(hash.t.href);
		$('a.print-friendly').attr('href', 'pdf/' + page + '.pdf');
		$('#modal-content').load(hash.t.href, null, function(){
			$('.scroll-pane', hash.w).jScrollPane({
				dragMinHeight: 0,
				dragMaxHeight: 10
			});
		});
	}
	var menuClose = function(hash) {
		$('.scroll-pane', hash.w).jScrollPaneRemove();
		hash.w.remove();
		hash.o.remove();
	}
	$('#menuDialog').jqm({trigger: 'a.showMenuDialog', toTop: true, onShow:menuOpen});
	
	// Hotpot Modal Window
	var hotpotOpen = function(hash){
		// console.log (hash.t.href);
		hash.o.css('opacity', 0).show().animate({"opacity": 0.85}, "fast");
		hash.w.css('opacity', 0).show().animate({"opacity": 1});
		$('#modal-hotpot-nav').load('inc/modal-hotpot-nav.inc.php', null, initModalNav);
		page = getPageName(hash.t.href);
		$('a.print-friendly').attr('href', 'pdf/' + page + '.pdf');
		$('#modal-hotpot-content').load(hash.t.href, null, function(){
			var $hotpotContent = $('#modal-hotpot-content .scroll-pane', hash.w)
			$hotpotContent.jScrollPane({
				dragMinHeight: 0,
				dragMaxHeight: 10
			});
			$('#modal-hotpot-nav a.scroll-to').bind(
				'click',
				function()
				{
					var targetElementSelectorString = $(this).attr('rel');
					$hotpotContent[0].scrollTo(targetElementSelectorString);
					return false;
				}
			);
		});
	}
	$('#hotpotDialog').jqm({trigger: 'a.showHotpotDialog', toTop: true, onShow:hotpotOpen});
});

var imageCounter = 0;
var imageLimit = -4;
var imageWidth = 1000;
var imageHeight = 600;

function changeImage(num) {
	var counterResult = imageCounter + num;
	var vElement = $("#menu");
	var arrowUp = $("#arrow-up");
	var arrowDown = $("#arrow-down");
	
	if (counterResult <= 0 && counterResult >= imageLimit) {
		imageCounter = counterResult;
		
		var vShiftTo = imageHeight * counterResult;
		vElement.animate ({
			top: vShiftTo
		}, 700);
	}
	// arrowDown.fadeIn("slow");
	arrowDown.show();
	// arrowUp.fadeIn("slow");
	arrowUp.show();
	if (counterResult == 0) {
		// arrowUp.fadeOut("slow");
		arrowUp.hide();
	}
	if (counterResult == imageLimit) {
		// arrowDown.fadeOut("slow");
		arrowDown.hide();
	}
}

function initModalNav () {
	var content = $('#modal-content');
	$('#modal-nav a').click(function(){
		content.empty();
		content.load($(this).attr("href"), null, initDialogScrollPane)
		page = getPageName($(this).attr("href"));
		$('a.print-friendly').attr('href', 'pdf/' + page + '.pdf');
		return false;
	});
}

function getPageName (e) {
	page = e.replace(/.*ajax\/(.*)\.html/, "$1");
	return page;
}

function initDialogScrollPane () {
	$('.scroll-pane').jScrollPane({
		dragMinHeight: 0,
		dragMaxHeight: 10
	});
}

function dialogNewContent (page) {
	$('.menu-overlay-content').empty();
	$('a.print-friendly').attr('href', 'pdf/' + page + '.pdf');
	$('.menu-overlay-content').load('ajax/' + page + '.html', null, initDialogScrollPane)
}

function changeRestaurant(x, y) {
	y |= false;
	// console.log (y);
	
	// Change navigation picture
	var activeButton = "a#select-" + x;
	var activeText = "#info-" + x;
	$(activeButton).addClass("active");
	$(":not(" + activeButton + ")").removeClass("active");
	
	// Change text
	if (y) { // If y == true, no animation 
		$(activeText).show();
		$(activeText + "-bg").show();
	} else {
		$("#info-1").hide();
		$("#info-2").hide();
		$("#info-1-bg").hide();
		$("#info-2-bg").hide();
		$(activeText).fadeIn("fast");
		$(activeText + "-bg").fadeIn("fast");
	}
}

function openPressWindow(pressArticle) {
	pressWinRef = window.open('press/' + pressArticle + '.php',pressArticle,'width=610,height=270,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=no,resizable=yes');
}