/*********************************************************************************************/
/**** Clockwork Marketing - St Martin's School Homepage Slider JS jQuery File			  ****/
/**** 																					  ****/
/**** This file should require no config - all configuration is done within the html file ****/
/**** Uses jQuery Easing and Timer plugins 												  ****/
/*********************************************************************************************/

/*
 * jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
 *
 * Uses the built in easing capabilities added In jQuery 1.1
 * to offer multiple easing options
 *
 * TERMS OF USE - jQuery Easing
 * 
 * Open source under the BSD License. 
 * 
 * Copyright © 2008 George McGinley Smith
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of 
 * conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list 
 * of conditions and the following disclaimer in the documentation and/or other materials 
 * provided with the distribution.
 * 
 * Neither the name of the author nor the names of contributors may be used to endorse 
 * or promote products derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 *  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE. 
 *
*/

jQuery.easing['jswing'] = jQuery.easing['swing'];
jQuery.extend( jQuery.easing,
{
	def: 'easeOutQuad',
	swing: function (x, t, b, c, d) {
		//alert(jQuery.easing.default);
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
	}
});
jQuery.fn.extend({
	everyTime: function(interval, label, fn, times, belay) {
		return this.each(function() {
			jQuery.timer.add(this, interval, label, fn, times, belay);
		});
	},
	oneTime: function(interval, label, fn) {
		return this.each(function() {
			jQuery.timer.add(this, interval, label, fn, 1);
		});
	},
	stopTime: function(label, fn) {
		return this.each(function() {
			jQuery.timer.remove(this, label, fn);
		});
	}
});
jQuery.event.special
jQuery.extend({
	timer: {
		global: [],
		guid: 1,
		dataKey: "jQuery.timer",
		regex: /^([0-9]+(?:\.[0-9]*)?)\s*(.*s)?$/,
		powers: {
			'ms': 1,
			'cs': 10,
			'ds': 100,
			's': 1000,
			'das': 10000,
			'hs': 100000,
			'ks': 1000000
		},
		timeParse: function(value) {
			if (value == undefined || value == null)
				return null;
			var result = this.regex.exec(jQuery.trim(value.toString()));
			if (result[2]) {
				var num = parseFloat(result[1]);
				var mult = this.powers[result[2]] || 1;
				return num * mult;
			} else {
				return value;
			}
		},
		add: function(element, interval, label, fn, times, belay) {
			var counter = 0;
			if (jQuery.isFunction(label)) {
				if (!times) 
					times = fn;
				fn = label;
				label = interval;
			}
			interval = jQuery.timer.timeParse(interval);
			if (typeof interval != 'number' || isNaN(interval) || interval <= 0)
				return;
			if (times && times.constructor != Number) {
				belay = !!times;
				times = 0;
			}
			times = times || 0;
			belay = belay || false;
			var timers = jQuery.data(element, this.dataKey) || jQuery.data(element, this.dataKey, {});
			if (!timers[label])
				timers[label] = {};
			fn.timerID = fn.timerID || this.guid++;
			var handler = function() {
				if (belay && this.inProgress) 
					return;
				this.inProgress = true;
				if ((++counter > times && times !== 0) || fn.call(element, counter) === false)
					jQuery.timer.remove(element, label, fn);
				this.inProgress = false;
			};
			handler.timerID = fn.timerID;
			if (!timers[label][fn.timerID])
				timers[label][fn.timerID] = window.setInterval(handler,interval);
			this.global.push( element );
		},
		remove: function(element, label, fn) {
			var timers = jQuery.data(element, this.dataKey), ret;
			if ( timers ) {
				if (!label) {
					for ( label in timers )
						this.remove(element, label, fn);
				} else if ( timers[label] ) {
					if ( fn ) {
						if ( fn.timerID ) {
							window.clearInterval(timers[label][fn.timerID]);
							delete timers[label][fn.timerID];
						}
					} else {
						for ( var fn in timers[label] ) {
							window.clearInterval(timers[label][fn]);
							delete timers[label][fn];
						}
					}	
					for ( ret in timers[label] ) break;
					if ( !ret ) {
						ret = null;
						delete timers[label];
					}
				}			
				for ( ret in timers ) break;
				if ( !ret ) 
					jQuery.removeData(element, this.dataKey);
			}
		}
	}
});
jQuery(window).bind("unload", function() {
	jQuery.each(jQuery.timer.global, function(index, item) {
		jQuery.timer.remove(item);
	});
});

