

/**
 * Integration code inspired by
 *
 * SWFObject v1.4.4: Flash Player detection and embed - http://blog.deconcept.com/swfobject/
 *
 * SWFObject is (c) 2006 Geoff Stearns and is released under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * SWFObject is the SWF embed script formerly known as FlashObject. The name was changed for
 * legal reasons.
 *
 */

// The url of the website.
var _portal_url = "http://spreed.com";
// The url of the application server.
var _app_url = "https://eu42.spreed.com";
// The url of the accounting server.
var _accounting_url = "https://eu42.spreed.com/checkin";
// The path of the page that loaded this file.
var _page_path = document.location.pathname;

var cookie_base = "spreedCall_";
var available_movies = new Object();
var repeatTimes = new Object();

function addMovie(name, swf, width, height) {
    var movie = new Object();
    movie['swf'] = swf;
    movie['width'] = width;
    movie['height'] = height;
    available_movies[name] = movie;
}

function getMovieProperties(name) {
    var movie = available_movies[name];
    if (!movie) {
        movie = available_movies['spreedCall Avatar'];
    }

    return movie;
}

// Register available spreedCall movies
addMovie('spreedCall Avatar', 'spreedCallLoader_avatar.swf', 450, 175);
addMovie('spreedCall Button', 'spreedCallLoader_button.swf', 460, 205);
addMovie('spreedCall Video', 'spreedCallLoader_video.swf', 320, 262);

// Store time deltas in seconds
repeatTimes['daily'] = 24*60*60;
repeatTimes['weekly'] = 7*repeatTimes['daily'];
repeatTimes['monthly'] = 30*repeatTimes['daily'];

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function saveLastPlayed(type) {
    if (!type) {
        type = "spreedCall Avatar";
    }
    var now = new Date();
    var oldCount = loadPlayCount(type);

    var expires = new Date();
    expires.setFullYear(2037);
    document.cookie = cookie_base+type+"_lastPlayed="+now.toGMTString()+"; expires="+expires.toGMTString()+"; path="+_page_path;
    document.cookie = cookie_base+type+"_playCount="+(oldCount+1).toString()+"; expires="+expires.toGMTString()+"; path="+_page_path;
}

function loadLastPlayed(type) {
    if (!type) {
        type = "spreedCall Avatar";
    }
    var value = readCookie(cookie_base+type+"_lastPlayed");
    if (!value) {
        return null;
    } else {
        return new Date(value);
    }
}

function loadPlayCount(type) {
    if (!type) {
        type = "spreedCall Avatar";
    }
    var value = readCookie(cookie_base+type+"_playCount");
    var result = 0;
    if (value) {
        result = parseInt(value);
        if (isNaN(result)) {
            result = 0;
        }
    }
    return result;
}

function openConference(meeting_id, lang, name, phone, question, latin1, wm_url){
    var url;
    if (name) {
        url = _accounting_url + '/jc/' + meeting_id + '?n='+escape(name);
        if (phone) { url += '&userdata_phone='+escape(phone); }
        if (question) { url += '&userdata_question='+escape(question); }
        if (wm_url) { url += '&userdata_url='+escape(wm_url); }
        if (latin1) { url += '&latin1=1'; }
    } else {
        url = _app_url + '/c/' + meeting_id + '/conference_gui';
    }
    if (lang) {
        if (url.indexOf('?') >= 0) { url += '&'; } else { url += '?'; }
        url += 'lang='+lang;
    }

    window.open(url, 'spreed_conference_gui_'+meeting_id,'width=960,height=665, directories=no,toolbar=no,location=no,menubar=no,scrollbars=no,status=no,resizable=yes,dependent=no');
    return false;
}

function getPlayerVersion() {
    var version = new PlayerVersion([0,0,0]);
    if (navigator.plugins && navigator.mimeTypes.length) {
        var x = navigator.plugins["Shockwave Flash"];
        if (x && x.description) {
            version = new PlayerVersion(x.description.replace(/([a-zA-Z]|\s)+/,"").replace(/(\s+r|\s+b[0-9]+)/,".").split("."));
        }
    } else {
        try {
            var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
        } catch(e) {
            try {
                var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
                version = new PlayerVersion([6,0,21]);
                axo.AllowScriptAccess = "always";
            } catch(e) {
                if (version.major == 6) {
                    return version;
                }
            }
            try {
                axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
            } catch(e) {
                // No flash player available
            }
        }
        if (axo != null) {
            version = new PlayerVersion(axo.GetVariable("$version").split(" ")[1].split(","));
        }
    }
    return version;
};

