$(document).ready(function(){
	
	$(".product-thumb-image").click(function() {
		$("#main-image").attr("src", $(this).attr("originalLargeImg"));
		$("#main-image").parents("a").attr("href", $(this).parent("a").attr("href"));
		return false;
	});
	
	
	$("a[rel=lightbox]").lightBox();
	pathway();
	
	//Init inputs
	dvInitInput($('.cosLogin #email'),'Email-ul tau');
	dvInitInput($('.cosLogin #password'),'parola');
	
	$(".select_culori").live("change", function() {
		checkOptions();
	});	
	
	$(".special").change(function() {
		var price = '';
		var oldPrice = '';
		
		if ($("select#addonsSelect option:selected").attr("discounted_price"))
			{
			price = $("select#addonsSelect option:selected").attr("discounted_price");
			oldPrice = $("select#addonsSelect option:selected").attr("price");
			}
		else
			{
			price = $("select#addonsSelect option:selected").attr("price");
			oldPrice = '';
			}
		
		if (oldPrice != '')
			{
			$("#oldpriceContainer").show();
			$("#oldprice").html(oldPrice);
			}
		else
			{
			$("#oldpriceContainer").hide();
			}
		
		$("span#price").html("Pret: " + price);
		$("a.add").attr("href", $("select#addonsSelect option:selected").attr("cartUrl"));
		$("a.add").attr("baseHref", $("select option:selected").attr("cartUrl"));
	});	
	
	$(".checkbox-filter-input").click(function () {
		$("#filter_form").submit();
	});	
	
	$(".filter-input").change(function () {
		$("#filter_form").submit();
	});	
	
	checkOptions();
	cantMinusPlus();
	displayAccountForms();
});

function checkOptions() {
	url = "";
	$(".select_culori").each(function() {
		if ($(this).attr("value") != "")
		{
			url = url + "&" + $(this).attr("name")+"="+$(this).attr("value");
		}
	});			
	
	$(".add_to_cart_button").each(function() {
		oldUrl = $(this).attr("baseHref");
		$(this).attr("href",oldUrl + url);
	});
}

function cantMinusPlus(){
	var loading = false;
	$('.minus').click(function(){ 
							   if (!loading)
							   {
								   loading = true;
								   var txtCantMinusPlus =  $(this).parent('.setCantitate').find('input');
								   if(parseInt(txtCantMinusPlus.val())!=0){
								   var tempCantMinusPlus = parseInt(txtCantMinusPlus.val()) - 1;
								   }
								   else
								   {
									   var tempCantMinusPlus = 1;
								   }
								   txtCantMinusPlus.val(tempCantMinusPlus);
								   updateCart();
							   }
							   });
	$('.plus').click(function(){ 
							   if (!loading)
							   {
								   loading = true;
								   var txtCantMinusPlus =  $(this).parent('.setCantitate').find('span input');
								   if(parseInt(txtCantMinusPlus.val())<99){
									   var tempCantMinusPlus = parseInt(txtCantMinusPlus.val()) + 1;
								   }
								   else
								   {
									   var tempCantMinusPlus = 1;
								   }
								   txtCantMinusPlus.val(tempCantMinusPlus);
								   updateCart();
							   }
							   });
	
	$('.setCantitate input').change(function(){
		 updateCart();
	})
	
}

function updateCart()
{
	string = "/cart/update-async/?";
	$("#quant input").each(function() {
		if ($(this).attr("value") != "")
		{
			string += $(this).attr("name") + "=" +  $(this).attr("value")+"&";
		}
	});
	string +="async=1";
	$.ajax({
		url: string,
		success: function(result){
			if(result != '')
			{
				$("#container").html(result);
				cantMinusPlus();
			}
			else
			{
				window.location = "/";
			}	
		}
	});	
}

function filtru(filtru){
	filtru.hover(
		function(){
				$(this).click(function(){
									   $(this).animate({height:filtru.css('height','auto').height()},100);
										});
    		   	},
		function(){
			
			$(this).animate({height:22},100);
    			  }
	);
	var fitem = filtru.find('a');
	var fselected = filtru.find('strong');
	var xxx = filtru.find('option.selected');
	fitem.click(function(){
						 fselected.text($(this).html());
						 });
}

function pathway (){
	 if(jQuery.browser.msie && jQuery.browser.version == 7.0)
	 {
		var firstpw = $("#firstpw");
		var dymanicW = 0; 
		firstpw.children("a, span").each(function(index){
			dymanicW += $(this).width();
		});
		firstpw.css('width', (dymanicW + 20) + 'px');
		$("#firstpw > .pw").css('margin-left', (dymanicW + 20) + 'px');
	 }	
	
	var pw = $('.pw');
	var pwOver = $('#pwOver');
	var pwPos = pw.position();
	pwOver.css({ 'left': pwPos.left -10, 'top':pwPos.top + 8});
//	pwOver.addClass('over');
	
 pw.hover(
	 function(){pwOver.not('over').addClass('over'); pw.not('over').addClass('over');},
	 function(){pwOver.not('over').addClass('over');}
 );
 
 pwOver.hover(
	 function(){pwOver.not('over').addClass('over');},
	 function(){pwOver.removeClass('over'); pw.removeClass('over');}
 );
}



function displayAccountForms(){
	var currentLocation = new String(window.location);
	if ((jQuery.browser.chrome || jQuery.browser.safari || jQuery.browser.opera || jQuery.browser.msie) 
		 && ( (currentLocation.search("/users/") > -1 && currentLocation.search("/register/") == -1 && currentLocation.search("/add-account/") == -1) || currentLocation.search("/order/") > -1) )
	 {
		$("p.padding").css("width", "21.2%");
		$(".s2 span.s4").css("width", "100%");
		
		$(".formWrapper").css("float","right");
		$(".formWrapper").css("margin-right","225px");
		$(".formWrapper").css("margin-top","-20px");
		
		$("table.orders").css("padding-left","20px");
		
		$("#viewOrder").css("padding-left", "20px");
		$("#viewOrder").css("width", "600px !important");
	 }	
}
