// left menu
$(document).ready(function(){
    


    $('.left_menu li ul').hide();
    $('.test > a').click(function(){
        if($(this).parent().children('ul').css('display')=='none'){
        $('.left_menu ul').hide();
        $('.left_menu li > a').removeClass('active-m');
        $(this).addClass('active-m');
        $(this).parent().children('ul').show(); 
        }
        else{
            $('.left_menu li > a').removeClass('active-m');
            $('.left_menu li ul li > a').removeClass('active-mm');
            $(this).parent().children('ul').hide();
            }
    });
    $('.test-2 > a').click(function(){
        if($(this).parent().children('ul').css('display')=='none'){
        $('.left_menu ul li ul').hide();
        $('.left_menu li ul li > a').removeClass('active-mm');
        $(this).addClass('active-mm');
        $(this).parent().children('ul').show(); 
        }
        else{
            $('.left_menu li ul li > a').removeClass('active-mm');
            $(this).parent().children('ul').hide();
            }
            
    }); 
    
        //$('.menu-level li ul').hide();
   /*     $('.click > a').click(function(){
        if($(this).parent().children('ul').css('display')=='none'){
        $('.menu-level li ul').hide();
        $('.menu-level li > a').removeClass('active-mm');
        $(this).addClass('active-mm');
        $(this).parent().children('ul').show(); 
        }
        else{
            $('.menu-level li > a').removeClass('active-mm');
            $('.menu-level li ul li > a').removeClass('active-mm');
            $(this).parent().children('ul').hide();
            }
    });*/

	$('.menu >li').hover(
	    function(){
		$(this).children('.popup').show();
		var index = $(this).children('a').attr('class');
		index = index.substring(index.length - 1, index.length);
		$(this).children('a').addClass('ca_'+index);
        },
		function(){
		$('.popup').hide();
		var index = $(this).children('a').attr('class');
		index = index.substring(index.length - 1, index.length);
		$(this).children('a').removeClass('ca_'+index);
        }
    );

    

        
        // карта
    $('.none').hide();
    $('.town').hover(
        function(){
        $('.none').show();
        },
        function(){
        $('.none').hide();
        }
    );
    
        // плашка
    $('.pla').hide();
    $('.servis').hover(
        function(){
        $('.pla').show();
        },
        function(){
        $('.pla').hide();
            $('.pla').hover(
            function(){
                $('.pla').stop();
                $('.pla').show();   
            },
            function(){
                $('.pla').hide();   
            });
        }
    );
    
    // Выпадающее меню
    $('.popup').hide();
    $('.menu > div').hover(
        function(){
        //$('.popup',this).animate({opacity: "show"}, "slow");
        //$('.popup',this).css('display','inline');
        var index = $('.n-a',this).attr('class');
        index = index.substring(index.length - 1, index.length); 
        $(this).children('a').removeClass('c_'+index);
        $(this).children('a').addClass('ca_'+index);
        },
        function(){
        //$('.popup ul li ul').hide();
        $('.popup ul li a').removeClass('a-act');
        //$('.popup',this).animate({opacity: "hide"}, "fast");
        var index = $('.n-a',this).attr('class');
        index = index.substring(index.length - 1, index.length); 
        $(this).children('a').removeClass('ca_'+index);
        $(this).children('a').addClass('c_'+index);
        
        }
    );
    
    $('.popup ul li ul').hide();
 /*   $('.popup ul li').click(
        function(){
        //$(this).children('ul').slideToggle()
        //$(this).children('.a-m').toggleClass('a-act');
        if($(this).children('ul').css('display')=='none'){
        $('.popup ul li ul').hide();
        $('.popup ul li a').removeClass('a-act');
        $(this).children('.a-m').addClass('a-act');
        $(this).children('ul').show();  
        }
        else{
            $(this).children('.a-m').removeClass('a-act');
            $(this).children('ul').hide();
            }
    });*/


    
    
   // калькулятор
    $('#press').click(
        function(){
        $('.display-n').slideToggle();
        

        }
    
    );
    
    // типа флешка

    //$('.text-panel p').text(volgograd[0]);
    $('#t-2').click(
        function(){
        
        if(cur_city==1) {
            $(this).addClass('pl-1');
            if($(this).hasClass('pl-2')){
                $('.main-town').text('Краснодар');
                $(this).removeClass('pl-2');
                cur_city=2;
                $('.k-1').hide();
                $('.v-1').show();
                $('.v-3').hide();
                $('.k-3').show();
                $('.text-panel p').html(krasnodar[0]);
                
                JsHttpRequest.query(
                    '?action=setCity',
                    {
                        "city": 2
                    },
                    function(result, errors) {
                        if (errors) {
                            alert(errors);
                            return;
                        }
                    },
                    true
                );
                    
            }
            else if ($(this).hasClass('pl-3')){
                $(this).removeClass('pl-3');
                $('.main-town').text('Астрахань');
                cur_city=3;
                $('.a-1').hide();
                $('.v-1').show();
                $('.a-3').hide();
                $('.k-3').show();
                $('.text-panel p').html(astrahan[0]);
                
                JsHttpRequest.query(
                    '?action=setCity',
                    {
                        "city": 3
                    },
                    function(result, errors) {
                        if (errors) {
                            alert(errors);
                            return;
                        }
                    },
                    true
                );
           }

            

        }
        
        else if(cur_city==2) {
            $(this).addClass('pl-2');
            if($(this).hasClass('pl-1')){
                $('.main-town').text('Волгоград');
                $(this).removeClass('pl-1');
                cur_city=1;
                $('.v-1').hide();
                $('.k-1').show();
                $('.k-3').hide();
                $('.v-3').show();
                $('.text-panel p').html(volgograd[0]);
                
                JsHttpRequest.query(
                    '?action=setCity',
                    {
                        "city": 1
                    },
                    function(result, errors) {
                        if (errors) {
                            alert(errors);
                            return;
                        }
                    },
                    true
                );
            }
            else if ($(this).hasClass('pl-3')){
               $('.main-town').text('Астрахань');
                $(this).removeClass('pl-3');
                cur_city=3;
                $('.a-1').hide();
                $('.k-1').show();
                $('.k-3').hide();
                $('.a-3').show();
                $('.text-panel p').html(astrahan[0]);
                
                JsHttpRequest.query(
                    '?action=setCity',
                    {
                        "city": 3
                    },
                    function(result, errors) {
                        if (errors) {
                            alert(errors);
                            return;
                        }
                    },
                    true
                );
           }
            
            
        }
        else if(cur_city==3) {
            $(this).addClass('pl-3');
            if($(this).hasClass('pl-2')){
                $('.main-town').text('Краснодар');
                $(this).removeClass('pl-2');
                cur_city=2;
                $('.k-1').hide();
                $('.a-1').show();
                $('.a-3').hide();
                $('.k-3').show();
                $('.text-panel p').html(krasnodar[0]);
                
                JsHttpRequest.query(
                    '?action=setCity',
                    {
                        "city": 2
                    },
                    function(result, errors) {
                        if (errors) {
                            alert(errors);
                            return;
                        }
                    },
                    true
                );
            }
            else if ($(this).hasClass('pl-1')){
               $('.main-town').text('Волгоград');
                $(this).removeClass('pl-1');
                cur_city=1;
                $('.v-1').hide();
                $('.a-1').show();
                $('.a-3').hide();
                $('.v-3').show();
                $('.text-panel p').html(volgograd[0]);
                
                JsHttpRequest.query(
                    '?action=setCity',
                    {
                        "city": 1
                    },
                    function(result, errors) {
                        if (errors) {
                            alert(errors);
                            return;
                        }
                    },
                    true
                );
           }
        }
 
     });
     
     $('#t-3').click(
        function(){

        if(cur_city==1) {
            $(this).addClass('pl-1');
            if($(this).hasClass('pl-2')){
                $('.main-town').text('Краснодар');
                $(this).removeClass('pl-2');
                cur_city=2;
                $('.k-2').hide();
                $('.v-2').show();
                $('.v-3').hide();
                $('.k-3').show();
                $('.text-panel p').html(krasnodar[0]);
                
                JsHttpRequest.query(
                    '?action=setCity',
                    {
                        "city": 2
                    },
                    function(result, errors) {
                        if (errors) {
                            alert(errors);
                            return;
                        }
                    },
                    true
                );
                
            }
            else if ($(this).hasClass('pl-3')){
               $('.main-town').text('Астрахань');
                $(this).removeClass('pl-3');
                cur_city=3;
                $('.a-2').hide();
                $('.v-2').show();
                $('.v-3').hide();
                $('.a-3').show();
                $('.text-panel p').html(astrahan[0]);
                
                JsHttpRequest.query(
                    '?action=setCity',
                    {
                        "city": 3
                    },
                    function(result, errors) {
                        if (errors) {
                            alert(errors);
                            return;
                        }
                    },
                    true
                );
           }

            

        }
        
        else if(cur_city==2) {
            $(this).addClass('pl-2');
            if($(this).hasClass('pl-1')){
                $('.main-town').text('Волгоград');
                $(this).removeClass('pl-1');
                cur_city=1;
                $('.v-2').hide();
                $('.k-2').show();
                $('.k-3').hide();
                $('.v-3').show();
                $('.text-panel p').html(volgograd[0]);
                
                JsHttpRequest.query(
                    '?action=setCity',
                    {
                        "city": 1
                    },
                    function(result, errors) {
                        if (errors) {
                            alert(errors);
                            return;
                        }
                    },
                    true
                );
                
            }
            else if ($(this).hasClass('pl-3')){
               $('.main-town').text('Астрахань');
                $(this).removeClass('pl-3');
                cur_city=3;
                $('.a-2').hide();
                $('.k-2').show();
                $('.k-3').hide();
                $('.a-3').show();
                $('.text-panel p').html(astrahan[0]);
                
                JsHttpRequest.query(
                    '?action=setCity',
                    {
                        "city": 3
                    },
                    function(result, errors) {
                        if (errors) {
                            alert(errors);
                            return;
                        }
                    },
                    true
                );
                
           }

            
            
        }
        else if(cur_city==3) {
            $(this).addClass('pl-3');
            if($(this).hasClass('pl-2')){
                $('.main-town').text('Краснодар');
                $(this).removeClass('pl-2');
                cur_city=2;
                $('.k-2').hide();
                $('.a-2').show();
                $('.a-3').hide();
                $('.k-3').show();
                $('.text-panel p').html(krasnodar[0]);
                
                JsHttpRequest.query(
                    '?action=setCity',
                    {
                        "city": 2
                    },
                    function(result, errors) {
                        if (errors) {
                            alert(errors);
                            return;
                        }
                    },
                    true
                );
            }
            else if ($(this).hasClass('pl-1')){
               $('.main-town').text('Волгоград');
                $(this).removeClass('pl-1');
                cur_city=1;
                $('.v-2').hide();
                $('.a-2').show();
                $('.a-3').hide();
                $('.v-3').show();
                $('.text-panel p').html(volgograd[0]);
                
                JsHttpRequest.query(
                    '?action=setCity',
                    {
                        "city": 1
                    },
                    function(result, errors) {
                        if (errors) {
                            alert(errors);
                            return;
                        }
                    },
                    true
                );
           }
            

        }
 
     });

});    

