/**
 * @author Stéphane Roucheray, Povilas Balzaravičius
 * @extends jquery
 *
 *
 */


jQuery.fn.icarousel = function(previous, next, options){
	var sliderList = jQuery(this).children()[0];
    var defaults = {
        step  : 1
    };

    function setOptions(new_options) {
        options = $.extend({}, defaults, new_options);
    }

    setOptions(options); //Set-up new options over defaults

    function getStep() {
        if (sliderList) {
            return jQuery(sliderList).children().outerWidth("true") * options.step;
        }
        return 0;
    }

    function reset() {
        $(sliderList).css({
            width: 'auto',
            marginLeft: '0'
        });
        $(previous).unbind('click');
        $(next).unbind('click');
    }

    function displayNavigation(width, total) {
        var display = 'block';
        if (total <= width) {
            display = 'none';
        }
        $(next).css({display: display});
        $(previous).css({display: display});

        return display == 'none' ? false : true;
    }

	if (sliderList) {
        reset();
		var increment = getStep(),
		elmnts = jQuery(sliderList).children(),
		numElmts = elmnts.length,
		sizeFirstElmnt = increment,
		shownInViewport = Math.round(jQuery(this).width() / sizeFirstElmnt),
		firstElementOnViewPort = 1,
		isAnimating = false;
        if (displayNavigation(shownInViewport, numElmts) == true) {
            for (i = 0; i < shownInViewport; i++) {
                jQuery(sliderList).css('width',(numElmts+shownInViewport)*increment + increment + "px");
                jQuery(sliderList).append(jQuery(elmnts[i]).clone());
            }

            jQuery(previous).click(function(event){
                if (!isAnimating) {
                    if (firstElementOnViewPort == 1) {
                        jQuery(sliderList).css('margin-left', "-" + numElmts * sizeFirstElmnt + "px");
                        firstElementOnViewPort = numElmts;
                    }
                    else {
                        firstElementOnViewPort--;
                    }

                    jQuery(sliderList).animate({
                        marginLeft: "+=" + increment,
                        y: 0,
                        queue: true
                    }, "swing", function(){isAnimating = false;});
                    isAnimating = true;
                }

            });

            jQuery(next).click(function(event){
                if (!isAnimating) {
                    if (firstElementOnViewPort > numElmts) {
                        firstElementOnViewPort = 2;
                        jQuery(sliderList).css('margin-left', "0px");
                    }
                    else {
                        firstElementOnViewPort++;
                    }
                    jQuery(sliderList).animate({
                        marginLeft: "-=" + increment,
                        y: 0,
                        queue: true
                    }, "swing", function(){isAnimating = false;});
                    isAnimating = true;
                }
            });
        }
	}
};

