$(document).ready(function(){
	
	//Menu hovers
	$("div.menuHolder").mouseover(function(){
		$(this).addClass("active_hover");
	})
	$("div.menuHolder").mouseout(function(){
		$(this).removeClass("active_hover");
	});	
	
	//Product pic hover with zoom
	
	$("div.productPic").mouseover(function(){
		$(this).addClass("active_pic");
	})
	$("div.productPic").mouseout(function(){
		$(this).removeClass("active_pic");
	});
	
	//Homepage newest and popular	
	$(".show_popular").click(function (event) {		
		$(".indexProducts.popular").show();		
		$(".indexProducts.newest").hide();		
		$(".homepage_popular").show();		
		$(".homepage_newest").hide();		
		event.preventDefault();	   			
	});	
	$(".show_newest").click(function (event) {		
		$(".indexProducts.newest").show();		
		$(".indexProducts.popular").hide();		
		$(".homepage_popular").hide();		
		$(".homepage_newest").show();		
		event.preventDefault();	   			
	});
	
	
	//Minus product
	$(".minus").click(function (event) {
		var parent1=$(this).parent();		
		var quantity = $(".count p",parent1).html();
		var new_quantity=quantity*1-1;
		if (new_quantity>=0)	$(".count p",parent1).html(new_quantity);		
		event.preventDefault();	   		
	});
	
	//Plus product
	$(".plus").click(function (event) {
		var parent1=$(this).parent();		
		var quantity = $(".count p",parent1).html();
		var new_quantity=quantity*1+1;
		if (new_quantity<=30) $(".count p",parent1).html(new_quantity);		
		event.preventDefault();	   		
	});
	
	//Add product to cart action
	$(".toCart").click(function (event) {
		var base_href=$("base").attr("href");
		var parent1=$(this).parent();
		var parent2=parent1.parent();
		var product_id=$("a",this).attr("product_id");		
		var q1=$(".q1",parent2).html(); if (q1==null) var q1=0;
		var q2=$(".q2",parent2).html(); if (q2==null) var q2=0;
		var q3=$(".q3",parent2).html(); if (q3==null) var q3=0;
		
		//Per modifikatorius judam
		var mods= new Array();
		$('.modificator',parent2).each(function(){			
			mods[$(this).attr("modificator_nr")]=$(this).val();
		});		
		if (mods==null) { var mods=null; }
								   	      	       	      	 				      	      	       	      	      	      	      	      	
      	      	
      	if (q1>0 || q2>0 || q3>0) {
	      	$('html').animate({scrollTop:0}, 'slow');
	      	$("#cart").slideUp("slow").load(base_href+",onlycart.1", 
	      			   {
		      			   	product_id: product_id, 
		      			   	q1: q1,
		      			   	q2: q2,
		      			   	q3: q3,
		      			  	'mods[]': mods,
		      			   	action: 'add'
	      			   }).slideDown("slow"); 
	    } else alert('Pasirinkite patiekalo kiekį ir tada spauskite "Į krepšelį"');						
		event.preventDefault();	   		
	});
				
	//Remove product to cart action
	$(".rightProductDelete a").livequery('click', function (event) {		
		var product_id=$(this).attr("product_id");
		var size=$(this).attr("portion_size");				
      	var base_href=$("base").attr("href");  
      	      	      	
      	if (product_id && size) {	      	
	      	$("#cart").fadeOut("slow").load(base_href+",onlycart.1", 
	      			   {
		      			   	product_id: product_id, 
		      			   	size: size,		      			   	
		      			   	action: 'remove'
	      			   }).fadeIn("slow"); 
	    }						
		event.preventDefault();	   		
	});
				
	
	//Vote stars
	//Default:
	var stars=$("#vote").attr("value");
	for (i=1;i<=stars;i++) {
		$("a#vote"+i).addClass("hover");
	}	
	
	$("a#vote1").mouseover(function(){
		$(this).addClass("hover");
		$("a#vote2").removeClass("hover");
		$("a#vote3").removeClass("hover");
		$("a#vote4").removeClass("hover");
		$("a#vote5").removeClass("hover");
		$("#vote").attr({value : '1'});
	})
	$("a#vote2").mouseover(function(){
		$("a#vote1").addClass("hover");		
		$(this).addClass("hover");
		$("a#vote3").removeClass("hover");
		$("a#vote4").removeClass("hover");
		$("a#vote5").removeClass("hover");
		$("#vote").attr({value : '2'});
	})
	$("a#vote3").mouseover(function(){
		$("a#vote1").addClass("hover");
		$("a#vote2").addClass("hover");
		$(this).addClass("hover");		
		$("a#vote4").removeClass("hover");
		$("a#vote5").removeClass("hover");
		$("#vote").attr({value : '3'});
	})
	$("a#vote4").mouseover(function(){
		$("a#vote1").addClass("hover");
		$("a#vote2").addClass("hover");
		$("a#vote3").addClass("hover");
		$(this).addClass("hover");		
		$("a#vote5").removeClass("hover");
		$("#vote").attr({value : '4'});
	})
	$("a#vote5").mouseover(function(){
		$("a#vote1").addClass("hover");
		$("a#vote2").addClass("hover");
		$("a#vote3").addClass("hover");
		$("a#vote4").addClass("hover");
		$("#vote").attr({value : '5'});
		$(this).addClass("hover");
	})
	$("a.vote_link").click(function(event){
		event.preventDefault();
	});
	/*
	$("a.vote_link").mouseout(function(){
		$("a.vote_link").removeClass("hover");
	});
	*/
	
	
	
	//Show/hide block
	$(".link").toggle(function() {		
		var parent1=$(this).parent();
		$(this).removeClass('link');
        $(this).addClass('nolink');
        $(".togleblock",parent1).show();
		event.preventDefault();
	}, function() {
		var parent1=$(this).parent();
		$(this).removeClass('nolink');
        $(this).addClass('link');
        $(".togleblock",parent1).hide();
		event.preventDefault();
	});
	
	//Show/hide block
	$(".nolink").toggle(function() {		
		$(this).removeClass('nolink');
        $(this).addClass('link');
        $(".togleblock").hide();		
		event.preventDefault();
	}, function() {
		$(this).removeClass('link');
        $(this).addClass('nolink');
        $(".togleblock").show();
		event.preventDefault();
	});
	
	
	//Show password reminder form	
	$(".remind_password_link").click(function (event) {
		$(".register_form").hide();
		$(".reminder_form").show();		
		event.preventDefault();
	});
	
	
	//Change between delivery block and takeaway block
	$(".show_takeaway").click(function (event) {
		$(".delivery_active").hide();
		$(".takeaway_active").show();
		$(".delivery_form").hide();
		$(".takeaway_form").show();			
		event.preventDefault();
	});	
	$(".show_delivery").click(function (event) {
		$(".takeaway_active").hide();
		$(".delivery_active").show();
		$(".delivery_form").show();
		$(".takeaway_form").hide();
		event.preventDefault();
	});
			
});


