/* --------------------------------------------------------------------------------*/	
/*                      IGOT-WEB.INDEX - MAIN JAVASCRIPT                          */
/* --------------------------------------------------------------------------------*/

igotweb.Tools.namespace("igotweb.igotweb");

/*
 *	Object: igotweb.index
 *	Object which manage the main functionnalities of igot-web index page.
 *	
 *	parameters:
 *		- none.
 *	return:
 *		- none.
 */
igotweb.igotweb.index = {

	_errors : null,
	_isInit : true,
	_isPageLoading : false,
	
	_navigation : null,
	_contactUsLink : null,

	/*
	 *	function: init
	 *	This function init the igot-web index page.
	 *	
	 *	parameters:
	 *		- none.
	 *	return:
	 *		- none.
	 */
	init : function() {
		this._errors = Errors.getErrors("indexErrors");
		
		this._navigation = jQuery(".navigation");
		this._contactUsLink = jQuery(".footer a.contactUs");
		
		this._contactUsLink.bind("click", jQuery.proxy(this._onContactUsLinkClick, this));
		
		var config = {
			"position" : "top",
			"containerClasses" : ["languages"],
			"onMenuActionEvent" : jQuery.proxy(this._onLanguagesLinkClick, this)
		};
		var languages = {
			"menu" : JSONData.availableLanguages
		};
		new igotweb.ButtonMenu("#menuBottomLanguagesLink",languages,config);
		
		this._currentPage = JSONData.defaultPage;
		
		// We init the google analytics (global) object
		_gaq = _gaq || [];
		
		var options = { 
			unescape: ",/" 
		};
		jQuery.history.init(jQuery.proxy(this._loadState,this),options);
		
		// We update the Recaptcha config
		igotweb.ext.Recaptcha.updateConfig({
			"publicKey" : JSONData.config.recaptchaPublicKey
		});
			
		this._isInit = false;
	},
	
	/*
	 *	function: _loadState
	 *	This function is called in case hash value in URL is updated.
	 *	
	 *	parameters:
	 *		- none.
	 *	return:
	 *		- none.
	 */
	_loadState : function(hash) {
		if(this._isInit) {
			// initialize your app
			// In case there is no hash, page is already loaded.
			if(hash == "") {
				// We track the default page
				_gaq.push(['_trackPageview', '/'+JSONData.currentLanguage+'/'+JSONData.defaultPage]);
				return;
			}
			else {
				// We unselect the default menu item
				jQuery("a.selected", this._navigation).removeClass("selected");
			}
		} 

		if(hash == "") {
			hash = "page/" + JSONData.defaultPage;
		}
		var params = hash.split("/");
		if(params != null && params.length > 1) {
			var type = params.shift();
			var action = params.shift(); 
			// restore the state from hash
			switch(action) {
				case "home":
				case "cv":
				case "portfolio":
				case "galleries":
					this._loadPage(action);
				break;
				default:
					this._loadPage("home");
				break;
			}
		}
	},
	
	/*
	 *	private function: _getPageData
	 *	This function method retrieve all required datas from server to display a page.
	 *	
	 *	parameters:
	 *		- pageName : the page name.
	 *		- callback : method to call once page data are loaded.
	 *	return:
	 *		- none.
	 */	
	_getPageData : function(pageName, callback) {
		var parameters = "action=getPageData&pageName=" + pageName;
	
		/* We send the ajax request */
		var _self = this;
		jQuery.post("./actions", parameters, function(data, status, request) { _self._getPageDataCallback(data, status, request, callback); }, "json");
	},

	/*
	 *	private function: _getPageDataCallback
	 *	This function is callback after getPageData action.
	 *	
	 *	parameters:
	 *		- data : the request output data.
	 *		- status : the request status.
	 *		- request : the request object.
	 *		- callback : the callback method to call.
	 *	return:
	 *		- none.
	 */	
	_getPageDataCallback : function(data, status, request, callback) {
		var listErrors = data.errors;
		if(listErrors.length > 0) {
			for(var i = 0 ; i < listErrors.length ; i++) {
				this._errors.addFormattedError(listErrors[i]);
			}
			this._errors.displayErrors();
			return;
		}
		
		// We get the templates included with the request
		igotweb.TemplateUtils.handleJSONRequest(data);
	
		if(data.response.pageData != null) {
			var pageData = data.response.pageData;
			var pageName = data.response.pageName;
			
			eval("JSONData.pages."+pageName+" = pageData;");
		}
		
		if(callback != null) {
			callback.call(null, pageName);
		}
	},

	/*
	 *	private function: _displayPageLoading
	 *	This function is called to put the page content in loading mode while loading a new page.
	 *	
	 *	parameters:
	 *		- none.
	 *	return:
	 *		- none.
	 */
	_displayPageLoading : function() {
		if(!JSONData.config.usePageLoading) {
			return;	
		}
		
		if(!this._isPageLoading) {
			var pageLoading = jQuery("<div class=\"pageLoading\"></div>");
			if(this._isInit) {
				jQuery("body").prepend(pageLoading);
				pageLoading.css("height","100%");
				pageLoading.css("width","100%");
			}
			else {
				var content = jQuery("body > .content");
				content.prepend(pageLoading);
				content.height(content.outerHeight(false));
				content.width(content.outerWidth(false));
				pageLoading.height(content.outerHeight(false));
				pageLoading.width(content.outerWidth(false));
			}
			this._isPageLoading = true;
		}
	},

	/*
	 *	private function: _hidePageLoading
	 *	This function is called to remove the loading mode for the page content.
	 *	
	 *	parameters:
	 *		- none.
	 *	return:
	 *		- none.
	 */
	_hidePageLoading : function() {
		if(!JSONData.config.usePageLoading) {
			return;	
		}
		
		jQuery(".pageLoading").remove();
		var content = jQuery("body > .content");
		content.height("auto");
		content.width("auto");
		this._isPageLoading = false;
	},
	
	/*
	 *	public function: loadPage
	 *	This function is called load a page.
	 *	
	 *	parameters:
	 *		- pageName : the page name.
	 *	return:
	 *		- none.
	 */
	loadPage : function(pageName) {
		// We check if the page to load is the default one.
		var hash = "page/" + pageName;
		if(pageName == JSONData.defaultPage) {
			hash = "";	
		}
		
		// We load the page
		jQuery.history.load(hash);
	},
	
	/*
	 *	private function: _loadPage
	 *	This function is called by the history manager to load page content.
	 *	
	 *	parameters:
	 *		- pageName : the page name.
	 *	return:
	 *		- none.
	 */
	_loadPage : function(pageName) {
		// We remove the navigation selected button
		jQuery("a.selected", this._navigation).removeClass("selected");
		
		// We check the navigation bar display
		if(pageName == "home") {
			// We hide the navigation bar
			this._navigation.hide();
		}
		else {
			// We show the navigation bar
			this._navigation.show();
		}
		
		// We put the page in loading mode.
		this._displayPageLoading();
		
		eval("var pageData = JSONData.pages."+pageName+";");
		if(pageData != null) {
			// We update selected button
			jQuery("a."+pageName, this._navigation).removeClass("loading").addClass("selected");
			var callback = jQuery.proxy(this._hidePageLoading, this);
			igotweb.TemplateUtils.loadTemplate(pageName,jQuery("#body"), pageData, callback);
			// We update the page title
			document.title = STRINGS[pageName + "PageTitle"];
			// We update the current page
			this._currentPage = pageName;
			// We track the page change
			_gaq.push(['_trackPageview', '/'+JSONData.currentLanguage+'/'+this._currentPage]);
		}
		else {
			jQuery("a."+pageName, this._navigation).addClass("loading");
			this._getPageData(pageName, jQuery.proxy(this._loadPage, this));
		}
	},
	
	/*
	 *	private function: _onContactUsLinkClick
	 *	This function is called when the user clicks on contact us link in footer.
	 *	
	 *	parameters:
	 *		- evt : the event.
	 *	return:
	 *		- none.
	 */
	_onContactUsLinkClick : function(evt) {
		// We prevent default behaviour
		evt.preventDefault();
		// We load the contact us template
		var settings = {
			"animate" : true,
			"position" : {
				
			},
			"contentCss" : {
				"width" : "500px"
			}
		};
		// We load the contact us lightbox
		igotweb.LightBoxManager.displayTemplate("contact",null,settings);
		// We track the event
		_gaq.push(['_trackEvent', 'Lightbox', 'Open', 'Contact-us']);
	},
	
	_onLanguagesLinkClick : function(evt, action) {
		// We get the languages to target
		var language = jQuery(evt.target).attr("_language");
		var currentLanguage = JSONData.currentLanguage;
		
		if(language == currentLanguage) {
			return;	
		}
	
		/* We send the ajax request */
		var _self = this;
		var parameters = "action=switchLanguage&pageName=" + this._currentPage + "&language=" + language;
		jQuery.post("./actions", parameters, function(data, status, request) { 
				// We get the URL to call in correct language
				var languagesForm = jQuery("form[name=languages]");
				languagesForm.attr("action","./" + data.response.url);
				languagesForm.submit();
			},
		"json");
	}
};

// We init index script when page is loaded
jQuery(document).ready(function() { 
	igotweb.igotweb.index.init(); 
});

