$(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);
}



