// t: current time, b: begInnIng value, c: change In value, d: duration
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);
	},
	easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInOutQuad: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	},
	easeInCubic: function (x, t, b, c, d) {
		return c*(t/=d)*t*t + b;
	},
	easeOutCubic: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t + 1) + b;
	},
	easeInOutCubic: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
	},
	easeInQuart: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t + b;
	},
	easeOutQuart: function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	},
	easeInOutQuart: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
		return -c/2 * ((t-=2)*t*t*t - 2) + b;
	},
	easeInQuint: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t*t + b;
	},
	easeOutQuint: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t*t*t + 1) + b;
	},
	easeInOutQuint: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
		return c/2*((t-=2)*t*t*t*t + 2) + b;
	},
	easeInSine: function (x, t, b, c, d) {
		return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
	},
	easeOutSine: function (x, t, b, c, d) {
		return c * Math.sin(t/d * (Math.PI/2)) + b;
	},
	easeInOutSine: function (x, t, b, c, d) {
		return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
	},
	easeInExpo: function (x, t, b, c, d) {
		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	},
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	},
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + 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;
	},
	easeInOutCirc: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
		return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
	},
	easeInElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
	},
	easeOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	},
	easeInOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
		return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + 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;
	},
	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;
	},
	easeInOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158; 
		if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
		return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
	},
	easeInBounce: function (x, t, b, c, d) {
		return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b;
	},
	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;
		}
	},
	easeInOutBounce: function (x, t, b, c, d) {
		if (t < d/2) return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;
		return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
	}
});


/**
* jQuery.Preload
* Copyright (c) 2008 Ariel Flesler - aflesler(at)gmail(dot)com
* Dual licensed under MIT and GPL.
* Date: 3/12/2008
*
* @projectDescription Multifunctional preloader
* @author Ariel Flesler
* @version 1.0.7
*
* @id jQuery.preload
* @param {String, jQuery, Array< String, <a>, <link>, <img> >} original Collection of sources to preload
* @param {Object} settings Hash of settings.
*
* @id jQuery.fn.preload
* @param {Object} settings Hash of settings.
* @return {jQuery} Returns the same jQuery object, for chaining.
*
* @example Link Mode:
*	$.preload( '#images a' );
*
* @example Rollover Mode:
*	$.preload( '#images img', {
*		find:/\.(gif|jpg)/,
*		replace:'_over.$1'
*	});
*
* @example Src Mode:
*	$.preload( [ 'red', 'blue', 'yellow' ], {
*		base:'images/colors/',
*		ext:'.jpg'
*	});
*
* @example Placeholder Mode:
*	$.preload( '#images img', {
*		placeholder:'placeholder.jpg',
*		notFound:'notfound.jpg'
*	});
*
* @example Placeholder+Rollover Mode(High res):
*	$.preload( '#images img', {
*		placeholder:true,
*		find:/\.(gif|jpg)/,
*		replace:'_high.$1'
*	});
*/
; (function($) {

    var $preload = $.preload = function(original, settings) {
        if (original.split)//selector
            original = $(original);

        settings = $.extend({}, $preload.defaults, settings);
        var sources = $.map(original, function(source) {
            if (!source)
                return; //skip
            if (source.split)//URL Mode
                return settings.base + source + settings.ext;
            var url = source.src || source.href; //save the original source
            if (typeof settings.placeholder == 'string' && source.src)//Placeholder Mode, if it's an image, set it.
                source.src = settings.placeholder;
            if (url && settings.find)//Rollover mode
                url = url.replace(settings.find, settings.replace);
            return url || null; //skip if empty string
        });

        var data = {
            loaded: 0, //how many were loaded successfully
            failed: 0, //how many urls failed
            next: 0, //which one's the next image to load (index)
            done: 0, //how many urls were tried
            //found:false,//whether the last one was successful
            total: sources.length//how many images are being preloaded overall
        };

        if (!data.total)//nothing to preload
            return finish();

        var imgs = '<img/>', //ensure one
			thres = settings.threshold; //save a copy

        while (--thres > 0)//it could be oddly negative
            imgs += '<img/>';
        imgs = $(imgs).load(handler).error(handler).bind('abort', handler).each(fetch);

        function handler(e) {
            data.found = e.type == 'load';
            data.image = this.src;
            var orig = data.original = original[this.index];
            data[data.found ? 'loaded' : 'failed']++;
            data.done++;
            if (settings.placeholder && orig.src)//special case when on placeholder mode
                orig.src = data.found ? data.image : settings.notFound || orig.src;
            if (settings.onComplete)
                settings.onComplete(data);
            if (data.done < data.total)//let's continue
                fetch(0, this);
            else {//we are finished
                if (imgs.unbind)//sometimes IE gets here before finishing line 84
                    imgs.unbind('load').unbind('error').unbind('abort'); //cleanup
                imgs = null;
                finish();
            }
        };
        function fetch(i, img, retry) {
            if ($.browser.msie && data.next && data.next % $preload.gap == 0 && !retry) {//IE problem, can't preload more than 15
                setTimeout(function() { fetch(i, img, true); }, 0);
                return false;
            }
            if (data.next == data.total) return false; //no more to fetch
            img.index = data.next; //save it, we'll need it.
            img.src = sources[data.next++];
            if (settings.onRequest) {
                data.image = img.src;
                data.original = original[data.next - 1];
                settings.onRequest(data);
            }
        };
        function finish() {
            if (settings.onFinish)
                settings.onFinish(data);
        };
    };

    // each time we load this amount and it's IE, we must rest for a while, make it lower if you get stack overflow.
    $preload.gap = 14;

    $preload.defaults = {
        threshold: 2, //how many images to load simultaneously
        base: '', //URL mode: a base url can be specified, it is prepended to all string urls
        ext: '', //URL mode:same as base, but it's appended after the original url.
        replace: ''//Rollover mode: replacement (can be left empty)
        /*
        find:null,//Rollover mode: a string or regex for the replacement
        notFound:''//Placeholder Mode: Optional url of an image to use when the original wasn't found
        placeholder:'',//Placeholder Mode: url of an image to set while loading
        onRequest:function( data ){ ... },//callback called every time a new url is requested
        onComplete:function( data ){ ... },//callback called every time a response is received(successful or not)
        onFinish:function( data ){ ... }//callback called after all the images were loaded(or failed)
        */
    };

    $.fn.preload = function(settings) {
        $preload(this, settings);
        return this;
    };

})(jQuery);





