var IE6 = false;

$(document).ready(function(){
	initProjectShow();
	initSearch();

	$("#leftright").each(function(){
		var click = function(){
			window.location.href = $(this).attr("href");
		};
		$(window).keydown(function(event){
			if (event.ctrlKey)
			{
				if (event.keyCode == 39) $("#right").each(click);
				else if (event.keyCode == 37) $("#left").each(click);
			}
		});
	});
	
	$(".b-form-application").bind("submit",function(){
		var error, form = $(this);

		$(this).find(".req").each(function(){
			if ($(this).val() == "")
			{
				error = true;
				$(this).focus();
				alert("Не все обязательные поля заполнены");
				return false;
			}
		});

		if (!error) $(this).ajaxSubmit({url:$(this).attr('rel')?$(this).attr('rel'):$(this).attr('action'),iframe:true,success:function(txt){
			var success = /<div class=.{0,1}nc_mail_from_site.{0,1}>([^<]*)<\/div>/.exec(txt);
			if (success)
			{
				form.hide();
				form.next(".success").each(function(){
					$(this).show();
					$(this).html(success[1].replace("\n","<br>"));
				});
			}
			else
			{
				var captcha = /nc_captcha_hash[',"]{0,1}\svalue=[',"]{0,1}(\w+)/.exec(txt);
				if (captcha)
				{
					form.find("input[name=nc_captcha_hash]").val(captcha[1]);
					form.find(".captcha img").attr("src", "/netcat/modules/captcha/img.php?code=" + captcha[1]);
				}
				var error = /<div class=.{0,1}warnText.{0,1}>(.+)<\/div>/.exec(txt);
				if (error) alert(error[1].replace(/<[^>]*>/g,''));
			}
		}});
		return false;
	});
	
	$("#blogLink").attr("target", "_blank");
});

function openTopFeedbackForm() { $("#topFeedbackForm").show(300); }
function closeTopFeedbackForm() { $("#topFeedbackForm").hide(300); }

/* Projects */
function initProjectShow()
{
	$(".projectShow a.img").each(function(){
		$(this).mouseover(projectMouseOver).mouseout(projectMouseOut);
		$(this).find("img").each(function(){
			$(this).mouseover(projectMouseOver).mouseout(projectMouseOut);
		});
	});
}

function projectMouseOver(event)
{
	var sender = $(event.target);
	if (sender.is("a")) { lightOnProject(sender); }
	else
	if (sender.is("img")) { lightOnProject(sender.parent()); }
}

function projectMouseOut(event)
{
	var sender = $(event.target);
	if (sender.is("a")) { lightOffProject(sender); }
	else
	if (sender.is("img")) { lightOffProject(sender.parent()); }
}

function lightOnProject(element)
{
	element = element.get(0);
	var imgArr = element.getElementsByTagName("img");
	if (imgArr[0].src.indexOf("white") != -1 || (IE6 && imgArr[0].runtimeStyle.filter.indexOf("white") != -1))
	{
		imgArr[0].src = "/images/img/c-left-white-sel.png";
		imgArr[1].src = "/images/img/c-right-white-sel.png";
	}
	else
	{
		imgArr[0].src = "/images/img/c-left-gray-sel.png";
		imgArr[1].src = "/images/img/c-right-gray-sel.png";
	}
	if (IE6) {
		Utils.fixPng(imgArr[0]);
		Utils.fixPng(imgArr[1]);
	}
}

function lightOffProject(element)
{
	element = element.get(0);
	var imgArr = element.getElementsByTagName("img");
	if (imgArr[0].src.indexOf("white") != -1 || (IE6 && imgArr[0].runtimeStyle.filter.indexOf("white") != -1))
	{
		imgArr[0].src = "/images/img/c-left-white.png";
		imgArr[1].src = "/images/img/c-right-white.png";
	}
	else
	{
		imgArr[0].src = "/images/img/c-left-gray.png";
		imgArr[1].src = "/images/img/c-right-gray.png";
	}
	if (IE6) {
		Utils.fixPng(imgArr[0]);
		Utils.fixPng(imgArr[1]);
	}
}

/* Search */
function initSearch()
{
	$("#searchInput").focus(function(){
		if (this.value.length == 0) $("#searchLabel").hide();
	}).blur(function(){
		if (this.value.length == 0) $("#searchLabel").show();
	});
}
 
function writeHelloEmail(id)
{
	var e = "hello"+String.fromCharCode(64)+"delaform.ru";
	$("#"+id).html("<span>"+e+"</span>");
	$("#"+id).attr("href", "mailto:"+e);
}

function writeInfoEmail(id)
{
	var e = "info"+String.fromCharCode(64)+"delaform.ru";
	$("#"+id).html("<span>"+e+"</span>");
	$("#"+id).attr("href", "mailto:"+e);
}