;(function(jQuery) {

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

	
	//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,
			activeSlideElement: 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'});
				if (this.slideType=='RIGHTTOLEFT') { //only when sliden right to left or left to right
					$slideul.css({'width': this.slideWidth*this.slideElementCount+'px', 'height': this.slideHeight+'px'});
				} else {
					$slideul.css({'width': this.slideWidth+'px', 'height': this.slideHeight+'px'});
					}
						
				$slideul.find('li.ifwscslider-element').css({'width': this.slideWidth+'px', 'height': this.slideHeight+'px'});
				if (this.slideType=='FADE') {
					$slideul.find('li.ifwscslider-element').css({'z-index': 105, 'display': 'none'});
					//the first one is visible 
					$slideul.find('li.ifwscslider-element[rel='+0+']').css('zIndex', 110).show();
				}
				
				//grab the width and calculate left value
				if (this.slideType=='RIGHTTOLEFT') {
					this.itemWidth = $slideul.find('li').outerWidth(); 
					this.leftValue = this.itemWidth * (-1);
				} 
		        
				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;
						});
					});
				}
				
				if (this.autoslide) {
					this.slideshowInterval = setInterval(function() { slideshow.rotate(); }, this.speed);
				}
			},

			slideTo: function(lielement) {
				this.dontAutoslide=1;
				var index=jQuery(lielement).attr('rel');
				var slide=this;
				var slideul=jQuery(this).find('ul.ifwscslider-slide-elementlist');
				//attention only slide to right to left
				//maybe later the direction will be coded
				if (this.slideType=='RIGHTTOLEFT') {
					var leftIndent=index*this.itemWidth*-1;
					jQuery(slideul).animate({'left' : leftIndent}, {queue: false, duration: this.animatespeed});
				} else {
					if (index != slide.activeSlideElement) {
						var $activeElement=jQuery(jQuery(slideul).find('li.ifwscslider-element[rel='+slide.activeSlideElement+']'));
						var $nextElement=jQuery(jQuery(slideul).find('li.ifwscslider-element[rel='+index+']'));
						$nextElement.fadeIn(this.animationspeed, function() {
							jQuery(this).css('zIndex',110);
							slide.activeSlideElement = index;
						});
						$activeElement.fadeOut(this.animationspeed, function() {
							jQuery(this).css('zIndex',105);
						});
					}
				}
			},
			
		    //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');
				
					if (this.slideType=='RIGHTTOLEFT') {
						//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}, this.animationspeed, 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});
							}
						});
					} else {
						//fade
						var $activeElement=jQuery(jQuery(slideul).find('li.ifwscslider-element[rel='+slide.activeSlideElement+']'));
						var next=slide.getNextSlideElement();
						var $nextElement=jQuery(jQuery(slideul).find('li.ifwscslider-element[rel='+next+']'));
						$nextElement.fadeIn(this.animationspeed, function() {
							jQuery(this).css('zIndex',110);
							slide.activeSlideElement = next;
						});
						$activeElement.fadeOut(this.animationspeed, function() {
							jQuery(this).css('zIndex',105);

						});
					}
				}
			},
		
			getNextSlideElement: function() {
				var next=this.activeSlideElement+1;
				if (next>=this.slideElementCount) next=0;
				return next;
			}
			
		});

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