if ("undefined" == typeof(Emv)) {
    Emv = {};
}

Emv.Clipplayer = function(playerId, width, heigth, config, initItem)
{
    // Clip width/height
    this.width  = width;
    this.height = heigth;

    // Id of the flash player div container.
    this.id   = playerId;

    // Name for embed und object "tags".
    this.name = playerId + "Name";

    // Default flashplayer domain.
    // Can be set via FlashplayerControlObject.setDomain()
    this.fpDomain = "http://flashplayer.e-media.de/";

    // Default Flashplayer style.
    // Can be set via FlashplayerControlObject.setStyle() to "default"|"small".
    this.style = "default";

    // Default Flashplayer skin.
    // Can be set via FlashplayerControlObject.setSkin().
    this.skin = "videode";

    // Default flashplayer swf file.
    // Can be set via FlashplayerControlObject.setSWF()
    this.swf = "flashplayerV7.swf";

    // Flashplayer Autostart/-play.
    // Can be set via FlashplayerControlObject.setAutoPlay()
    this.autoplay = false;

    // Configuration XML Url.
    // Can be set via FlashplayerControlObject.setConfigURL()
    this.configURL = config;

    // Initial Item XML Url.
    this.initItem = false;

    /**
     * sponsor text image - shown under stage player
     * @param string url
     */
    this.presentedByImageUrl;

    /**
     * link to sponsor
     * presentedByImageurl and logoImageUrl are linked to this url
     * @param link
     */
    this.sponsorLink;
    /**
     * used for stage clipplayer
     * sponsor logo shown in tv border
     * @param string url
     */
    this.logoImageUrl;

    this.backgroundImage;

    /**
     * current used observer - to get him from other plugins
     */
    this.observer;

    // Do we have an initial Clip XML to start the player with?
    if ("undefined" != typeof(initItem) && initItem !== false && initItem !== null && initItem !== '')
    {
        this.initItem = initItem;
    }

    this.getId = function()
    {
        return this.id;
    };

    this.isVisible = function()
    {
        return !('undefined' == typeof(this.getMovie()));
    };

    // Set the flashplayer domain.
    // Effects only before FlashplayerControlObject.draw().
    this.setDomain = function(domain)
    {
        // Prepend "http://"
        if (domain.indexOf("http://") == -1)
        {
            domain = "http://" + domain;
        }
        // Append slash.
        if (domain.substr(domain.length - 1) != "/")
        {
            domain = domain + "/";
        }

        this.fpDomain = domain;
    };

    // Set the swf file.
    // Effects only before FlashplayerControlObject.draw().
    this.setSWF = function(swf)
    {
        this.swf = swf;
    };

    // Set player autoplay on/off.
    // Effects only before FlashplayerControlObject.draw().
    this.setAutoPlay = function(bool)
    {
        this.autoplay = bool;
    };

    // Set player style ("default"|"small").
    // Effects only before FlashplayerControlObject.draw().
    this.setStyle  = function(style)
    {
        this.style = style;
    };

    // Set player skin.
    // Effects only before FlashplayerControlObject.draw().
    this.setSkin  = function(skin)
    {
        this.skin = skin;
    };

    // Set player config XML Url.
    // Effects only before FlashplayerControlObject.draw().
    this.setConfigURL = function(url)
    {
        this.configURL = url;
    };

    // Set player init item XML Url.
    // Effects only before FlashplayerControlObject.draw().
    this.setInitItem = function(url)
    {
        this.initItem = url;
    };

    /**
     * setting observer for which this player-instance is registered
     * @todo multiple observers ...
     */
    this.setObserver = function(obs)
    {
        this.observer = obs;
    };

    /**
     * returns observer for which this player is registered
     */
    this.getObserver = function()
    {
        return this.observer;
    };

    // Draw the flashplayer into the div element.
    this.draw = function()
    {
        // --------------------------------------------------
        // Push adobe flash plugin uninstall/install information into the div.

        var deinst_url  = "http://www.adobe.com/de/support/flash/ts/documents/remove_player.htm";
        var install_url = "http://www.adobe.com/go/getflashplayer";
        var reinst_div  = document.createElement("div");
        reinst_div.setAttribute("style", "color:#000; background-color:#ddd; border:1px solid #888; padding: 3px; margin: 3px;");

        var uups = document.createElement("p");
        uups.innerHTML = "Es gibt offensichtlich ein Problem mit Ihrer Flashplayer Installation";
        uups.setAttribute("style", "font-weight:bold;");
        reinst_div.appendChild(uups);

        var dothis = document.createElement("strong");
        dothis.innerHTML = "Gehen Sie wie folgt vor: ";
        reinst_div.appendChild(dothis);
        reinst_div.appendChild(document.createElement("br"));
        reinst_div.appendChild(document.createElement("br"));
        reinst_div.appendChild(document.createTextNode("Bitte laden Sie sich den "));

        var deinst_link = document.createElement("a");
        deinst_link.innerHTML = "Flash-Uninstaller";
        deinst_link.setAttribute("href", deinst_url);
        deinst_link.setAttribute("style", "color: 3333cc");
        deinst_link.setAttribute("target", "_blank");
        reinst_div.appendChild(deinst_link);
        reinst_div.appendChild(document.createTextNode(" herunter und führen Sie Ihn aus. Dies deinstalliert den Flashplayer komplett."));
        reinst_div.appendChild(document.createElement("br"));
        reinst_div.appendChild(document.createElement("br"));

        reinst_div.appendChild(document.createTextNode("Anschließend laden Sie bitte eine aktuelle Version des "));
        var reinst_link = document.createElement("a");
        reinst_link.innerHTML = "Flash-Installer";
        reinst_link.setAttribute("href", install_url);
        reinst_link.setAttribute("style", "color: 3333cc");
        reinst_link.setAttribute("target", "_blank");
        reinst_div.appendChild(reinst_link);
        reinst_div.appendChild(document.createTextNode(" herunter. Ein Doppelklick auf die Datei installiertden Flashplayer neu. Bitte schließen Sie zuvor alle Browser-Fenster."));
        reinst_div.appendChild(document.createElement("br"));
        reinst_div.appendChild(document.createElement("br"));
        reinst_div.appendChild(document.createTextNode("Nach der Installation starten Sie Ihren Browser neu und besuchen diese Seite wieder, Sie sollten nun keine Probleme mehr beim Betrachten unserer Trailer und Clips mehr haben ;)"));
        reinst_div.appendChild(document.createElement("br"));
        reinst_div.appendChild(document.createElement("br"));

        var infodiv = document.createElement("span");
        infodiv.innerHTML = "Info:";
        infodiv.setAttribute("style", "color: dd4433");
        reinst_div.appendChild(infodiv);
        reinst_div.appendChild(document.createTextNode(" Die oben genannten Links verweisen direkt auf die Seite des Flashplayer Herstellers Adobe."));

        // --------------------------------------------------
        // Now create the player. On success push it into the div.

        var containerDiv = document.getElementById(this.id);
        containerDiv.setAttribute("style", "width:" + this.width + "px; height:" + this.height + "px;");
        containerDiv.appendChild(reinst_div);

        // Configure the flash variables.
        var flashvars = {};
        flashvars.initItemXML = this.initItem;
        flashvars.configXML   = this.configURL;
        flashvars.style       = this.style;
        flashvars.skin        = this.fpDomain + this.skin + '.swf';

        //callbacks
        flashvars.divid               = this.id;
        flashvars.autoplay            = this.autoplay;
        flashvars.backgroundImage     = this.backgroundImage;
        flashvars.logoImageUrl        = this.logoImageUrl;
        flashvars.presentedByImageUrl = this.presentedByImageUrl;
        flashvars.sponsorLinkUrl      = this.sponsorLink;

        // Configure the flash parameters.
        var params = {};
        params.menu              = "false";
        params.allowfullscreen   = "true";
        params.allowscriptaccess = "always";
        params.wmode             = "opaque";

        // Configure the flash attributes.
        var attributes = {};
        attributes.id    = this.id;
        attributes.name  = this.name;
        attributes.align = "middle";

        // Create the flash object/embed element
        // Use swfobject class.
        swfobject.embedSWF(
            this.fpDomain + this.swf,
            this.id,
            width,
            heigth,
            Emv.Config.flashVersion,
            this.fpDomain + "install/expressInstall.swf",
            flashvars, params, attributes);
    };

    // Handle the player events.
    this.handleEvent = function(event)
    {
        if (event.name == Emv.Clipplayer.Event._LOAD_ITEM)
        {
            if ("object" == typeof(event.params.item)) {

                this.swfCall_LoadItem(event.params.item, event.params.autoplay);
            }
        }
        else if (event.name == Emv.Clipplayer.Event._PLAY)
        {
            this.swfCall_Play();
        }
        else if (event.name == Emv.Clipplayer.Event._PAUSE)
        {
            this.swfCall_Pause();
        }
        else if(event.name == Emv.Clipplayer.Event._PLAY_PAUSE)
        {
            this.swfCall_PlayPause();
        }
    };

    // Get the reference to the player flashmovie.
    this.getMovie = function()
    {
        if (navigator.appName.indexOf("Microsoft") != -1)
        {
            return window[this.name];
        }
        else
        {
            return document[this.name];
        }
    };

    this.setBackgroundImage = function(url)
    {
        this.backgroundImage = url;
    };

    // Resize the player and load with init item.
    this.loadAndResize = function(initItem, width, height)
    {
        this.resize(width, height);
        this.playInitItem(initItem);
    };

    // Resize the player element.
    this.resize = function(width, height)
    {
        try
        {
            this.getMovie().width = width;
            this.getMovie().height = height;
            this.getMovie().resizePlayer(width, height);
        }
        catch (err)
        {

        }
    };

    // Load the player width the item.
    this.playInitItem = function(item)
    {
        try
        {
            this.getMovie().assignInitItem(item);
        }
        catch(err)
        {
        }

        this.setInitItem(item);
    };
    this.loadInitItem = function(item)
    {

        try
        {
            this.getMovie().loadInitItem(item);
        }
        catch(err)
        {
        }

        this.setInitItem(item);
    };

    // Player callback "LoadItem".
    this.swfCall_LoadItem = function(item, autoplay)
    {
        if("undefined" == typeof(autoplay))
        {
            autoplay = false;
        }
        if ("undefined" != typeof(item.itemUrl)) {


            if(autoplay)
            {
                this.playInitItem(item.itemUrl);
            }
            else
            {
                this.loadInitItem(item.itemUrl);
            }
        }
    };

    // Player callback "LoadFLV".
    this.swfCall_LoadFLV = function(flvsrc, thumb, autoload)
    {
        try
        {
            this.getMovie().loadFLV(flvsrc, thumb, autoload);
        }
        catch (err)
        {

        }
    };

    // Player callback "playpause".
    this.swfCall_PlayPause = function()
    {
        try
        {
            this.getMovie().togglePlayPause(false);
        }
        catch (err)
        {

        }
    };

    // Player callback "play".
    this.swfCall_Play = function()
    {
        try
        {
            this.getMovie().play();
        }
        catch (err)
        {

        }
    };

    // Player callback "pause".
    this.swfCall_Pause = function()
    {
        try
        {
            this.getMovie().pause();
        }
        catch (err)
        {

        }
    };

    /**
     * url to the logo of video.de stage clipplayer sponsor (shown in tv border)
     * @param String url
     */
    this.setSponsorData = function(data)
    {
        this.logoImageUrl        = data.logoImageUrl;
        this.presentedByImageUrl = data.presentedByImageUrl;
        this.sponsorLink         = data.sponsorLink;
    };
};

