/* Author: Kevin Smith

*/
// ---------------- Zeitanzeige -----------------------------
function updateClock ( )
{
  var currentTime = new Date ( );
  
  var weekday = new Array(7);
  weekday[1] = "Montag";
  weekday[2] = "Dienstag";
  weekday[3] = "Mittwoch";
  weekday[4] = "Donnerstag";
  weekday[5] = "Freitag";
  weekday[6] = "Samstag";
  weekday[0] = "Sonntag";
  var currentDay = currentTime.getDay ( );
  var currentHours = currentTime.getHours ( );
  var currentMinutes = currentTime.getMinutes ( );
  var currentSeconds = currentTime.getSeconds ( );

  // Pad the minutes and seconds with leading zeros, if required
  currentMinutes = ( currentMinutes < 10 ? "0" : "" ) + currentMinutes;
  currentSeconds = ( currentSeconds < 10 ? "0" : "" ) + currentSeconds;

  // Compose the string for display
  var currentTimeString = weekday[currentTime.getDay ( )] + ", " + currentHours + ":" + currentMinutes + ":" + currentSeconds + " Uhr";
  
  var currentStatusString = "geschlossen";  

  if (currentHours >= 11 && currentHours < 22 && currentDay != 0) {
    currentStatusString = "geöffnet";
  } else if (currentHours >= 17 && currentHours <= 22 && currentDay == 0) {
	currentStatusString = "geöffnet";
  };

  // Update the time display
  document.getElementById("clock").firstChild.nodeValue = currentTimeString;
  document.getElementById("status").firstChild.nodeValue = currentStatusString;
};
// ---------------- Ende Zeitanzeige ------------------------
	
// ---------------- Wechselnde Gerichte ---------------------
$.getJSON('js/gerichte.json', function(data) {
	$.each(data.gerichte, function(i,data){
		if(i == 0) { 
			var div_data ="<li class=\"show\"><h3>"+data.name+"</h3><p>"+data.zutaten+" <span class=\"gericht_preis\">"+data.preis+"</span></p></li>";
		} else {
			var div_data ="<li class=\"hidden\"><h3>"+data.name+"</h3><p>"+data.zutaten+" <span class=\"gericht_preis\">"+data.preis+"</span></p></li>";
		}	
		$(div_data).appendTo("#gerichtrotation");
	});
});

rotate_gerichte();
function rotate_gerichte() {
	var element = $("#gerichtrotation .show"); 
	
	element.css({opacity: 1.0}).animate({opacity:0.0}, 600, function() {
		var next = element.removeClass('show').addClass('hidden').next();
		if (next.size() === 1) {
			next.css({opacity: 0.0}).removeClass('hidden').addClass('show').animate({opacity:1.0}, 1500);
		} else {
			$("#gerichtrotation li:first-child").css({opacity: 0.0}).removeClass('hidden').addClass('show').animate({opacity:1.0}, 1500);
		}
	});
	setTimeout("rotate_gerichte()", 8000);
}
// ---------------- Ende wechselnde Gerichte ----------------

//  ---------------- AJAX Formular --------------------------
$('#contactform').submit(function(){

	var action = $(this).attr('action');
	
	$("#message").slideUp(750,function() {
	$('#message').hide();
	
		$('#submit')
		.after('<img src="assets/ajax-loader.gif" class="loader" />')
		.attr('disabled','disabled');
	
	$.post(action, { 
		name: $('#name').val(),
		email: $('#email').val(),
		phone: $('#phone').val(),
		subject: $('#subject').val(),
		comments: $('#comments').val(),
		verify: $('#verify').val()
	},
		function(data){
			document.getElementById('message').innerHTML = data;
			$('#message').slideDown('slow');
			$('#contactform img.loader').fadeOut('slow',function(){$(this).remove()});
			$('#contactform #submit').attr('disabled',''); 
			if(data.match('success') != null) $('#contactform').slideUp('slow');
			
		}
	);
	
	});
	
	return false; 

});
//  ---------------- Ende AJAX Formular ---------------------

// ---------------- Bildrotation ----------------------------
theRotator();
$('div.rotator').fadeIn(1000);
$('div.rotator ul li').fadeIn(1000); // für den IE

function theRotator() {
	//Set the opacity of all images to 0
	$('div.rotator ul li').css({opacity: 0.0});
	
	//Get the first image and display it (gets set to full opacity)
	$('div.rotator ul li:first').css({opacity: 1.0});
		
	//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
	
	setInterval('rotate()',6000);
	
};

function rotate() {	
	//Get the first image
	var current = ($('div.rotator ul li.show')?  $('div.rotator ul li.show') : $('div.rotator ul li:first'));

	if ( current.length == 0 ) {
		current = $('div.rotator ul li:first');
	}

	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div.rotator ul li:first') :current.next()) : $('div.rotator ul li:first'));

	//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
	
};
//  ---------------- Ende Bildrotation ----------------------

// ---------------- Lightbox --------------------------------

	$('.lightbox').lightBox();

// ---------------- Ende Lightbox ---------------------------
