$(function() {
	// Init 
	$(".arrows_top").hide();
	$(".bg_main_l_gr").hide();
	$(".bg_main_r_gr").hide();
	$(".arrows").css({top:"675px"});
	$("#blocks, .logo, .lang").hide();
	$(".peoples").jparallax({yparallax:false, mouseport:$(".main")}).hide();
	//
	var img = new Image();
	$(img).load(function() {
		$(".logo, .lang").show()
		$("#blocks").show().jparallax({yparallax:false, mouseport:$(".main")});
	}).attr("src", "/images/tpl/bg_main1.png");
	
	var img2 = new Image();
	$(img2).load(function() {
		var img3 = new Image();
		$(img3).load(function() {
			var img4 = new Image();
			$(img4).load(function() {
				$(".arrows").animate({top:"204px"},1500,"easeOutElastic");
				setTimeout(function() {
					$(".bg_main_l_gr").fadeIn();
					$(".bg_main_r_gr").fadeIn("",function() {$(".arrows_top").fadeIn();});
					
					var w1 = $(".lineCont").width();
					$(".line").css({width:w1});
					$(".lineCont").css({visibility:"visible", width:0}).animate({width:w1+"px"},w1*2,"linear",function() {
						$(".lineCont").css({width:"100%"});
						$(".line").css({width:"100%"});
						var img5 = new Image();
						$(img5).load(function() {
							var img6 = new Image();
							$(img6).load(function() {
								var img7 = new Image();
								$(img7).load(function() {
									$(".peoples").fadeIn();
									$(".p1, .p2, .p3").hover(function () {
										$(this).addClass("vis");
									}, function () {
										$(this).removeClass("vis");
									});
								}).attr("src", "/images/tpl/p3.png");
							}).attr("src", "/images/tpl/p2.png");
						}).attr("src", "/images/tpl/p1.png");
					});
				}, 600);
			}).attr("src", "/images/tpl/arrows2.jpg");
		}).attr("src", "/images/tpl/line2.png");
	}).attr("src", "/images/tpl/arrows2_line.jpg");
	
	$(".bl").click(function() {
		popup($(this));
	});
	$(".bl:eq(2) li").click(function(event) {
		event.stopPropagation();
		popup($(".bl:eq(2)"), $(this).parent().find("li").index($(this)));
	});
	$(".imslink, .bl a").click(function(event) {
		if ($(this).attr("href") != '#') {
			event.stopPropagation();
		} else {
			//return false;
		}
	});
	
	$("#popup .close, #black").click(function() {
		$("#popup").css({visibility:"hidden",width:200, height:200, top:278, marginLeft:-100});
		$("#popup .inn, #popup .inn2, #popup .inn3").remove();
		$("#black").animate({opacity:0},500,function() { $(this).hide() });
	});
});

function textinInit(o) {
	$(".text", o).each(function() {
		if ($(this).val() == '') {
			$(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() == '') {
				$(this).get(0).value = $(this).attr("title");
			}
		});
	});
	o.submit(function() {
		if ($("input:eq(2)", this).val() == '' || $("input:eq(2)", this).val() == $("input:eq(2)", this).attr("title")) {
			$("input:eq(2)", this).css({borderColor:"#bd1111"});
		} else {
			$("input:eq(2)", this).css({borderColor:"#828282"});
		}
		if ($("input:eq(3)", this).val() == '' || $("input:eq(3)", this).val() == $("input:eq(3)", this).attr("title")) {
			$("input:eq(3)", this).css({borderColor:"#bd1111"});
		} else {
			$("input:eq(3)", this).css({borderColor:"#828282"});
		}
		if ($("textarea:eq(0)", this).val() == '' || $("textarea:eq(0)", this).val() == $("textarea:eq(0)", this).attr("title")) {
			$("textarea:eq(0)", this).css({borderColor:"#bd1111"});
		} else {
			$("textarea:eq(0)", this).css({borderColor:"#828282"});
		}
		
		if ($("input:eq(2)", this).val() != '' && $("input:eq(2)", this).val() != $("input:eq(2)", this).attr("title") && 
			$("input:eq(3)", this).val() != '' && $("input:eq(3)", this).val() != $("input:eq(3)", this).attr("title") && 
			$("textarea:eq(0)", this).val() != '' && $("textarea:eq(0)", this).val() != $("textarea:eq(0)", this).attr("title")) {
			var params = { 
					isAjax: "1",
					template: "5"
				};		
			var fields = $(this).serializeArray();
			jQuery.each(fields, function(i, field){
				params[field.name] = field.value;
			});
			$.post($(this).attr("action"), params, function(data) {
				o.hide();
				var mes = $(document.createElement("div"));
				mes.html('<div>'+$("message", data).text()+'</div>');
				mes.click(function() {
					$(this).remove();
					o.show();
				});
				mes.insertAfter(o);
			}, "xml");
		}
		return false;
	});
}

function popup(o, i) {
	var bc = $(".block_content", o);
	var bw = $("body").width() <= $("#root").width() ? $("#root").width() : "100%";
	var bh = $("body").height() <= $("#root").height() ? $("#root").height() : "100%";
	$("#black").show().css({width:bw, height:bh, opacity:0}).animate({opacity:"0.45"},500);
	$("#popup").css({visibility:"visible"}).animate({width:bc.width(), height:bc.height(), top:378-bc.height()/2, marginLeft:-bc.width()/2}, function() {
		$("#popup .close").after(bc.html());
		textinInit($("#popup form"));
		$("#popup .scroll").jScrollPane({showArrows:true, scrollbarMargin:0});
		if (!isNaN(i)) {
			$('#popup .scroll')[0].scrollTo($("#popup .scroll li").eq(i).offset().top - $("#popup .scroll").offset().top);
		}
		$("#popup .sch_tab1, #popup .sch_tab2, #popup .sch_tab3, #popup .sch_tab4").click(function() {
			if ($(this).attr("class").indexOf("sel")==-1) {
				$("#popup .sch_tab1").toggleClass("sel1");
				$("#popup .sch_tab2").toggleClass("sel2");
				$("#popup .sch_tab3").toggleClass("sel3");
				$("#popup .sch_tab4").toggleClass("sel4");
				$("#popup .v1, #popup .v2").toggleClass("v2").toggleClass("v1");
			}
		});
		if ($("#popup #flash_ru").get(0)) $("#popup #flash_ru").get(0).innerHTML = flhtml('/netcat_files/swf/flash_ru.swf');
		if ($("#popup #flash_en").get(0)) $("#popup #flash_en").get(0).innerHTML = flhtml('/netcat_files/swf/flash_en.swf');
	});
}
function flhtml(lnk) {
	var flhtml = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="730" height="500" id="fl" align="middle">'
						+'<param name="allowScriptAccess" value="sameDomain" />'
						+'<param name="allowFullScreen" value="false" />'
						+'<param name="movie" value="'+lnk+'" />'
						+'<param name="base" value="swf" />'
						+'<param name="quality" value="high" />'
						//+'<param name="wmode" value="transparent" />'
						+'<param name="menu" value="false" />'
						+'<param name="scale" value="noscale" />'
						+'<param name="bgcolor" value="#fff" />'
						+'<embed src="'+lnk+'" base="swf" quality="high" menu="false" /*wmode="transparent"*/ bgcolor="#fff" width="730" height="500" scale="noscale" name="fl" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />'
					+'</object>';
	return flhtml;
}
