	/*
	JS
	--------------------------------------------------------------------------------------------
	@site			sho.com/lalaland
	@file			Lalaland.js
	@author			ncavanagh
	@desc			Sets up/extends a modular js library and Series namespace.
	@depend			prototype
	
	/* =:Lalaland
	--------------------------------------------------------------------------------------------*/
	if(typeof Series =="undefined"){ var Series = new Object(); }

	Series.Lalaland = function()
	{ 
		var jsBase = "/site/lalaland/season1/js";
		var KEY_IN = '/site/lalaland/';
		var KEY_OUT = '.do';
		var TOKEN = '_';
		
		var library = 
		{
			video: [
				'brightcove',
				'title'
			],
			
			home: [
				'HomeUIHelp'
			],
			
			episodes: [
				'EpisodesUIHelp'
			],
			
			schedule: [
				'Schedule'
			],
			
			downloads: [
				'DownloadsUIHelp'
			]
			
		};
		
		
		/* =:Startup
		--------------------------------------------------------------------------------------------*/
		function init()
		{
			if( document.location.search.toString().indexOf('noscript') > -1 ) return;
			document.write( "\n" );
			parseLocation();
		}
		
		function parseLocation()
		{  
			var urlStr = document.location.pathname;
			var str1 = urlStr.substr( urlStr.indexOf( KEY_IN ) + KEY_IN.length );
			var str2 = str1.substr( 0, str1.indexOf( KEY_OUT )).replace(/-/, '_');
			var path = str2.split('/');
			
			// check for flattened homepage
			if( document.location.pathname.indexOf('/index.html') !== -1 ){
				importModules ( ['home'] );
			}
											   
			else importModules ( path );
			
		}
		
		function importModules ( path )
		{  
			var m = "";
			for( var i=0; i<path.length; i++ ){
				m += path[i];  
				if( typeof library[ m ] == "object" &&  library[ m ] !== null ){ 
					loadModule( library[ m ], path[0] );
				}
				m+=TOKEN;
			}
		}
		
		function loadModule( mod, package )   
		{   
			package = ( package == false ) ? '' : '/' + package;
			for( var i=0; i<mod.length; i++) 
			{   
				document.write( '<script language="javascript" src="'+ jsBase + package + '/'+
							   			mod [i] + '.js"></script>'+"\n" );	
			}
		}
		
		
		/* Reveal public methods 
		-------------------------*/
		return {
		deploy:init
		}
		
	}();

	Series.Lalaland.deploy(); 
	
