//TODO test webappURI (init) quand appellée avec #


/*********************************************************************************************/
/* Declaration of window document object													 */
/*********************************************************************************************/



function ComWeb_navigator(){
	this.initialize();
}

ComWeb_navigator.prototype = {

	NAVIGATOR_NAME_IE : "IE",

	NAVIGATOR_NAME_SAFARI : "SAFARI",

	NAVIGATOR_NAME_OTHER : "OTHER",

	name : "OTHER",

	version:0,

	plugFlashVersion:0,

	autorCookie:false,

	navigatorAppName : null,

	navigatorAppVersion : null,

	isComwebCompliant:true,

	initialize: function() {
		this.navigatorAppName  = new String(navigator.appName);
		this.navigatorAppVersion = new String(navigator.appVersion);
		this._setNavigatorName();
		this._setNavigatorPluginFalshVersion();
		this._setAutorCookie();
	},

	_setNavigatorName:function(){
		 if (this.navigatorAppName.toLowerCase().indexOf("microsoft") >=0){
	   		this.name = this.NAVIGATOR_NAME_IE;
	   		var ms = this.navigatorAppVersion.indexOf("MSIE");
	  		this.version = (parseInt(navigator.appVersion.substring(ms+5, ms+6)));
	  		if (!(this.version >= 4 && this.version < 9) ){
	  			this.isComwebCompliant = false;
	  		}
		}else {
	   		this.version = (parseInt(this.navigatorAppVersion.substring(0,1)));
	   		if (this.navigatorAppVersion.toLowerCase().indexOf("safari") >=0){
				this.name = this.NAVIGATOR_NAME_SAFARI;
			}else{
				this.name = this.NAVIGATOR_NAME_OTHER;
		   	}
		}
	},

	_setNavigatorPluginFalshVersion:function(){
		/*
		this.plugFlashVersion = parseInt((deconcept.SWFObjectUtil).getPlayerVersion().major);
		if (this.plugFlashVersion  < 8){
			this.isComwebCompliant = false;
		}
		*/
	},

	_setAutorCookie:function(){
		var cookieHelper = new ComwebCookieHelper();
		cookieHelper.setCookie("TESTCOOKIE", "test", null , null);
		try{
			this.autorCookie = ( cookieHelper.getCookieInfo("TESTCOOKIE") == "test");
			cookieHelper.setCookie("TESTCOOKIE", "test", null , -10000);

			if (this.autorCookie == false){
				this.isComwebCompliant = false;
			}

		}catch(e){
			alert("_setAutorCookie error:" + e);
		}
	},

	isSafari:function(){
		return this.name == this.NAVIGATOR_NAME_SAFARI;
	},


	isIE:function(){
		return this.name == this.NAVIGATOR_NAME_IE;
	},

	alert:function(){

		var txt = "";
		txt+= "this.name : " + this.name  + "\n";
		txt+= "this.plugFlashVersion: " + this.plugFlashVersion + "\n";
		txt+= "this.autorCookie: " + this.autorCookie + "\n";
		txt+= "this.version: " + this.version + "\n";
		txt+= "this.navigatorAppName: " + this.navigatorAppName + "\n";
		txt+= "this.navigatorAppVersion: " + this.navigatorAppVersion + "\n";
		txt+= "this.isComwebCompliant: " + this.isComwebCompliant + "\n";

		alert(txt);

	}

}

var ComWeb_navigator = new ComWeb_navigator();


/**
 * stores the configuration of the webapp,
 * instancied at the main page loading
 * @public
 * @type Config
 */
var CW_config;


/**
 * object which permits to get translation from a key, in the current langage
 * @see Config#initialize
 * @public
 * @type ComwebBean_internationalization
 */
var CW_internationalization;

/**
 * Create a new instance of ComwebBean, must be create when the main pageis loading
 * @class The following class it's a configuration resume of the service
 * @classDescription The following class it's a configuration resume of the service
 * @constructor
 */
