$(document).ready(function(){ 
	//Carousels
	$('#ticker .news').carousel( { loop: true, autoSlide: true, autoSlideInterval: 5000, effect: "fade"} );
	$('#wineselector .carousel > li').not(':first').css({display:"none",opacity:0});
	$('#wineselector .carousel-next').click(function(){
		$('#wineselector .carousel > li').first().animate({opacity:0},300,function(){
			if ($.browser.msie){ this.style.removeAttribute('filter');} 
			$(this).css("display","none").appendTo('#wineselector .carousel');
			$('#wineselector .carousel > li').first().css("display","block").animate({opacity:1},300,function(){
				if ($.browser.msie){ this.style.removeAttribute('filter');}
			});
		});		
	});	
	$('#wineselector .carousel-previous').click(function(){
		$('#wineselector .carousel > li').first().animate({opacity:0},300,function(){
			if ($.browser.msie){ this.style.removeAttribute('filter');}  
			$(this).css("display","none");
			$('#wineselector .carousel > li').last().prependTo('#wineselector .carousel');
			$('#wineselector .carousel > li').first().css("display","block").animate({opacity:1},300,function(){
				if ($.browser.msie){ this.style.removeAttribute('filter');}
			});
		});		
	});	
	// Zebra stripes
	$('.ingredients ul li:odd, .zebra li:odd').addClass('odd');	
	//list filters
	$('#filter-form select, #filter-form input').change(function() {	   
		$('#filter-form').submit();
	});
	//E-card
	$('.ecard-form #q1_0').attr('checked','checked');
	$('.ecard-form #q2').attr('disabled','disabled');
	$('.ecard-form input.preview').attr('disabled','disabled');
	$('.ecard-group li').mouseenter(function(){
		$(this).animate({opacity:1});
	});
	$('.ecard-group li').mouseleave(function(){
		$(this).animate({opacity:0.7});
	});
	var card;
	$('.ecard-group li').click(function() {
		card = {'name': $(this).children('img').attr('title'),'imgUrl':$(this).children('img').attr('src'),'defMsg': $(this).children('.cardMsg').text(),'cardId': $(this).children('.cardId').text()};
		$('#selectedImg').hide().html('<h3>'+ card.name +'</h3><img src="'+ card.imgUrl +'" alt="" />').fadeIn();
		$('#q1_0').next('label').text('Oletusviesti: ' + card.defMsg);
		$('#cardId').val(card.cardId);
		$('.ecard-form input.preview').attr('disabled','');
		
	});
	$('.ecard-form input[name=q1]').change(function() {
		if ($('.ecard-form #q1_1').is(':checked')) {	
			$('.ecard-form #q2').attr('disabled', '').focus();
		}else {
			$('.ecard-form #q2').val('').attr('disabled','disabled');
		}
	});
	$('.ecard-form input.preview').click(function() {
		var parsedImg = card.imgUrl.slice(0, -4);
		parsedImg = parsedImg.replace(/(Fthumb)[0-9]+_/,'');
		var msg;
		if($('.ecard-form #q2').val().length > 0) {
			msg = $('.ecard-form #q2').val();
		}else {
			msg = card.defMsg;
		}
		var sender = $('.ecard-form #q4').val();
		$('<div id="overlay" />').appendTo('body').show('slow');		
		$('<div id="ecard-preview"><div id="closelink">sulje (x)</div><img src="' + parsedImg + '" alt="" /><p>' + msg +'</p><p>t. '+ sender +'</p></div>').appendTo('body');	
	});	
	$('#closelink').live('click',function() {
		$(this).parent().fadeOut('fast', function(){$(this).remove();});
		$('#overlay').fadeOut('fast', function(){$(this).remove();});		
	});
	//Disclaimer
	if(!$.cookie('kv_disclaimer') && $('#nodisclaimer').length === 0) {
		$('<div id="overlay" />').appendTo('body');
		$('<div id="disclaimer" />').appendTo('body');
		var img = new Image();
		$(img).attr({ src: '/files/kuohuviini/img/kuohuviinifi_tunnus.png' }).appendTo('#disclaimer');
		$('<p>Olet siirtymässä Kuohuviini.fi-verkkosivuille. Käyttäessäsi sivustoa yksityisasiakkaana emme voi näyttää sivuillamme yli 22 % alkoholia sisältäviä tuotteita. Kielto koskee kaikkia alkoholialalla Suomessa toimivia yrityksiä. Siirtymällä sivustolle vakuutat olevasi yli 18-vuotias.<br/><br/>Katso sivuston käyttöehdot <a href="/kayttoehdot">tästä</a>.</p>').appendTo('#disclaimer');
		$('<small />').html('Ole hyvä ja anna syntymäaikasi').appendTo('#disclaimer');   
		$('<select id="dd" name="dd" />').appendTo('#disclaimer');
		$('<select id="mm" name="mm" />').appendTo('#disclaimer');
		$('<select id="yy" name="yy" />').appendTo('#disclaimer');    
		$('<option value="">pv</option>').appendTo('#dd');
		for(var i = 1; i <= 31; i++) {
			$('<option value="'+ i +'">'+ i  +'</option>').appendTo('#dd');
		}   
		$('<option value="">kk</option>').appendTo('#mm');
		for(i = 1; i <= 12; i++) {
			$('<option value="'+ i +'">'+ i  +'</option>').appendTo('#mm');
		}   
		$('<option value="">vuosi</option>').appendTo('#yy');
		for(i = new Date().getFullYear(); i >= 1900; i--) {
			$('<option value="'+ i +'">'+ i  +'</option>').appendTo('#yy');
		}    
		$('<input type="button" value="Jatka" class="continue" />').appendTo('#disclaimer');
		$('.continue').click(function() {
			var date = new Date();
			var allow = false;
			if(parseInt($('#yy').val()) == $('#yy').val() && parseInt($('#mm').val()) == $('#mm').val() && parseInt($('#dd').val()) == $('#dd').val()){
				if(parseInt($('#yy').val(),10) < date.getFullYear() - 18) {
					allow = true;
				}else if(parseInt($('#yy').val(),10) == date.getFullYear() - 18) {
					if (parseInt($('#mm').val(),10) < date.getMonth()+1) {
						allow = true;
					}else if(parseInt($('#mm').val(),10) == date.getMonth()+1 && parseInt($('#dd').val(),10) <= date.getDate()){
						allow = true;
					}
				}
			}
			if(allow === true) {
				$('#overlay, #disclaimer').remove();
				$.cookie('kv_disclaimer', 'true', {path: '/', duration: 9999});
			}else {			
				self.location="http://www.wineinmoderation.eu";
			}
		});
	}
	$('a[rel=modal]').modal();
});
//Modal
(function($) {
	$.fn.extend({
		modal: function() {
			function closeModal() {
				$(document).unbind("keydown", handleEscape);
				var remove = function() { 
					$(this).remove().empty(); 
				};
				overlay.fadeOut(remove);
				modalBox.fadeOut(remove);
			}
			function handleEscape(e) {
				//escape
				if (e.keyCode == 27) {
					closeModal();
				}
			}		
			var overlay = $('<div id="overlay" />');
			var modalBox = $('<div id="modal-box" />');
			var closeLink = $('<div id="closelink">sulje (x)</div>');
			return this.each(function() {
				$(this).click(function(e) {
					$('body').append(overlay.fadeIn('fast').click(function() { 
						closeModal(); 
					}));
					e.preventDefault();
					$(document).keydown(handleEscape);
					$('body').append('<div id="modal-load">Lataa...</div>');
					var img = new Image();
					$(img).load(function () {
						var imageWidth = img.width / 2;
						var imageHeight = img.height / 2;
						modalBox.css({
							"width": img.width,
							"margin-left": -imageWidth,
							"margin-top": -imageHeight
						});
						$('#modal-load').remove();
						modalBox.append(img);
						modalBox.append(closeLink.click(function() {
							closeModal();
						}));
						$(this).addClass("modal-image");
						$('body').append(modalBox);
						modalBox.fadeIn('fast');
					})
					.attr({ src: this.href }).click(function() {
						closeModal();
					});
				});
			});
		}
	});
})(jQuery);
$(window).load(function () {
	var tallest = 0;
	$("#wineselector .carousel").children().each(function(){
		var thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	$("#wineselector .carousel").height(tallest);
	$("#wineselector .carousel > li").height(tallest);
});

