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

(function($) {
		  
	$.fn.joelRotate = function(settings){
		//SETTINGS
		var config = {
			'delay'				: 8,
			'fadeSpeed' 		: 2,
			'useTitles'			: "no",
			'useDescriptions'	: "no",
			'useLinks'			: "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 structure = "<div id='" + myContainer.replace("#", "") + "'>"+
								"	<ul class='pictures' style='position:relative; list-style:none; margin:0px; padding:0px'></ul>"+
								"	<div class='overlay'></div>"+
								"</div>";	
				$(element).parent().parent().append(structure);
				//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
					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 a", 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 LI FOR EACH ITEM
					$("<li class='imgHolder loading' />").css({
						"margin" 	: "0px",
						"padding"	: "0px",
						"display"	: "none",
						"position"	: "absolute",
						"top"		: "0px",
						"left"		: "0px" 
					}).prependTo("ul.pictures", myContainer);
					
					//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){
							rotateImages();
							rotateTimer();
						}
					}).attr('src',images[currentFile][0]);		
				}
				
				//ROTATE FUNCTION
				function rotateTimer(){
					rotateTimer = setInterval(function(){
						rotateImages();
					}, config.delay*1000);	
				}
				
				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();
							$("ul.pictures li:last", myContainer).fadeOut(config.fadeSpeed*1000, function(){
								$(this).parent().prepend(this);
								$(this).show();
							});
						}
					}
				}
				
				
				
				
			}
			
			
			
		});
	};

})(jQuery); 
