if(typeof jQuery != 'undefined') {
	jQuery(function($) {
		$.fn.extend({
			slideshow: function(options) {
				var settings = $.extend({}, $.fn.slideshow.defaults, options);
			
				return this.each(
					function() {
						if($.fn.jquery < '1.2.6') {return;}
						var $t = $(this);
						var o = $.metadata ? $.extend({}, settings, $t.metadata()) : settings;
						
						var currentElement = 0;
						var elementCount;
						var firstChildren = $t.children();
						var elementWidth;
						firstChildren = firstChildren.eq(0);
						if(firstChildren){
							var firstItem = $(firstChildren).children();
							elementCount = firstItem.length;
							firstItem = firstItem.eq(0);
							elementWidth = $(firstItem).width();
							elementWidth+= parseInt($(firstItem).css('margin-left'));
							elementWidth+= parseInt($(firstItem).css('margin-right'));
							elementWidth+= parseInt($(firstItem).css('padding-left'));
							elementWidth+= parseInt($(firstItem).css('padding-right'));
							elementWidth+= parseInt($(firstItem).css('border-left-width'));
							elementWidth+= parseInt($(firstItem).css('border-right-width'));
						}
						function scrollToXpos(xpos){
							if($.browser.msie && $.browser.version.substr(0, 1) < 7) {
								$t.animate({right: xpos+'px'}, o.duration);
							} else {
								$t.stop().scrollTo(xpos, 0, {
									axis: 'x',
									duration: o.duration
								});
							}
						}
						
						function scrollToElement(element){
							if(element >= 0 && element + o.visibleItemCount <= elementCount){
								currentElement = element;
								scrollToXpos(currentElement * elementWidth);
								
								toggleControls(true);
							}
						}
						
						function toggleControls(animate){
							if(currentElement == 0){
								if(animate){
									$(o.prevControl).fadeOut('fast');
								}else{
									$(o.prevControl).css('display', 'none');
								}
							}else{
								if(animate){
									$(o.prevControl).fadeIn('fast');
								}else{
									$(o.prevControl).css('display', 'block');
								}
							}
							
							if(currentElement + o.visibleItemCount + 1 > elementCount){
								if(animate){
									$(o.nextControl).fadeOut('fast');
								}else{
									$(o.nextControl).css('display', 'none');
								}
							}else{
								if(animate){
									$(o.nextControl).fadeIn('fast');
								}else{
									$(o.nextControl).css('display', 'block');
								}
							}
						}
						
						if(o.prevControl){
							$(o.prevControl).click(function(){
								scrollToElement(currentElement - 1);
								return false;
							});
						}
						
						if(o.nextControl){
							$(o.nextControl).click(function(){
								scrollToElement(currentElement + 1);
								return false;
							});
						}
						
						toggleControls(false);
					}
				);
			}
		});
		
		/**
		* Set your Plugin Defaults Here…
		*/
		$.fn.slideshow.defaults = {
			prevControl: null,
			nextControl: null,
			duration: 500,
			visibleItemCount: 3
		};
	});
}
