(function($){  
	$.fn.extend({   
		gsd_slideshow:function(options){
            //Settings list and the default values  
            var defaults = {
				timerDuration: 4000,
				transitionTime: 1000,
				showControls: true,
				autoScroll: true,
				captionWidth: "auto",
				captionHeight: "auto",
				captionPositionX: "0px",
				captionPositionY: "0px",
				captionEffect: "fade",
				pictureWidth: "auto",
				pictureHeight: "auto",
				picturePositionX: "0px",
				picturePositionY: "0px",
				pictureEffect: "fade",
				captionId: "gsd-slideshow-captions",
				slideshowId: "gsd-slideshow-pics",
				randomStart: false
            };  
            var options = $.extend(defaults, options);
			
			//Get the dimensions of the container
			var cwidth = this.width();
			var cheight = this.height();
			var myArray = [];

			this.children().each(function(){
				
				var obj = $(this);
				var index = 0;
				
				//Get the items
				var items = $("li", obj);
				var totalitems = items.length;
				
				switch(this.id){
					//case 'gsd-slideshow-captions':
					case options.captionId:
						//Setup the caption size / position
						obj.css({
							"width":(options.captionWidth=="auto"?"auto":options.captionWidth),
							"height":(options.captionHeight=="auto"?"auto":options.captionHeight),
							"position":"absolute",
							"overflow":"hidden",
							"margin-left":(options.captionPositionX=="0px"?"0px":options.captionPositionX),
							"margin-top":(options.captionPositionY=="0px"?"0px":options.captionPositionY),
							"z-index":"10"
						});
						//Set the id prefix
						var idprefix = "cap_";
						//Setup the caption items
						items.css({
							'width':(options.captionWidth=="auto"?"auto":options.captionWidth),
							'height':(options.captionHeight=="auto"?"auto":options.captionHeight)
						});
						items.each(function(i){
							if(options.captionEffect=="fade"){
								$(this).css({
									"opacity":"0",
									"position":"absolute",
									"display":"block",
									"overflow":"hidden"
								});
							}
							//TODO: fade
							//Add an id
							$(this).attr("id",idprefix+i);
						});
						
					break;
					
					//case 'gsd-slideshow-pics':
					case options.slideshowId:
						//Setup the picture size / position
						obj.css({
							"width":(options.pictureWidth=="auto"?"auto":options.pictureWidth),
							"height":(options.pictureHeight=="auto"?"auto":options.pictureHeight),
							"position":"absolute",
							"overflow":"hidden",
							"margin-left":(options.picturePositionX=="0px"?"0px":options.picturePositionX),
							"margin-top":(options.picturePositionY=="0px"?"0px":options.picturePositionY),
							"z-index":"5"
						});
						//Set the id prefix
						var idprefix = "pic_";
						//Setup the picture items
						items.css({
							'width':(options.pictureWidth=="auto"?"auto":options.pictureWidth),
							'height':(options.pictureHeight=="auto"?"auto":options.pictureHeight)
						});
						items.each(function(i){
							if(options.pictureEffect=="fade"){
								$(this).css({
									"opacity":"0",
									"position":"absolute",
									"display":"block",
									"overflow":"hidden"
								});
							}
							//TODO: fade
							//Add an id
							$(this).attr("id",idprefix+i);
						});
					
					break;
				}
				
				//Hide all items
				items.hide();

				//Show first item
				if(options.randomStart){
					var rand = Math.floor(Math.random() * totalitems);
					var first = $("li", obj).eq(rand);
				}else{
					var first = $("li:first", obj);
				}
				first.show().css({
					'opacity':'1'
				}).addClass("gsdCrossFade-current");
				if($.browser.msie){
					first.get(0).style.removeAttribute('filter');
				}
				
				//Change slide
				obj.changeSlide = function(s){
					
					var curritem = $("li.gsdCrossFade-current", obj);
					var currid = parseInt(curritem.attr("id").substring(idprefix.length));
					
					//Check for a specified item
					if(s!=undefined){
						var nextid = s;
					}else{
						var nextid = currid;
						nextid++;
						if(nextid==totalitems){
							nextid = 0;
						}
					}
					
					if(nextid!=currid){
						//Find the next item
						var nextitem = $("#"+idprefix+nextid, obj);
						
						//Do the animations
						curritem.stop().animate({opacity: 0}, {queue:false, duration:options.transitionTime, complete:function(){ $(this).hide(); } }).removeClass("gsdCrossFade-current");
						nextitem.show().stop().animate({opacity: 1}, {queue:false, duration:options.transitionTime, complete:function(){
								if($.browser.msie){
									$(this).get(0).style.removeAttribute('filter');
								}
								if(options.showControls){
									updateCurrentControl(nextid);
								}
							}
						}).addClass("gsdCrossFade-current");
					}
				}
				
				obj.totalitems = totalitems;
				myArray.push(obj);
			});
			
			//Timer
			function setTimer(){
				var timeout = setTimeout(function(){
					$.each(myArray, function(){
						this.changeSlide();
					});
					timer = setTimer();
				},options.timerDuration+options.transitionTime);
				return timeout;
			}
			
			//Change item
			function changeNow(theitem){
				$.each(myArray, function(){
					this.changeSlide(theitem);
				});
			}
			
			//Controls
			if(options.showControls){
				//Build link list
				var html = '<div class="gsd-control-wrap"><ul class="slideshow-controls">';
				for(i=0;i<myArray[0].totalitems;i++){
					html += '<li><a href="#" id="cnt_'+i+'">'+(i+1)+'</a></li>';
				}
				html += '</ul></div><div style="clear:both"></div>';
				var list = this.append(html);
				$("ul.slideshow-controls a:first").addClass("slideshow-control-current");
				$("ul.slideshow-controls a").bind("click", function(){
					clearTimeout(timer);
					$("ul.slideshow-controls a").removeClass("slideshow-control-current");
					$(this).addClass("slideshow-control-current");
					var theitem = $(this).attr("id").substring(4);
					changeNow(theitem);
					return false;
				});
			}
			
			function updateCurrentControl(itemid){
				$("ul.slideshow-controls a.slideshow-control-current").removeClass("slideshow-control-current");
				$("#cnt_"+itemid).addClass("slideshow-control-current");
			}
			
			//Start
			if(options.autoScroll){
				var timer;
				timer = setTimer();
			}
		}
	});
})(jQuery);