//AUTOCOMPLETE FUNCTIONS
function lookup(inputString) {
	if(inputString.length == 0) {
		// Hide the suggestion box.
		$('#suggestions').hide();
	} else {
		$.post("src/pages/streets.php", {queryString: ""+inputString+""}, function(data){
			if(data.length >0) {
				$('#suggestions').show();
				$('#autoSuggestionsList').html(data);
			}
		});
	}
} // lookup

function fill(thisValue) {
	$('#inputString').val(thisValue);
	setTimeout("$('#suggestions').hide();", 200);
}

function startBannerCarousel(){
    bannerTimer = null;
    activeBanner = 0;
    bannerCount = $("#flash .carouselItem").length;
    if (bannerCount > 1) {
        bannerTimer = setInterval("rotateBannerCarousel()", 5000);
        $("#switcher a").click(function(e){
            e.preventDefault();
            stopBannerCarousel();
            skipBannerCarousel($("#switcher a").index(this));
            bannerTimer = setInterval("rotateBannerCarousel()", 5000);
        });
    }
}

function stopBannerCarousel(){
    if (bannerTimer === null) {
        return;
    }
    clearInterval(bannerTimer);
    bannerTimer = null;
}

function skipBannerCarousel(i){
    $("#flash .carouselItem:eq(" + activeBanner + ")").removeClass("active");
    $("#switcher a:eq(" + activeBanner + ")").removeClass("active");
    activeBanner = i;
    $("#flash .carouselItem:eq(" + activeBanner + ")").addClass("active");
    $("#switcher a:eq(" + activeBanner + ")").addClass("active");
}

