/**************************************************************
 * @author mpoisson
 * System initializacion class
 **************************************************************/
function Enviroment() {
	this.lang = new Language();
	this.langHandler = new LanguageHandler();
	this.user = new User();
	this.servicePath = "http://squa.re/woapps/WebObjects/SQUAREProduction.woa/wa/";
	this.imagePath = "images/";
	this.servletName = "squareServices";
	this.uploadServicePath = "services/";
	this.uploadServletName = "uploadHandler.php";
	this.usedInvitations = 0;
	this.maxInvitations = 0;

	this.debugMode = false;

	try {
		if (parseInt('0' + this.getURLVariable('forcedebug'),10)) {
			this.debugMode = true;
		}
	} catch (e) {
		// error activating the console!
	}
	this.autoPlay = true;
}

Enviroment.prototype.DEFAULT_LANGUAGE = 1;

Enviroment.prototype.initialize = function (stgy) {
	var service = null;

	try {
		if (this.getDebugMode()) {
			$('debugConsole').style.display = '';
		} else {
			$('debugConsole').style.display = 'none';
		}
	} catch (e) {
	}

	try {
		if (checkRefresh()) {
			// Checks if session cookie exists
			var handler = new UserHelper();
			this.user = handler.readSession(this.user);
			if (this.isUserLogged()) {
				// reloads user information using cookie data
				handler.loadProfileSync(this.user, this.getLanguage());
				if (stgy.getCheckSession()) {
					this.lang.setID(this.user.getLanguage().getID());
				}
			} else {
				if (stgy.getCheckSession()) {
					this.lang.setID(this.getURLVariable('lang'));
				}
			}
		}
	} catch (e) {
		//alert("Session initialization error: " + e);
		handler.deleteSession();
	}
	// In case language is not properly returned
	if (this.lang.getID() === null) {
		this.lang.setID(1);
	}

	/**
	try {
		// translates square elements to selected language
		service = this.langHandler.load(this.getLanguage(), stgy.getCurrentPage());
		this.langHandler.process(service);
	} catch (e) {
		alert("Language initialization error: " + e.name);
	}
	**/

	if (stgy.getHasHeaders()) {
		/**
		try {
			// Loads language selection dropdown
			var oLangDropdown = new Dropdown('login_lang', '');
			oLangDropdown.setOnChange('language_change');
			oLangDropdown.loadFromTableSync('XApplicationLanguages');
			oLangDropdown.selectItemByKey(this.getLanguage().getID());
		} catch (e) {
			alert("Language dropdown initialization error: " + e);
		}
		try {
			$('mainmenu_submenu-members').hide();
		} catch (e) {
			//alert("Members menu display error: " + e);
		}
		**/

		stgy.init();

		if (this.isUserLogged()) {
			try {
				// Sets the user name on the header if it's logged on square
				tmp = $('txt_login');
				tmp.innerHTML = 'My profile';
			} catch (e) {
			}
			$("logoutContainer").style.display= 'block';
		} else {
			$("logoutContainer").style.display= 'none';
		}

		showMainMenu();
	}
};

Enviroment.prototype.getURLVariable = function (key) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	var value = '';
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == key) {
			value = pair[1];
		}
	}
	if (value == null) value = '';
	return value;
};

Enviroment.prototype.getURL = function () {
	var url = window.location.toString();
	var index = url.indexOf("?");
	if (index<0) index = url.length;
	return url.substring(0,index);
};

Enviroment.prototype.getDomain = function () {
	return  window.location.host;
};

Enviroment.prototype.getLanguage = function() {
	return this.lang;
};

Enviroment.prototype.getUser = function() {
	return this.user;
};

Enviroment.prototype.getServicePath = function() {
	return this.servicePath;
};

Enviroment.prototype.getImagePath = function() {
	return this.imagePath;
};

Enviroment.prototype.getServletName = function() {
	return this.servletName;
};

Enviroment.prototype.getUploadServicePath = function() {
	return this.uploadServicePath;
};

Enviroment.prototype.getUploadServletName = function() {
	return this.uploadServletName;
};

Enviroment.prototype.getLangHandler = function() {
	return this.langHandler;
};

Enviroment.prototype.getDebugMode = function() {
	return this.debugMode;
};

Enviroment.prototype.getAutoPlayMode = function() {
	return this.autoPlay;
};

Enviroment.prototype.setAutoPlayMode = function(value) {
	this.autoPlay = value;
};

Enviroment.prototype.getMaxInvitations = function() {
	return this.maxInvitations;
};

Enviroment.prototype.setMaxInvitations = function(value) {
	this.maxInvitations = value;
};

Enviroment.prototype.getUsedInvitations = function() {
	return this.usedInvitations;
};

Enviroment.prototype.setUsedInvitations = function(value) {
	this.usedInvitations = value;
};

Enviroment.prototype.isUserLogged = function() {
	return ((this.getUser().getSID().length > 0) && (this.getUser().getID().length > 0));
};

/**************************************************************
 * System onload execution and initialization
 **************************************************************/
oEnviroment = new Enviroment();