if (typeof PlayerVersion == "undefined") {
    PlayerVersion = function(version) {
        this.major = version[0] != null ? parseInt(version[0]) : 0;
        this.minor = version[1] != null ? parseInt(version[1]) : 0;
        this.rev = version[2] != null ? parseInt(version[2]) : 0;
    };

    PlayerVersion.prototype.versionIsValid = function(version) {
        if (this.major < version.major) { return false; }
        if (this.major > version.major) { return true; }
        if (this.minor < version.minor) { return false; }
        if (this.minor > version.minor) { return true; }
        if (this.rev < version.rev) {
            return false;
        }
        return true;
    };
};

if (typeof IntegrationGenerator == "undefined") {
    IntegrationGenerator = function(url, w, h, background, quality) {
        this.params = new Object();
        this.variables = new Object();
        this.attributes = new Array();
        if (url) { this.setAttribute("swf", url); }
        if (w) { this.setAttribute("width", w); }
        if (h) { this.setAttribute("height", h); }
        if (background) { this.addParam("bgcolor", background); }
        var quality = quality ? quality : "high";
        this.addParam("quality", quality);
    };

    IntegrationGenerator.prototype.setAttribute = function(key, value) { this.attributes[key] = value; };
    IntegrationGenerator.prototype.getAttribute = function(key) { return this.attributes[key]; };
    IntegrationGenerator.prototype.addParam = function(key, value) { this.params[key] = value; };
    IntegrationGenerator.prototype.getParams = function() { return this.params; };
    IntegrationGenerator.prototype.addVariable = function(key, value) { this.variables[key] = value; };
    IntegrationGenerator.prototype.getVariable = function(key) { return this.variables[key]; };
    IntegrationGenerator.prototype.getVariables = function() { return this.variables; };
    IntegrationGenerator.prototype.getVariablePairs = function() {
        var result = new Array();
        var key;
        var vars = this.getVariables();
        for (key in vars) {
            result.push(key+"="+vars[key]);
        }
        return result;
    };
    IntegrationGenerator.prototype.parseOddcastUrl = function(url) {
        var oddcast_url = null;
        var oddcast_base = null;
        var pos = url.indexOf('?');
        if (pos > 0 && url.indexOf("oddcast.com") > 0) {
            var server = url.slice(0, pos+1);
            var start = url.slice(pos+1);
            var parts = start.split('&');
            var query = "";
            for (var i=0; i<parts.length; i++) {
                if (parts[i].indexOf("doc=") == 0) {
                    var oddcast_url = unescape(parts[i].slice(4))
                    pos = oddcast_url.indexOf('?');
                    oddcast_url = oddcast_url.slice(0, pos+1) + escape(oddcast_url.slice(pos+1));
                    continue;
                }

                if (parts[i].indexOf("bgcolor=") == 0) {
                    // Skip background color
                    continue;
                }

                query += parts[i]+'&';
            }
            oddcast_base = server+escape(query.slice(0, query.length-1));
        }
        return [oddcast_base, oddcast_url];
    };
    IntegrationGenerator.prototype.getSWFHTML = function() {
        var result="";
        if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) {
            result = "<embed type=\"application/x-shockwave-flash\" allowScriptAccess=\"always\" src=\""+this.getAttribute("swf")+"\" width=\""+this.getAttribute("width")+"\" height=\""+this.getAttribute("height")+"\" ";
            var params = this.getParams();
            for(var key in params)
                {result += [key] + "=\"" + params[key]+"\" ";
            }
            var vars = this.getVariablePairs().join("&");
            if (vars.length > 0) {
                result += "flashvars=\""+vars+"\"";
            }
            result += "/>";
        } else {
            result = "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" width=\""+this.getAttribute("width")+"\" height=\""+this.getAttribute("height")+"\"";
            var params = this.getParams();
            var name = params["name"];
            if (name) {
                result += " id=\""+name+"\"";
            }
            result += ">";
            result += "<param name=\"movie\" value=\""+this.getAttribute("swf")+"\" />";
            result += "<param name=\"allowScriptAccess\" value=\"always\" />";
            for (var key in params) {
                result += "<param name=\""+key+"\" value=\""+params[key]+"\" />";
            }
            var vars = this.getVariablePairs().join("&");
            if (vars.length > 0) {
                result += "<param name=\"flashvars\" value=\""+vars+"\" />";
            }
            result += "</object>";
        };

        return result;
    }
};

