// ROTATING SLIDESHOW GALLERY - NO LINKS
// @author Joel Grannas - Creative Director, Schoolwires, Inc. 
// Last updated on 6/28/2011

(function($) {
		  
	$.fn.joelRotate = function(settings){
		//SETTINGS
		var config = {
			'delay'				: 8,
			'fadeSpeed' 		: 2,
			'random'			: "no",
			'useTitles'			: "no",
			'useDescriptions'	: "no",
			'useLinks'			: "no",
			'controls'			: "no",
			'bulletControls'    : "no",
			'static'			: "no",
			'customClass'		: "no",
			'removeStyles'		: "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;
			//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 class='overlay'></div>"+
								"</div>";	
				$(element).parent().parent().append(structure);
				//ADD STYLES
				if(config.removeStyles == "no"){
					$("ul.pictures", myContainer).css(structureCSS);	
				}
				//ADD CLASS
				if(config.customClass != "no"){
					$(myContainer).addClass(config.customClass);
				}
				//ADD CONTROLS
				if(config.controls == "yes"){
					$(myContainer).append("<div class='controls-container'><div class='button back' title='Previous'></div><div class='button toggle pause' title='Pause'></div><div class='button next' title='Next'></div></div>");
					addControls();
				}
				//ADD BULLET CONTROLS
				if(config.bulletControls == "yes"){
					$(myContainer).append("<div class='controls-container'><div class='button back'></div><div class='bullets-container'></div><div class='button next'></div></div>");
					//ADD BULLET FOR EACH IMAGE
					for(i=0;i<totalFiles;i++){
						$(".bullets-container").append("<div id='bullet"+i+"' class='button bullet'></div>");
					}
					addBulletControls();
				}
				//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 !== "no"){
					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",
							"display"	: "none",
							"position"	: "absolute",
							"top"		: "0px",
							"left"		: "0px"
						}
					} else {
						var liCSS = {}	
					}
					//ADD LI FOR EACH ITEM
					var myCont = myContainer + " ul.pictures";
					$("<li id='image"+currentFile+"' class='imgHolder loading " + myID + "' />").css(liCSS).prependTo(myCont);
					
					//CURRENT LI TO LOAD TO
					var currentLI = $("li.imgHolder:eq(0)", myContainer);
												
					//ADD IMAGE
					$("<img />").load(function(){
						$(this).appendTo(currentLI);
						$(currentLI).removeClass("loading").show();
						//CHECKS FOR TITLE
						if(config.useTitles == "yes"){
							$(currentLI).append("<h1>" + images[currentFile][1] + "</h1>");	
						}
						//CHECKS FOR DESCRIPTION
						if(config.useDescriptions == "yes"){
							$(currentLI).append("<span>" + images[currentFile][2] + "</span>");	
						}
						//CHECKS FOR LINKS
						if(config.useLinks == "yes"){
							if(images[currentFile][3] != ""){
								$(this).wrap("<a href='" + images[currentFile][3] + "'></a>");	
							}
						}
						loadImage(currentFile+1, images);
						//RUN TIMER ON SECOND IMAGE COMPLETE
						if(currentFile == 1 && config.static == "no"){
							var timeOut = setTimeout(function(){
								rotateImages(0);
								rotateTimer();
							}, config.delay*1000);
						}
					}).attr('src',images[currentFile][0]);		
				}
			}
			
			//CONTROLS FUNCTION
			function addControls(){
				$("div.button.toggle", myContainer).click(function(){
					if($(this).hasClass("play")){
						rotateImages();
						rotateTimer();
						$(this).removeClass("play").addClass("pause");
						$(this).attr("title", "Pause");
					} else {
						killTimer();
						$(this).removeClass("pause").addClass("play");
						$(this).attr("title", "Play");
					}
				});
				$("div.button.next", myContainer).hover(function(){
					//DO NOTHING
				});
				$("div.button.next", myContainer).click(function(){
					if(!$(this).hasClass("animating")){
						moveNext();
					}
				});
				$("div.button.back", myContainer).click(function(){
					if(!$(this).hasClass("animating")){
						moveBack();
					}
				});
			}
			//BULLET CONTROLS FUNCTION
			function addBulletControls() {
				$("div.button.next", myContainer).hover(function(){
					//DO NOTHING
				});
				$("div.button.next", myContainer).click(function(){
					if(!$(this).hasClass("animating")){
						moveNext();
					}
				});
				$("div.button.back", myContainer).click(function(){
					if(!$(this).hasClass("animating")){
						moveBack();
					}
				});
				$("div.button.bullet", myContainer).click(function(){
					
				});
			}
			//ROTATE FUNCTION
			function rotateTimer(){
				rotateTimerVar = setInterval(function(){
				    //$("div.button.bullet").removeClass("active");
					//$("ul.pictures li.active").removeClass("active-img");
					rotateImages();
					var activeImage = $("ul.pictures li.active").attr("id");
					//alert(activeImage);
					//$("ul.pictures li").eq(0).addClass("active-img");
				}, config.delay*1000);	
			}
			function killTimer(){
				clearInterval(rotateTimerVar);
			}
			function rotateImages(currentFile){
				if(!$("ul.pictures li:last", myContainer).prev().hasClass("loading")){
					if(!$("ul.pictures li:last", myContainer).prev().hasClass("loading")){
						$("ul.pictures li:last", myContainer).prev().show();
						$("div.button",myContainer).addClass("animating");
						$("ul.pictures li:last", myContainer).fadeOut(config.fadeSpeed*1000, function(){
							$(this).parent().prepend(this);
							$(this).show();
							//$(this).addClass("active-img");
							$("div.button",myContainer).removeClass("animating");
						});
					}
				}
			}
			function moveNext(){
				var myList = myContainer + " ul.pictures";
				var myListLast = myContainer + " ul.pictures li:last";
				if(!$(myListLast).prev().hasClass("loading")){
					$(myListLast).prependTo(myList);
				}	
			}
			function moveBack(){
				var myList = myContainer + " ul.pictures";
				var myListFirst = myContainer + " ul.pictures li:first";
				if(!$(myListFirst).hasClass("loading")){
					$(myListFirst).appendTo(myList);
				}	
			}
			
		});
	};

})(jQuery); 
