/*Функции скролла слайдера продуктов.*/
scroll_id = 'pscroll';
scroll_step = 100;
scroll_timeout = false;
scroll_speed = false;
scroll_direction = false;

function products_scroll(direction, speed) {
    var scroll, scroll_position, max_right_scroll;

    if (direction != 'left' && direction != 'right') {
        return false; 
    }
    
    scroll = $('#' + scroll_id);
    scroll_position = scroll.scrollLeft();
    max_right_scroll = scroll.find('td').length * scroll_step;

    if (direction == 'left' && scroll_position > speed) {
        scroll.scrollLeft(scroll_position - speed);
    } else if (direction == 'right' && scroll_position <= max_right_scroll - speed) {
        scroll.scrollLeft(scroll_position + speed);
    }
    scroll_timeout = setTimeout('products_scroll("' + direction + '", ' + speed + ')', 50);
}

$(document).ready(function () {
    $(".main-category:first").css('margin-top', 0);

    max_scroll = $('#pscroll').find('td').length * scroll_step;
    $('#pscroll').scrollLeft(max_scroll/2);
    
    $('#pscroll').mouseover(function (e) {
        // Определяем направление и скорость.
        var scroll, scroll_p, window_width, cursor_position, minX, rel, direction, speed, sc;
        scroll = $('#pscroll');
        scroll_p = scroll.parent()

        sc = new $(window).get(0).screen;
        window_width = sc.width;
        max_width = scroll_p.innerWidth();
        cursor_position = e.clientX;
        minX = ( window_width - max_width ) / 2;
        rel = 100*(cursor_position - minX)/max_width;
        if (rel <= 25 ) {
            direction = 'left';
            speed = 5;
        } else if (rel > 25 && rel <= 50) {
            direction = 'left';
            speed = 2;
        } else if (rel > 50 && rel <= 75) {
            direction = 'right';
            speed = 2;
        } else {
            direction = 'right';
            speed = 5;
        }
        if (scroll_speed !== speed || scroll_direction !== direction) {
            scroll_speed = speed;
            scroll_direction = direction;
            if (scroll_timeout != false) {
                clearTimeout(scroll_timeout);
            }
            products_scroll(direction, speed);
        }
        
    });
    $('#pscroll').mouseout(function () {
        scroll_speed = false;
        scroll_direction = false;
        if (scroll_timeout != false) {
            clearTimeout(scroll_timeout);
        }
    });

    $(".category-link").hover(
        function () {
            if ($(this).hasClass('category-open') == false) {
                $(this).addClass('category-arrow');
            }
        },
        function () {
            if ($(this).hasClass('category-open') == false) {
                $(this).removeClass('category-arrow');
            }
        }
    );
    $(".category-link").click(function () {
            var cur_link, open_class;
            open_class = 'category-open';
            cur_link = $(this);
            if (cur_link.hasClass(open_class)) {
                cur_link.removeClass('category-arrow');
                cur_link.removeClass(open_class);
                $(cur_link.parent()).nextUntil('.category, .main-category').css('display', 'none');
            } else {
                cur_link.addClass('category-arrow');
                cur_link.addClass(open_class);
                $(cur_link.parent()).nextUntil('.category, .main-category').css('display', 'block');
            }
            return false;
    });
    $('#search_q').focus(function () {
        var defaultTerm;
        defaultTerm = $(this).attr('defaultTerm');
        if ($(this).val() == defaultTerm) {
            $(this).val('');
        }
    });
    $('#search_q').blur(function () {
        var defaultTerm;
        if ($(this).val() == '') {
            defaultTerm = $(this).attr('defaultTerm');
            $(this).val(defaultTerm);
        }
    });
    $('#note_image_print').click(function () {
        var src_img, fwin, win_url;
        src_img = $($('#note_image_print').prev('a').get(0)).attr('href');
        if (src_img == '') {
            return false;
        }
        win_url = $('#note_image_url').val() + '?image=' + src_img;
        fwin = window.open(win_url, 'Распечатка файла');
    });
});

