﻿/**
 * Main Namespace that contains all other components
 * @object	TCU  
 * @scope	public
 */

var TCU = {
	
	/**
	 * Allows debugging messages to be sent to the console
	 * (if it's available)
     * @property	DEBUG  
     * @scope		public
     */
	DEBUG : true,

    /**
	 * UI Component References for all main page areas.
	 * As scripts (below) are lazy loaded, the objects they create 
	 * are attached to TCI.UI
     * @object	UI  
     * @scope	public
     */
	UI : { 

			FireBug:{
				url:'/_Assets/_js/Helper/firebug/firebug.js',
				loaded:false,
        IsReady: false
			},

      Application:{
        url:'/_Assets/_js/TCU/TCU.UI.Application203153.js',
				loaded:false,
        IsReady: false      
      },

			HolidaySearchForm : {
				url:'/_Assets/_js/TCU/TCU.UI.HolidaySearchForm203153.js',
				loaded:false,
        IsReady: false
			},			

			Shortlist:{
				url:'/_Assets/_js/TCU/TCU.UI.Shortlist203153.js',
				loaded:false,
        IsReady: false
			},

			ResultsDetailView:{
				url:'/_Assets/_js/TCU/TCU.UI.ResultsDetailView203153.js',
				loaded:false,
        IsReady: false
			}, 

			ComparisonTable:{
				url:'/_Assets/_js/TCU/TCU.UI.ComparisonTable203153.js',
				loaded:false,
        IsReady: false
			},

			Destinations:{
				url:'/_Assets/_js/TCU/TCU.UI.Destinations203153.js',
				loaded:false,
        IsReady: false
			},

			Profile:{
				url:'/_Assets/_js/TCU/TCU.UI.Profile203153.js',
				loaded:false,
        IsReady: false
			},

			SignInSignOutRegister:{
				url:'/_Assets/_js/TCU/TCU.UI.SignInSignOutRegister203153.js',
				loaded:false,
        IsReady: false
			}, 

			Header:{
				url:'/_Assets/_js/TCU/TCU.UI.Header203153.js',
				loaded:false,
        IsReady: false
			},

			HolidayDeals:{
				url:'/_Assets/_js/TCU/TCU.UI.HolidayDeals203153.js',
				loaded:false,
        IsReady: false
			},

//			PackageOptions:{
//				url:'/_Assets/_js/TCU/TCU.UI.PackageOptions.js',
//				loaded:false,
//        IsReady: false
//			},

			Address:{
				url:'/_Assets/_js/TCU/TCU.UI.Address203153.js',
				loaded:false,
        IsReady: false
			},

			BookmarkingManager: {
        url:'/_Assets/_js/TCU/TCU.UI.BookmarkingManager203153.js',
        loaded:false,
        IsReady: false
      },

			SearchManager: {
        url:'/_Assets/_js/TCU/TCU.UI.SearchManager203153.js',
        loaded:false,
        IsReady: false
      },

			// Start stuff for booking screens...
      BookingStep1:{
        url:'/_Assets/_js/TCU/Booking/TCU.UI.BookingStep1203153.js',
        loaded:false,
        IsReady: false
      },

      BookingAgents:{
        url:'/_Assets/_js/TCU/Booking/TCU.UI.BookingAgents.js',
        loaded:false,
        IsReady: false
      }, 

      BookingPhoneScreen1:{
        url:'/_Assets/_js/TCU/Booking/TCU.UI.BookingPhoneScreen1.js',
        loaded:false,
        IsReady: false
      },	

      BookingScreen2:{
        url:'/_Assets/_js/TCU/Booking/TCU.UI.BookingScreen2203153.js',
        loaded:false,
        IsReady: false
      },

      BookingScreen3:{
        url:'/_Assets/_js/TCU/Booking/TCU.UI.BookingScreen3203153.js',
        loaded:false,
        IsReady: false
      },
			// End stuff for booking screens
			
			Test:{
				url:'/_Assets/_js/TCU/TCU.UI.Test.js',
				loaded:false,
        IsReady: false
			}

	},

	LoadComponent: function(component){

		if(component && component.IsLoaded == false){

			var s = document.createElement('script');
			s.setAttribute('type', 'text/javascript');
			s.setAttribute('src', component.SourceFile);
			document.getElementsByTagName('head')[0].appendChild(s);

		}

	},
  
  ComponentReady: function(component){

    this.UI[component].IsReady = true;

    if (component == 'SearchManager'){
      var x = this.UI['HolidaySearchForm'];
      if (x.IsReady == true){
        TCU.UI.SearchManager.DoCalendarSearch();
      }
    }

    if (component == 'HolidaySearchForm'){
      var x = this.UI['SearchManager'];
      if (x.IsReady == true){
        TCU.UI.SearchManager.DoCalendarSearch();
      }
    }


  },

			
	/**
     * Checks to see if an component is loaded, if its not, 
     * it'll attach the JS document to the head and it'll execute,
     * creating the component.
     * @param	component (object) - component to register
     * @method	addComponent  
     * @scope	public
     */
	
//	PreInitHooks: {}, 
//	addPreInitHook: function(component,method)
//	{
//	    if (this.PreInitHooks[component]==null) this.PreInitHooks[component] = {};
//	    this.PreInitHooks[component][this.PreInitHooks[component].length] = method;
//	},
	
	addComponent:function(component){
		var c = this.UI[component];
		if(c && c.loaded == false){
			var s = document.createElement('script');
			s.setAttribute('type', 'text/javascript');
			s.setAttribute('src',c.url);
			document.getElementsByTagName('head')[0].appendChild(s);
			//this.log("addComponent: %o", "log", c);
		}
	},

	
	/**
	* Allows a component to register and attach itself with 
	* the main TCU.UI namespace.
	* @param	component (object) - component to register
	* @method	register  
	* @scope	public
	*/
	register:function(component){
	
    var c = this.UI[component];
		c.loaded = true;


		//this.log("register: %o", "log", c);
		if(c.InitUI){
			//this.log("InitUI: %o", "log", c);
			$(c.InitUI);
		}



    
	//	if(this.PreInitHooks[component]){
		    //this.log("Firing PreInitHooks: %o", "log", c);
		//    for(i in this.PreInitHooks[component]) this.PreInitHooks[component][i](); 
		//};


		//this.log("register finished: %o", "log", c);
	},
	
	/**
	* Simple logging function that wraps the console commands
	* @param	message {string} - the message to output
	* @param	type {string} - type of message (log, debug, etc)
	* @param	obj {object} - object to inspect (optional)
	* @method	log  
	* @scope	public
	*/

	log:function(message,type,obj) {
		if ( ("console" in window) &&  this.DEBUG ) {		
			var msgobj = (obj != null ? obj : "");
			switch(type)
			{
				case "log" :
					console.log( message , msgobj );
					break;
				case "debug" :
					console.debug( message , msgobj );
					break;
				case "info" :
					console.info ( message , msgobj );
					break;
				case "warn" :
					console.warn ( message , msgobj );
					break;
				case "error" :
					console.error ( message , msgobj );
					break;
				case "assert" :
					console.assert ( message , msgobj );
					break;
			}
		}
	}
	
};