function integrateSpreedCallFlash(version, background, quality) {
    if (typeof spreedCall_ticket == 'undefined' || spreedCall_ticket == '') {
        // We need a ticket to progress.
        return;
    }
    if (version) {
        var baseVer = new PlayerVersion([6,0,65]);
        version = new PlayerVersion(version.toString().split("."));
        installedVer = getPlayerVersion();
        if (installedVer.versionIsValid(baseVer) && !installedVer.versionIsValid(version)) {
            // Need newer flash player version
            return;
        }
    }

    if (typeof spreedCall_type == 'undefined' || !spreedCall_type) {
        // Fallback if no type has been specified
        spreedCall_type = 'spreedCall Avatar';
    }

    var movie = getMovieProperties(spreedCall_type);
    if (!movie) {
        // Unknown movie type
        return;
    }

    url = _portal_url + '/' + movie['swf'];
    var swf = new IntegrationGenerator(url, movie['width'], movie['height'], background, quality);
    swf.addVariable('amfURL', _app_url + '/AMF');
    swf.addVariable('baseURL', _portal_url);
    swf.addVariable('ticket', spreedCall_ticket);
    if (typeof spreedCall_name != 'undefined' && spreedCall_name != '') {
        swf.addParam('name', spreedCall_name);
        swf.addVariable('setName', spreedCall_name);
    }
    if (typeof spreedCall_language != 'undefined' && spreedCall_language != '') {
        swf.addVariable('setLanguage', spreedCall_language);
    }
    if (typeof spreedCall_country == 'undefined' || spreedCall_country == '') {
        // Default to US
        spreedCall_country = 'US';
    }
    if (spreedCall_country.toLowerCase() == 'en') {
        // map "en" to "uk"
        spreedCall_country = 'UK';
    }
    swf.addVariable('setCountry', spreedCall_country);
    if (typeof spreedCall_title != 'undefined' && spreedCall_title != '') {
        swf.addVariable('setTitle', spreedCall_title);
    }
    if (typeof spreedCall_bubble != 'undefined' && spreedCall_bubble != '') {
        swf.addVariable('setBubble', spreedCall_bubble);
    }
    if (typeof spreedCall_mailMode != 'undefined' && spreedCall_mailMode != '') {
        swf.addVariable('mailMode', spreedCall_mailMode);
    }
    if (typeof spreedCall_link != 'undefined' && spreedCall_link != '') {
        swf.addVariable('setLink', spreedCall_link);
    }
    if (typeof spreedCall_conference != 'undefined' && spreedCall_conference != '') {
        swf.addVariable('setConference', spreedCall_conference);
    }
    if (typeof spreedCall_oddCast != 'undefined' && spreedCall_oddCast != '') {
        parts = swf.parseOddcastUrl(spreedCall_oddCast);
        if (parts[0] != null && parts[1] != null) {
            swf.addVariable('oddcastBase', escape(parts[0]));
            swf.addVariable('oddcastURL', escape(parts[1]));
        }
    }
    if (typeof spreedCall_stream != 'undefined' && spreedCall_stream != '') {
        swf.addVariable('setStream', spreedCall_stream);
    }
    if (typeof spreedCall_videoAutostart != 'undefined' && spreedCall_videoAutostart != '') {
        swf.addVariable('videoAutostart', spreedCall_videoAutostart);
    }
    if (typeof spreedCall_avatarType != 'undefined' && spreedCall_avatarType != '') {
        swf.addVariable('setAvatarType', spreedCall_avatarType);
    }
    if (typeof spreedCall_questions != 'undefined' && spreedCall_questions != '') {
        swf.addVariable('setQuestions', spreedCall_questions);
    }
    if (typeof spreedCall_WM != 'undefined' && spreedCall_WM != '') {
        swf.addVariable('setWM', spreedCall_WM);
    }
    if (typeof spreedCall_HL != 'undefined' && spreedCall_HL != '') {
        swf.addVariable('setHL', spreedCall_HL);
    }    
    //Colors
    if (typeof spreedCall_colorBgComplete != 'undefined' && spreedCall_colorBgComplete != '') {
        swf.addVariable('colorBgComplete', spreedCall_colorBgComplete);
    }
     if (typeof spreedCall_colorBgWindow != 'undefined' && spreedCall_colorBgWindow != '') {
        swf.addVariable('colorBgWindow', spreedCall_colorBgWindow);
    }
    if (typeof spreedCall_colorBgFirst != 'undefined' && spreedCall_colorBgFirst != '') {
        swf.addVariable('colorBgFirst', spreedCall_colorBgFirst);
    }
    if (typeof spreedCall_colorLabelWindow != 'undefined' && spreedCall_colorLabelWindow != '') {
        swf.addVariable('colorLabelWindow', spreedCall_colorLabelWindow);
    }
    if (typeof spreedCall_colorBgWave != 'undefined' && spreedCall_colorBgWave != '') {
        swf.addVariable('colorBgWave', spreedCall_colorBgWave);
    }
    if (typeof spreedCall_colorBgError != 'undefined' && spreedCall_colorBgError != '') {
        swf.addVariable('colorBgError', spreedCall_colorBgError);
    }
    if (typeof spreedCall_colorTextError != 'undefined' && spreedCall_colorTextError != '') {
        swf.addVariable('colorTextError', spreedCall_colorTextError);
    }
    if (typeof spreedCall_colorBgSuccess != 'undefined' && spreedCall_colorBgSuccess != '') {
        swf.addVariable('colorBgSuccess', spreedCall_colorBgSuccess);
    }
    if (typeof spreedCall_colorTextSuccess != 'undefined' && spreedCall_colorTextSuccess != '') {
        swf.addVariable('colorTextSuccess', spreedCall_colorTextSuccess);
    }
    if (typeof spreedCall_colorBgButtonActionBase != 'undefined' && spreedCall_colorBgButtonActionBase != '') {
        swf.addVariable('colorBgButtonActionBase', spreedCall_colorBgButtonActionBase);
    }
    if (typeof spreedCall_colorBgButtonActionGradient != 'undefined' && spreedCall_colorBgButtonActionGradient != '') {
        swf.addVariable('colorBgButtonActionGradient', spreedCall_colorBgButtonActionGradient);
    }   
    if (typeof spreedCall_colorBgButtonCancelBase != 'undefined' && spreedCall_colorBgButtonCancelBase != '') {
        swf.addVariable('colorBgButtonCancelBase', spreedCall_colorBgButtonCancelBase);
    }
    if (typeof spreedCall_colorBgButtonCancelGradient != 'undefined' && spreedCall_colorBgButtonCancelGradient != '') {
        swf.addVariable('colorBgButtonCancelGradient', spreedCall_colorBgButtonCancelGradient);
    }
    if (typeof spreedCall_colorBgButtonOpenBase != 'undefined' && spreedCall_colorBgButtonOpenBase != '') {
        swf.addVariable('colorBgButtonOpenBase', spreedCall_colorBgButtonOpenBase);
    }
    if (typeof spreedCall_colorBgButtonOpenGradient != 'undefined' && spreedCall_colorBgButtonOpenGradient != '') {
        swf.addVariable('colorBgButtonOpenGradient', spreedCall_colorBgButtonOpenGradient);
    }
    if (typeof spreedCall_colorTextButtonAction != 'undefined' && spreedCall_colorTextButtonAction != '') {
        swf.addVariable('colorTextButtonAction', spreedCall_colorTextButtonAction);
    }
    if (typeof spreedCall_colorTextButtonCancel != 'undefined' && spreedCall_colorTextButtonCancel != '') {
        swf.addVariable('colorTextButtonCancel', spreedCall_colorTextButtonCancel);
    }
    if (typeof spreedCall_colorTextButtonOpen != 'undefined' && spreedCall_colorTextButtonOpen != '') {
        swf.addVariable('colorTextButtonOpen', spreedCall_colorTextButtonOpen);
    }
    if (typeof spreedCall_colorPattern != 'undefined' && spreedCall_colorPattern != '') {
        swf.addVariable('colorPattern', spreedCall_colorPattern);
    }
    if (typeof spreedCall_colorAvatar != 'undefined' && spreedCall_colorAvatar != '') {
        swf.addVariable('colorAvatar', spreedCall_colorAvatar);
    }
    /*
    if (typeof spreedCall_currentCharacter != 'undefined' && spreedCall_currentCharacter != '') {
        swf.addVariable('currentCharacter', spreedCall_currentCharacter);
    }
    */
    if (typeof spreedCall_setAvatarCloth != 'undefined' && spreedCall_setAvatarCloth != '') {
        swf.addVariable('setAvatarCloth', spreedCall_setAvatarCloth);
    }
    if (typeof spreedCall_setPattern != 'undefined' && spreedCall_setPattern != '') {
        swf.addVariable('setPattern', spreedCall_setPattern);
    }
    if (typeof spreedCall_playRepeat != 'undefined' && spreedCall_playRepeat != '') {
        var count = parseInt(spreedCall_playRepeat);
        if (isNaN(count)) {
            // We got one of the daily|weekly|monthly strings
            var lastPlayed = loadLastPlayed(spreedCall_type);
            if (!lastPlayed) {
                // Not played yet
                swf.addVariable('startAvatarNow', 1);
            } else {
                var now = new Date();
                var delta = (now - lastPlayed) / 1000;
                var maxDelta = repeatTimes[spreedCall_playRepeat];
                if (maxDelta && maxDelta < delta) {
                    // Played long enough ago
                    swf.addVariable('startAvatarNow', 1);
                }
            }
        } else {
            var played = loadPlayCount(spreedCall_type);
            if (played < count) {
                // Not played often enough
                swf.addVariable('startAvatarNow', 1);
            }
        }
    } else {
        // Not limited, always start playing
        swf.addVariable('startAvatarNow', 1);
    }

    document.write(swf.getSWFHTML());
};

integrateSpreedCallFlash('8', '#ffffff');