function rotateBannerCarousel(){
    $("#flash .carouselItem:eq(" + activeBanner + ")").removeClass("active");
    $("#switcher a:eq(" + activeBanner + ")").removeClass("active");
    activeBanner++;
    if (activeBanner >= bannerCount) {
        activeBanner = 0;
    }
    $("#flash .carouselItem:eq(" + activeBanner + ")").addClass("active");
    $("#switcher a:eq(" + activeBanner + ")").addClass("active");
}

function startDishList(){
    dishListCount = $("#days-offer-holder .days-offer-tabs a").length;
    if (dishListCount > 1) {
        $("#days-offer-holder .days-offer-tabs a").click(function(e){
            e.preventDefault();
            changeDishList($("#days-offer-holder .days-offer-tabs a").index(this));
        });
    }
}

function changeDishList(i){
	$("#days-offer-holder .days-offer-info").hide();
    $("#days-offer-holder .days-offer-tabs a").removeClass("active");
    $("#days-offer-holder .days-offer-info:eq(" + i + ")").show();
    $("#days-offer-holder .days-offer-tabs a:eq(" + i + ")").addClass("active");
}

function startDishCarousel(){
    dishTimer = null;
    activeDish = 0;
    dishCount = $("#productsList .right .day-offer-holder .day-offer-top").length;
    if (dishCount > 1) {
    	dishTimer = setInterval("rotateDishCarousel()", 5000);
    }
}

function rotateDishCarousel(){
    $("#productsList .right .day-offer-holder .day-offer-top:eq(" + activeDish + ")").hide();
    $("#productsList .right .day-offer-holder .day-offer-middle:eq(" + activeDish + ")").hide();
    activeDish++;
    if (activeDish >= dishCount) {
    	activeDish = 0;
    }
    $("#productsList .right .day-offer-holder .day-offer-top:eq(" + activeDish + ")").show();
    $("#productsList .right .day-offer-holder .day-offer-middle:eq(" + activeDish + ")").show();
}

maxHeight = 0;

$(function(){
	$('#productsList .right .day-offer-holder .day-offer-middle')
	    .each(function() { maxHeight = Math.max(maxHeight, $(this).height()); })
	    .height(maxHeight);
	
    startBannerCarousel();
    startDishList();
    startDishCarousel();
    $('form#subscribeform').submit(function(e){
        e.preventDefault();
        $.ajax({
            cache: false,
            dataType: 'json',
            data: $(this).serialize(),
            type: $(this).attr('method') || 'POST',
            url: $(this).attr('action'),
            beforeSend: function(){
                $('form#subscribeform input').attr('disabled', 'disabled');
            },
            error: function(){
                $('form#subscribeform input').removeAttr('disabled');
                alert('Error!');
            },
            success: function(response){
                if (response.complete) {
                    alert(response.message);
                }
                else {
                    $('form#subscribeform input').removeAttr('disabled');
                    if (!response.valid) {
                        alert(response.message);
                    }
                    else {
                        alert('Error!');
                    }
                }
            }
        });
    });
});