var bgSpeed = 600; // Speed of background animation
var currentState = 1; // if 1(odd) then odd banner is on screen, else if 0(even) then even banner is on screen
var rotateSpeed = 6000;
var PRELOADED = false;
var easeType = 'easeInOutExpo';

$(document).ready(function() {
	
	//$('.controls li').css({ opacity: controllerOpacity });
	
	// ---------------------------- XML Read ------------------------------------
	$.ajax({
		type: "GET",
		url: "/data/banners.xml",
		dataType: "xml",
		success: function(xml) {
			
			var ban = $('banner', xml); 
			var nodeLevel = Math.floor(Math.random()*(ban.length));
			if (nodeLevel > 0 && nodeLevel < 3 ) {
				var lastNode = nodeLevel - 1;
			} else {
				var lastNode = 0;
			}
			
			// preload our images first
			$(xml).find('banner').each(function() {
				$.preload([$(this).find('background').text()], {
					base: '/images/'+region+'/',
					ext: ''
				});
			});

			$(xml).find('banner').each(function() {
				// on page load, this loads in first node into the odd banner
				lastNode = nodeLevel;
				var background = $(ban.get(nodeLevel)).find('background').text();
				$('#banner-odd').css('background', 'transparent url("/images/'+region+'/' + background + '") left top no-repeat');
			});
			
			function rightFunc(){
				
								
				//checks what's currently on and off screen, then changes order
				if (currentState == 1) {
					currentState = 0;
					offScreen = $('#banner-even');
					onScreen = $('#banner-odd');
				} else if (currentState == 0) {
					currentState = 1;
					offScreen = $('#banner-odd');
					onScreen = $('#banner-even');
				}
				//increment node level
				nodeLevel = nodeLevel + 1;
				// if current node level is higher than available nodes, reset to zero (first node)
				if (nodeLevel > (ban.length - 1)) {
					nodeLevel = 0;
				}
				lastNode = nodeLevel;
											
				clearInterval(auto);
				auto = setInterval(function(){rightFunc()}, rotateSpeed);
				
				//moves offScreen banner to the right
				offScreen.css('left', '558px');

				//gets next background node and adds it to offScreen div
				var background = $(ban.get(nodeLevel)).find('background').text();
				offScreen.css('background', 'transparent url("/images/'+region+'/' + background + '") left top no-repeat');

				//animates the divs
				onScreen.animate({ left: '-558px' }, bgSpeed, easeType);
				offScreen.animate({ left: '0' }, bgSpeed, easeType, function() {
				});

			};
			
			auto = setInterval(function(){rightFunc()}, rotateSpeed);
			
		}

	});
});
