$(document).ready(function(){
    //call order form
    $(".ilabel").ilabel();
    $("#tel_code").keypress(function(e){
        if ($(this).val().length>2){
            $("#tel_number").focus();
        }
    });
    //fix to put the cursor at the end of string
    $("#tel_code, #tel_number").focus(function(){
        this.value = this.value
    });
    $("#tel_number").keydown(function(e){
        if( e.which == 8 && $(this).val().length==0){
            $("#tel_code").focus();
            $(this).parent().find('.ilabel').css({
                'text-indent': '0px'
            });
        }
    });
    $("#b-call-me input").keypress(function(e){
        if (!(e.which>47 && e.which<58)&& e.which != 8) return false
    });
    

    $("#loader-close a").click(function(){
        $("#ajax").hide();
        $("#loader-content").html('');
        $("#loader-close").hide();
    });
    
    $("#b-call-me button").click(function(){
        $.ajax({  
            type: 'POST',
            url: '/contacts/callback/',
            data: 'tel_code=' + $('#tel_code').val() + '&tel_number=' + $('#tel_number').val(),
            beforeSend: function(){
                $("#loader-close").hide();
                $("#loader-content").html('Пожалуйста, подождите.');
                $("#ajax").show();
            },
            success: function(html){
                $("#loader-close").show();
                $("#loader-content").html(html);
            }
        });  
    });



    $(".call-me").click(function(){
        if ($("#b-call-me").is(":visible")){
            $("#b-call-me").hide();
            $(this).removeClass("noborder");
        }else{
            $("#b-call-me").show();
            $(this).addClass("noborder");
        }
    });
    $("#b-call-me .b-close").click(function(){
        $("#b-call-me").hide();
        $(".call-me").removeClass("noborder");
    });

    //nav hover
    $("header nav ul li").hover(function() {
        $(this).animate({
            backgroundColor: '#8A8A8A'
        });
    }, function() {
        $(this).animate({
            backgroundColor: '#505050'
        });
    });

    //opacity play
    $(".b-type").hover(function() {
        $(this).find("img").animate({
            opacity: .7
        });
    }, function() {
        $(this).find("img").animate({
            opacity: 1
        });
    });
    $(".b-labels li").hover(function() {
        $(this).animate({
            opacity: .8
        });
    }, function() {
        $(this).animate({
            opacity: 1
        });
    });

    //image align
    $("#content img").each(function(i,el){
        $(this).addClass("align" + $(this).attr('align'));
    });

    //b-labels stickyfloat
    var $scrollingDiv = $("aside .b-labels");
    $(window).scroll(function(){
        if ($(window).scrollTop()>330){
            $scrollingDiv.addClass("fixed");
            if ($(window).scrollTop()>$("#content").height()-50){
                $scrollingDiv.css({
                    "top":$("#content").height()-$(window).scrollTop()-63+"px"
                });
            }
            else{
                $scrollingDiv.css({
                    "top":"5px"
                });
            }
        }
        else{
            $scrollingDiv.removeClass("fixed");
        }
    });

    //summa stickyfloat	
    var $stickyblock = $(".summa");
    $(window).scroll(function(){
        if ($(window).scrollTop()>$(document).height()-$(window).height()-150)
            $stickyblock.removeClass("fixed");
        else
            $stickyblock.addClass("fixed");
    });

    //b-catalog-item even
    $(".b-catalog-item:odd").addClass("odd");

    //b-text-item even
    $(".b-text-item:odd").addClass("odd");

    $("a[rel=stones]").fancybox();

    //forms
    $("form.kbForm").kbForm();
   
    
});
