// ***************************************************************
// ***************************************************************
// ***************************************************************
// Banner JS

// ***************************************************************
// ***************************************************************


function text_change(){
	next_int = text_int+1;
	if( next_int == text_total)
		next_int = 0;
	
	var $cur = $('#'+text_container[text_int]);
	var $next = $('#'+text_container[next_int]);
	
	// reset current count
	text_int = next_int;
	
	//animate the current slide out
	$cur.fadeOut();
	$next.fadeIn('normal', function(){
		text_timer = setTimeout(text_change, text_speed);
	});
}


// ***************************************************************
// ***************************************************************
// Rotating Images


// Define Banner Variables
var banner_container = new Array(); 
var banner_count = 0;
var banner_total = 0;
var banner_index = 0;
var banner_speed = 4000;
var banner_timer = null;
var banner_width = 0;
var $_banners = null;

$(document).ready(function(){	

	$_banners = $('.banner-view .banner-view-item');
	banner_width = $_banners.width();
	banner_total = $_banners.length;
	
	if( banner_total > 0 ){
		//collect the banner ids
		$_banners.each(function(i, obj){
			var id = $(this).attr('id');
			banner_container.push(id);
		});
		//assign a z index in reverse
		for(var i=banner_total; i>=1; i--){
			banner_index++;
			$('#'+banner_container[(i-1)]).css('z-index', banner_index);
		}
		
		//show the first slide and start animation
		$_banners.css('left', '-'+banner_width+'px');
		$_banners.show();
		$('#'+banner_container[0]).css('left', '0px');
		
		banner_start();
	}
});

function banner_start(){
	if( banner_total <= 1) return;
	banner_timer = setTimeout(banner_change, banner_speed);			
}

function banner_stop(){
	clearTimeout( banner_timer );		
}

function banner_change(){
	next_count = banner_count+1;
	if( next_count == banner_total)
		next_count = 0;
	
	var $cur = $('#'+banner_container[banner_count]);
	var $next = $('#'+banner_container[next_count]);
	banner_index++;
	
	$next.css('z-index',banner_index);
	
	// reset current count
	banner_count = next_count;
	
	//animate the current slide out
	$next.css('opacity', '.2');
	$cur.animate({left:'-'+banner_width+'px', 'opacity':'.2'}, 800, 'easeInCirc' );
	
	window.setTimeout(function(){
		$next.animate({left:'0px', 'opacity':'1'}, 800, 'easeOutCirc', function(){
			// start the counter again
			banner_start();
		});
	}, 300);
}
/**
 * Based on Robert Penners easing functions
 * http://www.robertpenner.com/easing/
 */
 $(document).ready(function(){
	jQuery.extend( jQuery.easing,
	{
		easeOutBounce: function (x, t, b, c, d) {
			if ((t/=d) < (1/2.75)) {
				return c*(7.5625*t*t) + b;
			} else if (t < (2/2.75)) {
				return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
			} else if (t < (2.5/2.75)) {
				return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
			} else {
				return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
			}
		},
		easeInBack: function (x, t, b, c, d, s) {
			if (s == undefined) s = 1.70158;
			return c*(t/=d)*t*((s+1)*t - s) + b;
		},
		easeInCirc: function (x, t, b, c, d) {
			return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
		},
		easeOutCirc: function (x, t, b, c, d) {
			return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
		}
	});
});
