var active_panel_head_item = 1;
var active_panel_item = 1;

var cycle_interval = 12000;

var products_count = 0;

$(function () {
    $('.product', '#plist').each(function (idx, el) {
        products_count++;
    });
    
    /* window.setTimeout(function () {
        hmpg_panels_head_timer = window.setInterval(function () {
            cyclePanelsHead();
        }, cycle_interval);
    }, 500);
    
    hmpg_panels_timer = window.setInterval(function () {
        cyclePanels();
    }, cycle_interval); */
    
    /* $('#productsPanelHead a').click(function () {
        clearCycle();
        
        var active = $(this).attr('rel');
        
        $('#productsPanelHead li').removeClass('active');
        $(this).parent().parent().parent().addClass('active');
        
        $('.hmpg_panel', '#content').fadeOut(500);
        $('.hmpg_panel', '#content').removeClass('hmpgActive');
        window.setTimeout(function () {
            $('#' + active).fadeIn(500);
            $('#' + active).addClass('hmpgActive');
        }, 600);
    }); */
    
    $('#productsPrev').click(function () {
        moveProducts('prev');
    });
    
    $('#productsNext').click(function () {
        moveProducts('next');
    });
});

/* function cyclePanelsHead() {
    var item_count = 0;
    $('#productsPanelHead li').each(function (idx, el) {
        item_count++;
        if (idx == active_panel_head_item) {
            $(el).addClass('active');
        } else {
            $(el).removeClass('active');
        }
    });
    
    if (active_panel_head_item >= item_count - 1) {
        active_panel_head_item = 0;
    } else {
        active_panel_head_item++;
    }
} */

/* function cyclePanels() {
    var item_count = 0;
    var active = null;
    $('.hmpg_panel', '#content').each(function (idx, el) {
        item_count++;
        if (idx == active_panel_item) {
            active = $(el).attr('id');
            // $(el).show();
        } else {
            // $(el).hide();
        }
    });
    
    $('.hmpgActive', '#content').fadeOut(500);
    $('.hmpgActive', '#content').removeClass('hmpgActive');
    window.setTimeout(function () {
        $('#' + active).fadeIn(500);
        $('#' + active).addClass('hmpgActive');
    }, 600);
    
    if (active_panel_item >= item_count - 1) {
        active_panel_item = 0;
    } else {
        active_panel_item++;
    }
} */

function moveProducts(direction) {
    clearCycle();
    
    var product_width = 192;
    var left = parseInt($('#plist').css('left'), 10);
    
    if (direction == 'prev') {
        if (left < 0) {
            var new_left = left + product_width;
            $('#plist').animate({
                left: '+=' + product_width + 'px'
            }, 'slow');
        }
    }
    if (direction == 'next') {
        if (left > -1 * ((products_count - 3) * product_width)) {
            var new_left = left - product_width;
            $('#plist').animate({
                left: '-=' + product_width + 'px'
            }, 'slow');
        }
    }
}

