// Copyright 2011 by China Restaurant Jade
// By Felix Duong
// custom.js
// Tested JQuery-Version: 1.4.2

function AppPhotoShow_Description() {
	$('#AppPhotoShow_Description').html('').append(this.alt);
}
function showApps(name) {
var nr =  parseInt(name);

	$("#AppMenuCart").hide();
	$("#AppFeedback").hide();
	$("#AppPhotoShow").hide();
	$("#AppGoogleMaps").hide();
	$("#AppPhotoShow_Aboutus").hide();

	if(nr==14) {
		$("#AppPhotoShow").show();
	}
	else if(nr==18) {
		$("#AppGoogleMaps").show();
	}
	else if(nr==10) {
		$("#AppFeedback").show();
	}
	else if(nr==29) {
		$("#AppMenuCart").show();
	}
	else if(nr==13) {
		$("#AppPhotoShow_Aboutus").show();
	}
}

function AppPhotoShow_Menu() {
//Get our elements for faster access and set overlay width
    var div = $('div.AppPhotoShow_Menu'),
                 ul = $('ul.AppPhotoShow_Menu'),
                 // unordered list's left margin
                 ulPadding = 15;

    //Get menu width
    var divWidth = div.width();

    //Remove scrollbars
    div.css({overflow: 'hidden'});

    //Find last image container
    var lastLi = ul.find('li:last-child');

    //When user move mouse over menu
    div.mousemove(function(e){
      //As images are loaded ul width increases,
      //so we recalculate it each time
      var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;

      var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
      div.scrollLeft(left);
    });	
}
function init() {
	// Initialize Website
	// Set default-value

	// Hide No-Javascript Message
	$("#siteNoJavascript").css("display","none");
	// Hide all Site Apps
	$("#AppPhotoShow, #AppGoogleMaps, #AppFeedback, #AppMenuCart").css("display","block");
	// Set AppMenuCart Pager to 0 because Javascript is enabled
	$('#AppMenuCart_Pager').html("");
	// Initialize Slideshow to AppMenuCart Apps
	$('#AppMenuCart_Cart').cycle({fx:'scrollRight',speed:'fast',timeout: 0,pager:'#AppMenuCart_Pager'});
	// Initialize Slideshow to AppPhotoShow_AboutusPicture
	$('#AppPhotoShow_AboutusPicture').cycle({fx:'fade', speed: 2500});
	// Initialize site Slideshow
	$('#siteContentphotoexample').cycle({fx:'fade', speed: 2500});
	// Load Google Maps
	$("#AppGoogleMaps_map").googleMap({
		zoomLevel: 13,
		center: 'Cusanusstrasse 26, 54470 Bernkastel-Kues, Deutschland',
		tooltip: 'China Restaurant Jade<br>Cusanusstra&szlig;e 26<br>54470 Bernkastel-Kues<br>Tel.: +49 6531 2838<br>Fax: +49 6531 970270',
		startstreet: '#AppGoogleMaps_startstreet',
		startcity: '#AppGoogleMaps_startcity',
		startcountry: '#AppGoogleMaps_startcountry',
		end: '#AppGoogleMaps_end',
		submit: '#AppGoogleMaps_getdirections',
		directions: '#AppGoogleMaps_directions'
		}).load();
}

function LoadEffects(e) {
	$("#siteContentphotoexample").cycle('resume');
	$("#siteContent").hide().delay(300).slideDown("slow");
	e.preventDefault();
}

$(document).ready(function() {
	var language_id = 0;

	if ($.getURLParam("lang") != null) {
               	language_id = $.getURLParam("lang");
        }

	init();
	AppPhotoShow_Menu();
	showApps(0);

	// Set click-Function to some div's
	$("#siteNavigationbar a,#siteFooterContentlinks a, #siteInfo a").click(function(e){
		var nr = parseInt($(this).attr("name"));
		LoadEffects(e);
		showApps(nr);
		$.post("php/xml.php", {lang: language_id, name: $(this).attr("name")}, function(data) {
			$("#siteContenttitle").html($("titel", data).text());
			$("#siteContenttitle").css("background","url("+$("titelicon", data).text()+") left no-repeat");
			if(nr==13) {
				$("#siteContentphotoexample").cycle('pause');
				$("#siteContentText").html("");
				$("#AppPhotoShow_AboutusText").html($("text", data).text());
			}
			else {
				$("#siteContentText").html($("text", data).text());
			}
		});
		
	});

	$("#siteContentphotoexample, #AppPhotoShow_PhotoContainer, #siteContentmenucart").click(function(e){
		LoadEffects(e);			
		$(this).cycle('pause');
		$("#AppPhotoShow_Navigation").show();
		showApps($(this).attr("name"));
		$.post("php/xml.php", {lang: language_id, name: $(this).attr("name")}, function(data) {
			$("#siteContenttitle").html($("titel", data).text());
			$("#siteContenttitle").css("background","url("+$("titelicon", data).text()+") left no-repeat");
			$("#siteContentText").html($("text", data).text());
			$("#AppPhotoShow_PhotoContainer").html($("menubeispiele", data).text());
			$("#AppPhotoShow_PhotoContainer").cycle({fx:'fade', speed: 2500, after: AppPhotoShow_Description, next: '#AppPhotoShow_Navigationright',prev: '#AppPhotoShow_Navigationleft'});
		});
	});	


	$("ul.AppPhotoShow_Menu a").click(function(e) {
		$("#AppPhotoShow_PhotoContainer").cycle('pause');
		$("#AppPhotoShow_Navigation").hide();
		e.preventDefault();
		$.post("php/xml.php", {lang: language_id, bildnr: $(this).attr("name")}, function(data) {
			$("#AppPhotoShow_PhotoContainer").html("<img src="+$("photos", data).text()+">");
			$("#AppPhotoShow_Description").html($("description", data).text());
		});
	});

	$("#AppFeedback_submit").click(function(e) {
		LoadEffects(e);
		$.post("feedback.php", {name: $("#AppFeedback_inputs input").val(),nachricht: $("#AppFeedback_inputs textarea").val()}, function(data) {
			$("#AppFeedback").html(data);
		});
	});
		
 });
