window.onload = function() {
	/*$('.slider-top').jCarouselLite({
		scroll: 1,
		visible: 1,
		rows: 1,
		btnNext: ".next",
		btnPrev: ".prev",
		speed: 800,
		circular: true,
		auto:10000
	});*/

	$('.side-slider').jCarouselLite({
		scroll: 1,
		visible: 3,
		rows: 1,
		btnNext: ".down",
		btnPrev: ".up",
		speed: 800,
		circular: true,
		vertical: true
	});

	$('.slider-foot').jCarouselLite({
		scroll: 1,
		visible: 5,
		rows: 1,
		btnNext: ".go-right",
		btnPrev: ".go-left",
		speed: 800,
		circular: true
	});

	$.mask.definitions['~']='[+-]';
	$('#feeback_tel').mask('(999) 999-99-99');

	$("#hover_navig li").hover(function(){
		var obj = $(this).children("ul");
		if (obj.length){
			obj.show();
		}
	},function(){
		var obj = $(this).children("ul");
		if (obj.length){
			obj.hide();
		}
	});

	$(".collection_selector").click(function(e){
		e.preventDefault();
		var id = $(this).parent().children("#val").val();
		$("#item_weight").show(200);

		$.ajax({
			type: "POST",
			url: "/collection/ajax_item",
			dataType: "json",
			data: "id="+id,
			success: function(msg){
				$("#thisid").val(msg.id);
				$("#full_icon").attr("src","/img/items/big/"+msg.img);
				$("#layer_name").empty().append(msg.layer_name);
				$("#size_name").empty().append(msg.size_name);
				$("#texture_name").empty().append(msg.texture_name);
				$("#color_name").empty().append(msg.color_name);
				$("#item_name").empty().append(msg.name);
				$("#item_weight").fadeOut();
				$(".fav_button img").remove();
				$(".fav_button").append(msg.button);
				$("#item_text_container").empty().append(msg.text);
				$("#table_item").empty().append(msg.table);
			}
		});
		//
	});

	$("#add_to_fav").live("click",function(){
		var id = $(this).parent().children("#thisid").val();
		$("#item_weight").show(200);

		$.ajax({
			type: "POST",
			url: "/collection/add_fav",
			dataType: "json",
			data: "id="+id,
			success: function(msg){
				$("#item_weight").fadeOut();
				$(".fav_button img").remove();
				$(".fav_button").append(msg.button);
				if (msg.count == "1"){
					$("#afterizbr").after('<li id="isbrlink"><a href="/collection/selected/">Избранное</a></li>');
				}
			}
		});
		//
	});

	$("#remove_from_fav").live("click",function(){
		var id = $(this).parent().children("#thisid").val();
		$("#item_weight").show(200);

		$.ajax({
			type: "POST",
			url: "/collection/remove_fav",
			dataType: "json",
			data: "id="+id,
			success: function(msg){
				$("#item_weight").fadeOut();
				$(".fav_button img").remove();
				$(".fav_button").append(msg.button);
				if(msg.count == "0"){
					$("#isbrlink").remove();
				}
			}
		});
		//
	});

	$(".remove_from_favlist").click(function(e){
		e.preventDefault();
		var id = $(this).parent().children("#thisid").val();
		var obj = $(this);

		$.ajax({
			type: "POST",
			url: "/collection/remove_fav",
			dataType: "json",
			data: "id="+id,
			success: function(msg){
				obj.parent().parent().remove();
				if(msg.count == "0"){
					$("#isbrlink").remove();
				}
			}
		});
		//
	});

	$("#frm_brand").change(function(){
		$("#frm_categs").attr("disabled","disabled");

		var id = $(this).val();

		$.ajax({
			type: "POST",
			url: "/collection/ch",
			dataType: "json",
			data: "id="+id,
			success: function(msg){
				$("#frm_categs").removeAttr("disabled");
				$("#frm_categs").empty().append(msg.html);
			}
		});
		//
	});

	/*var status_vsplyvashka = parseInt($("#show_slider").val());
	if (status_vsplyvashka == 1){
		var vsplyvashka_enabled = true;
	}else{
		var vsplyvashka_enabled = false;
	}

	var vsplyvashka_timeout;
	if ($("#vsplyvashka").css('left') == '-315px' && vsplyvashka_enabled){
		$("#vsplyvashka_hide").removeClass("vsplyvashka_hide").addClass("vsplyvashka_open");
		vsplyvashka_timeout=setTimeout('$("#vsplyvashka").animate({"left":"0"},function(){$("#vsplyvashka_hide").removeClass("vsplyvashka_open").addClass("vsplyvashka_hide");})', 5000);
	}else{
		clearTimeout(vsplyvashka_timeout);
	}*/

	$(".vsplyvashka_hide").live("click",function(){
		$("#vsplyvashka").animate({"left":"-315"},function(){
			$("#vsplyvashka_div").css("width","60px");
		});
		vsplyvashka_enabled = false;

		$("#show_slider").val(0);

		$.ajax({
			type: "POST",
			url: "/collection/slideswitchoff",
			dataType: "json",
			data: "id=1",
			success: function(msg){
				$("#frm_categs").removeAttr("disabled");
				$("#frm_categs").empty().append(msg.html);
			}
		});

		$("#vsplyvashka_hide").removeClass("vsplyvashka_hide").addClass("vsplyvashka_open");
	});

	$(".vsplyvashka_open").live("click",function(){
		$("#vsplyvashka_div").css("width","375px");

		$("#vsplyvashka").animate({"left":"0"});
		vsplyvashka_enabled = false;

		$("#vsplyvashka_hide").removeClass("vsplyvashka_open").addClass("vsplyvashka_hide");
	});
}

function preloadimages(){
	for(var i = 0; i<arguments.length; i++){
		$("<img>").attr("src", arguments[i]);
	}
}

preloadimages("/image/button-add-fav.gif","/image/button-remove-fav.gif");
