var typeStr = 1;
var firstRaz = false;
$(document).ready(function(){
	
	$("a.catalogBanner").click(function(){
		for (i in bannerMas){
			$("#"+bannerMas[i]).click();
		}
		return false;
	});
	
//    	$("a.fancya").fancybox();
	$("a.fancya").fancybox({
		'titleShow' : false,
       		'autoDimensions' : true,
		'scrolling' : 'no',
		'onComplete' : function(){
			$.fancybox.resize();
		}
	});


	$("#feedbackForm").hide();
	
	var ShowHideWriteLetter = function(){
		$("#feedbackForm").slideToggle(function(){
			$("#feedbackPlace").toggleClass('slideOn');
		});
		$("#feedbackPlace a.close").toggleClass('hide');
		return false;
	};
	
	$("#feedback_openLink").click(ShowHideWriteLetter);
        $("#feedbackPlace a.close").click(ShowHideWriteLetter);

	$("#feedbackForm .g-red").hide();
	$("#feedbackForm .error").hide();

	$("#feedbackForm").submit(function(){
		$("#feedbackForm .g-red").hide();
		$("#feedbackForm .error").hide();

		var error = false;
		if ($("#feedback_name").val() == ""){
			$("label[for='feedback_name'] span").show();
			error = true;
		}
		if ($("#feedback_email").val() == ""){
			$("label[for='feedback_email'] span").show();
			error = true;
		}
		if ($("#feedback_message").val() == ""){
			$("label[for='feedback_message'] span").show();
			error = true;
		}
		if (error)
			$("#feedbackForm p.error").show();
		else{
			$.post("/classes/ajax/sendmessage.php", {name:$("#feedback_name").val(), email:$("#feedback_email").val(), message: $("#feedback_message").val()}, function(a){
				if (a == 'done'){
					alert("Ваше сообщение успешно послано. Спасибо!");
					$("#feedbackForm").resetForm();
					$("#feedbackPlace .close").click();

				}else{
					alert("Ваше сообщение по каким то причинам не было послано. Попробуйте чуть позже!");
				}
			});
		}
		return false;	
		
	});
	
	$("#sendQuestionForm").submit(function(){
		$("#sendQuestionForm .g-red").hide();
		$("#sendQuestionForm .error").hide();
		var error = false;
		if ($("#sendQuestion_name").val() == ""){
			$("label[for='sendQuestion_name'] span").show();
			error = true;
		}
		if ($("#sendQuestion_email").val() == ""){
			$("label[for='sendQuestion_email'] span").show();
			error = true;
		}
		if ($("#sendQuestion_message").val() == ""){
			$("label[for='sendQuestion_message'] span").show();
			error = true;
		}

		if (error)
			$("#sendQuestionForm p.error").show();
		else{
			$.post("/classes/ajax/sendquestion.php", {name:$("#sendQuestion_name").val(), email:$("#sendQuestion_email").val(), message: $("#sendQuestion_message").val()}, function(a){
				if (a == 'done'){
					alert("Ваш вопрос успешно послан. Спасибо!");
					$("#sendQuestionForm").resetForm();
				}else{
					alert("Ваше сообщение по каким то причинам не было послано. Попробуйте чуть позже!");
				}
			});
		}
		return false;	
		
	});
	
	if (typeStr == 'press'){
		$(".articlesYears").removeClass("active");
		
		$(".articlesYears").click(function(){
			$(".articlesYears").removeClass("active");
			$(".articlesYears").attr("href", "#");
			$(this).addClass('active').blur();
			$(this).removeAttr("href");
			$(".articles .item").hide();
			$(".articles .articlesYears_"+$(this).text()).show();
			
			return false;
		});
		if (thisYear)
			$(".articlesYears[type='"+thisYear+"']").click()
		else
			$(".articlesYears:first").click();

	}

	if (typeStr == 'news'){
		$(".articless").hide();
		$(".years a").removeClass("active");
		$(".months a").removeClass("active").hide();

		$(".months a").click(function(){
			$("#newsListZagolovok").show();
			$(".b-archive-right").removeClass("article");
			$(".new-alone").remove();

			$(".months a").removeClass("active");
			$(".months a").attr("href", "#");
			$(this).blur().addClass('active');
			$(this).removeAttr("href");
			
			$("#monthNow").text($(this).text() + ", ");
			$(".articless").hide();
			$(".article_" + $(this).attr("id")).show();

			return false;
		});

		$(".years a").click(function(){
			$(".years a").removeClass("active");
			$(".years a").attr("href", "#");
			$(this).blur().addClass('active');
			$(this).removeAttr("href");

			$("#yearNow").text($(this).attr('rel'));

			$(".months a").removeClass("active").hide();
			$(".months a").attr("href", "#");
	
			$(".months a.year_" + $(this).attr('rel')).show();

			if (firstRaz)
				firstRaz = false;
			else
				$(".year_" + $(this).attr('rel')+":first").click();

			return false;
		});
		if (notOpen)
			$(".years a[rel='"+notOpen+"']").click();
		else
			$(".years a:first").click();
		return false;
	}
	
	if (typeStr == 'catalog'){
	
		$(".second").hide();
		$(".third").hide();
		
		$(".first a").click(function(){
			thisCatalogLevel = 0;
			catalogNames[thisCatalogLevel] = $(this).text();
			$(".second").hide();
			$(".third").hide();
			
			$(".a_cat_1").show();
			$(".dfn_cat_1").hide();
			
			$(".first li").removeClass("active");
			$(this).parent().addClass("active");
			
			var id = $(this).attr('type');
			$("#fff_"+id).hide();
			$("#dfn_"+id).show();
			
			
			if ($(this).attr("rel") == 1)
				listProducts(id);
			else
				$("#menu_"+id).show();
			
			return false;
		});

		$(".second a").unbind("click").click(function(){
			thisCatalogLevel = 1;
			catalogNames[thisCatalogLevel] = $(this).text();
			$(".third").hide();
			
			$(".a_cat_2").show();
			$(".dfn_cat_2").hide();
			
			$(".second li").removeClass("active");
			$(this).parent().addClass("active");

			var id = $(this).attr('type');
			$("#fff_"+id).hide();
			$("#dfn_"+id).show();
			
			
			if ($(this).attr("rel") == 1)
				listProducts(id);
			else
				$("#menu_"+id).show();
				
			return false;
		});

		$(".third a").unbind("click").click(function(){
			thisCatalogLevel = 2;
			catalogNames[thisCatalogLevel] = $(this).text();

			$(".a_cat_3").show();
			$(".dfn_cat_3").hide();

			$(".third li").removeClass("active");
			$(this).parent().addClass("active");

			var id = $(this).attr('type');
			$("#fff_"+id).hide();
			$("#dfn_"+id).show();
			
			listProducts(id);

			return false;
		});
		
		function listProducts(id){
			$(".breadcrumbs-wrap").show();
			$("#firstLevelCatalog").text("");
			$("#secondLevelCatalog").text("");
			$("#defaultTextCatalog").hide();
			$("#pageName").show();
			if (thisCatalogLevel > 0){
				$("#firstLevelCatalog").text(catalogNames[0]);
				if (thisCatalogLevel > 1)
					$("#secondLevelCatalog").html(" &mdash; " + catalogNames[1]);
			}
			$("#pageName").text(catalogNames[thisCatalogLevel]);
			$(".product").hide();
			$(".catal_" + id).show();
		}
	}	
	
	if (typeStr == 'catalogq'){
		$('.a_first').click(function(){
			var ID =	$(this).attr('id');
			$(this).hide();
			$("#dfn_"+ID).show();
			$("#"+catalogChangeMain).show();
			$("#dfn_"+catalogChangeMain).hide();
			catalogChangeMain = ID;
			catalogNames[0] = $(this).text();
			catalogNames.length = 1;
//			catalogNames[1] = "";
//			catalogNames[2] = "";
			
			if (catalogChangeSecond){
				$("#"+catalogChangeSecond).show();
				$("#dfn_"+catalogChangeSecond).hide();
			}
			$(".second").hide();
			
			if ($(this).attr('rel') == 1)
				showProducts(ID);
			else{
				$("#menu_"+ID).show();
			}
			return false;
		});
	
		$(".second a").click(function(){
			var ID =	$(this).attr('id');
			$(this).hide();
			$("#dfn_"+ID).show();
			$("#"+catalogChangeSecond).show();
			$("#dfn_"+catalogChangeSecond).hide();
			catalogChangeSecond = ID;
			catalogNames[1] = $(this).attr('type');
			catalogNames[2] = $(this).text();
			
			if ($(this).attr('rel') == 1)
				showProducts(ID);
			return false;
		});
		
		function showProducts(id){
			/*убираем дефолтные штуки и крошки*/
			$(".breadcrumbs-wrap").show();
			$("#firstLevelCatalog").text("");
			$("#secondLevelCatalog").text("");
			$("#defaultTextCatalog").hide();
			$("#pageName").show();
			if (catalogNames.length > 1){
				$("#pageName").text(catalogNames[2]);
				$("#firstLevelCatalog").text(catalogNames[0]);
				if (catalogNames[1] != "")
					$("#secondLevelCatalog").html(" &mdash; " + catalogNames[1]);
			}else{			
				$("#pageName").text(catalogNames[0]);
			}
			$(".product").hide();
			$(".catal_" + id).show();


		
		}
	}
	
	
});
var catalogChangeMain = 0;
var catalogChangeSecond = 0;

var catalogNames = [];
catalogNames.push("");
catalogNames.push("");
catalogNames.push("");

