$(document).ready(function(){

    content_height = $("#product_slider ul li").height();
    
    tab_count = $("#product_slider").children('.tab').length;

    $.each($("#product_slider li ul"), function(i, elem){

       current_x = 0;

       if($(elem).children('li').length > 1){
           $.each($(elem).children('li'), function(si, content_elem){
               $(content_elem).css('top', current_x+'px');
               current_x = current_x + parseInt(content_height);
           })
       }

    });

    current_y = 0;

    $.each($("#product_slider li.tab"), function(i,elem){
       $(elem).css('left', current_y+'px');

       header_width = parseInt($(elem).children('.header').width()) + 5;
       item_width = parseInt($(elem).parents('#slider_container').width()) - ((header_width) * (tab_count-1));

       $(elem).css('width', item_width);
       
       content_item = $(elem).find('.content');

       content_padding = parseInt(content_item.css('padding-left').replace('px',''))*2;
       content_width = item_width - header_width - content_padding;

       content_item.css('width', content_width);
 
       current_y = current_y + parseInt($(elem).children('.header').width()) + parseInt($(elem).children('.header').css('margin-left'));
    });

    $("#product_slider .header").click(function(e){

        //don't animate if another animation is running
        if($("#product_slider").children(":animated").length > 0){
            return;
        }

        $(".active").removeClass('active');
        $(this).addClass('active');

        tab_width = $(this).width();
        tab_margin = parseInt($(this).css('margin-left'));
        full_width = $("#product_slider").parent().width();
        content_width = parseInt(full_width) - (parseInt(tab_width) * parseInt(tab_count));

        this_pos = 0;
        next_pos = 0;
        prev_pos = 0;

        this_tab = $(this).parents('.tab');

        this_pos = this_tab.css('left').replace('px','');

        if(this_tab.next('.tab_lft').length > 0){
            next_pos = parseInt(this_tab.next('.tab_lft').css('left').replace('px','')) + tab_margin;
        }else if(this_tab.next('.tab_rgt').length > 0){
            next_pos = parseInt(this_tab.next('.tab_rgt').css('left').replace('px','')) + tab_margin;
        }

        if(this_tab.prev('.tab_lft').length > 0){
            prev_pos = parseInt(this_tab.prev('.tab_lft').css('left').replace('px','')) + tab_margin;
        }else if(this_tab.prev('.tab_rgt').length > 0){
            prev_pos = parseInt(this_tab.prev('.tab_rgt').css('left').replace('px','')) + tab_margin;
        }

        if(this_tab.hasClass('tab_lft') && (parseInt(this_pos) + tab_width + (tab_margin*2)) == next_pos){

            $.each(this_tab.nextUntil('.tab_rgt'), function(elem,i){
                curr_lft = parseInt($(this).css('left'));
                new_lft = curr_lft + (content_width - (tab_margin*tab_count));
                $(this).removeClass('tab_lft').addClass('tab_rgt').animate({'left': new_lft+'px'}, 500);
            });

        }else if(this_tab.hasClass('tab_rgt') && parseInt(prev_pos) + tab_width  == this_pos){

            $.each(this_tab.prevUntil('.tab_lft'), function(elem,i){
                curr_lft = parseInt($(this).css('left'));
                new_lft = curr_lft - content_width + (tab_margin*tab_count);
                $(this).removeClass('tab_rgt').addClass('tab_lft').animate({'left': new_lft+'px'}, 500);
            });

            curr_lft = parseInt(this_tab.css('left'));
            new_lft = curr_lft - (content_width - (tab_margin*tab_count));
            this_tab.removeClass('tab_rgt').addClass('tab_lft').animate({'left': new_lft+'px'}, 500);

        }else if(this_tab.hasClass('tab_rgt') && this_tab.prev('tab_rgt').length == 0){

            new_lft = parseInt(this_tab.prev('.tab_lft').css('left').replace('px','')) + tab_width + tab_margin;
            this_tab.removeClass('tab_rgt').addClass('tab_lft').animate({'left': new_lft+'px'}, 500);
        }

        e.preventDefault();

    });

    $("#product_slider .more_next").click(function(e){
        content_item = $(this).parent('div').parent('li');

        curr_top = 0;
        check_top = $(this).css('top').replace('px', '');

        if(!isNaN(check_top)){
            curr_top = check_top;
        }

        new_top = curr_top - content_height;

        $(this).parent('div').parent('li').animate({'top': new_top+'px'}, 500);

        $.each(content_item.siblings(), function(i, elem){
            curr_top = $(elem).css('top').replace('px', '');
            new_top = parseInt(curr_top) - parseInt(content_height);
            $(elem).animate({'top': new_top+'px'}, 500)
        });

        e.preventDefault();
    });

    $("#product_slider .more_prev").click(function(e){
        content_item = $(this).parent('div').parent('li');

        curr_top = 0;
        check_top = $(this).css('top').replace('px', '');

        if(!isNaN(check_top)){
            curr_top = check_top;
        }

        new_top = curr_top + content_height;

        $(this).parent('div').parent('li').animate({'top': new_top+'px'}, 500)

        $.each(content_item.siblings(), function(i, elem){
            curr_top = $(elem).css('top').replace('px', '');
            new_top = parseInt(curr_top) + parseInt(content_height);
            $(elem).animate({'top': new_top+'px'}, 500);
        });

        e.preventDefault();
    });

 
});
