$(document).ready(function(){
	inputValue();
	function inputValue(){
		var asInitVals = new Array;
		var input = $("input.inputinit")
		$(input).each(function(i){
			asInitVals[i] = $(this).val();
		});
		$(input).focus(function(){
			if ($(this).hasClass("inputinit")) {
				$(this).removeClass();
				$(this).val("");
			}
		});
		$(input).blur(function(i){
			if (!(jQuery.trim($(this).val()))) {
				$(this).addClass('inputinit');
				$(this).val(asInitVals[$(input).index(this)]);
			}
		});
	}
	
	$("#flag .area").hover(function(){
		$("#flag").toggleClass("hover");
	}, function(){
		$("#flag").toggleClass("hover");
	})
	
	
	if ($("#promonews").length) {
		galleries("#promonews", 500, 7000, ".promonews_hold .number", 1);
	}
	if ($("#clients").length) {
		galleries("#clients", 500, 7000, ".clients_hold .number", 1);
	}
	if ($("#review").length) {
		galleries("#review", 500, 25000, ".reviews .number", 1);
	}
	function galleries(gSelector, time1, time2, numbSelector, bool){
		var gItem = $(gSelector + "> li");
		var gLength = $(gItem).length;
		var numbItem = $(numbSelector + "> li");
		var n, m;
		var fIter;
		
		if (bool) {
			$.each(gItem, function(i, gtItem){
				$(gItem[i]).css({
					"zIndex": gLength - i,
					opacity: 0
				});
			});
			playHead(0);
		}
		function playHead(activePos){
			$(gItem).css({
				"opacity": "0"
			});
			$(gItem[activePos]).css({
				"opacity": "1"
			});
			$(numbItem).removeClass("active");
			$(numbItem[activePos]).addClass("active");
			fIter = setTimeout(function(){
				gPlay(activePos);
			}, time2);
		}
		function gPlay(n){
			if (n < gLength - 1) {
				m = n + 1;
				gNext(n, m);
				n++;
			}
			else {
				gNext(gLength - 1, 0);
				n = 0;
			}
			fIter = setTimeout(function(){
				gPlay(n);
			}, time2);
		}
		function gNext(gi, gj){
			$(gItem[gi]).animate({
				opacity: 0
			}, time1).css({
				"zIndex": 0
			});
			$(gItem[gj]).animate({
				opacity: 1
			}, time1).css({
				"zIndex": 40
			});
			$(numbItem[gi]).removeClass("active");
			$(numbItem[gj]).addClass("active");
		}
		$(numbItem).live("click", function(event){
			event.preventDefault();
			changePos(this)
		});
		
		function changePos(thisItm){
			clearTimeout(fIter);
			var iNew = $(numbItem).index(thisItm);
			thisActive = $(numbItem).parent().find(".active");
			var iOld = $(numbItem).index(thisActive);
			gNext(iOld, iNew);
			fIter = setTimeout(function(){
				gPlay(iNew);
			}, time2);
		}
	}
	if ($(".callmeback").length > 0) {
		$(".callmeback").fancybox({
			'transitionIn': 'elastic',
			'transitionOut': 'elastic',
			onComplete: function(){
				form2 = new adl_form($("#callback"), $("#callback .send"));
				form2.init();
				
				$("#callback .radio label").click(function(){
					$("#callback .radio label").removeClass("act").removeClass("unact").addClass("unact");
					$(this).removeClass("unact").addClass("act");
				});
				$("#callback .check label").click(function(){
					if ($(this).parent(".check").find("input:checked").length>0){
							$(this).removeClass("act").addClass("unact");
					}
					else{
							$(this).removeClass("unact").addClass("act");
					}
				});
					$('#callback').parent("form").submit(function() { 
					$(this).ajaxSubmit(options); 
					return false; 
				}); 
			}
			
		});
	};
	// catalog tovar height fix
	//tovarHeightFix();
	function tovarHeightFix(){
		var tovar = $(".portfolio ul.cat li");
		var tovarImg = $(".portfolio .img");
		var tovarH2 = $(".portfolio .razd");
		var maxHeightTov = $(tovar).height();
		var maxHeightImg = $(tovarImg).height();
		var maxHeightH2 = $(tovarH2).height();
		tovarLength = tovar.length;
		
		$(window).load(function(){
			for (i = 0; i < tovarLength; i++) {
				tovHeightImg = tovarImg[i];
				tovHeightImg = $(tovHeightImg).height();
				if (maxHeightImg < tovHeightImg) {
					maxHeightImg = tovHeightImg;
				}
			}
			$(tovarImg).height(maxHeightImg);
			for (i = 0; i < tovarLength; i++) {
				tovHeightH2 = tovarH2[i];
				tovHeightH2 = $(tovHeightH2).height();
				if (maxHeightH2 < tovHeightH2) {
					maxHeightH2 = tovHeightH2;
				}
			}
			$(tovarH2).height(maxHeightH2);
			
			for (i = 0; i < tovarLength; i++) {
				tovHeight = tovar[i];
				tovHeight = $(tovHeight).height();
				if (maxHeightTov < tovHeight) {
					maxHeightTov = tovHeight;
				}
			}
			$(tovar).height(maxHeightTov);
			//img{vertical-align:middle;}
			for (i = 0; i < tovarLength; i++) {
				tovImg = $(tovarImg)[i];
				tovImgHeight = $(tovImg).height();
				imgMarginTop = maxHeightImg - tovImgHeight;
				$(tovarH2[i]).css({
					paddingTop: imgMarginTop
				})
				
			}
		});
	};
	if ($(".buttons_block table.bt_bl td.bt_bl_h").length > 0){
		$(".buttons_block table.bt_bl td.bt_bl_h").hover(
			function(){$(this).toggleClass("bt_bl_hover")},
			function(){$(this).toggleClass("bt_bl_hover")}
		)
		$(".buttons_block table.bt_bl td.bt_bl_h").click(function(){
				href = $(this).find('a').attr("href");
				window.location = href;
			});
	}
});


