/**
 *  tinycarousel optimised and enhanced for a loop
 */
(function ($) {
    $.fn.tinycarousel = function (options) {
        var defaults = {
            start: 1,
            display: 1,
            controls: true,
            interval: false,
            intervaltime: 3000,
            animation: true,
            duration: 1000,
            callback: null,
            loop: false
        };
        var options = $.extend(defaults, options);
        var oSlider = $(this);
        var oViewport = $('.viewport:first', oSlider);
        var oContent = $('.overview:first', oSlider);
        var oPages = oContent.children();
    	var iPages = oPages.length;
        var oBtnNext = $('.next:first', oSlider);
        var oBtnPrev = $('.prev:first', oSlider);
        var iPageSize, iCurrent, oTimer, bPause;
        return this.each(function () {
            initialize();
        });

        function initialize() {
            iPageSize = $(oPages[0]).outerWidth(true);
            posReset();
            oContent.css('width', (iPageSize * oPages.length));
            move(0);
            setEvents();
            return this;
        }

        function setEvents() {
            if (options.controls && oBtnPrev.length > 0 && oBtnNext.length > 0) {
                oBtnPrev.click(function () {
                    move(-1);
                    return false;
                });
                oBtnNext.click(function () {
                    move(1);
                    return false;
                });
            }
            if (options.interval) {
                oSlider.hover(function () {
                    clearTimeout(oTimer);
                    bPause = true
                }, function () {
                    bPause = false;
                    setTimer();
                });
            }
        }

        function setTimer() {
            if (options.interval && !bPause) {
                clearTimeout(oTimer);
                oTimer = setTimeout(function() {
                    move(1);
                }, options.intervaltime);
            }
        }

        function move(iDirection) {
        	var 
        		animate = options.animation,
        		wrapover = 0;
        	;
        	iCurrent += iDirection;
        	if ( (iCurrent + options.display) > iPages || iCurrent < 0) {
    			if (iCurrent < 0) {
    				iCurrent = iPages - options.display;
    			}
    			else {
    				iCurrent = 0;	
    			}
        		doMove( false );
        		iCurrent += iDirection;
            }
        	doMove(animate);
            setTimer();
        }
        
        function doMove(animate) {
        	var oPosition = {};
            oPosition['left'] = -(iCurrent * iPageSize);
            oContent.animate(oPosition, {
                queue: false,
                duration: animate ? options.duration : 0,
                complete: function () {
                    if (typeof options.callback == 'function') options.callback.call(this, oPages[iCurrent], iCurrent);
                }
            });
        }
        
        function resetDetect() {
        	return (iCurrent + 1 == iPages);
        }
        
        function posReset() {
        	iCurrent = 0;
        }

    };
})(jQuery);

// Prep the carousel
$(document).ready(
	function(){
		var 
			c = $('#autostr'),
			display = 2, 
			b, k=[], l, m=[], len, pags
		;
		l = $('li', c);
		/* add an extra page */ 
		len = l.length;
		m = $('li:lt(' + display + ')', c);
		d = c.find('div.viewport')
			.wrap('<div id="slider-code"></div>')
			.append('<a class="buttons prev" href="#"></a><a class="buttons next" href="#"></a>')
			.find('ul')
				.append(m.clone())
			.end()
		.end()
		.tinycarousel({display:display, duration:1000, intervaltime: 4000, interval:true});
		b = $('a.buttons',c);
		c.bind('mouseover',b,function(e){
			e.data.toggle( true );
		});
		c.bind('mouseout',b,function(e){
			e.data.toggle( false );
		});
	}
);