/**
 * holding all urls for sponsored clipplayer
 * just fill object an give it to emv.clipplayer.setSponsorData
 */
Emv.Clipplayer.SponsorData = function(logo, pbImage, link)
{
    /**
     * logo which is shown in the player (12px height to fit)
     * @param string url
     */

    this.logoImageUrl = logo;

    /**
     * presented by text as image (to show logos etc in it)
     * @param string url
     */
    this.presentedByImageUrl = pbImage;

    /**
     * link to sponsor
     * @param string url
     */
    this.sponsorLink = link;
};

Emv.Clipplayer.RttObserver = function(rttSubelement) {

    this.id = "ClipplayerRttObserver";
    this.rttSubelement = rttSubelement;

    this.getId = function() {

        return this.id;
    };

    this.handleEvent = function(event) {

        if (event.name == Mediacenter.Clipplayer.Event._PLAYING_VIDEO &&
            typeof(this.rttSubelement) !== "undefined") {

            this.rttSubelement.refreshPixel();
        }
    };
};

Emv.Clipplayer.Event = {
    // loadItem: request to load an item.
    // playerLoaded: the initial event, after the player is loaded.
    // play: on start playing tht video video clip
    // pause: on pause the video clip
    // stop: on stop the video clip (not jet implemented)
    // playPause: (not jet implemented)
    // loadFLV: (not jet implemented)
    // playingPreroll: on start playing the preroll
    // playingVideo: on start playing the video
    // playingPostroll: on start playing the postroll
    // done: on the final finish

    _LOAD_ITEM       : "loadItem",
    _PLAYER_LOADED   : "playerLoaded",
    _PLAY            : "play",
    _PAUSE           : "pause",
    _STOP            : "stop",
    _PLAY_PAUSE      : "playPause",
    _LOAD_FLV        : "loadFLV",
    _PLAYING_PREROLL : "playingPreroll",
    _PLAYING_VIDEO   : "playingVideo",
    _ASSIGNED_VIDEO  : "assignedVideo",
    _PLAYING_POSTROLL: "playingPostroll",
    _DONE            : "done"
};
