jQuery.iPhotoScroller = {
    build: function(options) {
        return this.each(
			function() {
			    var ps = jQuery(this);
			    ps.scrollTimer = null;
			    ps.cfg = {
			        stepSize: (options.stepSize) ? options.stepSize : 2,
			        interval: (options.interval) ? options.interval : 50,
			        direction: /left|right/.test(options.direction) ? options.direction : 'left',
			        hoverClass: options.hoverClass,
			        onClick: (options.onClick && typeof (options.onClick) == 'function') ? options.onClick : false,
			        onMouseOver: (options.onMouseOver && typeof (options.onMouseOver) == 'function') ? options.onMouseOver : false,
			        onMouseOut: (options.onMouseOut && typeof (options.onMouseOut) == 'function') ? options.onMouseOut : false,
			        size: ps.width(),
			        items: ps.children()
			    };

			    var totalWidth = 0;
			    ps.cfg.items.each(function() {
			        var eWidth = $(this).width();
			        var eTop = parseInt((ps.height() - $(this).height()) / 2);

			        $(this).css({ left: totalWidth, top: eTop });
			        totalWidth += eWidth;
			    });
				var itemsWidth = totalWidth;

			    var loopCounter = 0;
			    while (totalWidth < ps.cfg.size || loopCounter != ps.cfg.items.length) {
			        ps.append($(ps.cfg.items[loopCounter]).clone().css("left", totalWidth));
			        totalWidth += $(ps.cfg.items[loopCounter]).width();
			        loopCounter++;
			        if (loopCounter == ps.cfg.items.length && totalWidth < (itemsWidth + ps.cfg.size))
						loopCounter = 0;
			    }

			    ps.cfg.items = ps.children();
			    ps.cfg.items.bind("mouseover", function() { jQuery.iPhotoScroller.stopScroller(this, ps); }).bind("mouseout", function() { jQuery.iPhotoScroller.startScroller(this, ps); });

			    if (ps.cfg.onClick)
			        ps.cfg.items.bind("click", ps.cfg.onClick);
			    if (ps.cfg.onMouseOver)
			        ps.cfg.items.bind("mouseover", ps.cfg.onMouseOver);
			    if (ps.cfg.onMouseOut)
			        ps.cfg.items.bind("mouseout", ps.cfg.onMouseOut);

			    jQuery.iPhotoScroller.startScroller(null, ps);
			}
		);
    },
    startScroller: function(el, ps) {
        if (el && ps.cfg.hoverClass)
            $(el).removeClass(ps.cfg.hoverClass);

        ps.scrollTimer = window.setInterval(function() { jQuery.iPhotoScroller.doScroll(ps); }, ps.cfg.interval);
    },
    stopScroller: function(el, ps) {
        if (el && ps.cfg.hoverClass)
            $(el).addClass(ps.cfg.hoverClass);

        window.clearInterval(ps.scrollTimer);
    },
    doScroll: function(ps) {
        var items = ps.children();
        items.each(function(i) {
            var leftPos = parseInt($(this).css("left"));
            switch (ps.cfg.direction) {
                case 'left':
                    $(this).css("left", leftPos - ps.cfg.stepSize); break;
                case 'right':
                    $(this).css("left", leftPos + ps.cfg.stepSize); break;
            }
        });

        var elFirst = $(items.get(0));
        var elLast = $(items.get(items.length - 1));

        switch (ps.cfg.direction) {
            case 'left':
                if (parseInt(elFirst.css("left")) < -(elFirst.width())) {
                    var newLeftPos = parseInt(elLast.css("left")) + elLast.width();
                    elFirst.css("left", newLeftPos);
                    ps.append(elFirst);
                }
                break;
            case 'right':
                if (parseInt(elLast.css("left")) > ps.cfg.size) {
                    var newLeftPos = parseInt(elFirst.css("left")) - elLast.width();
                    elLast.css("left", newLeftPos);
                    ps.prepend(elLast);
                }
                break;
        }
    }
};

jQuery.fn.PhotoScroller = jQuery.iPhotoScroller.build;