var defaultValues = Array();
$(document).ready(function(){
	var nextid = 1;
	$('.clearonfocus').each(function(){
		if ($(this).attr('id')=='')
			$(this).attr('id','clearonfocus_'+nextid);
		defaultValues[$(this).attr('id')] = $(this).val();
		nextid++;
	});
	
	$('.clearonfocus').focus(function(){
		if ($(this).val()==defaultValues[$(this).attr('id')]){
			$(this).val('');
		}
	});
	$('.clearonfocus').blur(function(){
		if ($(this).val()==''){
			$(this).val(defaultValues[$(this).attr('id')]);
		}
	});
	
	$('a').focus(function(){
		$(this).blur();
	});
	
	$("#search a").click(function(event){
		event.preventDefault();
		document.location = gv_baseurl+'/katalog/szukaj/fraza/'+$("#search_input").val();
	});
	
	$('.fancybox').fancybox({
		titlePosition : 'over'
	});
	
	$(".addtocart").click(function(event){
		event.preventDefault();
		var id = $(this).prev().children(':first').attr('id').substr(7);
		var count = $(this).prev().children(':first').val();
		//alert(count)
		ajaxCart.add(id,count);
	});
	
	$('#dialoglink').fancybox();
	
	$(".changeincart").click(function(event){
		event.preventDefault();
		var productIndex = $(this).parent().children(":first").attr('id').substring(3);
		var productCount = $(this).parent().children(":first option:selected").val();
		$.ajax({
			url: gv_baseurl+'/ajax/cart/do/setcount/id/'+productIndex+'/count/'+productCount,
			success: function(response){
				$("#dialog p").html('Liczba produków w koszyku została zmieniona.');
				$('#dialoglink').trigger('click');
			}
		});
	});
	
	$('.removefromcart').click(function(event){
		event.preventDefault();
		var productIndex = $(this).parent().children(":first").attr('id').substring(3);
		var productCount = $(this).parent().children(":first option:selected").val();
		$.ajax({
			url: gv_baseurl+'/ajax/cart/do/remove/id/'+productIndex+'/count/'+productCount,
			success:function(response){
				$("#dialog p").html('Produkt został usunięty z koszyka.');
				needReload = true;
				$('#dialoglink').trigger('click');
			}
		});
	});
	
	$("#order_shipment").change(function(){
		var totalPrice = $(this).attr('rel')*1;
		var shipmentPrice = $("#order_shipment option:selected").attr('rel')*1;
		$("#order_totalcount").html((totalPrice+shipmentPrice)+" PLN");
	});
});

function closeFancy()
{
	$.fancybox.close();
	if (needReload){
		document.location = document.location;
	}
}

function showOrderDetails(orderId)
{
	$.ajax({
		url: gv_baseurl+'/zamowienie/szczegoly/id/'+orderId,
		success: function(response){
			$("#dialog p").html(response);
			$("#dialoglink").trigger('click');
		}
	});
}