function Config(){
	this.initialize();
}
Config.prototype = {
	/**
	 * stores the langId of the service value must like 'EN','FR','DE' ...
	 * @type String
	 */
	langId : "FR",
	/**
	 * stores the service Id, the value is a number
	 * @type int
	 */
	serviceId : 1,
	/**
	 * stores the webapp name service,
	 * @type String
	 */
	webappName: null,
	/**
	 * stores the webapp uri, initialize when the config instance is build
	 * @see Config#initWebAppUri
	 * @type String
	 */
	webappUri : null,

	webappDomaine : null,

	/**
	 * boolean which indicates if we launch the popup CW_logger
	 * @type boolean
	 */
	activLog : true,
	/**
	 * action (uri) which deliver xml configuration, initialize when the config instance is build
	 * @see Config#initConfig
	 * @see ComwebBean_config
	 * @type String
	 */
	initializeAction : "xml/config.xml",
	/**
	 * config HashTable wich store all the config keys with theirs values
	 * @type ComwebBean_config
	 */
	config : null,
	/**
	 * prefix of the action (uri) which deliver internationalization values {@link ComwebBean_internationalization}
	 * the complete action will be [prefixLanAction]+langId;
	 * @see Config#changeLang
	 * @type String
	 */
	prefixLangAction: "lang/internationalization_",

	/**
	 * Stores the initial document title
	 * @type String
	 */
	titleDocument: "",
	/**
	 * Stores the connected user, null if there is not any one
	 * @type ComwebBean_user
	 */
	connectedUser: null,

	isProd:true,

	/**
	 * Stores the ip site adress
	 * @type String
	 */
	siteIp:null,
	/**
	 * Stores the domain site name
	 * @type String
	 */
	siteDomainName:null,


	/**
	 * call by the constructor in order to initialize attributes values
	 */
	initialize: function() {
		this.config = new HashTable();
		this.initWebAppUri();



	},

	/**
	 *
	 */
	initValues : function(){

		if (this.activLog){
			CW_logger = new Logger_Activ();
			CW_logger.initialize();
			CW_logger.showPopup();
		}

		var validHistory = (document.getElementById("dhtmlHistoryIFrame") != null && document.getElementById("dhtmlHistoryStorageField") != null);
		if (validHistory){
			window.historyStorage.init();
			window.dhtmlHistory.create();
			dhtmlHistory.initialize();
			dhtmlHistory.addListener(handleHistoryChange);
		}else{
			CW_logger.log("Error","can't initialize dhtml history, document must content 'dhtmlHistoryIFrame' and 'dhtmlHistoryStorageField' hidden div")
		}



		// TODO uncomment when there will be initialize action availible: CW_comwebAjax.sendRequestRefresh(this.initializeAction,"CW_config.initConfig",false);

		//delete this action when initConfig will be activate

		this.titleDocument = document.title;




		//set the first page
		var curHash = window.location.hash;
		if (curHash!= null && curHash!=""){
			if (curHash.indexOf("#")==0){
				curHash = curHash.substr(1);
			}
			var curHashLang = "";
			var curHashLangIndex = curHash.indexOf("lng=");

			if (curHashLangIndex > 0 ){
				curHashLang = curHash.substr(curHashLangIndex+4,2);
				CW_logger.log("NOTICE","load the lang for the current permanent link:" + curHashLang);
				this.langId = curHashLang;
			}

			CW_logger.log("NOTICE","the first called page is permanent link:" + curHash);

			this.changeLang(this.langId);
			try{
				loadMainElements();
			}catch(e){
				CW_logger.log("Error","error while loading main elements, check tou have loadMainElements function , error:" + e);
			}


			if (curHash.indexOf("play=") == 0 ){
				var mediaId = parseInt(curHash.replace("play=",""));
				CW_logger.log("NOTICE","permanent link on media:" + mediaId);
				 goToMediaPlay("action=alone&listing.page=1",mediaId);
			}else if (curHash.indexOf("rkey=") == 0 ){
				 var rkey =  (curHash.replace("rkey=",""));
				 CW_logger.log("NOTICE","permanent link on registration mail:" + rkey);
			     CW_Action_User.validRegisterKey(rkey);
			}else{
				//handleHistoryChange(curHash,"");
				CW_AjaxPage_Manager.handleHistoryChange(curHash);
			}

		}else{
			//set the initial page here:
			this.changeLang(this.langId);
			try{
				try{
					loadMainElements();
				}catch(e){
					CW_logger.log("Error","error while loading main elements, check tou have loadMainElements function , error:" + e);
				}

				try{
					loadMainPage();
				}catch(e){
					CW_logger.log("Error","error while loading main elements, check tou have loadMainPage function , error:" + e);
				}

			}catch(e){
				CW_logger.log("Error","error while loading main page or main elements, check tou have loadMainPage and loadMainElements functions , error:" + e);
			}
		}


		CW_logger.log("NOTICE","configuration:" + this.toString());
	},


	/**
	 * permits to initialize the webappUri attribute must called only by the constructor
	 */
	initWebAppUri : function (){
		var href = window.location.href;

		var extensionIndexOf = href.indexOf(".jsp");
		if (extensionIndexOf < 0 ){extensionIndexOf = href.indexOf(".htm");}
		if (extensionIndexOf < 0 ){extensionIndexOf = href.indexOf(".html");}
		if (extensionIndexOf < 0 ){extensionIndexOf = href.indexOf("#");}

		var cutIndexOf = 0;

		if (extensionIndexOf > 0 ) {
			href = href.substring(0,extensionIndexOf);
		}
		cutIndexOf = href.lastIndexOf("/") + 1;

		this.webappUri = href.substring(0,cutIndexOf);
		this.webappDomaine = this.webappUri.substring(0,this.webappUri.indexOf("/",7) + 1)

	},
	/**
	 * permits to initialize the configuartion attribute
	 * called by the initialize method with comwebAjax.sendRequestRefresh(this.initializeAction,"config.initConfig",false);
	 * @param {ComwebBean_config}
	 */
	initConfig : function (comwebBean){

		this.serviceId = comwebBean.getServiceId();
		this.langId = comwebBean.getLangId();
		this.webappName = comwebBean.getWebappName();
		this.config = comwebBean;

		this.changeLang(this.langId);



		//OLD move in changeLang method:comwebAjax.sendRequestRefresh(this.prefixLangAction + this.langId + ".xml","CW_config.initInternationalization",false);
	},
	/**
	 * permits to initialize internationalization object
	 * called by the initConfig method with comwebAjax.sendRequestRefresh(this.prefixLangAction + this.langId + ".xml","config.initInternationalization",false);
	 * @param {ComwebBean_Internationalization}
	 */
	initInternationalization : function (comwebBean_Internationalization){
		CW_internationalization = comwebBean_Internationalization;
		//CW_logger.log("Information", "lang: page.value.1 || value:"+  CW_internationalization.getValue("page.value.1"));
		//CW_logger.log("Information", "lang: page.value.2 || value:"+ CW_internationalization.getValue("page.value.2","login=charles"));
		//CW_logger.log("Information", "lang: page.value.3 || value:"+ CW_internationalization.getValue("page.value.3"));
		//CW_logger.log("Information", "lang: page.value.4 || value:"+ CW_internationalization.getValue("page.value.4"));
		//CW_logger.log("Information", "lang: page.value.5 || value:"+ CW_internationalization.getValue("page.value.5","JJ=02","MM=03","YY=07"));
		//CW_logger.log("Information", "lang: page.value.6 || value:"+ CW_internationalization.getValue("page.value.6"));


		CW_logger.log("NOTICE","CW_config.initInternationalization, comwebbean:" + comwebBean_Internationalization.getName());

		this.langId = CW_internationalization.getLangId();
	},
	/**
	 * Permits to change the langage
	 * @param {String} lang value must like 'EN','FR','DE' ...
	 */
	changeLang : function(lang) {
		CW_logger.log("NOTICE","CW_config.changelang:" + lang);
		CW_comwebAjax.sendRequest(this.prefixLangAction + lang+ ".xml","CW_config.initInternationalization",null,"lang");
	},

	/**
	 * alow to set the transalation files uri prefix
	 * must be do before initValues
	 * @param {String} uriPrefix the new uri prefix
	 */
	setTranslationFilesURIPrefix :  function(uriPrefix) {
		this.prefixLangAction = uriPrefix;
	},

	setNotInProd:function(){
		this.isProd = false;
	},

	/**
	 * @param langId ('FR','EN','IT')
	 */
	setLangId:function(langId){
		this.langId = langId;
	},

	setServiceId:function (serviceId){
		this.serviceId = serviceId;
	},

	desactivateLog:function(){
		this.activLog = false;
	},

	/**
	 * Permits obtain configuration values from this key
	 * @see ComwebBean#getValues
	 * @param {String} key
	 */
	getConfig :  function(key) {
		this.config.getValues(key);
	},


	/**
	 * The following function allow to set the bean which represents the
	 * connected user, set it only with the login or inscription operations
	 */
	setConnectedUser : function (comwebBean_user){
		this.connectedUser = comwebBean_user;
	},
	/**
	 * The following fuction indicates if this instance has a connected user
	 * @return {boolean} indicates if there is a connected user
	 */
	hasConnectedUser:function(){
		return (this.connectedUser!=null)
	},
	/**
	 * Guetter
	 * @return {ComwebBean_user} the connected user bean, or null if there is not anyone
	 */
	getConnectedUser : function (){
		return this.connectedUser;
	},

	/**
	 * The following function allow to set the ip adress site to the corresponding
	 * domain name
	 * @param {String} ip
	 * @param {String} domainName
	 * @return {void}
	 */
	setDomainName : function (ip,domainName){
		this.siteIp = ip;
		this.siteDomainName = domainName;
	},
	/**
	 * The following function allow to replace the ip adress site by the domain name site
	 * in the given url
	 * @param {String} url
	 * @return {String} given url where the ip adress site is replaced by the domain site name
	 */
	getDomainUrl : function (url){
		if (this.siteIp !=null && this.siteDomainName!=null ){
			return (new String (url)).replace("http://" + this.siteIp + "/", "http://" + this.siteDomainName + "/");
		}

		return url;
	},


	/**
	 *  Returns a short description of this object
	 * @return {String} Returns a short description of this object
	 */
	toString : function (){
		return  "webappURI:" + this.webappUri + " || " +
				"webappDomaine:" + this.webappDomaine + " || " +
				"webappName:" + this.webappName + " || " +
				"langId:" + this.langId + " || " +
				"serviceId:" + this.serviceId + " || " +
				"activLog:" + this.activLog + " || " +
				"titleDocument:" + this.titleDocument + " || " +
				"navigator:" + ComWeb_navigator.name + " || " +
				"initializeAction:" + this.initializeAction;
	}
};


