/*!
 #####################################################################
 # 
 # Phenotype HTML5+Minimal Flash Boilerplate Template
 # JavaScript setup routines
 #
 # Hand-crafted by Phenotype (phenotype.net)
 #
 #####################################################################
 */
	
	/* =Assign setup routines
	------------------------------------------------------------------- */
	
		// Called when DOM is ready
		jQuery(document).ready( domSetup );
		
		// Called when entire page is loaded
		jQuery(window).load( pageSetup );
		
		// Called when DOM is unloaded
		jQuery(window).unload( domUnload );
	
	/* =Global variables
	------------------------------------------------------------------- */
    
    var deadSlides = [];
    var running = false;
    
    
    /* =Declare setup routines
	------------------------------------------------------------------- */
	
	/**
	 * domSetup()
	 *
	 * All JavaScript requiring initialisation on DOM LOAD should be called
	 * from this routine.
	 */
	function domSetup() {

		/* =Enhancements
		------------------------------------------------------------------- */
		
			jQuery(".tbSlideClick").click(function () {
				jQuery("#content"+this.id).slideToggle("slow");
			});
		
		/* =Navigation
		------------------------------------------------------------------- */
			
			/*
			Setup external links (target attribute not allowed by XHTML 1.1)
			(see http://www.sitepoint.com/article/standards-compliant-world/)
			*/
			jQuery('a[rel="external"]').attr('target','blank');
            
            
            
            // COUNTERS
            jQuery("#counterCardiff").countdown({date:"October 7, 2012 10:00:00", onComplete:removeSlide});
            jQuery("#counterNottingham").countdown({date:"October 13, 2012 10:00:00", onComplete:removeSlide});
            jQuery("#counterEdinburgh").countdown({date:"October 21, 2012 10:00:00", onComplete:removeSlide});
            jQuery("#counterLondon").countdown({date:"November 11, 2011 10:00:00", onComplete:removeSlide});
            
            // SCROLL LEFT
            jQuery('#slideshow2').cycle({
                fx: 'scrollLeft',
                speed:    2000, 
                timeout:  5000,
                pause:    1,
                after:    onAfter  
            });
            
            running = true;
	}
	
	/**
	 * pageSetup()
	 *
	 * All JavaScript requiring initialisation on PAGE LOAD should be called
	 * from this routine (all images and elements should be loaded and ready to
	 * manipulate by this point)
	 */
	function pageSetup() {

	}
	
	/**
	 * domUnload()
	 *
	 * Called when page/DOM is unloaded
	 */
	function domUnload() {

	}
    
    
    /**
	 * removeSlide()
	 *
	 * Add slide to dead queue when it is no longer needed
     */
    function removeSlide() {
        if(running) 
            deadSlides.push( jQuery(this).parent() );
        else
            jQuery(this).parent().remove();
    }
    
    
    /**
	 * onAfter()
	 *
	 * After a slide has finished its display check if any need to be removed.
     */
    function onAfter() { 
        
    
        if(deadSlides.length){
            
            // Stop previous slide show
            jQuery('#slideshow2').cycle('stop');
            
            for(i=0; i < deadSlides.length; i++){
                
                var elem = deadSlides[i];
                deadSlides.splice(i, 1);
                elem.remove();
            }
            
            deadSlides = [];
        
            jQuery('#slideshow2').cycle({
                fx: 'scrollLeft',
                speed:    2000, 
                timeout:  5000,
                pause:    1,
                after:    onAfter  
            });
        }
    }
    
    
    
    
    
    
    
    
