$(function() {

$("#enter").click( function() 
{ 
	//toggle enter_cont			
	$("#enter_cont").toggle(); 
	$("TR.tr-error").remove();

	return false; 
});

//bind click handler on cross to hide enter_cont
$("#auth-close").click( function() { $("#enter_cont").hide(); return false; } );

//bind focus handler to replace password areas
$("#enter_cont").find("INPUT.auth-text").focus( function() 
	{
	
		if($(this).attr("changed")!="changed")
		{
		
			if ($(this).attr("name")=="fake_pasw") 
			{
				$(this).hide().next().show().focus();
			}
			
			$(this).val("").removeClass("gray").attr("changed","changed");
		}
			
	});
});


function resizeimg(id, width, height)
{
    var s_height = height;
    var width_body = $("body").width();
    var height_body = $("body").height() * 0.5;
    if (height_body < 440) {
        var koef = (height_body - 90) / 340;
        if (koef < 0.55) {
            koef = 0.55;
        }
    }
    else {
        koef = 1;
    }
    width *= koef;
    height *= koef;
    width = Math.round(width);
    height = Math.round(height);
    $("#" + id).attr({
        "width" : width, "height" : height
    });
    $("#" + id).css({
        "margin-top" : s_height - height + "px"
    });
    if (id == "lend") {
        $("#" + id).css({
            "left" :- width + 0 + "px" 
        });
    }
}

function validation(){
	var q_error=n_error=e_error="";
	if ($("textarea[name=text]").val()=="") {
		q_error = "<b>Введите задачу</b>";
		$("table.search-box tr:eq(1) span").css("color", "red");
		$("textarea[name=text]").css("border", "1px red solid");
	}
	
	if (!$("input[name=name]").attr("value")) {
		n_error = "<b>Введите имя</b>";
		$("table.search-box tr:eq(2) span").css("color", "red");
		$("input[name=name]").css("border", "1px red solid");
	}
		
	var thereg = /[0-9a-z_]+@[0-9a-z_\-^\.]+\.[a-z]{2,3}/;
	if (!thereg.test($(".search-box tr:eq(3) input").val())==true) {
		e_error = "<b>Некорректный e-mail</b>";
		$("table.search-box tr:eq(3) span").css("color", "red");
		$(".search-box tr:eq(3) input").css("border", "1px red solid");
	}
	
	if (!q_error && !n_error && !e_error) {
		document.getElementById("qform").submit();
	}


	
	if (q_error=="") {q_error = "Ваш вопрос"; $("table.search-box tr:eq(1) span").css("color", "white"); $("textarea[name=text]").css("border", "inherit");}
	if (n_error=="") {n_error = "Ваше имя"; $("table.search-box tr:eq(2) span").css("color", "white"); $("input[name=name]").css("border", "inherit");}
	if (e_error=="") {e_error = "E-mail"; $("table.search-box tr:eq(3) span").css("color", "white"); $(".search-box tr:eq(3) input").css("border", "inherit");}
	
	
	
	$("table.search-box tr:eq(1) span").html(q_error);
	$("table.search-box tr:eq(2) span").html(n_error);
	$("table.search-box tr:eq(3) span").html(e_error);
	
}
	
$(document).ready(function ()
{

	var questions = ["как передать видеосигнал на 3 км", "сколько стоит RADWIN 2000 B", "как передать по WiFi 100 Мбит/с на 10 км"], question_n=Math.floor(Math.random()*3);
	
	if ($("#td_search_box").length>0) {
		typing();
		window.setInterval(typing, 15000);
	}
	
	function typing() {
		question_n = (question_n+1)%3;
		$("input.prim[name=prim]").attr("value", questions[question_n]);
		$("input.prim[name=prim]").typewriterz();
	}
	
	$("#lend").click(function(){
		$(".head").trigger("click");
	});
	
	$("#lend-left").click(function(){
		$(".head").trigger("click");
	});

	$(".head").click(function(){
		if (-[1,]) {
			$(".prim_field").show();
			$(".additional_field").hide();	
			$("TD#td_search_box").css({"background": "url(/img/fon_centre3.jpg)", "borderBottom": "none"});
		}
	});
	
    resizeimg("lend", 777, 363);
    resizeimg("lend-left", 238, 78);
    $(window).resize(function ()
    {
        resizeimg("lend", 777, 363);
        resizeimg("lend-left", 238, 78)
    });
	
	
	$(".but_send_msg").append('<input type="hidden" name="send" value="ok" /><div class="q-button" onclick="validation();" />');

	
    $("input[Name='prim']").focus(function ()
    {
		$("#td_search_box").css({"background": "url(/img/fon_centre4.jpg)", "borderBottom": "1px #131313 solid"});
        $(".prim_field").hide();
        $(".additional_field").show();
        $(".text-problem").focus()
    });
    $(".servise-menu DIV").bind("mouseenter", function ()
    {
        $(this).css({
            "cursor" : "pointer"
        });
        var id = $(".servise-menu DIV").index(this) + 1;
        $(".arrow DIV").css({
            "background" : "none"
        });
        $(".arrow DIV:nth-child(" + id + ")").css({
            "background" : "url(img/triangle.gif) no-repeat 16px 6px"
        });
        $(".text DIV").hide();
        $(".text DIV:nth-child(" + id + ")").show()
    })
});

