/*
JS
--------------------------------------------------------------------------------------------
@site				sho.com/site
@project			sho (970)
@package			n/a
@file				lib.js
@modified			06.14.10
@author				dpaul
@note				Define global/common packages to import in the layout file, not here.  
@note               Added analytics to Brick homepage

/* =:sho
--------------------------------------------------------------------------------------------*/
var sho = {}; sho.settings = {};
sho.Library = function()
{ 
	var LIVE_URL = 'http://www.sho.com';
	var JS_BASE = "/site/sho/js/com/sho";
	var JS_SELF = '/site/sho/js/lib.js';
	var KEY_IN = '/site/';
	var KEY_OUT = '.do';
	var TOKEN = '_';
	var GLOBAL_PACKAGE; 
	 
	
	var lib = 
	{
		core: [
			'Debugger',
			'Sugar',
			'UserAgent',
			'Utils',
			'Cosmetics',
			'Overlay'
		],
		common: [
		    'Dashboard',
			'Navigation',
			'Membership',
			'Dialogs',	
			'SimpleVideo'	
		],
		components: [
			'Calendar',
			'DropDownMenu',
			'DateMenu',
			'Glider',
			'SelectionMatrix',
			'Tabs'
		],
		forms : [
			'Validation',
			'FieldLimiter'
		],
		global: [
			'Backgrounds',   
			'MoviesModule',
			'Scaffolding'  
		],
		ecards:[
			'FormHelp'
		],
		home:[
			'Carousel',
			'RSSTicker',
    	    'Analytics'
		],
		schedules_channel:[
				'HDModule',
				'ChannelData',
			'Channel'
		],
		schedules_grid:[
				'ChannelData',
				'ProductInfo',
				'GridSlider',
			'Grid'
		],
		schedules_product:[
				'PhotoGallery',
			'Product'
		],
		schedules_ondemand:[
				'OnDemandModel',
				'OnDemandController',
				'OnDemandView',
			'OnDemand'
		],
		series:[
			'Pager',
			'HomePage'
		],
		search:[
	    ]
				
	};
	
	/* =:Private
	----------------------------------------------------------------------------------------*/
	function initialize()
	{
		if( document.location.search.toString().indexOf('noscript') > -1 ) return;
		checkEnvironment();
		
		document.write( "\n\t" );
		parseScriptParams();
		parseLocation();
	}
	
	function checkEnvironment()
	{
		// set flag for local hosting mode (ie not thirdparty)
		if(([ 'miami.sho.com','dev.sho.com','qa.sho.com','posting.sho.com','www.sho.com','search.sho.com' ].join(' ').indexOf( document.location.host ) > -1 ) ||
		document.location.port == '8080' ) // producer tomcat instance
        {
            sho.settings.isLocal = true;
        }
        else
        {
            sho.settings.isLocal = false;
        }
        
        // set flag for development mode (show scaffolding, use new accounts links in nav..)
	    if(((['dev.sho.com','qa.sho.com'].join(' ').indexOf( document.location.host ) > -1 ) ||
	    document.location.port == '8080' ) && sho.settings.isLocal )
	    {
	        sho.settings.environment = 'DEVELOPMENT';
	    }
	    else
	    {    
	        sho.settings.environment = 'PRODUCTION';
	    }
	}
	
	function parseScriptParams()
	{
		$$('head script[src]').findAll(function(s)
		{
			// find the the <script> element that links to this file
			return s.src.match(JS_SELF);
			
		}).each(function(s)
		{
			var modules = [];
			// pull out the params stashed on the end
			if( s.src.match(/\?.*load=([a-z,1-9]+)/)){
	    		modules = s.src.match(/\?.*load=([a-z,1-9]+)/)[1].split(',');
			}
			// load each module that is specified 
			modules.each(function(m){
				loadModule(lib[m],m)
			});
	    });
	}
	
	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 transitional brick homepage
		if( document.location.pathname.indexOf('/site/index-brick.html') !== -1 ){
			loadModule ( lib.home, 'home' );
		}
		// check for flattened homepage
		if( document.location.pathname.indexOf('/site/index.html') !== -1 ){
			loadModule ( lib.home, 'home' );
		}
		// check for flattened schedule grid
		if( document.location.pathname.indexOf('/grid.html') !== -1 ){
			loadModule ( lib.schedules_grid, 'schedules' );
		}

		// check for search.sho.com
		if( document.location.host.indexOf('search.sho.com') !== -1 ||
			document.location.pathname.indexOf('search.html') !== -1 ){   	
			loadModule ( lib.search, 'search' );
		}        
    			
		else importModules ( path );	
	}
	
	function importModules ( path )
	{
		var m = "";
		for( var i=0; i<path.length; i++ )
		{
			m += path[i];
			if( typeof lib[ m ] == "object" &&  lib[ m ] !== null )
			{ 
				var module = lib[ m ];
				var package = GLOBAL_PACKAGE == undefined ?
					path[0] : GLOBAL_PACKAGE;
				
				loadModule( module, package );
				
			}
			m+=TOKEN;
		}
	}
	
	function loadModule( mod, p )
	{ 
		var prefix = sho.settings.isLocal ? '' : LIVE_URL;
		var package = ( p == false ) ? '' : '/' + p;
		for( var i=0; i<mod.length; i++)
		{
			document.write((['',
			'<script language="javascript" src="', prefix, JS_BASE, package , '/',
				mod[i], '.js"></script>',"\n\t",
			'']).join(''))
		}
	}	
	
	/* =:Reveal as Public
 	----------------------------------------------------------------------------------------*/
	return { 
		init:initialize
	}
	
}();

sho.Library.init(); 

/* No surrender, No delete! */
