(function($){
    $.fn.extend({ 
        newsSlider: function(options) {
			// set default options
			var defaults = {
				speed : 1000,
				pause : 4000,
				transition : 'fade'
			},
	
			// Take the options that the user selects, and merge them with defaults.
			options = $.extend(defaults, options);
			
			var nextIndex = false;
			var distance;
			var main_width;
			
			var panels = $('.image_thumb ul').children();
			var nrOfPanels = panels.length;
			var total_width = 0;
			$.each(panels,function() {
			    total_width += this.offsetWidth;
			});
			
			var container = $('.newsSlider');
			var thumb_container = $('.image_thumb ul');
			var main_width = parseInt($(".image_thumb").css("width"));
			
			thumb_container.css('width', total_width).css('overflow', "hidden").css("position", "relative");
			
            return this.each(function() {
				//Show Banner
				$(".main_image .desc").show(); //Show Banner
				$(".main_image .block").animate({ opacity: 0.85 }, 1 ); //Set Opacity
			
				//Click and Hover events for thumbnail list
				$(".image_thumb ul li:first").addClass('active'); 
				
				
				
				$(".image_thumb ul li").click(function(){ 
					//Set Variables
					var imgAlt = $(this).find('img').attr("alt"); //Get Alt Tag of Image
					var imgTitle = $(this).find('a').attr("href"); //Get Main Image URL
					var imgDesc = $(this).find('.block').html(); 	//Get HTML of block
					var imgDescHeight = $(".main_image").find('.block').height();	//Calculate height of block	
					
					if ($(this).is(".active")) {  //If it's already active, then...
						return false; // Don't click through
					} else {
						//Animate the Teaser				
						$(".main_image .block").animate({ opacity: 0, marginBottom: -imgDescHeight }, 250 , function() {
							$(".main_image .block").html(imgDesc).animate({ opacity: 0.85,	marginBottom: "0" }, 250 );
							$(".main_image img").attr({ src: imgTitle , alt: imgAlt});
						});
					}
					
					$(".image_thumb ul li").removeClass('active'); //Remove class of 'active' on all lists
					$(this).addClass('active');  //add class of 'active' on this list only
					return false;
					
				}) .hover(function(){
					$(this).addClass('hover');
					}, function() {
					$(this).removeClass('hover');
				});
						
				//Toggle Teaser
				$("a.collapse").click(function(){
					$(".main_image .block").slideToggle();
					$("a.collapse").toggleClass("show");
				});
				
				
				$(".image_thumb ul li").hover(
					function() {
						clearInterval(timer);
					},
					function() {
						slide();
					}
				);
				
				
				slide();
				
				function slide() {
					//Om det finns får få bilder slidea ej!
					if((main_width+2) >= total_width)
						return ;
					
					if(nextIndex == false){
						getNextPanelIndex();
						distance = 0;
					}
						
					timer = setInterval(function() {
					  if(nextIndex > 0){
					  	  distance = distance + panels[nextIndex].offsetWidth;
						  $(thumb_container).animate({
						    left: '-'+distance+'px'
						  }, options.speed, function() {
						  	var thisIndex = nextIndex;
						  	if(thisIndex == nrOfPanels-2)
						  		nextIndex = 0;
						  	else
						    	nextIndex = nextIndex + 1;
						  });
					  }	else	{
					  	  distance = 0;
						  $(thumb_container).animate({
						    left: distance+'px'
						  }, options.speed, function() {
						    getNextPanelIndex();
						  });					  	
					  }
					  
					}, options.pause);
				} // end slide
			
				function getNextPanelIndex(){
					var cur_width = 0; var i = 0;
					$.each(panels,function() {
						cur_width += this.offsetWidth;
						if(cur_width > main_width){
					    	nextIndex = i;
					    	return ;
					    }
						i = i + 1;
					});
				}
				
            });
        }
    });
})(jQuery);
