// 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,
			'useTitles'			: "no",
			'useDescriptions'	: "no",
			'useLinks'			: "no",
			'controls'			: "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'></div><div class='button toggle pause'></div><div class='button next'></div></div>");
					addControls();
				}
				//INITIAL FUNCTION RUN
				loadImage(0);
			}
			
			//LOAD ALL DATA/IMAGES
			function loadImage(currentFile){
				if(currentFile<totalFiles){
					//BUILD THE DATA ARRAY
					var images = new Array();
					images[currentFile] = new Array();
					//IMAGE PATH
					if($(".ui-filelist-container",element).size()){
						images[currentFile][0] = $("div.ui-article:eq(" + currentFile + ") div.ui-filelist-container a:eq(0)", element).attr("href");
					} else {
						images[currentFile][0] = $("div.ui-article:eq(" + currentFile + ") div.ui-article-controls a:eq(0)", element).attr("href");
					}
					//TITLE
					images[currentFile][1] = $.trim($("div.ui-article:eq(" + currentFile + ") h1.ui-article-title", element).text());
					//DESCRIPTION
					images[currentFile][2] = $.trim($("div.ui-article:eq(" + currentFile + ") div.ui-article-description", element).html());
					//AUTHOR
					images[currentFile][3] = $.trim($("div.ui-article:eq(" + currentFile + ") span.ui-article-detail:eq(0) i", element).text());
					
					//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 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);
						//RUN TIMER ON SECOND IMAGE COMPLETE
						if(currentFile == 1){
							var timeOut = setTimeout(function(){
								rotateImages();
								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");
					} 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")){
						moveNext();
					}
				});
				$("div.button.back", myContainer).click(function(){
					if(!$(this).hasClass("animating")){
						moveBack();
					}
				});
			}
			//ROTATE FUNCTION
			function rotateTimer(){
				rotateTimerVar = setInterval(function(){
					rotateImages();
				}, config.delay*1000);	
			}
			function killTimer(){
				clearInterval(rotateTimerVar);
			}
			function rotateImages(){
				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();
							$("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); 
