if ( !window.EST ) EST = {};



if ( !EST.WaterFight )
EST.WaterFight =
{
	Constants:
	{
		path:'',
		heights:[ 872, 1806, 2761, 3714, 4192 ]
	},
	browser: {type: null, majorVersion: null, fullVersion: null},

	setup: function()
	{
		// retrieve assets path
		var scriptSrcs = document.getElementsByTagName( 'script' );
		var path = '';
		for ( var x = 0; x < scriptSrcs.length; x++ ) if ( scriptSrcs[x].src && ( path = scriptSrcs[x].src.match( /(.+?)javascripts\/waterfight.loader.js$/ ) ) ) break;
		EST.WaterFight.Constants.path = path[1];

		// set up body overflow
		jQuery( 'body' ).css( { overflow:'hidden' } ).addClass( 'dynamic' );

		// set up loader
		EST.WaterFight.UI.Loader.setup();

		// kick off base cache
		new EST.WaterFight.Cache.Class( { group:'base' } );
		
		EST.WaterFight.browser.type = jQuery.browser;
		EST.WaterFight.browser.version = jQuery.browser.version.charAt(0);
		if(jQuery.browser.msie){
			EST.WaterFight.browser.type = "msie";
			EST.WaterFight.browser.fullVersion = jQuery.browser.version;
			EST.WaterFight.browser.majorVersion = Math.floor(EST.WaterFight.browser.fullVersion);
			jQuery("html").addClass(EST.WaterFight.browser.type+EST.WaterFight.browser.majorVersion);
		}
			
	},
	
	Cache:
	{
		images:[],
		Text: { assets:'Loading Assets', images:'Loading Images' },
		Groups:
		{
			base:
			{
				js:		[ 'http://www.google-analytics.com/ga.js', 'lib/jquery.ui.js', 'lib/jquery.ui.easing.js', 'lib/cufon.js', 'waterfight.main.js' ],
				//js:		[ 'lib/ga.js', 'lib/jquery.ui.js', 'lib/jquery.ui.easing.js', 'lib/cufon.js', 'waterfight.main.js' ],
				css:	[ 'waterfight.base.css', 'waterfight.base.ie8.css', 'waterfight.base.ie7.css', 'waterfight.base.ie6.css' ],
				fonts:	[ 'miso.ttf', 'miso.ie.eot' ],
				images: [],
				html:	'/Module_WaterFight_Base.html',
				cached: false
			},
			group0:
			{
				js:		[],
				css:	[ 'waterfight.how.css' ],
				fonts:	[],
				images: [],
				html:	'Module_WaterFight_How.php',
				cached: false
			},
			group1:
			{
				js:		[],
				css:	[ 'waterfight.ways.css' ],
				fonts:	[],
				images: [],
				html:	'Module_WaterFight_Ways.html',
				cached: false
			},
			group2:
			{
				js:		[],
				css:	[ 'waterfight.about.css' ],
				fonts:	[],
				images:	[],
				html:	'Module_WaterFight_About.html',
				cached: false
			}
		},
		
		Class: function( data )
		{
			var group = data.group;
			var counter = 0;
			var total = 0;
			var loader;
			var loaderOffset = data.offset ? data.offset : 0;
			var buffer = { js:[], css:[], images:[], html:'' };

			function cache()
			{
				var x = 0;
				var image;
				var filename;
				var cachegroup;
	
				cacheGroup = EST.WaterFight.Cache.Groups[ group ];
				cacheGroup.cached = true;
	
				// create loader
				loader = group == 'base' ? new EST.WaterFight.UI.Loader.Class() : new EST.WaterFight.UI.Loader.Class( loaderOffset );
				loader.setMessage( EST.WaterFight.Cache.Text.assets );
				loader.update( { count:0, total:1 } );
	
				jQuery.each( cacheGroup, function( key, value )
				{
					if ( key == 'js' )
					{
						total += value.length;
	
						jQuery.each( value, function()
						{
							filename = this.match( /http\:\/\// ) ? this : EST.WaterFight.Constants.path + 'javascripts/' + this;
							jQuery.ajax( { dataType:'script', url:filename, success:function() { notify( { type:'js', filename:filename } ) } } );
						} );
					}
					else if ( key == 'css' )
					{
						jQuery.each( value, function()
						{

							browserTarget = this.match( /\.ie[0-9]\./ );

							if ( !this.match( /\.ie[0-9]\./ ) == true || (browserTarget != null && jQuery.browser.version.charAt(0) == browserTarget[0].charAt(3)) )
							{
								
								filename = EST.WaterFight.Constants.path + 'css/' + this;
								
								jQuery.ajax( { dataType:'text', type:'POST', url:filename, success:function( text ) { notify( { type:'css', filename:filename, content:text } ) } } );
								
								total++;
							}
							
							
						} );
					}
					else if ( key == 'fonts' && !jQuery.browser.msie )
					{
						jQuery.each( value, function()
						{
							if ( !this.match( /\.ie\./ ) || jQuery.browser.msie )
							{
								filename = EST.WaterFight.Constants.path + 'fonts/' + this;
								jQuery.ajax( { dataType:'text', type:'POST', url:filename, success:function( text ) { notify( { type:'font', filename:filename } ) } } );
								
								total++;
							}
						} );
					}
					else if ( key == 'html' )
					{
						filename = this;
						jQuery.ajax( { dataType:'text', url:filename, success:function( text ) { notify( { type:'html', filename:filename, content:text } ) } } );
						
						total++;
					}
				} );
			}
			
			function notify( data )
			{
				var cacheGroup;
				var x, y, cssImages, htmlImages, htmlContainer, bufferedImages, insertHtml, height;
	
				cacheGroup = EST.WaterFight.Cache.Groups[ group ];
	
				// increase cache counter
				counter++;
	
				// if it's css or html, buffer it until complete load
				if ( data.type == 'css' ) buffer.css.push( data.content );
				else if ( data.type == 'html' ) buffer.html = data.content;
	
				// if all css/js has loaded, load the images
				if ( counter == total && data.type != 'image' )
				{
					// set loader message
					loader.setMessage( EST.WaterFight.Cache.Text.images );
					loader.update( { count:0, total:1 } );
	
					// add css images to cache list
					for ( x = 0; x < buffer.css.length; x++ )
					{
						cssImages = buffer.css[x].match( /[a-z0-9-]+?\.jpg|[a-z0-9-]+?\.png/g );
	
						if ( cssImages && cssImages.length )
						{
							for ( y = 0; y < cssImages.length; y++ )
							{
								if ( jQuery.inArray( cssImages[y], EST.WaterFight.Cache.images ) == -1 )
								{
									cacheGroup.images.push( cssImages[y] );
									EST.WaterFight.Cache.images.push( cssImages[y] );
								}
							}
						}
					}
	
					// add html images to cache list
					htmlImages = buffer.html.match( /[a-z0-9-]+?\.jpg|[a-z0-9-]+?\.png/g );
					if ( htmlImages && htmlImages.length )
					{
						for ( y = 0; y < htmlImages.length; y++ )
						{
							if ( jQuery.inArray( htmlImages[y], EST.WaterFight.Cache.images ) == -1 )
							{
								cacheGroup.images.push( htmlImages[y] );
								EST.WaterFight.Cache.images.push( htmlImages[y] );
							}
						}
					}

					// reset the counter and loader ui
					counter = 0;
					total = cacheGroup.images.length;

					// cache images
					for ( x = 0; x < cacheGroup.images.length; x++ )
					{
						image = new Image();
						image.onload = function() { notify( { type:'image' } ) };
						image.src = EST.WaterFight.Constants.path + 'images/' + cacheGroup.images[x];
	
						// add buffering item to the buffer queue
						buffer.images.push( image );
					}
				}
				else if ( counter == total && data.type == 'image' )
				{
					// load css into the page
					if ( !jQuery.browser.msie ) jQuery.each( cacheGroup.css, function()
					{
							if ( !this.match( /\.ie\./ ) ) jQuery( 'head' ).append( '<link rel="stylesheet" href="' + EST.WaterFight.Constants.path + 'css/' + this + '" type="text/css" />' );
					} );
					else jQuery.each( buffer.css, function()
					{
						jQuery( 'body' ).append( '<style type="text/css">' + this.replace( /url\(\.\.\//g, 'url(' + EST.WaterFight.Constants.path ) + '</style>' );
					} );
	
					// load the html fragment into the page
					htmlContainer = jQuery( '#WaterFightContainer' );
					if ( htmlContainer.length == 0 )
					{
						jQuery( 'body' ).append( '<div id="WaterFightContainerOuter"><div id="WaterFightContainerMiddle"><div id="WaterFightContainerInner"><div id="WaterFightContainer"></div></div></div></div>' );
						htmlContainer = jQuery( '#WaterFightContainer' );
					}
	
					insertHtml = jQuery( buffer.html );
					htmlContainer.append( insertHtml );

					// fix navigation in ie
					if ( jQuery.browser.msie && jQuery.browser.version < 8 ) jQuery( 'body' ).append( jQuery( '#WaterFightNavigation' ) );

					// update loader
					loader.update( { count:counter, total:total } );

					// set up cufon
					if ( group == 'base' ) EST.WaterFight.preSetup();
					else
					{
						if ( group == 'group1' ) EST.WaterFight.TipsCarousel.setup();
						else if ( group == 'group2' ) EST.WaterFight.AboutCarousel.setup();
					
						EST.WaterFight.Cufon.refresh();
					}
	
					// set finish function
					var appendDidEnd = group == 'base' ? function()
					{
						// set the viewport height
						jQuery( '#WaterFightContainerOuter' ).css( { height:jQuery( window ).height() } );
						jQuery( 'body' ).css( { overflowY:'auto' } );

						height = EST.WaterFight.Constants.heights[ EST.WaterFight.Constants.heights.length - 1 ];
						jQuery( '#WaterFightContainerOuter' ).animate( { height:height }, 750, 'easeOutQuad', EST.WaterFight.setup );
					} : EST.WaterFight.Cufon.refresh;
	
					// animate in
					if ( !jQuery.browser.msie ) insertHtml.css( { opacity:0 } ).animate( { opacity:1 }, 1000, 'easeInOutQuad', appendDidEnd );
					else if ( group == 'base' ) appendDidEnd();

					// clear out the buffered images
					bufferedImages = cacheGroup.images;
					while ( bufferedImages.length )
					{
						delete bufferedImages[0];
						bufferedImages.splice( 0, 1 );
					}
	
				}
				else
				{
					// update loader
					loader.update( { count:counter, total:total } );
				}
			}

			cache();
		}
	},

	UI:
	{
		Loader:
		{
			element: null,
			instances: [],

			setup: function()
			{
				EST.WaterFight.UI.Loader.element = jQuery( '#WaterFightLoader' );
			},

			Class: function( loaderOffset )
			{
				var element;
				var offset = loaderOffset ? loaderOffset + 300 : 0;
				
				function create()
				{
					if ( loaderOffset )
					{
						// create loader
						element = EST.WaterFight.UI.Loader.element.clone();
						element.removeAttr( 'id' );
						element.css( { top:offset } );

						// append to parent
						jQuery( '#WaterFightContainer' ).append( element );
					}
					else element = EST.WaterFight.UI.Loader.element;
				}

				function update( data )
				{
					var progress = Math.ceil( ( data.count / data.total ) * 100 ) + '%';
					var easing = jQuery.easing.easeInOutQuad ? 'easeInOutQuad' : 'swing';
	
					element.find( 'strong' ).text( progress );
	
					if ( data.count == 0 ) element.show().stop().animate( { opacity:1 }, 500, easing );
					else if ( data.count == data.total ) element.stop().animate( { opacity:0 }, 500, easing, function() { jQuery( this ).hide() } );
				}

				function setMessage( text )
				{
					element.find( 'span' ).text( text );
				}

				create();
				
				return (
				{
					update:update, setMessage:setMessage
				} );
			}
		}
	}
};
