/* 
 * ASSETS LOADER
 * Loads External CSS and Javascript Files Dynamically/On-Runtime.
 *
 * Written by: ~Kai~ [WebXpress CSS Developer]
 * Version: Beta 1.0
 */ 
 

/* @ EDIT THIS AREA : Read the Instructions Below this Function */
function PrepareAssets() {	
	
	// True if site is being developed locally, otherwise, false, especially on BC.
	this.isLocal = false;
	
	/* ESSENTIALS - DO NOT REMOVE - 
	this.AddCSS("global.css", "screen" );
	this.AddJS("jquery-1.4.3.js", this.DESIG_PRELOAD );
	this.AddJS("iepngfix_tilebg.js", this.DESIG_IE6PNGHACK );
	this.AddJS("ie6pnghover.js", this.DESIG_IE6PNGHACK );*/
	
	/* JS Scripts (Plugins, etc.) - INSERT HERE   <<-----------------  */
	
	this.AddJS('utools.js', this.DESIG_POSTLOAD );
			
	/* Add the CuFon API script - DO NOT REMOVE/REORDER - */
	this.AddJS("cufon.js", this.DESIG_POSTLOAD ); /* uncomment if needed */
	
	/* CuFon embedded fonts (js): - INSERT HERE   <<-----------------   */	
	this.AddJS('dinc.js', this.DESIG_POSTLOAD );
	
	/* Add the CuFon styles script file - DO NOT REMOVE/REORDER - */	
	this.AddJS("cufon-styles.js", this.DESIG_POSTLOAD ); /* uncomment if needed */
	
	return;
}

/* ADDING ASSETS

 Adding CSS --> use the function "this.AddCSS( filename, media );
 @ filename: this is the name of the file ( ex. "global.css" )
 @ media: the type of media the css properties should be rendered ( ex. "screen" or "print" )
 
 Adding JS --> use the function "this.AddJS( filename, designation );
 @ filename: this is the name of the file ( ex. "cufon.js" )
 @ designation: a constant variable indicating where or which part of the html template the script should be written/placed.
 
   Designation Constants ( 
   
   this.DESIG_PRELOAD 		- value : 0   ** Places the script inside the HEAD tag. "<head> ... </head>" **
   this.DESIG_IE6PNGHACK 	- value : 1   ** Places the script inside the IE6 PNG HACK area : for ie6 **
   this.DESIG_POSTLOAD		- value : 2   ** Places the script at the bottom, before the end BODY "</body>" tag. **	
   
   * Note: You can also pass the numerical value as parameter instead of the whole constant variable name. * 
*/



 /************************************************\
 | ASSETS LOADER OBJECT : DO NOT MODIFY THIS AREA |
 \************************************************/

_Assets = new Object();
_Assets.jsItems = [];
_Assets.cssItems = [];

function _AssetLoader() {
	
	this.name = "Asset Loader";
	this.version = "Beta 1.0";
	this.isLocal = true;
	this.default_JsPath = "js/";
	this.default_CssPath = "css/";	
	
	this.TYPE_CSS = 0;
	this.TYPE_JS = 1;
	this.DESIG_PRELOAD = 0;
	this.DESIG_POSTLOAD = 1;
	this.DESIG_IE6PNGHACK = 2;
	
	function _JS ( location, filename, type, designation ) {	
	    this.location = location;
		this.name = filename;
		this.type = type;
		this.designation = designation;		
		this.ToString = function() {
			return this.location + this.name + "(type: " + this.type + "; designation: " + this.designation + ")";
		}
	}
	
	function _CSS ( location, filename, media, type, designation ) {	
	    this.location = location;
		this.name = filename;
		this.media = media;
		this.type = type;
		this.designation = designation;		
		this.ToString = function() {
			return this.location + this.name + "(type: " + this.type + "; designation: " + this.designation + ")";
		}
	}
	
	this.PrepUp = PrepareAssets;
	
	this.Initialize = function() {
		this.PrepUp();
	}
	
	this.AddAsset = function ( filename, media, type, designation ) {
		
		var preSlash = (( this.isLocal ) ? "" : "/");
		
		if ( type == this.TYPE_CSS ) {
			var curIndex = _Assets.cssItems.length;
			var newLength = curIndex + 1;
			
			asset = new _CSS( preSlash + this.default_CssPath, filename, media, type, designation );
						
			_Assets.cssItems.length = newLength;	
			_Assets.cssItems[curIndex] = asset;
		}
		else if ( type == this.TYPE_JS ) {
			var curIndex = _Assets.jsItems.length;
			var newLength = curIndex + 1;
			
			asset = new _JS( preSlash + this.default_JsPath, filename, type, designation );
			
			_Assets.jsItems.length = newLength;	
			_Assets.jsItems[curIndex] = asset;
		}
		else {
			alert ( "Failed to load! Please specify the correct type." );
		}
				
	}
	
	this.AddCSS = function( filename, media ) {
		this.AddAsset( filename, media, this.TYPE_CSS, this.DESIG_PRELOAD );
	}
	
	this.AddJS = function( filename, designation ) {
		this.AddAsset( filename, null, this.TYPE_JS, designation );
	}
	
	this.WriteCSSLink = function () {
		for ( i=0; i<_Assets.cssItems.length; i++ ) {
			css = _Assets.cssItems[i];
			document.write('<link type="text/css" rel="stylesheet" href="' + css.location + css.name + '" media="' + css.media + '" />');
		}
	}
	
	this.WriteJSRefs = function ( designation ) {
		for ( i=0; i<_Assets.jsItems.length; i++ ) {
			js = _Assets.jsItems[i];
			if ( js.designation == designation ) {
				document.write('<script type="text/javascript" src="' + js.location + js.name + '"></script>');
			}
		}		
	}
	
	this.LoadPreScripts = function () { 		
		/* load css first */
		this.WriteCSSLink();		
		/* load top js */
		this.WriteJSRefs( this.DESIG_PRELOAD );				
	}	
	
	this.LoadIE6Scripts = function () {		
		/* load ie6 png hacks js */
		this.WriteJSRefs( this.DESIG_IE6PNGHACK );		
	}	
	
	this.LoadPostScripts = function () { 	
		/* load bottom js */
		this.WriteJSRefs( this.DESIG_POSTLOAD ); 
	}	
}


/* Initialize */
try {	
	AssetLoader = new _AssetLoader();
	AssetLoader.Initialize();	

} catch ( err ) {
	console.log( "Failed to Initialize Assets Loader: " + err.description );
}