function check (name, type){
    if (type=='text'){
        if(document.getElementById(name).value ==""){
            document.getElementById(name+"_check").className='not';
        }
        if(document.getElementById(name).value !=""){
            document.getElementById(name+"_check").className='yes';
        }
        
    }
    if (type=='email'){
        if(checkMail(document.getElementById(name).value)){
            document.getElementById(name+"_check").className='yes';
        }
        else{
            document.getElementById(name+"_check").className='not';
        }
        
    }
    
    
}

function checkMail(text)
    {
    var email = text;
    var errors = '';
    emailTest = "^[_\\.0-9a-z-]+@([0-9a-z][0-9a-z_-]+\\.)+[a-z]{2,4}$";
    var regex = new RegExp(emailTest);
    if (!regex.test(email) || !(email.length > 0)) {return false;}
    else {return true;}
}

function calculate(deposit){

    var city=document.getElementById('town').value;  	//город
    var val=document.getElementById('currency').value; 	//валюта
    var summm=document.getElementById('summm').value; 	//сумма вклада
    var time=document.getElementById('days').value; 	//срок вклада
    
    JsHttpRequest.query(
            '?action=calculate',
            {
            	"deposit": deposit,
                "city": city,
                "val": val,
                "summm": summm,
                "time": time
            },
            function(result, errors) {
                if (errors) {
                    alert(errors);
                    return;
                }
                reloadBlock(result);
                return result['summ'];
            },
            true
        );
    
}

