function setPrintCSS(isPrint)
{
    var link;

    if (document.getElementsByTagName)
        link = document.getElementsByTagName('link');
    else if (document.all)
        link = document.all.tags('link');
    else
        return;

    for (var i=0; i < link.length; i++)
        {
        if (link[i].title == 'print')
            link[i].disabled = !isPrint;
    }
    $('.scroll-block').css('height', 'auto');
}
if (document.location.hash == '#print') setPrintCSS(true);

$.preloadImages = function(){
    for(var i = 0; i<arguments.length; i++)
        $("<img>").attr("src", arguments[i]);
}

function bannerPagerClicker()
{
    if (jQuery.data($('#banner-pager').get(0), "bannerPagerClicker"))
        {
        var e = $('#banner-pager i.sel').next().get(0) || $('#banner-pager i:first').get(0);
        var id = $(e).siblings().removeClass('sel').end().addClass('sel').text();

        $('#tab-goods').find('div.banner div.b'+id).dequeue().animate({opacity: "1"}, 800).show().siblings().not('#banner-pager').animate({opacity: "0"}, 800).hide();

        setTimeout('bannerPagerClicker()', 10000);
    }
}

function SetUpFormFastOrder()
{
    SubForm = $('#fast_order_form form[name="SIMPLE_FORM_1"]');
    $.post("/inca/fast_order.php",  
    SubForm.serialize(),  
    function(data){
        $('#fast_order_form').html(data);
        dy = parseInt($('.question-form').height(), 10) - 380;
        if (dy>0)
            $('#content-catalog div.bg-wrapper').css('background-position', '0px ' + dy + 'px');
        $('#fast_order_form form[name="SIMPLE_FORM_1"]').submit(function(){return SetUpFormFastOrder();});
    });
    return false;
}

