/**
 * FlashObjekt
 *
 * @author Tibor Claassen
 * @author tibor@imagerator.com
 * @date 05-29-2004
 *
 */
//UserAgent-Klasse

function UserAgent() {
    
    this._userAgentString = navigator.userAgent;
}

UserAgent.prototype.getUserAgentString = function(lowerCase) {
    
    if (lowerCase && !this._userAgentStringLowerCase) this._userAgentStringLowerCase = this._userAgentString.toLowerCase();
    return (lowerCase) ? this._userAgentStringLowerCase : this._userAgentString;
}

UserAgent.prototype.isIE = function() {
    
    if (!this._isIE) this._isIE = (this.getUserAgentString(true).indexOf("msie") > -1) && !this.isOpera() && !this.isWebTV();
    return this._isIE;
}

UserAgent.prototype.isOpera = function() {
    
    if (!this._isOpera) this._isOpera = (this.getUserAgentString(true).indexOf("opera") > -1);
    return this._isOpera;
}

UserAgent.prototype.isWebTV = function() {
    
    if (!this._isWebTV) this._isWebTV = (this.getUserAgentString(true).indexOf("webtv") > -1);
    return this._isWebTV;
}

UserAgent.prototype.isWin = function() {
    
    if (!this._isWin) this._isWin = (this.getUserAgentString(true).indexOf("win") > -1);
    return this._isWin;
}

UserAgent.prototype.getPlugInArchitecture = function() {
    
    if (!this._pluginArchitecture) {
        if (navigator.plugins && navigator.plugins.length)  this._pluginArchitecture = "Netscape-PlugIn";
        else if (this.isIE() && this.isWin() && (parseInt(navigator.appVersion) > 3))    this._pluginArchitecture = "Active-X";
        else this._pluginArchitecture = "unknown";
    }
    return this._pluginArchitecture;
}

UserAgent._instance = null;

UserAgent.getInstance = function() {

    if (!UserAgent._instance)  UserAgent._instance = new UserAgent();
    return UserAgent._instance;
};


// FlashDetection-Klasse

function FlashDetection() {
    
    this._cookie = new Cookie(document, "FlashDetection", null, "/");
    this._cookie.load();
    
    var version = this._cookie.getValue("installedVersion");
    this._version = (version) ? version : -1;
 	
}

FlashDetection.prototype.getVersion = function() {
    
    var version;
    
    if (this._version == -1) { 
               
        var plugIn = UserAgent.getInstance().getPlugInArchitecture();
    
        if (plugIn == "Netscape-PlugIn") {
        
            var description = navigator.plugins['Shockwave Flash'].description;
            if (description) version = description.charAt(description.indexOf(".") - 1);
            else version = 0;
               
        } else if (plugIn == "Active-X") {
            
            version = 0;
    
            succ_VBScript = false;
            for (var i = 12; i >= 3; i--) {
                execScript("on error resume next: succ_VBScript = IsObject(CreateObject('ShockwaveFlash.ShockwaveFlash." + i + "'))","VBScript");
               if (succ_VBScript) {
                  version = i;
                  break;
               }
            }
        }
        
        this.setVersion(version);
    }
      
    return this._version;
}


FlashDetection.prototype.setVersion = function(version) {

    this._version = version;
    
    this._cookie.setValue("installedVersion", this._version);
    this._cookie.store();
}

FlashDetection.prototype.reset = function() {
    this._version = -1;
}

FlashDetection.prototype.success = function() {
    return (this._version > -1);
}

FlashDetection._instance = null;

FlashDetection.getInstance = function() {

    if (!FlashDetection._instance)  FlashDetection._instance = new FlashDetection();
    return FlashDetection._instance;
};


// FlashObject-Klasse

function FlashObject(id, url, version, width, height, parameter, variables) {

    this._XHTML = "";
    
    var prop;
    var variableString = "";
    for (prop in variables) {
        if(variableString != "") variableString += "&";
        variableString += prop + "=" + escape(variables[prop]);
    }
    
    var plugIn = UserAgent.getInstance().getPlugInArchitecture();
	var baseDirectory = location.href.substr(0, location.href.lastIndexOf("/") + 1);
    
    if (plugIn == "Netscape-PlugIn") {
    
        this._XHTML = "<embed id='" + id + "' type='application/x-shockwave-flash' src='" + url + "' width='" + width + "' height='" + height + "' base='" + baseDirectory + "'" ;
        for (prop in parameter) this._XHTML += " " + prop + "='" + parameter[prop] + "'";
        if (variableString != "") this._XHTML += " flashVars='" + variableString + "'";
        this._XHTML += "></embed>";
        
    } else if (plugIn == "Active-X") {
        
        this._XHTML = "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' id='" + id + "' width='" + width + "' height='" + height + "'>";
        this._XHTML += "<param name='movie' value='" + url + "' />";
		this._XHTML += "<param name='base' value='" + baseDirectory + "' />";
        for (prop in parameter) this._XHTML += "<param name='" + prop + "' value='" + parameter[prop] +"' />";
        if (variableString != "") this._XHTML += "<param name='flashVars' value='" + variableString + "' />";
        this._XHTML += "</object>";
    }
}

FlashObject.prototype.getXHTML = function() {
    return this._XHTML;   
};


// FlashSwitch-Klasse

function FlashSwitch(id, width, height) {
    
    this._id = id;
    this._width = width;
    this._height = height;
    
    this._url = "/" + id + ".swf";
    this._version = 3;
    this._params = {quality: "high"};
    this._variables = {};
}

