var window_height = 185;
var time = 10;
var time_flag;
var count;
var scroll_pixels = 12;

function start_carousel()
{
    count = 0;
    time_flag = setTimeout("carousel_step()", 3000);
}


function carousel_step()
{
     if(add_count() > 3)
        return;
     next_product();
     time_flag = setTimeout("carousel_step()", 3000);
}

function add_count()
{
    count++;
    return count;
}


function item_click(id)
{
    var li = document.getElementById("home_products_list").getElementsByTagName("li");
    var current_id = li[1].id
    
    if(id == current_id)
        return false;
    if(current_id == 'item1')
    {
        if(id == 'item3')
        {
            prev_product();
            return false;
        }
        if(id == 'item2')
        {
            next_product();
            return false;
        }
    }
    if(current_id == 'item2')
    {
        if(id == 'item1')
        {
            prev_product();
            return false;
        }
        if(id == 'item3')
        {
            next_product();
            return false;
        }
    }
    if(current_id == 'item3')
    {
        if(id == 'item1')
        {
            next_product();
            return false;
        }
        if(id == 'item2')
        {
            prev_product();
            return false;
        }
    }
    return false;
}


function color_side_panel_css(id)
{
    id = "product_list_" + id;
    color_item = document.getElementById(id);
    color_item.style["backgroundColor"] = "#7f1113";
    color_item.style["color"] = "white";
}


function reset_side_panel_css(id)
{
    id = "product_list_" + id;
    reset_item = document.getElementById(id);
    reset_item.style["backgroundColor"] = "white";
    reset_item.style["color"] = "black";
}


function swap_next_nodes()
{
    var li = document.getElementById("home_products_list").getElementsByTagName("li");
    var list = document.getElementById("home_products_list");
    var num = li.length - 1;
    list.insertBefore(li[num], list.firstChild);
    reset_side_panel_css(li[2].id); 
    color_side_panel_css(li[1].id); 
}


function swap_prev_nodes()
{
    var li = document.getElementById("home_products_list").getElementsByTagName("li");
    var list = document.getElementById("home_products_list");
    list.insertBefore(li[0], list.lastChild.nextSibling); 
    reset_side_panel_css(li[0].id); 
    color_side_panel_css(li[1].id);   
}


function next_product()
{
    if(time_flag)
        clearTimeout(time_flag);
    swap_next_nodes();
    var id = document.getElementById("home_products_list");
    var pos = -2 * window_height;
    id.style["marginTop"] = pos + "px"; 
    scroll_next_position();
}


function prev_product()
{
    if(time_flag)
        clearTimeout(time_flag);
    swap_prev_nodes();
    var id = document.getElementById("home_products_list");
    var pos = 0;
    id.style["marginTop"] = pos + "px"; 
    scroll_prev_position();
}


function get_current_pos(id)
{
    var str = eval("id.style.marginTop"); 
    if((str != "") && (str != null)) 
        str = str.replace(/px/,"");
    else
        str = "0";            
    a = parseInt(str);   
    
return a;
}


function scroll_next_position()
{
    var id = document.getElementById("home_products_list");
    var next_pos = get_current_pos(id);
    next_pos = next_pos + get_scroll_pixels();
    if(next_pos >= -window_height)
    {
        next_pos = next_pos - get_scroll_pixels();
        dif = next_pos % window_height;
        next_pos = next_pos - dif;
        id.style["marginTop"] = next_pos + "px"; 
        return;
    }
    id.style["marginTop"] = next_pos + "px"; 
    setTimeout("scroll_next_position()", time);
}


function scroll_prev_position()
{
    var id = document.getElementById("home_products_list");
    var prev_pos = get_current_pos(id);
    prev_pos = prev_pos - get_scroll_pixels();
    if(prev_pos <= -window_height)
    {
        dif = prev_pos % window_height;
        prev_pos = prev_pos - dif;
        id.style["marginTop"] = prev_pos + "px"; 
        return;
    }
    id.style["marginTop"] = prev_pos + "px"; 
    setTimeout("scroll_prev_position()", time);
}


function get_scroll_pixels()
{
    return scroll_pixels;
}