function reloadBlock(result){
    var procent=document.getElementById('procent');
    var psumm=document.getElementById('psumm');
    var summ=document.getElementById('summ');
    var summm=document.getElementById('summm').value;

    procent.innerHTML=number_format(result.rate, 2, ',', ' ');
    res = parseFloat(result.summ)-parseFloat(summm);

    psumm.innerHTML=number_format(res, 2, ',', ' ');

    summ.innerHTML=number_format(parseFloat(result.summ), 2, ',', ' ');
    if(document.getElementById('minsumm')){
		document.getElementById('minsumm').innerHTML=result.minsumm;
		document.getElementById('minval').innerHTML=result.valute_head;
	}
	document.getElementById('pr_val').innerHTML=result.valute_head;
	document.getElementById('sum_val').innerHTML=result.valute_head;

/*
    if(document.getElementById('cuselFrame-params')){
        var txt = '<option value="0">Нет</option>';
        if (basket.length > 0) {            
            
            for (i = 0; i < basket.length; i++) {
                txt = txt + '<option value="' + basket[i]['id'] + '">' + basket[i]['params'] + '</option>';                
            }            
            
        }

        
        document.getElementById("cost_d").innerHTML = "";
        document.getElementById("add").innerHTML = "";
        document.getElementById("num").innerHTML = "";
        document.getElementById("summ_item").innerHTML = "";

      var addedSelect =   '<select name="params" id="params" onChange="loadItem();">'+txt+'</select>';

      jQuery("#cuselFrame-params").replaceWith(addedSelect);
       
      var params = {
          changedEl: "#params",
          visRows: 5,
          scrollArrows: true ,
          checkZIndex: true
      }
      cuSel(params);
        
    }*/
    

}
 

