//Safe slideshow

$(function(){
	  $('ol.slides').cycle({
      fx:'fade',
      pager: '.slideControls',
      before:function(){
	         //Find any child elements that need animating:
	        $(this).addClass('active').siblings().removeClass('active');
	      },
      after:function(){  
        //Sliding panels animate in from one edge:
         var subPanels = $(this).children('.slidingPanel');
         var initialised = false;
 	       subPanels.each(function(){
 	          if ($(this).is('.fromRight')) {
 	            $(this).animate(
 	              {'width': $(this).css('max-width')}, 
 	              function(){
 	                childComponents($(this))
 	              });
 	          }
 	          else if ($(this).is('.fromLeft')) {
 	             $(this).animate({'width': $(this).css('max-width')}, 
 	               function(){
  	                childComponents($(this))
  	              });
 	          }
 	          else if ($(this).is('.fromBottom')) {
 	            $(this).animate({'height': $(this).css('max-height')},
 	              function(){
 	                childComponents($(this))
 	              });
 	          }
 	          else if ($(this).is('.fromTop')) {
 	            $(this).animate({'height': $(this).css('max-height')},
 	              function(){
 	                childComponents($(this))
 	              });
             }
           });
           
           //Fading panels fade in:
     	    $(this).children('.fadingPanel').each(
     	      function(){
   	          $(this).fadeIn(200);
   	        }
   	      );
   	      
           //Anything we need to do after the initial animation is complete we can handle here:
           var childComponents = function(elem) {
             initialised = true;
             if (!elem) return false;
             
             //Fade in subPanels in sequence
   	        var subPanels = elem.find('.panel'),
   	          sequenceIndex = 0;
   	          (function(){$(subPanels[sequenceIndex++] || []).fadeIn(300, arguments.callee)})();
           }
           
           //if no-one has initialised yet, fire it now:
           if (!initialised) childComponents($(this));
 	       }
       });
 });

