$(function() {

	loadImgs([$("<img src=\"/netcat_files/img/logo.png\" />"), $("<img src=\"/netcat_files/img/bg.png\" />"), $(".nav img")/*, $("<img src=\"/netcat_files/img/content/IMG_6613.jpg\" />"), $("<img src=\"/netcat_files/img/content/IMG_6611.jpg\" />"), $("<img src=\"/netcat_files/img/content/IMG_6612.jpg\" />")*/], function() { start(); });
	

});

function start() {
	$(".loading").css({visibility:"visible"});
	
	$(".content, .flash2, .content_index, .flash_inner").hide();
	
	$("#main").css({visibility:"visible", left:$("#root").width()+$("#main").width()/2+100}).animate({left:"50%"}, 700, "easeOutBack", function() {
		$(".loading").css({visibility:"hidden"});
		
		if ($(".content_index").length > 0) {
			loadImgs([$(".content_index img")], function() { $(".content_index").fadeIn(); });
		}
		if ($(".flash_inner").length > 0) {
			loadImgs([$(".flash_inner img")], function() { $(".flash_inner").fadeIn(); });
		}
		if ($(".content").length > 0) {
			loadImgs([$(".content img")], function() { $(".content").fadeIn(); });
		}
		if ($(".flash2").length > 0) {
			loadImgs([$(".flash2 img")], function() { $(".flash2").fadeIn(); });
		}
		if ($(".gmap").length == 1) {
			gmap();
		}
		
		$(".nav2 li a").click(function() {
			var th = $(this);
			if (!th.hasClass("sel")) {
				var linkId = $(this).attr("href").replace("#", "").replace("_", "");
				var opac = 0.2;
				if ($(this).parents(".nav2").css("left").replace("px", "") > 0) {
					opac = 1;
					$(this).parents(".nav2").animate({left:0}, 800);
				}
				$(".nav2 li a").removeClass("sel");
				th.addClass("sel");
				$(".content_inner:visible").css({opacity:opac}).animate({left:"-615px"}, 800, function() {
					$(this).addClass("serv_closed");
				});
				$("#"+linkId).css({left:"888px",opacity:1}).removeClass("serv_closed").animate({left:"266px"}, 800);
			}
		})
	});
}

function loadImgs(images, callback) {
	var loaded = {};
	$(images).each(function() {
		if ($(this).length > 0) {
			$(this).each(function() {
				loaded[$(this).attr("src")] = false;
			});
		}
	});
	$(images).each(function() {
		if ($(this).length > 0) {
			$(this).each(function() {
				$(new Image()).load(function() {
					loaded[$(this).attr("src")] = true;
					var l = true;
					$.each(loaded, function(i, n) {
						if (this != true) l = false;
						$(".eee").append(i+' - '+this+' ('+l+') <br />')
					});
					$(".eee").append('<em>'+l+'</em><br />')
					if (l) callback.call();
				}).attr("src", $(this).attr("src"));
			});
		} else {
			callback.call();
		}
	});
}

function textinInit(o) {
	$("input, textarea", o).each(function() {
		if ($(this).val() == '') {
			if ($(this).attr("type") == "password") {
				var th = $(this);
				th.after('<input type="text" class="'+$(this).attr("class")+'" value="'+$(this).attr("title")+'" />').hide();
				$("input:text", th.parent()).focus(function() {
					$(this).hide();
					th.show().focus();
				});
			}
			$(this).get(0).value = $(this).attr("title");
		};
		$(this).focus(function() {
			if ($(this).val() == $(this).attr("title")) {
				$(this).get(0).value = '';
			}
		}).blur(function() {
			if ($(this).val() == '') {
				if ($(this).attr("type") == "password") {
					$(this).hide();
					$("input:text", $(this).parent()).show();
				}
				$(this).get(0).value = $(this).attr("title");
			}
		});
	});

	$(".feedbackform").find(".textarea").next("input").click(function() {
		$(".feedbackform").validate({
			submitHandler: function(form) {
				$(".feedbackform").ajaxSubmit({
					target: '#feedresult'
				});
			},
			rules: {
				f_Name: "required",
				f_Email: "required"
			},
			messages: {
				f_Name: "Введите имя",
				f_Email: "Введите email или телефон"
			}
		});
	});

}