$(document).ready(function(){

    $('#fast_order_form form[name="SIMPLE_FORM_1"]').submit(function (){ return SetUpFormFastOrder();});



    $('#search-div').find('input[name="q"]').placeholder({values: ['поиск'], blockSubmit: true});

    //banner-pager
    var bannerDiv = $('#tab-goods div.banner')[0];
    if (bannerDiv)
        {
        $(".b02, .b03", bannerDiv).css("opacity", "0").hide();
        $('<div id="banner-pager"><i class="sel">01</i><i>02</i><i>03</i></div>').css('z-index', 10)
        .find('i').each(function(){
            this.onclick = function(){
                jQuery.data($('#banner-pager')[0], "bannerPagerClicker", false);

                var id = $(this).siblings().removeClass('sel').end().addClass('sel').text();
                $('.b'+id, bannerDiv).dequeue().animate({opacity: "1"}, 800).show().siblings().not('#banner-pager').animate({opacity: "0"}, 800).hide();

                jQuery.data($('#banner-pager')[0], "bannerPagerClicker", true);  // мое
                setTimeout('bannerPagerClicker()', 10000);                       //мое
            }
        })
        .end().prependTo(bannerDiv);

        //jQuery.data($('#banner-pager')[0], "bannerPagerClicker", true);             // мое  
        //setTimeout('bannerPagerClicker()', 10000);                                  // мое
    }

    //tabs
    $('#index-showcase').find('div.tabs-header a').each(function(){
        $(this).click(function(){
            if (this.className.indexOf('sel-') == -1)
                {
                //$('#content').hide();

                this.className = this.className.replace(/(tab\d)/g, "sel-$1");
                $(this).siblings().each(function(){this.className = this.className.replace(/sel-(tab\d)/g, "$1");});

                var id = this.href.substring(this.href.indexOf('#'));
                $tab = $( $(id)[0] || $(id+'-catalog')[0] );
                $tab.show().siblings().hide();

                if (id=="#content")
                    $("#top-sub-menu").show();
            }
            return false;
        });
    });

    //tab-goods popup menu
    var tgElem = $('#tab-goods .catalog-pad-wrap')[0];
    $('.item-wrap .submenu', tgElem).each(function(){$(this).prependTo(this.parentNode.parentNode)});
    $('.item', tgElem).hover(

    function(e){
        if($.browser.msie)
            $(this).css({backgroundPosition: '-96px 0'}).children('.submenu').show();
        else
            $(this).css({backgroundPosition: '-96px 0'}).children('.submenu').fadeIn('fast');
    },
    function(e){
        if($.browser.msie)
            $(this).css({backgroundPosition: '0 0'}).children('.submenu').hide();
        else
            $(this).css({backgroundPosition: '0 0'}).children('.submenu').fadeOut('fast');
    }

    );
    $('.item-wrap', tgElem).click(function(){window.location = $(this).find('a')[0].href});

    //popup auth form toggle
    $('#basket').find('div.links a.login-link').click(function(){ $('#at-frm-bitrix').toggle(); return false; });

    //news-catalog
    var newsCatalogDiv = $('#news-catalog')[0];
    if (newsCatalogDiv)
        {
        //scrollable
        $("div.scrollable", newsCatalogDiv).scrollable({size: 4, clickable: false, keyboard: false}).mousewheel(500); //.circular(); //.mousewheel();

        $("div.scrollable div.items div.item", newsCatalogDiv).each(function(){
            var cur = $(this).find('a')[0];
            $(this).find('div.pic i').css('cursor', 'pointer').click(function(){if (cur && cur.href) location.href = cur.href;});
        });

        // растягиваем крутилку с новинками по высоте новинок
        var items_height = $('div.scrollable > div.items').height();
        //alert(items_height);
        if (items_height<300)
            $('div.scrollable').height(items_height);
        else 
            $('div.scrollable').height(300);

        //выравниваем стрелки прокрутки по центру по вертикали
        //$('a.next').css('margin-top', items_height/2-35);
        //$('a.prev').css('margin-top', items_height/2-35);
    }

    // растягиваем блок рекомендуемых товаров справа
    //console.log($('.card').height());
    //console.log($('.question-form').height());
    //console.log($('.recomend .top').height());
    //console.log($('.recomend .middle').height());
    //console.log($('.card').height()-$('.question-form').height()-10-$('.recomend .top').height() - $('.recomend .middle').height()+28);
    if($('.right-block').height() < $('.card').height())
        {
        $('.right-block').height($('.card').height()+14);
        if($('.recomend').height() > 0)
            $('.right-block').height($('.right-block').height()+14);
    }
    else
        $('.card').height($('.right-block').height());
    $('.right-block').css('overflow','hidden');
    $('.card').css('overflow','hidden');

    if($.browser.msie)
        $('#bottom').height($('.right-block').height()-$('.question-form').height()-10-$('.recomend .top').height() - $('.recomend .middle').height()+1);
    else
        $('#bottom').height($('.right-block').height()-$('.question-form').height()-10-$('.recomend .top').height() - $('.recomend .middle').height()+28);



    brcrel = $('#ToBreadCrumbElement').html();
    if(brcrel.length > 0)
        $('#BreadCrumbElement').html(brcrel);

    $('.btn-small-add2basket, .btn-add2basket').fancybox({
        frameHeight: 70, 
        hideOnContentClick: false, 
        hideOnOverlayClick: true, 
        enableEscapeButton: true
    });

    // убираем подсвечивание верхнего меню если выбрана закладка товары и тп.
    $('.tabs-header a').click(function(){
        $('#top-menu li').removeClass('sel');
    })
    
    // скрываем и показываем поля юридического лица на регистрации на втором шаге заказа товара
    if ($('.fields.boolean input').is(':checked'))
        console.log('checked!');
    
    if ($('.fields.boolean input').is(':checked'))
        $('.fields.string').parent().parent().show();
    else
        $('.fields.string').parent().parent().hide();

    $('.fields.boolean input').click(function(){
        if ($('.fields.boolean input').is(':checked'))
            $('.fields.string').parent().parent().show();
        else
            $('.fields.string').parent().parent().hide();

    });
       
    // скрываем или раскрываем поля юр лица в профиле пользователя
    if ($('.user-profile .boolean input').is(':checked'))
        $('.fields.string').parent().parent().show();
    else
        $('.fields.string').parent().parent().hide();
    
    $('.user-profile .boolean input').click(function(){
        if ($(this).is(':checked'))
            $('.fields.string').parent().parent().show();
        else 
            $('.fields.string').parent().parent().hide();
    });
    // email вместо логина
    $('.reg_ok').click(function(){
        $('input.login').val($('input.email').val());
    })

    // косяк графических кнопок в корзине
    $('input.delete').click(function(){
        $(this).prev('input').val('Y');
    })

    // косяк (картики начинают меняться только после того как нажать на другую)
    setTimeout('$("#banner-pager i:eq(1)").click();', 10000);    
    
});

