var Browser = {}, Util = {};
(function(){
	var ua = navigator.userAgent.toLowerCase();	
	Browser = Prototype.Browser;
	Browser.Opera = ua.indexOf("opera") > -1;
	Browser.Safari = (/webkit|khtml/).test(ua) && ua.indexOf("chrome") == -1;	
	Browser.Safari3 = Browser.Safari && ua.indexOf('webkit/5') != -1 && ua.indexOf("version/4") == -1;
	Browser.Safari4 = Browser.Safari && ua.indexOf('webkit/5') != -1 && ua.indexOf("version/4") != -1;	
	Browser.Chrome = ua.indexOf("chrome") != -1;
        Browser.Chrome4 = ua.indexOf("chrome/4") != -1;
	Browser.Chrome3 = ua.indexOf("chrome/3") != -1;
	Browser.IE = !Browser.Opera && ua.indexOf("msie") > -1 && window.attachEvent;
	Browser.IE7 = !Browser.Opera && ua.indexOf("msie 7") > -1;
	Browser.IE8 = !Browser.Opera && ua.indexOf("msie 8") > -1;
	Browser.IE6 = Browser.IE && !Browser.IE7  && !Browser.IE8;
	Browser.Gecko = !Browser.Chrome && !Browser.Safari && ua.indexOf("gecko") > -1;
	Browser.Gecko3 = !Browser.Chrome && !Browser.Safari && ua.indexOf("rv:1.9") > -1;
	Browser.Gecko2 = Browser.Gecko && !Browser.Gecko3;
	Browser.Strict = document.compatMode == "CSS1Compat";
	Browser.BorderBox = Browser.IE && !Browser.Strict;
	Browser.PngSupport = true;
	Browser.AjaxSupport = Ajax.getTransport();
	Browser.Secure = window.location.href.toLowerCase().indexOf("https") === 0;
	Browser.Nice = (typeof encodeURIComponent != 'undefined') && (typeof document.getElementById != 'undefined');
	
	if (Browser.IE){
		var v = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
		if (v != null && Number(v[1]) < 5.5) Browser.PngSupport = false;
	};

	// remove css image flicker
	if(Browser.IE6){
		try{
			document.execCommand("BackgroundImageCache", false, true);
		}
		catch(e){}
	};
	
	document.observe('dom:loaded', function() {
		var bd = $(document.body || document.getElementsByTagName('body')[0]);
		if(bd){ 
			var cls = [
					Browser.IE ? "ie " + (Browser.IE6 ? 'ie6' : (Browser.IE8 ? 'ie8' : 'ie7'))
					: Browser.Gecko ? "gecko " + (Browser.Gecko2 ? 'gecko2' : 'gecko3')
					: Browser.Opera ? "opera"
					: Browser.Safari ? "safari " + (Browser.Safari3 ? "safari3" : (Browser.Safari4 ? "safari4" : ""))
					: Browser.Chrome ? "chrome " + (Browser.Chrome3 ? "chrome3" : (Browser.Chrome4 ? "chrome4" : "")) : ""];
		
			if(Browser.PngSupport) cls.push("png");
			if(Browser.Mac) cls.push("mac");
			if(Browser.Linux) cls.push("linux");
			if(Browser.BorderBox) cls.push('border-box');
			if(Browser.Strict){ // add to the parent to allow for selectors like ".strict .ie"
				var p = bd.parentNode;
				if(p) p.className += ' strict';
			}
			bd.addClassName(cls.join(' '));
		}
	});
	
})();

Ajax.Responders.register({
  onAbort: function() { Ajax.activeRequestCount-- }
});

Ajax.Request.prototype.abort = function(){
	if (!this._complete) {
		this.transport.abort();
		// avoid memory leak in MSIE: clean up
      	this.transport.onreadystatechange = Prototype.emptyFunction;
		Ajax.Responders.dispatch('onAbort', this);
	}
};

/** State Manager */

StateManager = {};

StateManager.CookieProvider = function(config){
    this.path = "/";
    this.expires = new Date(new Date().getTime()+(1000*60*60*24*30*12)); //1 year
    this.domain = null;
    this.secure = false;
    
    Object.extend(this, config);
	
    this.state = this._readCookies();
};

Object.extend(StateManager.CookieProvider.prototype, {
	
    set : function(name, value){
        if(typeof value == "undefined" || value === null){
            this.clear(name);
            return;
        }
		this.state[name] = value;
        this._setCookie(name, value);
    },
   
	get : function(name, defaultValue){
        return typeof this.state[name] == "undefined" ?
            defaultValue : this.state[name];
    },
   
    clear : function(name){
		delete this.state[name];
        this._clearCookie(name);
    },
    
    _readCookies : function(){
        var cookies = {};
        var c = document.cookie + ";";
        var re = /\s?(.*?)=(.*?);/g;
    	var matches;
    	while((matches = re.exec(c)) != null){
            var name = matches[1];
            var value = matches[2];
            if(name/* && name.substring(0,3) == "ec-"*/){
                cookies[name] = value;
            }
        }
        return cookies;
    },
	
    _setCookie : function(name, value){
        document.cookie = /*"ec-" + */name + "=" + value +
           ((this.expires == null) ? "" : ("; expires=" + this.expires.toGMTString())) +
           ((this.path == null) ? "" : ("; path=" + this.path)) +
           ((this.domain == null) ? "" : ("; domain=" + this.domain)) +
           ((this.secure == true) ? "; secure" : "");
    },
	
    _clearCookie : function(name){
        document.cookie = /*"ec-" + */name + "=null; expires=Thu, 01-Jan-70 00:00:01 GMT" +
           ((this.path == null) ? "" : ("; path=" + this.path)) +
           ((this.domain == null) ? "" : ("; domain=" + this.domain)) +
           ((this.secure == true) ? "; secure" : "");
    }
});

StateManager = function(){
    var provider = new StateManager.CookieProvider();
    return {
        setProvider : function(stateProvider){ provider = stateProvider },
        get : function(key, defaultValue){ return provider.get(key, defaultValue) },
        set : function(key, value){ provider.set(key, value) },
        clear : function(key){ provider.clear(key) },
        getProvider : function(){ return provider }
    };
}();
