
var Inspired = new Class({
	
	historyManager: null,
	options: {},
	mainMovie: null,
	language: null,
	
	
	initialize: function(options) {
		this.setOptions(options);
		
		var flashvars = {};
		flashvars.LANG = this.options.language;
		flashvars.LANGUAGES = this.options.siteLanguagesString;
		flashvars.CONTENT_PATH = this.options.content_path;
		flashvars.ABS_ROOT = this.options.site_abs_root;
		flashvars.ENVIRONMENT = "web";
		flashvars.HOME_SECTION_PATH = this.options.homeSectionPath;
		flashvars.SCREEN_WIDTH = screen.width;
		flashvars.SCREEN_HEIGHT = screen.height;
		flashvars.movie = this.options.site_abs_root + "_content/inspired" + this.options.siteMovieVersion + ".swf";
		
		this.language = this.options.language;
		
		var params = {};
		params.menu = "false";
		params.scale = "noscale";
		//params.salign = "tl";
		params.wmode = "opaque";
		params.swliveconnect = "true";
		params.allowfullscreen = "true";
		params.allownetworking = "all";
		params.allowscriptaccess = "sameDomain";
		var attributes = { id: "mainMovie" };
		swfobject.embedSWF(this.options.site_abs_root + "_content/inspired_wrapper.swf", "mainMovie", "100%", "100%", "9.0.0", this.options.site_abs_root + "_content/expressInstall.swf", flashvars, params, attributes);
		
		if(swfobject.hasFlashPlayerVersion("9.0.0")) { 
		  swfobject.addLoadEvent(this.setFocusOnFlash);
		}
		
		window.addEvent('domready', this.onDOMReady.bind(this));
		window.addEvent('load', this.checkHeight.bind(this));
		window.addEvent('resize', this.checkHeight.bind(this));
	},
	
	
	/*
		-- defined flash movie object var and the historyManager
	*/
	onDOMReady: function() { 
		// the flash movie is not instantiated immediately, so catch it after 1/10 of a second
		this.onDOMReady2.delay(100, this);
	},
	onDOMReady2: function() {
		this.mainMovie = swfobject.getObjectById('mainMovie');
		this.historyManager = new HistoryManager();
		this.historyManager.addEvent('onHistoryChange', this.navigate.bind(this));
		
		var startLocation = this.historyManager.getCurrentLocation();
		//console.debug(startLocation);
		if(startLocation) 
			this.navigate(startLocation, true);
		else if(this.options.homeSectionPath)
			this.navigate(this.options.initiallyOpenedSectionPath, true);
		
		swfmacmousewheel.registerObject("mainMovie");
	},
	
	
	/*
		-- puts the flash object in focus to receive keyboard events without clicking on it
	*/
	setFocusOnFlash: function() {
		var fl = $('mainMovie');
		if(fl && fl.focus) { try { fl.focus(); } catch(e) { } }
	},
	
	/*
		-- adds/removes scrollbars
	*/
	checkHeight: function() {
		var allCSize = $('allContainer').getSize();
		if(allCSize.y <= 530) {
			$('innerContainer').setStyle("height", "580px");	
		} else {
			$('innerContainer').setStyle("height", "100%");	
		}
		
		if(allCSize.x < 920) {
			$('innerContainer').setStyle("width", "930px");	
		} else {
			$('innerContainer').setStyle("width", "100%");	
		}
	},
	
	
	
	/*
		-- determines the language number from the lenauge string pathLangStr
	*/    
	getLanguage: function(pathLangStr) {
		var curLang = this.language;
		var nextLang = curLang;
		if(pathLangStr) {
			for(var i = 0; i < this.options.siteLanguages.length; i++) {
				if(this.options.siteLanguages[i]) {
					if(pathLangStr.toLowerCase() == this.options.siteLanguages[i]) {
						nextLang = i;
						break;
					}
				}
			}
		}
		
		return nextLang;
	},
	
	/*
		-- navigates the page or the apporpriate page part to the location specified by newLocation
	*/
	navigate: function(newLocation, bDontUpdateHistory) {
		var queryString = '';
		if(newLocation.indexOf('?') != -1) {
			queryString = newLocation.substr(newLocation.indexOf('?') + 1);
			newLocation = newLocation.substr(0, newLocation.indexOf('?') - 1);
		}
		
		// amend the history object
		if(!bDontUpdateHistory) {
			var newLocationForHistory = newLocation;
			if(newLocationForHistory.substr(-1, 1) != '/') newLocationForHistory += '/';
			this.amendBrowserHistory(newLocationForHistory);
		}
		
		// remove first and last slashes (if present)
		if(newLocation.substr(-1, 1) == '/') newLocation = newLocation.substr(0, newLocation.length - 1);
		if(newLocation.substr(0, 1) == '/') newLocation = newLocation.substr(1);
		
		var newPathInfo  = newLocation.split('/');
		if(newLocation.length < 1) return;
		
		// update language and content in flash
		this.language = this.getLanguage(newPathInfo[0]);
		newPathInfo.shift();	// remove the language part
		this.mainMovieNavigate(newPathInfo.join('/'));
	},
	
	
	/*
		-- updates historyManager
	*/
	amendBrowserHistory: function(newLocation) {
		if(this.historyManager.getCurrentLocation() != newLocation);
			this.historyManager.addState(newLocation);
		
		if(window.pageTracker && window.pageTracker._trackPageview) {
			//console.log('TRACK: ' + newLocation);
			window.pageTracker._trackPageview(newLocation);
		}
		
		document.title = "Inspired";
	},
	
	
	/*
		-- connect to the flash movie via ExternalInterface and pass the section path to it
	*/
	mainMovieDelayed: 0,
	mainMovieNavigate: function(sPath) {
		var extIntResult;
		$clear(this.mainMovieDelayed);
		if(!this.mainMovie || !this.mainMovie.navigateToSection || !(extIntResult = this.mainMovie.navigateToSection(this.language, sPath))) {
			this.mainMovieDelayed = this.mainMovieNavigate.delay(100, this, sPath);
		}
	},
	
	
	/*
		-- FROM FLASH via ExternalInterface: update historyManager
	*/
	external_SetHistory: function(sPath) {
		this.amendBrowserHistory(
			'/' + this.options.siteLanguages[this.language] + '/' + sPath + '/'
		);
	},


	/*
		-- FROM FLASH via ExternalInterface: update language
	*/
	external_ChangeLanguage: function(newLangId) {
		this.language = newLangId;
	}
	
	
	
	
	
	
	
	
});

Inspired.implement(new Options);
var inspired = new Inspired(globalOptions);