$(document).ready(function() { 	
	var currentSlide = 0;
	var inProgress = false;// Blocks multiple animations happening at once.
	
	function doAnimate(){
		if(allowForward == true){
			if (inProgress != true){
				inProgress = true;
				currentSlide++;
				var textSpeed2 = textSpeed/2;
				$('.slideText').animate({marginTop:'300px'},textSpeed2);
				if ((currentSlide) <= numofslides){
					if(currentSlide > 0){
					$('#sliderContainer').stop().oneTime(textSpeed2,function(){
						$('#sliderContainer').animate({marginLeft: '-' + (currentSlide * bannerWidth) + 'px'}, transitionSpeed,'swing',function(){
							inProgress = false;
							var slideText = currentSlide + 1;
							var newMargin = $('#slideText'+ slideText).height();
							newMargin = bannerHeight - newMargin;
							$('.slideText').animate({marginTop:newMargin+'px'},textSpeed);
																																				});
																																				
					});
				}
				if (currentSlide == (numofslides)){
					$('#sliderContainer').oneTime(textSpeed2,function(){
					$('#sliderContainer').animate({marginLeft: '0px'}, 1);
					currentSlide = 0;
																	  });
					//var slideText = currentSlide +1;
					//var newMargin = $('#slideText'+ slideText).height();
					//newMargin = bannerHeight - newMargin;
					//$('.slideText').delay(500).animate({marginTop:newMargin+'px'},textSpeed);
				}
			}else{
//				alert('eol');
				$('#sliderContainer').oneTime(textSpeed,function(){
				$('#sliderContainer').animate({marginLeft: '-'+(bannerWidth*0)+'px'}, transitionSpeed,'swing',function(){
				$('#sliderContainer').oneTime(textSpeed,function(){
				$('#sliderContainer').animate({marginLeft: '-'+(bannerWidth*1)+'px'}, 1,'swing',function(){
					currentSlide = 0;
					inProgress = false;
				
						var slideText = currentSlide +1;
						var newMargin = $('#slideText'+ slideText).height();
						newMargin = bannerHeight - newMargin;
						$('.slideText').animate({marginTop:newMargin+'px'},textSpeed);
																														  });
																																		});
					});	
			});
		}
		}
		}
	}
	/*************** Start Up Functions ******************/
	/*													 */
	/* Collects data from html page					  	 */
	/* Moves the DOM and CSS around to make a new slider */
	/*													 */
	/*****************************************************/
	jQuery.easing.def = "easeOutBack";	
	var numofslides = $('#sliderContainer > div:last').attr('title');
	var transitionSpeed = 1000;
	var textSpeed = 500;
	var auto = true;
	var bannerHeight = 300;
	var bannerWidth = 960;
	var allowForward = true;
	var allowBackward = false;
	var slideText = currentSlide + 1;
	var newMargin = $('#slideText1').height();
	newMargin = bannerHeight - newMargin;
	$('.slideText').animate({marginTop:newMargin+'px'},10);
	// Clone slides to allow for seamless transitions between start and finish
	$('#slide1').clone().insertAfter($('#sliderContainer > div:last'));
	$('#slide2').clone().insertAfter($('#sliderContainer > div:last'));
	$(this).everyTime(7000, "hide", function() {if(auto == true){doAnimate();}});
	$('#slider').mouseover(function(){inProgress = true;}).mouseleave(function(){inProgress = false;});
}); //End doc ready function