function number_format( number, decimals, dec_point, thousands_sep ) {
    var n = number, prec = decimals;
    var toFixedFix = function (n,prec) {
        var k = Math.pow(10,prec);
        return (Math.round(n*k)/k).toString();
    };
 
    n = !isFinite(+n) ? 0 : +n;
    prec = !isFinite(+prec) ? 0 : Math.abs(prec);
    var sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
    var dec = (typeof dec_point === 'undefined') ? '.' : dec_point;
 
    var s = (prec > 0) ? toFixedFix(n, prec) : toFixedFix(Math.round(n), prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;
 
    var abs = toFixedFix(Math.abs(n), prec);
    var _, i;
 
    if (abs >= 1000) {
        _ = abs.split(/\D/);
        i = _[0].length % 3 || 3;
 
        _[0] = s.slice(0,i + (n < 0)) +
              _[0].slice(i).replace(/(\d{3})/g, sep+'$1');
        s = _.join(dec);
    } else {
        s = s.replace('.', dec);
    }
    if (s.indexOf(dec) === -1 && prec > 1) {
        s += dec+new Array(prec).join(0)+'0';
    }
    
    var e = s.split(dec_point);
    var start = e[0] ? e[0] : '';
    var end = e[1] ? e[1] : '';
    
    if (end.length < decimals) {
        for (var i = end.length; i < decimals; i++) {
            end += '0';
        }
        s = start + dec_point + end;
    }
    
    return s;
}


