var Goofy = function( param_config_path){
	this.init( param_config_path);
}


Goofy.prototype = {
		
	init:function(param_config_path){
		var ref = this;
		var current = this;
		this.config;
		this.pathFinder;
		this.localization;
		this.sitemap;
		this.navigator;
		this.dispatcher;
		this.sectionManager = new SectionManager();
		this.mainLoader;
		this._sitemapLoader;		
			
		this.config = new Config(param_config_path);
		$(document).bind('config_ready' , {scope:current}, function(e){
			e.data.scope.applyConfig();
		} );
	},
	
	
	applyConfig:function(){
		this.pathFinder = new PathFinder(this.config.get('paths'));
		this.initializeLocalization();
	},
	
	
	initializeLocalization:function() {
		if ( this.config.get( 'enable_deeplinking' ) == 'true' && this.config.get( 'show_locale_in_url' ) == 'true')
			this.detectLocale();
		else
			this.launchLocalization();
				
	},
	
	
	detectLocale:function(e){
		var foundLocale = undefined;
		var tabURL = StringUtils.cleanHash(window.location.hash.split('/'));
		console.log(tabURL)
		if(tabURL.length > 0)
			foundLocale = tabURL[0];
		this.launchLocalization(foundLocale);	
	},
	
	
	launchLocalization:function(param_locale){
		var current = this;
		this.localization = new Localization( this.pathFinder.get( 'data' ) , this.pathFinder.get( 'i18n' ) , param_locale != undefined ? param_locale : this.config.get( 'force_locale' ));
		$(document).bind('localization_ready' , $.proxy(this.onLocalizationReady , this));
	},
		
	
	
	onLocalizationReady:function(){
		$(document).unbind('localization_ready');
		this.loadSitemap();
	},
	
	
	loadSitemap:function() {
		this._sitemapLoader = new SitemapLoader( this.pathFinder.get( 'i18n' ) + this.localization.getLocale() + '/sitemap.xml' );			
		$(document).bind('sitemap_loaded' , $.proxy(this.sitemapLoaded , this));
	},
	
	
	sitemapLoaded:function(){
		this.sitemap = new Sitemap(this._sitemapLoader._xmlStructure , this._sitemapLoader._objectStructure);
		var showLocale = this.config.get('show_locale_in_url') == "true" ? true : false;
		var langInfo = this.localization.getLocaleInfo(this.localization.getLocale());
		if(langInfo.hasOwnProperty('show_locale_in_url'))
			showLocale = langInfo.show_locale_in_url == "true" ? true : false;
		this.navigator = new Navigator(this.sitemap , this.localization.getLocale() , showLocale);
		this.dispatcher = new Dispatcher(this.sitemap);
		this.prepareDownload();
	},
	
	
	prepareDownload:function(){
		this.mainLoader = new MainLoader(this.sitemap ,this.loadComplete );
		this.mainLoader.launch();
	},
	
	
	loadComplete:function(){
		$(document).trigger('goofy_ready');
	}
	
};
