$(document).ready(function(){
    $(".ilabel").ilabel();
    $("#tel_code").keypress(function(e){if ($(this).val().length>2){$("#tel_number").focus();}});
    $("#tel_code").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'});}});
    $("#callorder input").keypress(function(e){if (!(e.which>47 && e.which<58)&& e.which != 8) return false});
    $("#ajax_close_button").click(function(){$("#ajax").fadeOut("slow");$("#ajax_close_button").hide();});
    $("#callorder button").click(function(){
        $.ajax({  
            type: 'POST',
            url: '/index.php?do=callback',
            data: 'tel_code=' + $('#tel_code').val() + '&tel_number=' + $('#tel_number').val(),
            beforeSend: function(){
                $("#ajax_message").html('<p class="ajax_loader">' + pleaseWait + '</p>');
                $("#ajax").fadeIn("slow");
            },
            success: function(html){
                $("#ajax_message").html('<p>' + html + '</p>');
                $("#ajax_close_button").show();
            }
        });  
        return false;  
    });

    //#m1 dropdown
    var dropdownmenu = {    
        over: m1_show,
        timeout: 400,
        out: m1_hide
    };
    $("#m1 li").hoverIntent(dropdownmenu);
    function m1_show(){$(this).find('ul').slideDown();}
    function m1_hide(){$(this).find('ul').slideUp();}
		
    //image align
    $("#content img").each(function(i,el){
        $(this).addClass("align" + $(this).attr('align'));
    });

    //carousel
    $("#sale .door-scroller").jCarouselLite({
        btnNext: ".r.scroll-arrow",
        btnPrev: ".l.scroll-arrow",
        visible:5
    });
    $("#related .door-scroller").jCarouselLite({
        btnNext: ".r.scroll-arrow",
        btnPrev: ".l.scroll-arrow",
        visible:4
    });

    //complectator
    $("#load_complectator").click(function(){$("#complectator").fadeIn("slow");});
    $("#cmpl_close").click(function(){$("#complectator").fadeOut("slow");});
    $('#patterns li').each(function (index, el) {
        var idx = ('-' + (index*33)-1 + 'px');
        $(el).find('.pattern_pic').css('background-position', idx + ' 0');
    });
    $(".pattern").click(function(){
        if ($(this).hasClass("selected")==true){
            $(this).removeClass("selected");
            $("#param_9").val(0);
        }
        else{
            $(".pattern").each(function() {
                $(this).removeClass("selected");
            });
            $(this).addClass("selected");
            $("#param_9").val($(this).attr('rel'));
        }
    });
    $("#door_price").change(function(){
        var priceArr = $(this).attr('value').split("-");
        $("#search_price_from").val(priceArr[0]);
        $("#search_price_to").val(priceArr[1]);
    });
    $("#door_price").trigger('change');


    zebraRows('.tbl tbody tr:odd td', 'odd');
});

function zebraRows(selector, className) {
    $(selector).removeClass(className).addClass(className);
}
