
 /*! Copyright (c) 2009 Radgeofrey Mission (http://www.codezyne.com)
 *
 * jQuery.CDZ_IMAGEPRELOADER.js (IE6+, FF, OPERA, SAFARI)
 *
 * Version: 1.0.0
 * Date: 14th November, 2009
 * 
 * Requires: jQuery 1.3.2+
 *
 * About: A very simple yet lightweight preloader, this preload images from <IMG> or CSS Backrounds, 
          I often use this to my site and now im sharing it.
 *
 * Example : $(document).cdzPreloadImages(function(total){alert("Preloader has started")},
										  function(loaded, total){alert("Preload Progress : " + loaded + " / " + total)}, 
										  function(){$('#loadingstatus').hide(); $('#mainpage').show();} );
	      The sample above will preload images of entire document, including background images. And assumimg that #loadingstatus div is visible
		  and #mainpage div is not visible, so upon complete preload : a callback function will be called to hide #loadingstatus and show #mainpage.
		  
Revision As of Dec 02, 2009 : trigger complete event if no image found
 */
(function($){
		  
		  
		  $.fn.preloadLinks = ''
		  $.fn.preloadSuccess = ''
		  
		  $.fn.cdzPreloadImages = function(onstart, onprogress, oncomplete)
		  {
			$.fn.preloadLinks = new Array();
		  	$.fn.preloadSuccess = new Array();
			$(this).find('[class], [style]').each(function(){
														
						if($(this).css("background-image")!='none')
						{
						$(this).preloadLinks[$(this).preloadLinks.length] = $(this).css("background-image").match(/url\((.*)\)/)[1];
						}												  
						});
			$(this).find('img').each(function(){
														
						if($(this).attr("src")!='')
						{
						$(this).preloadLinks[$(this).preloadLinks.length] = $(this).attr("src");
						}												  
						});
			onstart($(this).preloadLinks.length);
			for(q in  $(this).preloadLinks)
			{

						$.ajax({type: "GET", url: $(this).preloadLinks[q], complete: function() { 
																	
																	$(this).preloadSuccess[$(this).preloadSuccess.length]='Complete';
																	onprogress($(this).preloadSuccess.length, $(this).preloadLinks.length)
																	if($(this).preloadLinks.length == $(this).preloadSuccess.length)oncomplete();
																	
																								 }});
						
			
			}
			if($(this).preloadLinks.length==0)oncomplete();
			return $(this);
		  }
		  
		  
 })(jQuery);
/*
 # cc : content controller - functionality for slideshow etc
 */ 
$(function(){

	// config variables
	var config = {
		slideshowNav : false,
		slideshowDisplay : 6000,
		slideshowTransitionSpeed : 2000,
		captionDelay : 200,
		captionTransitionSpeed : 500
	};
	
	// content controller
	var cc = {
		
		init : function() {
			
			var $slideshowRef = $("ul#slideshowImages li");
			$slideshowRef.each(function() {
				$(this).css("display", "none");
			});
			
		  $(document).cdzPreloadImages(function(total){},
		  function(loaded, total){}, 
		  function(){
			  var counter=0;
			  $slideshowRef.each(function() {
				
				if (counter==1) {
					$(this).fadeIn("slow");
				} else {
					$(this).css("display", "block");
				}
				counter++;
			  });
			  cc.initSlideshow();
	      });
			
		},
	
		// slideshow setup with navigation
		initSlideshow : function() {
			
			$("ul#slideshowImages li:first").addClass("active");
			$("ul#slideshowNav li:first").addClass("active");
			
			// Set initial z-indexes and display images (initially hidden by css to stop flash of last slideshow image in list)
			var $slideshowRef = $("ul#slideshowImages li");
			var $slideshowImgRef = $("ul#slideshowImages li img");
			var slideshowIndex = 100;
			$slideshowRef.each(function() {
				$(this).css("z-index", slideshowIndex);
				slideshowIndex -=1;
			});
			
			/*function displayImages() {
				$slideshowImgRef.each(function() {
					$(this).css("display", "block");
				});*/
				/*clearInterval(initDisplayImages);
			};*/
			
			// init slideshow if more than one image
			if($("ul#slideshowImages li img").length >1) {
				
				// Initialise slideshow
				//var initDisplayImages = setTimeout(displayImages, 500);

				// Interval timer for loop, cleared when user selects link from slideshow nav
				var slideshowLoop = setInterval(showNextSlideshow, config.slideshowDisplay);

				if(config.slideshowNav) {
					// Timeout to slide in numbers
					setTimeout(showSlideshowNav, 300);
				
					// Activate links
					$("ul#slideshowNav li").click(function() {
						clearInterval(slideshowLoop);
						if ($("ul#slideshowImages li.active").length) showSlideshow(this);
					});
				};
				
			} 
			
			function showSlideshowNav() {
			};
			
			function showNextSlideshow() {
				if ($("#slideshowNav .active").next().length) {
					showSlideshow($("ul#slideshowNav li.active").next());
				}
				else {
					showSlideshow($("ul#slideshowNav li:first").get());
				}
			};
			
			function showSlideshow(linkRef) {
				// slideshow link is already active
				if ($(linkRef).hasClass("active")) return false;

				// Stop current animation
				$("ul#slideshowImages").stop();

				// Set link to active etc
				$("ul#slideshowNav li").removeClass("active");
				$(linkRef).addClass("active");
				var slideshowNumber = $(linkRef).prevAll().length;
				var $slideshowNew = $("ul#slideshowImages li").eq(slideshowNumber);
				$slideshowNew.css("z-index", "99");

				// Animate
				$("ul#slideshowImages li.active").removeClass("active").fadeOut(config.slideshowTransitionSpeed, function() {
					$(this).css("z-index", "90").show();
					$slideshowNew.css("z-index", "100").addClass("active");
				});
			};
			
		}
	}; // end slideshow controller
	
	cc.init();
		
});
