(function($){  
    $.fn.extend({   
        gsdCrossFadeNew: function(options) {  
            //Settings list and the default values  
            var defaults = {
				timerDuration: 4000,
				transitionTime: 1000,
				captionHeight: -34,
				doubleCaptionHeight: -100,
				showControls: false,
				autoScroll: true
            };  
            var options = $.extend(defaults, options);  
            return this.each(function() {  
                var o = options;  
				
                //Assign current element to variable
                var obj = $(this);
				
				obj.css({
					'width':'490px',
					'height':'350px',
					'overflow':'hidden'
				});
				
				var ul = $("ul", obj);
				ul.css({
					'list-style':'none',
					'padding':'0px',
					'margin':'0px'
				});
                  
                //Get all LI in the div
                var items = $("li", obj);
				var totalitems = items.length;
				
				items.css({
					'position':'absolute',
					'display':'block',
					'width':'490px',
					'height':'350px',
					'text-align':'center',
					'vertical-align':'middle',
					'overflow':'hidden',
					'opacity':'0'
				});
				items.hide();
				
				//Add an id and the caption
				items.each(function(i){
					var singleitem = $(this);
					singleitem.attr('id',i);
					var image = singleitem.find('img');
				});
				
				var captions = $("div.caption", obj);
				captions.css({
					'position':'relative'
				});
				
				//Controls
				if(o.showControls){
					//Build link list
					var html = '<div class="image-control-wrap"><table cellpadding="0" cellspacing="0" border="0" class="image-control-table"><tr><td><ul class="image-controls">';
					for(i=0;i<totalitems;i++){
						html += '<li><a href="#" id="img_'+i+'">'+(i+1)+'</a></li>';
					}
					html += '</ul></td></tr></table></div><div style="clear:both"></div>';
					var list = obj.after(html);
					$("ul.image-controls a:first").addClass("image-control-current");
					$("ul.image-controls a").bind("click", function(){
						clearTimeout(timeout);
						$("ul.image-controls a").removeClass("image-control-current")
						$(this).addClass("image-control-current");
						var img = $(this).attr("id");
						changeImage(img);
						return false;
					});
				}
				
				//Show first item
				$("li:first", obj).show().css({
					'opacity':'1'
				}).addClass("gsdCrossFade-current");
				
				//Get all a tags
				var links = $("a", items);
				links.css({
					'display':'block',
					'width':'490px',
					'height':'350px'
				});


                //Attach mouseover and mouseout event to the a    
                items.mouseover(function() {  
					if(o.autoScroll){
						clearTimeout(timeout);
					}
					captionUp($(this),o.doubleCaptionHeight);
                      
                }).mouseout(function() {  
					if(o.autoScroll){
						timeout = setTimeout(function(){
							changeImage();
						},o.timerDuration/2);
					}
					captionDown($(this),o.captionHeight);
                });
				
				function captionUp(ref,pos){
					ref.find("div.caption").stop().animate({top: pos}, {queue:false, duration:250});
				}
				function captionDown(ref,pos){
					ref.find("div.caption").stop().animate({top: pos}, {queue:false, duration:250});
				}
				
				function changeImage(img){

					var curritem = $("li.gsdCrossFade-current");
					var currid = parseInt(curritem.attr("id"));
					
					//Check for a specified item
					if(img!=undefined){
						var nextid = img.substr(4);
					}else{
						var nextid = currid;
						nextid++;
						if(nextid==totalitems){
							nextid = 0;
						}
					}
					
					if(nextid!=currid){
						//Find the next item
						var nextitem = $("#"+nextid, obj);
						
						//Do the animations
						captionDown(curritem,0);
						curritem.stop().animate({opacity: 0}, {queue:false, duration:o.transitionTime, complete:function(){ $(this).hide(); captionDown($(this),0); } }).removeClass("gsdCrossFade-current");
						nextitem.show().stop().animate({opacity: 1}, {queue:false, duration:o.transitionTime, complete:function(){ captionUp($(this),o.captionHeight); } }).addClass("gsdCrossFade-current");
						
						if(o.autoScroll){
							//Reset timer
							timeout = setTimeout(function(){
								changeImage();
							},o.timerDuration);
						}
					}
				}
				if(o.autoScroll){
					//Init timer
					var timeout = setTimeout(function(){
						changeImage();
					},o.timerDuration);
				}
            });
			
        }  
    });  
})(jQuery); 