/*
	name : DLCarousel
	author : Sobia Ali
	(c) Copyright 2009 AOL LLC
	LastChangedDate : 2009-11-27
	Dependencies : jQuery
*/

(function($){
	$.fn.dlCarousel = function (customOptions) {
		/*default options*/
		var defaultOptions = {
			slideSpeed : 600,
			displayTime : 6000,
			thumbnailWidth: '70px',
			thumbnailHeight: '60px',	
			slideTextHeight : 80,
			imgHeight : 213,
			imgWidgth : 295 /*add 2pixels for the border*/
		};
		
		/*merged options*/
		var options = {};
		$.extend(true, options, defaultOptions, customOptions );

		/*function variables*/
		$this = this;
		var currentIter = 1;
		var slideImages = $('li img', $this);    		
		var numImages = $('li img', $this).length; 
		/*This DL currently only supports 6 images*/
		var numGroups = numImages/2; 
		var imgHeight = options.imgHeight;
		var imgWidth = options.imgWidgth;
		var slideSpeed = options.slideSpeed;
		var displayTime = options.displayTime;
		var pause = false;
		var even = false;
		
		var UI = {
			slideP: $('li p', $this),
			lastLi: $('li:last', $this),
			slidesUL: $('ul', $this),
			slideLi: $('li', $this),
			slideText: $('#slideText', $this),
			slideTextBackground: $('#slideTextBackground', $this),
			slideTextLeft: $('#slideTextLeft', $this),
			slideTextRight: $('#slideTextRight', $this),
			thumbnails: $('#thumbnails', $this),
			playButton: $('#playButton', $this),
			pauseButton: $('#pauseButton', $this),
			nextButton: $('#nextButton', $this),
			prevButton: $('#prevButton', $this),
			progressBar: $('#progressBar', $this)			
		};

		var init = function (){
			$this.css('display','block');
			UI.slideP.hide(); 
			UI.slidesUL.width(9999);
			UI.slideText.height((options.slideTextHeight));
			UI.slideTextBackground.css({'opacity':'0.5'});
		};		
		init();		
		
		(function() {	
			buildThumbnails();
			showSlideText($('li:eq('+((currentIter*2)-2)+') p', $this).html(), $('li:eq('+((currentIter*2)-1)+') p', $this).html());
			UI.pauseButton.bind('click', pauseClick);	
			UI.nextButton.bind('click', nextClick);	
			UI.prevButton.bind('click', prevClick);	

			function pauseClick() {
				if (pause) {
					currentIter = currentIter+1; moveSlide('next'); pause = false; resetAnimation();
					UI.pauseButton.toggleClass('playButton');
				}
				else { pause = true; clearTimeout(clearAnimation); UI.pauseButton.toggleClass('playButton');};			
			};		
			
			function nextClick() {
				pause = false; 
				UI.pauseButton.removeClass('playButton');
				resetAnimation();
				currentIter = currentIter+1;				
				moveSlide('next');				
			};
			
			function prevClick() {
				pause = false; 
				UI.pauseButton.removeClass('playButton');
				resetAnimation();
				currentIter = currentIter-1;
				moveSlide('prev');				
			};

			function resetAnimation() {
				clearTimeout(clearAnimation);
				clearAnimation = setInterval(function(){currentIter=currentIter+1; moveSlide('next', 1);},displayTime);			
			};

			function buildThumbnails() {
				UI.thumbnailList = $("<ul></ul>").attr('id', 'thumbnailList').appendTo(UI.thumbnails);
				var iter=0;	
				for(var i=0;i<=numImages-1;i++) {
					if (even) { even = false; iter+1;}
					else { even = true; iter = iter+1;};
					var thumbnailLi = $("<li></li>").appendTo(UI.thumbnailList).bind('click', thumbnailClick).attr('rel', iter).addClass("thumbnail");
					var thumbnail = $("<img></img>").attr('src', slideImages[i].src);
					thumbnail.width(options.thumbnailWidth);
					thumbnail.height(options.thumbnailHeight);
					thumbnail.appendTo(thumbnailLi);									
				};
				UI.thumbnail = $('.thumbnail');			
			};

			function thumbnailClick(event) {				
				var thumbNumber = $(this).attr('rel'); 	
				if(currentIter != thumbNumber) {
					clearTimeout(clearAnimation);					 
				};
				if(thumbNumber > currentIter) {
					difference = thumbNumber - currentIter	
					currentIter = currentIter + difference;			
					moveSlide('next');
				};
				if(thumbNumber < currentIter) {
					difference = currentIter - thumbNumber;
					currentIter = currentIter - difference;
					moveSlide('prev');
				};
			};

			function bindButtonClicks() {
				UI.nextButton.css({'cursor':'pointer'}).bind('click', nextClick);	
				UI.prevButton.css({'cursor':'pointer'}).bind('click', prevClick);	
				UI.pauseButton.css({'cursor':'pointer'}).bind('click', pauseClick);
				UI.thumbnail.css({'cursor':'pointer'}).bind('click', thumbnailClick);				
			};

			function unbindButtonClicks() {
				UI.nextButton.css({'cursor':'default'}).unbind('click');
				UI.prevButton.css({'cursor':'default'}).unbind('click');
				UI.pauseButton.css({'cursor':'default'}).unbind('click');	
				UI.thumbnail.css({'cursor':'default'}).unbind('click');			
			};

			function moveSlide(direction) {
				unbindButtonClicks();
				UI.slideText.animate({marginBottom:-options.slideTextHeight+'px'},500);				
				if(direction === "next") {					
					if(currentIter == numGroups+1) currentIter = 1;
					UI.slidesUL.animate({left:-(((currentIter)*2)-2)*imgWidth},slideSpeed,function () {
						UI.progressBar.animate({left:(currentIter-1)*150},400);
						showSlideText($('li:eq('+((currentIter*2)-2)+') p', $this).html(), $('li:eq('+((currentIter*2)-1)+') p', $this).html());
						bindButtonClicks();
						resetAnimation();
						
					});
				};
				if(direction === "prev") {
					if(currentIter == 0) currentIter = numGroups;
					UI.slidesUL.animate({left:-(((currentIter)*2)-2)*imgWidth},slideSpeed,function () {
						UI.progressBar.animate({left:(currentIter-1)*150},400);
						showSlideText($('li:eq('+((currentIter*2)-2)+') p', $this).html(), $('li:eq('+((currentIter*2)-1)+') p', $this).html());
						bindButtonClicks();
						resetAnimation();
					});
				};
			};		

			function showSlideText(textData1, textData2) {
				if(textData1 != null) {
					UI.slideText.animate({marginBottom:'0'},400);
					UI.slideTextLeft.html(textData1); 
				}
				if(textData2 != null) {
					UI.slideTextRight.html(textData2); 
				}
			};
			var clearAnimation = setInterval(function(){currentIter=currentIter+1; moveSlide('next');},displayTime);
  		})();
  		return $this;
    }
})(jQuery);
/* END DL */
/*Other Enhancements*/
jQuery(document).ready(function() {
	/*Hover effect for categories in the right rail*/
	$("#categories .sideNavLinks li").hover(function() {$(this).addClass("categoryHover");}, function() {$(this).removeClass("categoryHover");});
	$("#categories .sideNavLinks li").click(function() {window.location = this.firstChild.href;});
	/*Hover effect for subCategories on the Category page*/
	$("#subCategories li:has(a)").hover(function() {$(this).addClass("categoryHover");}, function() {$(this).removeClass("categoryHover");});	
	$("#subCategories li:has(a)").click(function() {window.location = this.firstChild.href;});
	/*Featured Authors*/
	$(".opaqueBackground").css({'opacity':'0.5'})
});