(function(jQuery){ 
    jQuery.fn.extend({ 
        slideshow: function(options) { 
 
            var defaults = { 
                imgArr: [],
				textArr: [],
				arrLength: 3,
				speed: 7000,
				fadeSpeed: 500,
				opacity: 1,
				description: true,
				controls: true,
				currentClass: "current",
				sprite: false,
				bannerHeight: 239
            }; 
         
            var options = jQuery.extend(defaults, options); 
             
            return this.each(function() { 
                
	            var obj = jQuery(this);
	            var imgArr = options.imgArr;
				var textArr = options.textArr;
				var arrLength = options.arrLength;
				var speed = options.speed;
				var fadeSpeed = options.fadeSpeed;
				var opacity = options.opacity;
				var description = options.description;
				var controls = options.controls;
				var counter = 0;
				var ctrls = "";
				var active = false;
				var currentPage = 0;
				var currentClass = options.currentClass;
				var sprite = options.sprite;
				var bannerHeight = options.bannerHeight;
				obj.css({'backgroundPosition':'0px 0px'});
				obj.css({'backgroundImage':'url(_assets/js/images/'+imgArr[counter]+')'});
				if (description == true){
					//obj.append("<div id='iDesc'><div class='body'>"+textArr[counter]+"</div></div>");
					jQuery('#iDesc div.body').html(textArr[counter]);
					jQuery('#iDesc').animate({opacity: opacity}, 1)
				}
				
				
				for (var x = 1; x <= arrLength; x++){
					var ctrls = ctrls +"<li><a href='#' rel='"+x+"' class='page'>"+x+"</a></li>";
				}
				
				if(controls == true){
					if(arrLength>1){
						obj.append("<div id='iControls'><ul>"+ctrls+"<li><a href='#' class='next'><img src='_assets/css/images/imgIconNext.gif' alt='next' alt='' /></a></li></ul></div>");
						jQuery('#iControls ul li a').each(function(){
							if (jQuery(this).attr('rel')=='1'){
									jQuery(this).parent().addClass(currentClass);
								}
							}
						);
					}
				}
				
				if(arrLength>1){
					var timerInt = setInterval( changeImg, speed );
				}
				
				obj.mouseover(function(){
					active = true;
				})
				obj.mouseout(function(){
					active = false
				})

				
				function changeImg() {
					if(active == false){
						if(counter < (arrLength-1)){
							counter = counter+1;
						}
						else{
							counter=0;
						}
						fadeOut();
					}
				};
				
				function fadeOut(){
					obj.fadeOut(fadeSpeed, function(){
						jQuery('#iDesc').hide();
						if (textArr[counter])
						{
							jQuery('#iDesc div.body').html(textArr[counter]);
							jQuery('#iDesc').show();
						}
						currentPage = counter+1;
						jQuery('#iControls ul li a').each(function(){
							if (jQuery(this).attr('rel')==currentPage){
									jQuery(this).parent().addClass(currentClass);
								}
								else{
									jQuery(this).parent().removeClass(currentClass);
								}
							}
						);
						
						if(sprite == true){
							var inc = (counter * bannerHeight) * -1;
							obj.css({'backgroundPosition':'0 '+inc+'px'});
						}
						else{
							obj.css({'backgroundImage':'url(_assets/js/images/'+imgArr[counter]+')'});
						}
						obj.fadeIn(fadeSpeed);
						
					});
				}
				
				function resetInterval(){
					clearInterval(timerInt);
					timerInt = setInterval( changeImg, speed );
				}
				
				jQuery('#iControls li a.prev').click(function(){
					if(counter==0){
						counter = (arrLength-1);
					}
					else if(counter > 0){
						counter = counter-1;
					}	
					resetInterval();
					fadeOut();
					return false;
				})
				jQuery('#iControls li a.next').click(function(){
					if(counter < (arrLength-1)){
						counter = counter+1;
					}
					else{
						counter=0;
					}
					resetInterval();
					fadeOut();
					return false;
				})
				jQuery('#iControls li a.page').click(function(){
					if(counter != jQuery(this).attr('rel')-1){
						counter = jQuery(this).attr('rel')-1;
						resetInterval();
						fadeOut();
					}
					return false;
				})
				jQuery('#iControls li a.pause').click(function(){
					if (jQuery(this).attr('rel')=='pause'){
						clearInterval(timerInt);
						jQuery(this).attr("rel", 'play');
						jQuery(this).text("play");
					}
					else if (jQuery(this).attr('rel')=='play'){
						timerInt = setInterval( changeImg, speed );
						jQuery(this).attr("rel", 'pause');
						jQuery(this).text("pause");
					}
					return false;
				})
				
                       
            }); 
        } 
    }); 
})(jQuery);