;(function(jQuery) {

	var defaults = {
		slideWidth: 250,
		slideHeight: 150,
		autoslide: 1,
		rotation: 1,
		useImageTabs: 0,
		useHeaderTabs: 0,
		speed: 5000
	};

	
	//extend the fn for the methods
	
	jQuery.fn.ifwsContentSlider = function(settings) {
		
		jQuery.extend(this,{
			//rotation speed and timer
			itemWidth: 0,
			leftValue: 0,
			slideshowInterval: undefined,
			slideElementCount: 0,
			dontAutoslide: 0,
			
			initialize: function() {
				$slide=jQuery(jQuery(this).find('div.ifwscslider-slide'));
				$slideul=jQuery($slide.find('ul.ifwscslider-slide-elementlist'));
				//get count of elements
				this.slideElementCount = $slideul.find('li').length;
				//set the width and height
				$slide.css({'width': this.slideWidth+'px', 'height': this.slideHeight+'px'});
				//$slide.css('height',this.slideHeight+'px');
				$slideul.css({'width': this.slideWidth*this.slideElementCount+'px', 'height': this.slideHeight+'px'});
				//$slideul.css('height',this.slideHeight+'px');
				$slideul.find('li.ifwscslider-element').css({'width': this.slideWidth+'px', 'height': this.slideHeight+'px'});
				//$slideul.find('li.ifwscslider-slide').css('height',this.slideHeight+'px');
				
				//grab the width and calculate left value
				this.itemWidth = $slideul.find('li').outerWidth(); 
				this.leftValue = this.itemWidth * (-1); 
		        
				//move the last item before first item, just in case user click prev button //i don't need this
				//jQuery('#slides li:first').before($('#slides li:last'));
			
				//set the default item to the correct position 
				//jQuery(this).find('ul:first').css({'left' : this.leftValue});
				
				var slideshow = this;
				
				//initialize the slidetabs
				if (this.useImageTabs || this.useHeaderTabs) {
					$tabs=jQuery(jQuery(this).find('ul.ifwscslider-slide-tab'));
					$tabs.find('li').each(function(i) {
						jQuery(this).click(function() {
								slideshow.slideTo(this);
							})
							.hover(function() {
								slideshow.slideTo(this);
							}, function() {
								slideshow.dontAutoslide=0;
							});
					});
					/*					this.$thumbs.find('li').each(function(i) {
						//add the rollover effects
						jQuery(this).css('opacity',ga.thumbOpacity)
							.hover(function(){
								jQuery(this).fadeTo('fast',1);
							}, function(){
								if (!jQuery(this).hasClass('selected')) 
									jQuery(this).fadeTo('fast', ga.thumbOpacity);
							});
						
						//add the images
						ga.addImage(jQuery(this));
					});*/

				}
				
				//this.slideshowTimeout = setTimeout(function() { slideshow.rotate(); }, this.speed);
				if (this.autoslide) {
					this.slideshowInterval = setInterval(function() { slideshow.rotate(); }, this.speed);
				}
			},

			slideTo: function(lielement) {
				this.dontAutoslide=1;
				var index=jQuery(lielement).attr('rel');
				var slideul=jQuery(this).find('ul.ifwscslider-slide-elementlist');
				//attention only slide to right to left
				//maybe later the direction will be coded
				var leftIndent=index*this.itemWidth*-1;

				jQuery(slideul).animate({'left' : leftIndent}, {queue: false, duration: 300});
			},
			
		    //if user clicked on next button
			rotate: function() {
				//only rotate if mouse is out one the thumbs
				if (this.dontAutoslide==0) {
				
					var slide=this;
					var slideul=jQuery(this).find('ul.ifwscslider-slide-elementlist');
				
					//get the right position
					var leftIndent = parseInt(jQuery(slideul).css('left')) - this.itemWidth;
					//if no rotation then we must return to the start if end is reached
					//works only if slide is from right to left //attention to the direction
					if ((!this.rotation) && ((leftIndent/this.itemWidth*-1)>=this.slideElementCount)) {
						leftIndent = 0;
					}
				
					//slide the item
					jQuery(slideul).animate({'left' : leftIndent}, 400, function () {
						
						if (slide.rotation) {
							//move the first item and put it as last item
							jQuery(slide).find('li:last').after(jQuery(slide).find('li:first'));                 	
							//set the default item to correct position
							// orig jQuery(slideul).css({'left' : slide.leftValue});
							jQuery(slideul).css({'left' : 0});
						}
					});
				}
			}
			
		});

		jQuery.extend(this, defaults, settings);
		
		return this;
	};
})(jQuery);
