var currentImage=1;
var video;
var slideshowInterval;
var player;
var slide = 1;
var activeSlide = 1;
var max = 8;
var changeInterval = 5000;
var changeSpeed = 1000;
var images = Array();
var i = 0;

$(window).resize(function() {
	resize();
	resize();
});

jQuery.fn.center = function () {
    this.css("top", ( $(window).height() - this.height() ) / 2 + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2 + "px");
    return this;
};

$(document).ready(function() {
	$('#open_seasons').click(function() {
		if($('#seasons').hasClass('seasons_open')) {
			$('#seasons').removeClass('seasons_open');
			$('#seasons').hide(500);
		} else {
			$('#seasons').addClass('seasons_open');
			$('#seasons').show(500);
		}
	});
	$('#preview').click(function(e) {
		$(this).parent().attr('target','preview');
		$('#ispreview').val('1');
	});	
	$('#send_newsletter').click(function(e) {
		$(this).parent().attr('target','');
		$('#ispreview').val('0');
	});	
	var j;
	for (i = 0; i < max; i++) {
		images[i] = new Image();
		images[i].src = "assets/images/slide" + (i + 1) + ".jpg";
		j=i;
	}
	images[j] = new Image();
	images[j++].src = BASEURL+'templates/'+TEMPLATE+'/img/fastfood_duell_ro.png';
	images[j] = new Image();
	images[j++].src = BASEURL+'templates/'+TEMPLATE+'/img/nyny_small_ro.png';
	images[j] = new Image();
	images[j++].src = BASEURL+'templates/'+TEMPLATE+'/img/btn_saisonkarte_sommer_ro.png';
	
	$(window).scroll(function() {
		check_scroller();
		check_warenkorb_slider();
	});
	$('.add_to_cart').click(
			function(e) {
				e.preventDefault();
				$.ajax( {
					url : 'ajax.php?action=add_to_cart&nr=' + $(this)
							.parent().children('input').val(),
					success : function(data) {
						$('#warenkorb_content').html(data);
						cart_funtionality();
					}
				});
			});
	cart_funtionality();
	resize();
	slideshowInterval = setInterval("slideshow()", changeInterval);
	$('#send_newsletter').click(function(e) {		
		if(confirm('Wirklich an '+$('input[name="count"]').val()+' Adressen versenden?')) {
			$(this).attr("disabled","disabled");
			$('#info').html("Newsletter wurde versendet.");
		} else {
			e.preventDefault();
		}
	});
	$('#show_recipients').click(function(e) {
		e.preventDefault();
		$('#newsletter_list').show(500);
	});
	$('#date').datepicker({ dateFormat: 'dd.mm.yy' , dayNamesMin: ['So', 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa'], monthNames: ['Januar','Februar','M&auml;rz','April','Mai','Juni','Juli','August','September','Oktober','November','Dezember']});
	$('.rules').click(function(e) {
		e.preventDefault();
		show('<h1>Einverst�ndniserkl�rung</h1><p>Ich stimme damit dem Erhalt des Newsletters der Mamma Mia GmbH zu. Der Newsletter wird per E-Mail verschickt und enth�lt Informationen �ber neue Produkte und Angebote. Wir werden Ihre personenbezogenen Daten, die wir f�r den Versand des Newsletters verarbeiten, nicht Dritten zur Verf�gung stellen. Sie k�nnen den Erhalt des Newsletters jederzeit mit Wirkung f�r die Zukunft abbestellen, per E-Mail [info@mamma-mia.de], telefonisch [Tel.: 0441-15470] oder postalisch.</p>');
	});
	$('#fade_to_black,#popup').click(function(e) {
		e.preventDefault();
		fadeout();
	});
	$('#videolink').mouseenter(function() {
		$(this).attr('src',BASEURL+'templates/'+TEMPLATE+'/img/fastfood_duell_ro.png');
	});
	$('#videolink').mouseleave(function() {
		$(this).attr('src',BASEURL+'templates/'+TEMPLATE+'/img/fastfood_duell.png');
	});
	$('#gaestebuchlink').mouseenter(function() {
		$(this).children('img').attr('src',BASEURL+'templates/'+TEMPLATE+'/img/gaestebuch_ro.png');
	});
	$('#gaestebuchlink').mouseleave(function() {
		$(this).children('img').attr('src',BASEURL+'templates/'+TEMPLATE+'/img/gaestebuch.png');
	});
	$('#nyny_link img').mouseenter(function() {
		$(this).attr('src',BASEURL+'templates/'+TEMPLATE+'/img/nyny_small_ro.png');
	});
	$('#nyny_link img').mouseleave(function() {
		$(this).attr('src',BASEURL+'templates/'+TEMPLATE+'/img/nyny_small.png');
	});
	$('#open_seasons img').mouseenter(function() {
		$(this).attr('src',BASEURL+'templates/'+TEMPLATE+'/img/btn_saisonkarten_ro.png');
	});
	$('#open_seasons img').mouseleave(function() {
		$(this).attr('src',BASEURL+'templates/'+TEMPLATE+'/img/btn_saisonkarten.png');
	});
	$('#valentines img').mouseenter(function() {
		$(this).attr('src',BASEURL+'templates/'+TEMPLATE+'/img/btn_valentinsmenue_ro.png');
	});
	$('#valentines img').mouseleave(function() {
		$(this).attr('src',BASEURL+'templates/'+TEMPLATE+'/img/btn_valentinsmenue.png');
	});
	$('#easter img').mouseenter(function() {
		$(this).attr('src',BASEURL+'templates/'+TEMPLATE+'/img/btn_osterkarte_ro.png');
	});
	$('#easter img').mouseleave(function() {
		$(this).attr('src',BASEURL+'templates/'+TEMPLATE+'/img/btn_osterkarte.png');
	});
	$('#christmas img').mouseenter(function() {
		$(this).attr('src',BASEURL+'templates/'+TEMPLATE+'/img/btn_ws_ro.png');
	});
	$('#christmas img').mouseleave(function() {
		$(this).attr('src',BASEURL+'templates/'+TEMPLATE+'/img/btn_ws.png');
	});
	$('#videolink').click(function() {
		clearInterval(slideshowInterval);
		fadeToBlack();
		$('#video').css('display','block');
		$('#video').fadeIn(500);
		$('#video').center();		
//		video=$('#video').html();
		if(player==undefined) {
//			debug("videojs anschmei�");
			player=VideoJS.setup("example_video_1");			
		}
		player.play();
//		show('<!-- Begin VideoJS -->'+
//  '<div class="video-js-box">'+
//    '<!-- Using the Video for Everybody Embed Code http://camendesign.com/code/video_for_everybody -->'+
//    '<video id="example_video_1" class="video-js" width="640" height="264" controls="controls" preload="auto" poster="http://video-js.zencoder.com/oceans-clip.png">'+
//      '<source src="http://video-js.zencoder.com/oceans-clip.mp4" type=\'video/mp4; codecs="avc1.42E01E, mp4a.40.2"\' />'+
//      '<source src="http://video-js.zencoder.com/oceans-clip.webm" type=\'video/webm; codecs="vp8, vorbis"\' />'+
//      '<source src="http://video-js.zencoder.com/oceans-clip.ogv" type=\'video/ogg; codecs="theora, vorbis"\' />'+
//      '<!-- Flash Fallback. Use any flash video player here. Make sure to keep the vjs-flash-fallback class. -->'+
//      '<object id="flash_fallback_1" class="vjs-flash-fallback" width="640" height="264" type="application/x-shockwave-flash"'+
//        'data="http://releases.flowplayer.org/swf/flowplayer-3.2.1.swf">'+
//        '<param name="movie" value="http://releases.flowplayer.org/swf/flowplayer-3.2.1.swf" />'+
//        '<param name="allowfullscreen" value="true" />'+
//        '<param name="flashvars" value=\'config={"playlist":["http://video-js.zencoder.com/oceans-clip.png", {"url": "http://video-js.zencoder.com/oceans-clip.mp4","autoPlay":false,"autoBuffering":true}]}\' />'+
//        '<!-- Image Fallback. Typically the same as the poster image. -->'+
//        '<img src="http://video-js.zencoder.com/oceans-clip.png" width="640" height="264" alt="Poster Image"'+
//          'title="No video playback capabilities." />'+
//      '</object>'+
//    '</video>'+
//    '<!-- Download links provided for devices that can\'t play video in the browser. -->'+
//    '<p class="vjs-no-video"><strong>Download Video:</strong>'+
//      '<a href="http://video-js.zencoder.com/oceans-clip.mp4">MP4</a>,'+
//      '<a href="http://video-js.zencoder.com/oceans-clip.webm">WebM</a>,'+
//      '<a href="http://video-js.zencoder.com/oceans-clip.ogv">Ogg</a><br>'+
//      '<!-- Support VideoJS by keeping this link. -->'+
//      '<a href="http://videojs.com">HTML5 Video Player</a> by VideoJS'+
//    '</p>'+
//  '</div>'+
//  '<!-- End VideoJS -->',640);
//		player=VideoJS.setup("example_video_1");
	});
	$('.hasImage').mouseleave(function() {
		$('#img_slider'+currentImage).stop(true,true);
		$('#img_slider'+currentImage).fadeOut(500);
	});
	$('.hasImage').mouseenter(function() {
		currentImage=currentImage==1?2:1;
		$('#img_slider'+currentImage).attr('src',BASEURL+'assets/uploads/'+$('#karte_table').val()+'_'+$(this).attr('id').substring(4)+'.jpg');
	
		var pos=$(this).position();
		$('#img_slider'+currentImage).css('top',pos.top+"px");
		setTimeout('moveImage("'+$(this).attr('id')+'")',50);
	});
});


function moveImage(nr) {
	if($('#img_slider'+currentImage).height()>0) {
		var pos=$('#'+nr).position();
		$('#img_slider'+currentImage).css('top',($('#'+nr).height()/2)+pos.top-($('#img_slider'+currentImage).height()/2)+"px");
		$('#img_slider'+currentImage).stop(true,true);
		$('#img_slider'+currentImage).fadeIn(500);
	} else setTimeout('moveImage("'+nr+'")',50);
}

	

function show(text,width) {
	if(width==undefined) width=400;
	$('#popup').html(text);
	$('#popup').fadeTo(500,100);
	$('#popup').css("left",$(document).width()/2-(width/2)+'px');
	$('#popup').css("width",width+'px');
	fadeToBlack();
}

function fadeToBlack() {
	$('#fade_to_black').css("width",$(document).width()<800?800:$(document).width());
	$('#fade_to_black').css("height",$(document).height()<800?800:$(document).height());
	$('#fade_to_black').css("opacity","0");
	$('#fade_to_black').css("display","block");
	$('#fade_to_black').fadeTo("slow",0.9);
}

function unfadeBlack() {
	$('#fade_to_black').fadeTo(1000,0);
}
function fadeout() {
	unfadeBlack();
	$('#video').fadeTo(500,0);
	$('#popup').fadeTo(500,0);
	setTimeout("hide()",500);
}

function hide() {
	$('#fade_to_black').css("display","none");
	$('#popup').css("display","none");
	$('#video').css("display","none");
//	if(video!=undefined) {
//		alert(video);
//		$('#video').html(video);
//	}
	$('#popup').html("");
	if(player!=undefined) document.location=BASEURL;
}

function cart_funtionality() {
	$('.increment_cart').click(
			function(e) {
				e.preventDefault();
				$.ajax( {
					url : 'ajax.php?action=add_to_cart&nr=' + $(this).parent()
							.children('input').val(),
					success : function(data) {
						$('#warenkorb_content').html(data);
						cart_funtionality();
					}
				});
			});
	$('.decrement_cart').click(
			function(e) {
				e.preventDefault();
				$.ajax( {
					url : 'ajax.php?action=remove_from_cart&nr=' + $(this)
							.parent().children('input').val(),
					success : function(data) {
						$('#warenkorb_content').html(data);
						cart_funtionality();
					}
				});
			});
}
function check_warenkorb_slider() {
	if ($('#warenkorb_slider').length == 0) {
		return;
	}
	var push = $('#warenkorb_slider').height() - $(window).height();
	push = push < 0 ? 0 : push+190;
	// $('#warenkorb_slider h1').html("Warenkorb "+$(window).scrollTop()+"
	// "+$('.speisekarte_item').position().top);
	if ($(window).scrollTop()-push >= ($('.speisekarte_item').length==0?$('h2'):$('.speisekarte_item')).position().top) {
		var left = $(window).width() / 2 - 480;
		left = left > 0 ? left : 0;
		$('#warenkorb_slider').css('left', (left + 727) + 'px');
		$('#warenkorb_slider').css('position', 'fixed');
		$('#warenkorb_slider').css('top', (20 - push) + 'px');
		$('#warenkorb_slider').css('right', '');
	} else {
		$('#warenkorb_slider').css('position', 'absolute');
		$('#warenkorb_slider').css('right', '0px');
		$('#warenkorb_slider').css('left', '');
		$('#warenkorb_slider').css('top', ($('.speisekarte_item').length==0?$('h1'):$('.speisekarte_item')).position().top + 'px');
	}
}

function check_scroller() {
	if ($('.scroller').length == 0) {
		return;
	}
	var push = $('.scroller').height() - $(window).height();
	push = push < 0 ? 0 : push+100;
	// $('.scroller h1').html("Warenkorb "+$(window).scrollTop()+"
	// "+$('.speisekarte_item').position().top);
	if ($(window).scrollTop()>=224+push) {
		$('#up').css('display','inline');
		var left = $(window).width() / 2 - 480;
		left = left > 0 ? left : 0;
		$('.scroller').css('left', (left+175) + 'px');
		$('.scroller').css('position', 'fixed');
		$('.scroller').css('top', (20-push) + 'px');
	} else {
		$('#up').css('display','none');
		$('.scroller').css('left', '25px');
		$('.scroller').css('position', '');		
		$('.scroller').css('top', '');
	}
}

function debug(text) {
	if($('#debug').length==0) {
		$('body').append('<div id="debug" style="font-size:16px;background:#000;color:#ff0000;font-weight:bold;position:fixed;top:0;left:0;z-index:100000;"></div>');
	}
	if(text==undefined) {
		$('#debug').html('');		
	} else {
		$('#debug').html($('#debug').html()+text+'<br />');
	}
}

function resize() {
	$('#background').height($(window).height());
	$('#background').width($(window).width());
	$('#background_grad').height($(window).height());
	$('#background_grad').width($(window).width());
	$('#bow').height($(window).height() - 86);
	$('#back').height($(window).height());
	$('#backpos').height($(window).height());
	$('#back').width(960);
	$('#backpos').width(960);
	var left = $(window).width() / 2 - 480;
	left = left > 0 ? left : 0;
	$('#bowpos').css("left", left + "px");
	$('#backpos').css("left", left + "px");
	$('#footer').css("left", left + "px");
	$('#content_holder').css("left", (left + 150) + "px");
	check_warenkorb_slider();
	check_scroller();
}

function slideshow() {
	slide++;
	if (slide > max)
		slide = 1;
	$('#slide' + (activeSlide == 1 ? 2 : 1)).attr('src',
			'assets/images/slide' + slide + '.jpg');
	if (navigator.userAgent.indexOf('MSIE') > -1) {
		$('#slide' + (activeSlide == 1 ? 2 : 1)).show(0);
		$('#slide' + (activeSlide == 1 ? 1 : 2)).hide(0);
	} else {
		$('#slide' + (activeSlide == 1 ? 2 : 1)).fadeTo(changeSpeed, 1);
		$('#slide' + (activeSlide == 1 ? 1 : 2)).fadeTo(changeSpeed, 0);
	}
	activeSlide = activeSlide == 1 ? 2 : 1;
}
