// ROTATING SLIDESHOW PLUG-IN
// @author Joel Grannas - Creative Director, Schoolwires, Inc. 
// Last updated on 9/28/2011

(function($) {
		  
	$.fn.joelRotate = function(settings){
		//SETTINGS
		var config = {
			'delay'				: 8,
			'fadeSpeed' 		: 2,
			'random'			: "no",
			'useTitles'			: "no",
			'useDescriptions'	: "no",
			'useLinks'			: "no",
			'controls'			: "no",
			'itemize'		    : "no",
			'itemizeThumbs'		: "no",
			'static'			: "no",
			'customClass'		: "no",
			'removeStyles'		: "no",
			'overlay'			: "no",
			'hoverPause'		: "no"
		};
		if (settings){$.extend(config, settings);}
		
		// loop thru each matched element
		return this.each(function(index, list) {
			
			var element = this;
			$(element).hide();
			var totalFiles = $("ul.ui-articles li", element).size();
			var myID = $(element).parent().parent().attr("id").replace("module-content-", "");
			var myContainer = "#rotate-container-" + myID;
			window["rotateTimer" + myID] = 0;
			
			//IF IMAGES EXIST
			if(totalFiles>0){
				buildStructure();
			}
			
			function buildStructure(){
				//BUILD CONTAINER FOR ROTATOR
				var structureCSS = {
					'position'		: 'relative',
					'list-style'	: 'none',
					'margin'		: '0px',
					'padding'		: '0px'	
				}
				var structure = "<div id='" + myContainer.replace("#", "") + "' class='joel-rotate-container'>"+
								"	<ul class='pictures'></ul>" +
								"</div>";	
				$(element).parent().parent().append(structure);
				//ADD OVERLAY
				if(config.overlay == "yes"){
					if(config.useLinks == "yes") {
						$(myContainer).append("<a class='link-overlay'></a>");
					} else {
						$(myContainer).append("<div class='overlay'></div>");
					}
				}
				//ADD STYLES
				if(config.removeStyles == "no"){
					$("ul.pictures", myContainer).css(structureCSS);	
				}
				//ADD CLASS
				if(config.customClass == "yes"){
					$(myContainer).addClass(config.customClass);
				}
				//ADD CONTROLS
				if(config.controls == "yes"){
					$(myContainer).append("<div class='controls-container'><div class='button back'></div><div class='button toggle pause'></div><div class='button next'></div></div>");
					addControls();
				}
				//ADD BULLET CONTROLS
				if(config.itemize == "yes"){
					$(myContainer).append("<div class='itemize-container ui-clear'><div class='button back'></div><ul class='bullets-container ui-clear'></ul><div class='button next'></div></div>");
					if(config.itemizeThumbs == "yes") {
						$("div.itemize-container", myContainer).addClass("with-thumbs");	
					}
					itemize();
				}
				//ADD HOVER PAUSE
				if(config.hoverPause == "yes"){
					$(myContainer).mouseenter(function(){
						killTimer();
					});
					$(myContainer).mouseleave(function(){
						if($("div.button.toggle", myContainer).hasClass("pause")){
							window["hoverTimeOut" + myID] = setTimeout(function(){
								rotateImages();
								rotateTimer();
							}, (config.delay/2)*1000);
						}
					});
				}
				//INITIAL FUNCTION RUN
				var images = buildArray();
				loadImage(0, images);
			}
			
			function buildArray(){
				//BUILD THE DATA ARRAY
				var imagesArray = new Array();
				$("div.ui-article", element).each(function(index){
					imagesArray[index] = new Array();
					//IMAGE PATH
					if($(".ui-filelist-container",this).size()){
						imagesArray[index][0] = $("div.ui-filelist-container a:eq(0)", this).attr("href");
					} else {
						imagesArray[index][0] = $("div.ui-article-controls a:eq(0)", this).attr("href");
					}
					//TITLE
					imagesArray[index][1] = $.trim($("h1.ui-article-title", this).text());
					//DESCRIPTION
					imagesArray[index][2] = $.trim($("div.ui-article-description", this).html());
					//AUTHOR
					imagesArray[index][3] = $.trim($("span.ui-article-detail:eq(0) i", this).text());
				});
				//RANDOMIZE
				if(config.random == "yes"){
					imagesArray = imagesArray.sort(function(){return 0.5 - Math.random()});
				}
				return imagesArray;
			}
			
			//LOAD ALL DATA/IMAGES
			function loadImage(currentFile, images){
				if(currentFile<totalFiles){
					
					//ADD STYLES
					if(config.removeStyles == "no"){
						var liCSS = {
							"margin" 	: "0px",
							"padding"	: "0px",
							"position"	: "absolute",
							"top"		: "0px",
							"left"		: "0px"
						}
					} else {
						var liCSS = {}	
					}
					//ADD LI FOR EACH ITEM
					var myCont = myContainer + " ul.pictures";
					switch (currentFile){
						case 0 : $("<li class='imgHolder loading " + myID + " active' style='z-index:2' />").css(liCSS).appendTo(myCont);
						break;
						case 1 : $("<li class='imgHolder loading " + myID + " next' style='z-index:1' />").css(liCSS).appendTo(myCont);
						break;
						default : $("<li class='imgHolder loading " + myID + "' style='z-index:0' />").css(liCSS).appendTo(myCont);
					}
						
					//CURRENT LI TO LOAD TO
					var currentLI = $("li.imgHolder:last", myContainer);
					
					//ADD BULLET FOR EACH IMAGE
					if(config.itemize == "yes"){
						if(currentFile == 0) {
							$("ul.bullets-container", myContainer).append("<li class='button bullet bullet" + currentFile + " loading active'></li>");
						} else {
							$("ul.bullets-container", myContainer).append("<li class='button bullet bullet" + currentFile + " loading'></li>");	
						}
					}
												
					//ADD IMAGE
					$("<img />").load(function(){
						$(this).appendTo(currentLI);
						$(currentLI).removeClass("loading").show();
						//PUTS IN TITLE-DESC CONTAINER
						if(config.useTitles == "yes" || config.useDescriptions == "yes"){
							$(currentLI).append("<div class='text-container'></div>");	
						}
						//CHECKS FOR TITLE
						if(config.useTitles == "yes"){
							if(config.useLinks == "yes"){
								$(".text-container", currentLI).append("<a href='" + images[currentFile][3] + "'><h5 class='img-title'>" + images[currentFile][1] + "</h5></a>");
							} else {
								$(".text-container", currentLI).append("<h5 class='img-title'>" + images[currentFile][1] + "</h5>");
							}
						}
						//CHECKS FOR DESCRIPTION
						if(config.useDescriptions == "yes"){
							var descText = $.trim(images[currentFile][2]);
							if(descText == "" && config.useTitles == "no"){
								$(".text-container", currentLI).remove();	
							} else {
								$(".text-container", currentLI).append("<span class='img-desc'>" + images[currentFile][2] + "</span>");	
							}
						}
						//CHECKS FOR LINKS
						if(config.useLinks == "yes"){
							if(images[currentFile][3] != ""){
								$(this).wrap("<a href='" + images[currentFile][3] + "'></a>");	
							}
						}
						//OVERLAY WITH LINKS
						if(config.useLinks == "yes" && config.overlay == "yes"){
							$("a.link-overlay", myContainer).attr("href", $(".imgHolder.active a", myContainer).attr("href"));	
						}
						
						//DISPLAY BULLET
						if(config.itemize == "yes"){
								$("li.button.bullet", myContainer).eq(currentFile).removeClass("loading");
								if(config.itemizeThumbs == "yes") {
									$("li.button.bullet", myContainer).eq(currentFile).append("<div class='itemize-thumb'><img src='" + images[currentFile][0] + "'/></div>")	
								}
						}
						loadImage(currentFile+1, images);
						//RUN TIMER ON SECOND IMAGE COMPLETE
						if(currentFile == 1 && config.static == "no"){
							window["timeOut" + myID] = setTimeout(function(){
								rotateImages();
								rotateTimer();
							}, config.delay*1000);
						}
					}).attr('src',images[currentFile][0]);		
				}
			}
			
			function setZindexes(){
				$(".imgHolder", myContainer).css("z-index","0");
				$(".imgHolder.active", myContainer).css("z-index","2");
				$(".imgHolder.next", myContainer).css("z-index","1");
			}
			
			//CONTROLS FUNCTION
			function addControls(){
				$("div.button.toggle", myContainer).click(function(){
					if($(this).hasClass("play")){
						rotateImages();
						rotateTimer();
						$(this).removeClass("play").addClass("pause");
					} else {
						killTimer();
						$(this).removeClass("pause").addClass("play");
					}
				});
				$("div.button.next", myContainer).hover(function(){
					//DO NOTHING
				});
				$("div.button.next", myContainer).click(function(){
					if(!$(this).hasClass("animating")){
						var moveNumber = $(".imgHolder.active", myContainer).index()+1
						if($(".imgHolder.active", myContainer).index()+1 > (totalFiles - 1)) {
							moveNumber = 0;
						}
						moveToItem(moveNumber);
					}
				});
				$("div.button.back", myContainer).click(function(){
					if(!$(this).hasClass("animating")){
						var moveNumber = $(".imgHolder.active", myContainer).index()-1;
						if($(".imgHolder.active", myContainer).index()-1 < 0) {
							moveNumber = totalFiles -1;
						}
						moveToItem(moveNumber);
					}
				});
			}
			//BULLET CONTROLS FUNCTION
			function itemize() {
				$("div.button.next", myContainer).hover(function(){
					//DO NOTHING
				});
				$("div.button.next", myContainer).click(function(){
					if(!$(this).hasClass("animating")){
						var moveNumber = $(".imgHolder.active", myContainer).index()+1
						if($(".imgHolder.active", myContainer).index()+1 > (totalFiles - 1)) {
							moveNumber = 0;
						}
						moveToItem(moveNumber);
					}
				});
				$("div.button.back", myContainer).click(function(){
					if(!$(this).hasClass("animating")){
						var moveNumber = $(".imgHolder.active", myContainer).index()-1;
						if($(".imgHolder.active", myContainer).index()-1 < 0) {
							moveNumber = totalFiles -1;
						}
						moveToItem(moveNumber);
					}
				});
				$("li.button.bullet", myContainer).live('click',function(){
					moveToItem($(this).index());
				});
			}
			//ROTATE FUNCTION
			function rotateTimer(){
				window["rotateTimer" + myID] = setInterval(function(){
					rotateImages();
				}, config.delay*1000);	
			}
			function killTimer(){
				if(window["hoverTimeOut" + myID]){
					window.clearTimeout(eval("hoverTimeOut" + myID));
				}
				window.clearInterval(eval("timeOut" + myID));
				window.clearInterval(eval("rotateTimer" + myID));
			}
			function rotateImages(){
				if(!$("imgHolder.next", myContainer).prev().hasClass("loading")){
					//$("ul.pictures li:last", myContainer).prev().show();
					$(".button",myContainer).addClass("animating");
					//CHANGE BULLET ACTIVE
					if(config.itemize == "yes") {
						$("li.button.bullet", myContainer).removeClass("active");
					}
					//OVERLAY WITH LINKS
					$("a.link-overlay", myContainer).removeAttr("href");	
					$(".imgHolder.active", myContainer).fadeOut(config.fadeSpeed*1000, function(){
						$(this).removeClass("active");
						$(this).siblings(".next").addClass("active").removeClass("next");
						if($(".imgHolder.active", myContainer).next().size()){
							$(".imgHolder.active", myContainer).next().addClass("next");
						} else {
							$(".imgHolder:first", myContainer).addClass("next");	
						}
						setZindexes();
						$(".button",myContainer).removeClass("animating");
						$(this).show();
						//OVERLAY WITH LINKS
						if(config.useLinks == "yes" && config.overlay == "yes"){
							$("a.link-overlay", myContainer).attr("href", $(".imgHolder.active a", myContainer).attr("href"));	
						}
						//CHANGE BULLET ACTIVE
						if(config.itemize == "yes") {
							var myBullet = $(this).siblings(".active").index();
							$("li.button.bullet", myContainer).eq(myBullet).addClass("active");
						}
					});
				}
			}
			function moveToItem(moveToBullet){
				if(!$("div.animating", myContainer).size()){
					$(".imgHolder.active", myContainer).removeClass("active");
					$(".imgHolder.next", myContainer).removeClass("next");
					$(".imgHolder", myContainer).eq(moveToBullet).addClass("active");
					if($(".imgHolder.active", myContainer).next().size()){
						$(".imgHolder.active", myContainer).next().addClass("next");
					} else {
						$(".imgHolder:first", myContainer).addClass("next");	
					}
					setZindexes();
					//OVERLAY WITH LINKS
					if(config.useLinks == "yes" && config.overlay == "yes"){
						$("a.link-overlay", myContainer).attr("href", $(".imgHolder.active a", myContainer).attr("href"));	
					}
					//CHANGE BULLET ACTIVE
					if(config.itemize == "yes") {
						$("li.button.bullet", myContainer).removeClass("active");
						$("li.button.bullet", myContainer).eq(moveToBullet).addClass("active");
					}
				}
			}
	
	
		});
	};

})(jQuery); 
