(function($){
    $.fn.bannerScroller = function(options) {

		var defaults = {
			length: 300,
			minTrail: 20,
			moreText: "more",
			lessText: "less",
			ellipsisText: "..."
		};

		var options = $.extend(defaults, options);
		var tabsLinks = $(this).find(".tabs_links");

		function rotateIt() {
			var panelsList = $(this).find(".panels_list");
			var firstPanel = panelsList.find("li:first").html();
			panelsList.prepend("<li>" + firstPanel + "</li>");
			//panelsList.find("li:first .panel_information").show(); //Show Banner
			panelsList.find("li:not(:first)").hide(); //Show Banner
			panelsList.find(".panel_information .description").animate({ opacity: 0.85 }, 1 ); //Set Opacity

			//Click and Hover events for thumbnail list
			tabsLinks.find("li:first").addClass('active');

			tabsLinks.find("li").click(tabClicked).hover(function(){
				$(this).addClass('hover');
			}, function() {
				$(this).removeClass('hover');
			});

			//Toggle Teaser
			$(".collapse").click(function(){
				$(".description").slideToggle();
				$(".collapse").toggleClass("show");
			});
		}
		
		function tabClicked(){
			//Set Variables
			var tabLink = $(this).find('a').attr("href");
			//var imgUrl = $(tabLink).find('img').attr("src"); //Get Main Image URL
			var panelDesc = $(tabLink).find('.description').html(); 	//Get HTML of description
			var panelID = $(tabLink).find('.panel_content').attr("id"); 	//Get HTML of description
			var panelContent = $(tabLink).find('.panel_content').html(); 	//Get HTML of description
			var panelDescHeight = $(tabLink).find('.description').height();	//Calculate height of description

			if ($(this).is(".active")) {
				return false;
			} else {
				//Animate the Teaser
				$(tabLink).parent().find(".description:first").fadeOut("slow" , function() {
					$(this).html(panelDesc).animate({ opacity: 0.85,	marginBottom: "0" }, 250 );
					$(tabLink).parent().find('li:first .panel_content').fadeOut("slow", function() {
						$(this).fadeIn("normal");
						$(this).attr("id", panelID);
						$(this).html(panelContent);
						//$(this).attr({ src: imgUrl, alt: imgAlt });
					});
				});
				$(tabLink).find(".collapse").animate({ bottom: panelDescHeight }, 250 );
			}

			tabsLinks.find("li").removeClass('active'); //Remove class of 'active' on all lists
			$(this).addClass('active');  //add class of 'active' on this list only
			return false;
		}

		function autoRotate() {
			var currentItem = tabsLinks.find("li.active");
			var nextItem;
			if (currentItem.is(":last-child")) { nextItem = currentItem.parent().find("li:first") } else { nextItem = currentItem.next() };
			nextItem.trigger('click');
		}
		//$('head').append('<link href="js/jquery.bannerScroller.css" rel="stylesheet" type="text/css">');
		this.each(rotateIt);

		setInterval(autoRotate, 9000);
    }

	jQuery.fn.log = function (msg) {
		console.log("%s: %o", msg, this);
		return this;
	};

})(jQuery);
