/**
 * Classe estática com vários recursos de ambiente
 * 
 * @requires Util
 * @requires String
 * @requires Array
 * @constructor
 */
function Enviroment() {
    return null
}

/**
 * @type Integer
 */
Enviroment.AGENT_UNKNOWN = 0;

/**
 * @type Integer
 */
Enviroment.AGENT_WEBKIT = 1;

/**
 * @type Integer
 */
Enviroment.AGENT_IE = 2;

/**
 * @type Integer
 */
Enviroment.AGENT_OPERA = 3;

/**
 * @type Integer
 */
Enviroment.AGENT_FIREFOX = 4;

/**
 * @type Integer
 */
Enviroment.AGENT_SAFARI = 5;

/**
 * Retorna a constante identificando o navegador do cliente
 * 
 * @type Integer
 */

Enviroment.getAgent = function() {
    var agent = Enviroment.AGENT_UNKNOWN,
        userAgent = navigator.userAgent;
        
    if ((/KHTML/gi).test(userAgent)) {
        agent = Enviroment.AGENT_SAFARI;
    } else {
        v = userAgent.match(/AppleWebKit\/([^ ]*)/);
        if (v&&v[1]) {
            agent = Enviroment.AGENT_WEBKIT;
        } else if (/opera/gi.test(userAgent)) {
            agent = Enviroment.AGENT_OPERA;                
        } else if (/firefox/gi.test(userAgent)) {
            agent = Enviroment.AGENT_FIREFOX;
        } else if (/msie/gi.test(userAgent)) {
            agent = Enviroment.AGENT_IE;
        }
    }
    
    Enviroment.getAgent = function() {
        return agent;        
    };
    
    return Enviroment.getAgent();
}

/**
 * Retorna a largura interna da janela
 * 
 * @type Integer
 */
Enviroment.windowWidth = function() {
    if (!Util.isUndefined(window.innerWidth)) {
        return window.innerWidth;
    }
    if (!Util.isUndefined(document.body.clientWidth)) {
        return document.body.clientWidth;
    }

    return null;
}

/**
 * Retorna a altura interna da janela
 * 
 * @type Integer
 */
Enviroment.windowHeight = function() {
    if (!Util.isUndefined(window.innerHeight)) {
        return window.innerHeight;
    }
    if (!Util.isUndefined(document.body.clientHeight)) {
        return document.body.clientHeight;
    }

    return null;
}

/**
 * @type String
 */
Enviroment.context = null;

/**
 * @type String
 */
Enviroment.location = null;

/**
 * @type Integer
 */
Enviroment.port = null;

/**
 * @type String
 */
Enviroment.protocol = null;

/**
 * @type String
 */
Enviroment.host = null;

/**
 * Retorna o identificador do host
 * 
 * @type String
 */
Enviroment.getHost = function() {
	if (this.host === null) {
		this.host = this.getLocation().split("://", 2)[1].split("/" ,2)[0];
		if (this.host.inString(":")) {
			this.host = this.host.split(":")[0];
		}
	}
	
	return this.host;
}

/**
 * Retorna o endereço da janela
 * 
 * @type String
 */
Enviroment.getLocation = function() {
	if (this.location === null) {
		this.location = "" + window.location;
	}
	
	return this.location;
}

/**
 * Retorna a porta utilizada
 * 
 * @type Integer
 */
Enviroment.getPort = function() {
	if (this.port === null) {
		this.port = 80;
		var part = this.getLocation().split("://", 2).pop();
		if (part.inString(":")) {
			this.port = +part.split(":")[0];
		}
	}
	
	return this.port;
}

/**
 * Retorna o protocolo utilizado
 * 
 * @type String
 */
Enviroment.getProtocol = function() {
	if (this.prototol === null) {
		this.protocol = this.getLocation().split("://")[0];
	}
	
	return this.protocol;
}

/**
 * Retorna o contexto da página
 * 
 * @type String
 */
Enviroment.getContext = function() {
	if (this.context === null) {
		var parts = this.getLocation().split("://", 2)[1];
		parts = parts.inString("?") ? parts = parts.split("?", 2)[0] : parts;
		parts = parts.split("/");
		parts.shift();
		parts.pop();
		this.context = Util.parsePath("/" + parts.join("/") + "/");
	}
	
	return this.context;
}
