/*
Slider
Author: taihip
Release Date: Sunday, 02 November, 2008
*/

//settings
var isAutoSlide = true;
var duration_per_item = 10000;
var direction = 'left';
var number_item_displayed = 5;

//data container
var itemWidth;
var itemHeight;
var img_container;
var slider_screen;
var orginal_length;
var aditional_length;

function autoSlide()
{
    if(isAutoSlide)
    {
        var items = $('.slider .item');
        var cssLeft = img_container.css('left');
        if(cssLeft == null)
            return false;
        
        var left = Math.abs(cssLeft.substring(0, cssLeft.length - 2));
        
        if (direction == 'left')
        {
            var slide_length = Math.round(orginal_length - left);
            var duration = Math.round(duration_per_item * slide_length / itemWidth);

            img_container.animate(
                {left: '-=' + slide_length}
                , duration
                , 'linear'
                , function(){
                    img_container.css('left', 0 + 'px');
                    autoSlide();
                }
            );
        }
        else if (direction == 'right')
        {
            var slide_length = left;
            var duration = Math.round(duration_per_item * slide_length / itemWidth);

            img_container.animate(
                {left: '+=' + slide_length}
                , duration
                , 'linear'
                , function(){
                    img_container.css('left', '-' + orginal_length + 'px');
                    autoSlide();
                }
            );
        }
    }
}

$(function(){
    //get data
    img_container = $(".slider .img_container");
    var items = img_container.children('.item');
    slider_screen = $('.slider .slider_screen');
    itemWidth = 108//$(items[0]).width();
    itemHeight = 110//$(items[0]).height();

    //clone number item displayed and insert to end of items list
    items.filter(':lt(' + number_item_displayed + ')').clone().each(function(){
        items = img_container.children('.item');
        $(this).insertAfter(items.filter(':last'));
    });

    //set container width & height
    items = img_container.children('.item');
    img_container.width(items.size() * itemWidth).height(itemHeight);

    //set screen width & height
    slider_screen.width(itemWidth * number_item_displayed).height(itemHeight);

    //get orginal length
    orginal_length = Math.round(img_container.width() - itemWidth * number_item_displayed);

    //get aditional length
    aditional_length = itemWidth * number_item_displayed;

    //start slide
    autoSlide();

    //stop event
    slider_screen.hover(
        function(){
            img_container.stop();
            isAutoSlide = false;
        },
        function(){
            isAutoSlide = true;
            autoSlide();
        }
    );

    //change direction event
    $('.direction_left').hover(
        function(){
            if(direction == 'right')
            {
                img_container.stop();
                direction = 'left';
                autoSlide();
            }
        },
        function(){
            img_container.stop();
            autoSlide();
        }
    );

    $('.direction_right').hover(
        function(){
            if(direction == 'left')
            {
                img_container.stop();
                direction = 'right';
                autoSlide();
            }
        },
        function(){
            img_container.stop();
            autoSlide();
        }
    );

    //next & prev event
//    $('.direction_left').click(function(){
//        img_container.stop();
//        var cssLeft = img_container.css('left');
//        var left = Math.abs(cssLeft.substring(0, cssLeft.length - 2));
//        if(left > (orginal_length - aditional_length))
//        {
//            var cssRight = img_container.css('right');
//            var remain_length = Math.abs(cssRight.substring(0, cssLeft.length - 2));
//            if(!remain_length) remain_length = 0;
//            img_container.css('left', '-' + remain_length + 'px');
//            left = '-' + remain_length;
//        }
//        var slide_length = Math.round((Math.round(left / (itemWidth + left%itemWidth)) + number_item_displayed) * itemWidth);

//        img_container.css('left', '-' + slide_length + 'px');
//        return false;
//    });

//    $('.direction_right').click(function(){
//        img_container.stop();
//        var cssLeft = img_container.css('left');
//        var left = cssLeft.substring(0, cssLeft.length - 2) * 1;

//        if(Math.abs(left) < Math.abs(aditional_length))
//            img_container.css('left', '-' + (orginal_length - number_item_displayed * itemWidth + Math.abs(left) + left%itemWidth) + 'px');
//        else
//            img_container.css('left', left + number_item_displayed * itemWidth - left%itemWidth + 'px');

//        return false;
//    });

    //hover image event
    $('.item').hover(
        function(){
            //alert('in');
        },
        function(){
            //alert('out');
        }
    );
});
