var TimerSize = null;

function HoverImg(i) {
    $(".footer-brands img:eq("+i+")").fadeTo(500, 1); 
    setTimeout(' $(".footer-brands img:eq('+i+')").fadeTo(500, 0.35); ',3000);
    i++;
    if (i == TimerSize) { i = 0; }
    setTimeout("HoverImg("+i+")",3000);
}

$(document).ready(function(){
    TimerSize = $(".footer-brands img").size()  
    HoverImg(-1);
    $(".footer-brands img").fadeTo(0, 0.35); 
    $(".footer-brands img").hover(function(){
        $(this).fadeTo(500, 1);
    },function(){
        $(this).fadeTo(500, 0.35); 
    });    
    
	$("#loading").bind("ajaxStart", function(){
		$(this).fadeIn("slow");
	}).bind("ajaxComplete", function(){
		$(this).fadeOut("slow");
	})
	jQuery('#newproducts').jcarousel({
		auto: 2,
		scroll: 4,
		wrap: 'last',
		animation: 2000,
		buttonPrevHTML: '',
		buttonNextHTML: '',
		initCallback: mycarousel_initCallback
	});
	jQuery('#brands').jcarousel({
		auto: 2,
		scroll: 4,
		wrap: 'last',
		animation: 2000,
		buttonPrevHTML: '',
		buttonNextHTML: '',
		initCallback: mycarousel_initCallback
	});
	$("#client_logout").click(function () {
		$.ajax({
			url: "/ajax/client-logout",
			cache: true,
			async: true,
			timeout: 100000,
			beforeSend: function(html){
				$('input').attr("disabled","disabled");
				$("#client_logout").text("Proszę czekać");
			},
			success: function(code){
				code = parseInt(code);
				if (code != 1) {
					alert ('Wystąpił problem z połączeniem...\nOdczekaj chwilę, do kolejnej próby.');
				}
				else{
					document.location.href = document.location.href;
				}
				$("input").attr("disabled","");
				$("#client_logout").text("Wyloguj");
			},
			error: function(){
				alert ('Wystąpił problem z połączeniem...\nOdczekaj chwilę, do kolejnej próby.');
				$("input").attr("disabled","");
				$("#client_logout").text("Wyloguj");
			}
		});
		return false;
	});
	$("#fav-close").click(function () {
		$("#fav").fadeOut("slow");
		return false;
	});
	$("#fav-empty").click(function() {
		$.ajax({
			type: "post",
			url: "/ajax/fav-empty",
			cache: true,
			async: true,
			timeout: 100000,
			beforeSend: function(){
				$("#fav-content").hide();
				$("#fav-content").empty();
			},
			success: function(code){
				$("#fav-content").html(code);
				$("#fav-content").fadeIn("slow");
				$("#fav").fadeIn("slow");
			},
			error: function(){
				$("#fav-content").hide();
				$("#fav-content").empty();
			}
		});
	return false;
	});
	$("#cart-close").click(function () {
		$("#cart").fadeOut("slow");
		return false;
	});
	$("#fav-compare").click(function () {
		document.location.href='/shop/product/compare';
		return false;
	});
	$("#cart-buy").click(function () {
		document.location.href='/user/profil/cart';
		return false;
	});
	$("#cart-empty").click(function() {
		$.ajax({
			type: "post",
			url: "/ajax/cart-empty",
			cache: true,
			async: true,
			timeout: 100000,
			beforeSend: function(){
				$("#cart").hide();
				$("#cart-content").empty();
			},
			success: function(code){
				$("#cart-content").html(code);
				$("#cart-content").fadeIn("slow");
				$("#cart").fadeIn("slow");
			},
			error: function(){
				$("#cart").hide();
				$("#cart-content").empty();
			}
		});
	return false;
	});
});
function fav_add(id) {
	$.ajax({
		type: "post",
		url: "/ajax/fav-add",
		data: "id="+encodeURIComponent(id),
		cache: true,
		async: true,
		timeout: 100000,
		beforeSend: function(){
			$("#fav").hide();
			$("#fav-content").empty();
		},
		success: function(code){
			if (code != "1"){
				$("#fav-content").html(code);
				$("#fav-content").fadeIn("slow");
				$("#fav").fadeIn("slow");
			}
			else	{
				alert ("Nie można dodać produktu do ulubionych...");
			}
		},
		error: function(){
			$("#fav").hide();
			$("#fav-content").empty();
		}
	});
	return false;
}
function fav_remove(id) {
	$.ajax({
		type: "post",
		url: "/ajax/fav-remove",
		data: "id="+encodeURIComponent(id),
		cache: true,
		async: true,
		timeout: 100000,
		beforeSend: function(){
			$("#fav").hide();
			$("#fav-content").empty();
		},
		success: function(code){
			if (code != "1"){
				$("#fav-content").html(code);
				$("#fav-content").fadeIn("slow");
				$("#fav").fadeIn("slow");
			}
			else	{
				alert ("Nie można usunąć produktu...");
			}
		},
		error: function(){
			$("#fav").hide();
			$("#fav-content").empty();
		}
	});
	return false;
}
function cart_remove(id) {
	$.ajax({
		type: "post",
		url: "/ajax/cart-remove",
		data: "id="+encodeURIComponent(id),
		cache: true,
		async: true,
		timeout: 100000,
		beforeSend: function(){
			$("#cart").hide();
			$("#cart-content").empty();
		},
		success: function(code){
			if (code != "1"){
				$("#cart-content").html(code);
				$("#cart-content").fadeIn("slow");
				$("#cart").fadeIn("slow");
				}
			else	{
				alert ("Błąd: Nie można usunąć produktu.");
			}
		},
		error: function(){
			$("#cart").hide();
			$("#cart-content").empty();
		}
	});
	return false;
}
function zoom(adr) {
	$.fn.colorbox({href: adr, open:true});
	return false;
}
function mycarousel_initCallback(carousel){
	carousel.clip.hover(function() {
		carousel.stopAuto();
	},
	function() {
		carousel.startAuto();
	});
};
