(function () {
    $.fn.createCarousel = function () {

        return this.each(function () {

	var $this= this;
            var $wrapper = $('> div', this).css('overflow', 'hidden'),
                $slider = $wrapper.find('> ul').width(9999),
                $items = $slider.find('> li'),
                $single = $items.filter(':first')
                
                singleWidth = $single.outerWidth(),
                visible = Math.ceil($wrapper.innerWidth() / singleWidth),
                $this._currentPage = 1,
                pages = $items.length;


            function gotoPage(page) {
                var dir = page < $this._currentPage ? -1 : 1,
                    left = singleWidth * dir * visible;

                $wrapper.filter(':not(:animated)').animate({
                    scrollLeft : '+=' + left
                }, 500, function () {
                    // if page == last page - then reset position
		$('a.scroll-right', this).removeClass('disabled');
		$('a.scroll-left', this).removeClass('disabled');

                    if (page >= pages) {
			$('a.scroll-left', this).addClass('disabled');
                    }
		   if (page == 1) {
			$('a.scroll-right', this).addClass('disabled');
                    }
                    
                    $this._currentPage = page;
                });
            }
            
            // bind the back and forward links
	$('a.scroll-right', this).addClass('disabled');
	    if(pages > 1) {
            	$('a.scroll-right', this).click(function () {
			if($this._currentPage != 1) {
                		gotoPage($this._currentPage - 1);
			}
                	return false;
            	});
            
                $('a.scroll-left', this).click(function () {
			if($this._currentPage != pages) {
			   gotoPage($this._currentPage + 1);
			}
                   return false;
                });
	   } else {
		$('a.scroll-left', this).addClass('disabled');
	   }
            
        });
    };
	
	
})(jQuery);


$(function() {
	$('#wp-create, #gemz-create, #theme-create').createCarousel();
});

