var path = document.location.pathname;
if ( path.charAt(path.length-1) != '/' ) path = path+'/';

var epos = document.location.pathname.indexOf('/',1) == -1 ? document.location.pathname.length : document.location.pathname.indexOf('/',1)-1;
var section = document.location.pathname.substr(1, epos );	

$(document).ready(init);

var menuImages = new Array();

var streetSelected = true;

function init() {
	$("#menu a").each (
		function () {
			var key = $(this).attr("href").replace('/','');
			
			menuImages[key] = new Array();
			menuImages[key]['default'] = new Image();
			menuImages[key]['default'].src = "/images/menu/default/"+key+".gif";
			
			menuImages[key]['over'] = new Image();
			menuImages[key]['over'].src = "/images/menu/over/"+key+".gif";
			
			menuImages[key]['selected'] = new Image();
			menuImages[key]['selected'].src = "/images/menu/selected/"+key+".gif";
		}
	);
    
    try {
        if ( path != '/' ) {        
            setTimeout('showAlso()',2000);
            $("#menu a[href^='/"+section+"'] img").get(0).src = menuImages[section]['selected'].src;
            
            $("table.data-c1 tr:nth-child(odd)").addClass("grey");
            $("table.data-c2 tr:nth-child(odd)").addClass("grey");
            $("table.data-c3 tr:nth-child(odd)").addClass("grey");            
            $("table.data-c4 tr:nth-child(odd)").addClass("grey");

            $("table.data-c1 tr th:first, table.data-c2 tr th:first, table.data-c3 tr th:first, table.data-c4 tr th:first").addClass("left");
            $("table.data-c1 tr th:last, table.data-c2 tr th:last, table.data-c3 tr th:last, table.data-c4 tr th:last").addClass("right");
        }        
    }
    catch ( e ) {}
    
    $("#add-form-step3").click(attachForm2Step3);
    $("a.close-form3").click(
        function () {
            $(this).parent().remove();
            return false;
        }
    );
	
	$("#menu a").hover(
		function () {            
			var key = $(this).attr("href").replace('/','');
            if ( section == key ) return;
			$("img", this).get(0).src = menuImages[key]['over'].src;            
		},
		function () {
			var key = $(this).attr("href").replace('/','');
            if ( section == key ) return;
			$("img", this).get(0).src = menuImages[key]['default'].src;
		}
	);
    
    $("a.connect-step2-type").toggle(
        function () { $("#"+$(this).attr("rel")).fadeIn(); },
        function () { $("#"+$(this).attr("rel")).fadeOut(); }
    );
    
    
    $("input.select").keyup (
        function () {            
            streetSelected = false;
            setTimeout("getStreets('"+$(this).attr("value")+"','"+$(this).attr("rel")+"')", 2000);
        }
    );
    
    /*$("input.select").blur(
        function () {            
            streetSelected = true;
            $(this).removeClass("opened");
            $("#"+$(this).attr("rel")+" .select-load").hide();
            $("#"+$(this).attr("rel")+" .select-data").hide();
        }
    );*/
	
	$("#ireadit").change(function() {
		if ($("#goto-billing :checked").length == 1) 
			$.post('/ajax/ireadit.php', {
				'ireadit': $("#ireadit").val()
			}, function(){
				$("#login-button").show();
				$("#login-title").show();
			});
		else {
			$("#login-button").hide();
			$("#login-title").show();
			$.post('/ajax/ireadit.php',{
				'idontreadit': $("#ireadit").val()
			},function(){
				$("#login-button").show();
				$("#login-title").show();
			});
		}
	});
	
	$("#goto-billing").submit(function(){
		if ( $("#goto-billing input[name='login']").val() == '' || $("#goto-billing input[name='password']").val() == '' ) {
			$("#goto-billing label").css({'color':'#f00'});
			return false;
		}
		
		if ( $("#goto-billing").attr("rel") != 'need-read' )
			return true;
		
		$("#login-form").slideUp();
		$("#login-title").hide();
		$("#all-news").slideUp(function(){$("#required-news").slideDown();});
			
		if ( $("#goto-billing :checked").length == 1 ) {
			return true;
		}
		
		$("label[for='ireadit']").css('color','#ef0000');
		return false;
	});
}

function showAlso() {    
    $("#submenu-"+section).fadeIn();
}

function getStreets( street, select ) {
    
    if ( !streetSelected ) {
        $("#"+select+" .select-load").show();
        
        $("#"+select+" div.select-data").load("/ajax/streets.php", { 'street': street, 'action': 'get-streets' }, onAjaxSuccess);
    }
}

function onAjaxSuccess ( data ) {    
    if ( data.length > 0 ) {
        $(this).show();                
        $("input[rel='"+$(this).parent().attr("id")+"']").addClass("opened");
        
        $("#"+$(this).parent().attr("id")+" .select-data a").click (
            function () {
                
                var id = $(this).parent().parent().attr("id");
                $("input[rel='"+id+"']").attr("value", $(this).text());
                $("input[rel='"+id+"']").removeClass("opened");
                $(this).parent().hide();
                return false;
            }            
        );
        
    }
    else {
        $(this).hide();
        $("input[rel='"+$(this).parent().attr("id")+"']").removeClass("opened");
    }
    $("#"+$(this).parent().attr("id")+" .select-load").hide();
}

function attachForm2Step3() {
    var tpl = "<div class='form-step3'>\
            <div class='hline'></div>\
            <a href='' class='close-form3'><img src='/images/content/close.gif'/></a>\
            <label>Фамилия \
                <input type='text' name='surname[]'/>\
            </label>\
            <label>Имя\
                <input type='text' name='name[]'/>\
            </label>\
            <label>Отчество\
                <input type='text' name='lastname[]'/>\
            </label>\
            <label>Дата рождения \
                <input type='text' name='bdate[]'/>\
            </label>\
            <label>Рабочий телефон\
                <input type='text' name='wtel[]'/>\
            </label>\
            <label>Мобильный телефон\
                <input type='text' name='stel[]'/>\
            </label>\
            <label>Адрес электронной почты\
                <input type='text' name='email[]'/>\
            </label>\
            ";
            
    if ( $("#form-step3-container").attr("rel") == 2 ) {
        tpl += "<label>Должность\
                <input type='text' name='position[]'/>\
            </label>";
    }
    tpl += '</div>';
            
    $("#form-step3-container").append(tpl);
    
    return false;
}