FlashSwitch.prototype.addParameter = function(name, value) {

    this._params[name] = value;    
};

FlashSwitch.prototype.getParameter = function(name) {

    return this._params[name];
};


FlashSwitch.prototype.setVariable = function(name, value) {

    this._variables[name] = new URIString(value).encode();
};

FlashSwitch.prototype.getVariable = function(name) {

    return this._variables[name];
};

FlashSwitch.prototype.unsetVariable = function(name) {

    delete this._variables[name];
};

FlashSwitch.prototype.activate = function(url, version) {
    
    this._url = url;
    if (arguments.length > 1)  this._version = version;
    
    if (this._version > FlashDetection.getInstance().getVersion() && arguments.length > 1) return false;
     
    var flashObj = new FlashObject(this._id, this._url, this._version, this._width, this._height, this._params, this._variables); 
    document.getElementById(this._id).innerHTML = flashObj.getXHTML();
    
    return true;
};

// URI-Klasse

function URIString(string) {

	this._string = this._initString = string.toString();
	this.toUtf8();
}

URIString.prototype.encode = function() {
	
	var string = this._string;
	var charCode, nextCharCode;
	var encodedString = "";
	var i = 0;
	
	while (i < string.length) {
	
		if (URIString.allowedChars.indexOf(string.charAt(i)) == -1)	encodedString += "%" + URIString.hexValueOf(string.charCodeAt(i));
		else encodedString += string.charAt(i);
		
		i++;
	}
	
	this._string = encodedString;
	return this._string;
};

URIString.prototype.toUtf8 = function() {

	var string = this._string;
	var charCode, nextCharCode;
	var encodedString = "";
	var i = 0;
	
	while (i < string.length) {
	
		charCode = string.charCodeAt(i++);
		
		//utf-16 surrogates
		if (charCode >= 0xdc00 && charCode < 0xe000) continue;
		
		if (charCode >= 0xd800 && charCode < 0xdc00) {
			
			if (i >= string.length) continue;
			
			nextCharCode = string.charCodeAt(i++);
			if (nextCharCode < 0xdc00 || charCode >= 0xde00) continue;
			charCode = ((charCode - 0xd800) << 10) + (nextCharCode - 0xdc00) + 0x10000;	
		}
		
		//output
		if (charCode < 0x80) encodedString += String.fromCharCode(charCode);
		
		else if (charCode < 0x800) encodedString += String.fromCharCode(0xc0 + (charCode >> 6), 0x80 + (charCode & 0x3f));
		
		else if (charCode < 0x10000) encodedString += String.fromCharCode(0xe0 + (charCode >> 12), 0x80 + (charCode >> 6 & 0x3f), 0x80 + (charCode & 0x3f));
		
		else encodedString += String.fromCharCode(0xf0 + (charCode >> 18), 0x80 + (charCode >> 12 & 0x3f), 0x80+(charCode >> 6 & 0x3f), 0x80 + (charCode & 0x3f));
	}

	this._string = encodedString;
	return this._string;
};

URIString.allowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";

URIString.hexChars = "0123456789abcdef";

URIString.hexValueOf = function(charCode) {

	return this.hexChars.charAt(charCode >> 4) + this.hexChars.charAt(charCode & 0xf);
}



// Cookie-Klasse

function Cookie(document, name, hours, path, domain, secure) {
    
    this._document = document;
    this._name = name;
    this._data = {};
    this._hours = (hours) ? hours : null;
    this._path = (path) ? path : null;
    this._domain = (domain) ? domain : null;
    this._secure = (secure) ? secure : null;
}

Cookie.prototype.setValue = function(name, value) {

    this._data[name] = value;
};

Cookie.prototype.getValue = function(name, value) {

    return (this._data[name]) ? this._data[name] : false;
};

Cookie.prototype.unsetValue = function(name) {

    delete this._data[name];
};

Cookie.prototype.store = function() {
    
    var dataString = "";
    for (var p in this._data) {
        if (dataString != "") dataString += "&";
        dataString +=  p + ":" + escape(this._data[p]);
    }
    
    var cookieString = this._name + "=" + dataString;
    if (this._hours) cookieString += "; expires=" + new Date((new Date()).getTime() + this._hours * 3600000);
    if (this._path) cookieString += "; path=" + this._path;
    if (this._domain) cookieString += "; domain=" + this._domain;
    if (this._secure) cookieString += "; secure=true";
    
    this._document.cookie = cookieString;
    return cookieString;
};

Cookie.prototype.load = function() {
    
    var cookies = this._document.cookie;
    
    if(cookies == "") return false;
    
    var start = cookies.indexOf(this._name + "=");
    if (start == -1) return false;
    start += this._name.length + 1;
    
    var end = cookies.indexOf(";", start);
    if (end == -1) end = cookies.length;
    
    var dataString = cookies.substring(start, end);
    var data = dataString.split("&");
    for (var i = 0; i < data.length; i++) {
        data[i] = data[i].split(":");
        this._data[data[i][0]] = unescape(data[i][1]);
    }
    
    return dataString;
};

Cookie.prototype.remove = function() {
    
    var cookieString = this._name + "=";
    if (this._path) cookieString += "; path=" + this._path;
    if (this._domain) cookieString += "; domain=" + this._domain;
    cookieString += "; expires=Fri, 02-Jan-1970 00:00:00 GMT";

    this._document.cookie = cookieString;
    return cookieString;
};
