/*var $j = jQuery.noConflict();*/

$(document).ready(
function(){

	var tmp = 1;

	var splashes = [];
	splashes[0] = "assets/images/logo/logo1.jpg";
	splashes[1] = "assets/images/logo/logo2.jpg";
	splashes[2] = "assets/images/logo/logo3.jpg";
	splashes[3] = "assets/images/logo/logo4.jpg";
	splashes[4] = "assets/images/logo/logo5.jpg";
	splashes[5] = "assets/images/logo/logo6.jpg";
	splashes[6] = "assets/images/logo/logo7.jpg";
	splashes[7] = "assets/images/logo/logo8.jpg";



	var i = 0;

	var showSplash =
	function() {
		i++;
		if (i > (splashes.length - 1)) {
			i = 0;
		}
		var  tmp = splashes[i];

		$("#im").fadeTo(1000,0, function () {
			$("#img").attr("src", tmp);
		}).fadeTo(1000,0).fadeTo(1000,1);
		tmp = splashes[i];
	};

	var holdTheInterval = setInterval(showSplash, 5000);





	//captcha reload
	$("span.reload").click(
	function(){
		$("#imgCaptcha").attr("src", "image.gif?id="+ Math.random());
	});

	$("img.imgThumb").mouseover(
	function(){
		allThumbs = $(".imgThumb");
		for(var i = 0; i < allThumbs.length; i++) {
			if ($("img.imgThumb:eq("+i+")").is(".selected")) {
				$("img.imgThumb:eq("+i+")").removeClass("selected").fadeTo(100,1);
			}
		}
		$(this).addClass("selected");
		$(this).fadeTo(100,0.5);

		for(var i = 0; i < allThumbs.length; i++) {
			if ($("img.imgThumb:eq("+i+")").is(".selected")) {
				id = i+1;
			}
		}


		$("#product_image_wrapper").fadeTo(500,0, function () {
			$("#product_image").attr("src", src[id]);
		}).find("#product_image").bind("load", function() {
			$("#product_image_wrapper").fadeTo(500,1);
		});
	});
	$("#imgThumb1").css("opacity", "0.5");



	$("a.open").click(
	function(){
		var size = 0;
		if($(this).children("img").attr("src") == "assets/templates/main/images/plus.gif") {

			// получаю уровень вложенности нажатого элемента
			var size1 = $(this).parent("li").parent("ul").prev("a").prev("a.close").size();
			var size2 = $(this).parent("li").parent("ul").prev("a").prev("a.close").parent("li").parent("ul").prev("a").prev("a.close").size();
			var size3 = $(this).parent("li").parent("ul").prev("a").prev("a.close").parent("li").parent("ul").prev("a").prev("a.close").parent("li").parent("ul").prev("a").prev("a.close").size();

			if(size1 > 0) {
				size = 1;
				if(size2 > 0) {
					size = 2;
					if(size3 > 0) {
						size = 3;
					}
				}
			}



			if (size == 3) {
				$("a.close").each(function(j){

					if ($(this).parent("li").parent("ul").prev("a").prev("a.close").parent("li").parent("ul").prev("a").prev("a.close").parent("li").parent("ul").prev("a").prev("a.close").size() > 0){

						$(this).children("img").attr("src", "assets/templates/main/images/plus.gif");
						$(this).children("img").attr("alt", "Развернуть");
						$(this).attr("title", "Развернуть");
						$(this).next("a").next("ul").hide("fast");
						$(this).addClass("open");
						$(this).removeClass("close");
					}
				});
			} else if (size == 2) {
				$("a.close").each(function(j){

					$("a.close").each(function(j){

						if ($(this).parent("li").parent("ul").prev("a").prev("a.close").parent("li").parent("ul").prev("a").prev("a.close").parent("li").parent("ul").prev("a").prev("a.close").size() > 0){
							$(this).children("img").attr("src", "assets/templates/main/images/plus.gif");
							$(this).children("img").attr("alt", "Развернуть");
							$(this).attr("title", "Развернуть");
							$(this).next("a").next("ul").hide("fast");
							$(this).addClass("open");
							$(this).removeClass("close");
						}
					});


					if ($(this).parent("li").parent("ul").prev("a").prev("a.close").parent("li").parent("ul").prev("a").prev("a.close").size() > 0){
						$(this).children("img").attr("src", "assets/templates/main/images/plus.gif");
						$(this).children("img").attr("alt", "Развернуть");
						$(this).attr("title", "Развернуть");
						$(this).next("a").next("ul").hide("fast");
						$(this).addClass("open");
						$(this).removeClass("close");
					}
				});
			} else if (size == 1) {
				$("a.close").each(function(j){

					if ($(this).parent("li").parent("ul").prev("a").prev("a.close").parent("li").parent("ul").prev("a").prev("a.close").parent("li").parent("ul").prev("a").prev("a.close").size() > 0){
						$(this).children("img").attr("src", "assets/templates/main/images/plus.gif");
						$(this).children("img").attr("alt", "Развернуть");
						$(this).attr("title", "Развернуть");
						$(this).next("a").next("ul").hide("fast");
						$(this).addClass("open");
						$(this).removeClass("close");
					}
				});

				$("a.close").each(function(j){

					if ($(this).parent("li").parent("ul").prev("a").prev("a.close").parent("li").parent("ul").prev("a").prev("a.close").size() > 0){
						$(this).children("img").attr("src", "assets/templates/main/images/plus.gif");
						$(this).children("img").attr("alt", "Развернуть");
						$(this).attr("title", "Развернуть");
						$(this).next("a").next("ul").hide("fast");
						$(this).addClass("open");
						$(this).removeClass("close");
					}
				});
				$("a.close").each(function(j){
					if ($(this).parent("li").parent("ul").prev("a").prev("a.close").size() > 0){
						$(this).children("img").attr("src", "assets/templates/main/images/plus.gif");
						$(this).children("img").attr("alt", "Развернуть");
						$(this).attr("title", "Развернуть");
						$(this).next("a").next("ul").hide("fast");
						$(this).addClass("open");
						$(this).removeClass("close");
					}
				});
			} else if(size == 0){
				$("a.close").each(function(j){
					$(this).children("img").attr("src", "assets/templates/main/images/plus.gif");
					$(this).children("img").attr("alt", "Развернуть");
					$(this).attr("title", "Развернуть");
					$(this).next("a").next("ul").hide("fast");
					$(this).addClass("open");
					$(this).removeClass("close");
				});
			}

			$(this).children("img").attr("src", "assets/templates/main/images/minus.gif");
			$(this).children("img").attr("alt", "Свернуть");
			$(this).removeClass("open");
			$(this).addClass("close");
			$(this).attr("title", "Свернуть");

			$(this).next("a").next("ul").show("slow");

		} else {
			//скрываем все дочерние элементы close
			var ind = $("a.close").index($(this));
			$("a.close").each(function(j){
				if (j>=ind) {
					$(this).children("img").attr("src", "assets/templates/main/images/plus.gif");
					$(this).children("img").attr("alt", "Развернуть");
					$(this).attr("title", "Развернуть");
					$(this).next("a").next("ul").hide("fast");
					$(this).addClass("open");
					$(this).removeClass("close");
				}
			});
		}
	});
});
