/**
 * basic coverflow object
 * handles access to coverflow.swf
 * @param string playerId - htmlid of coverflow container
 * @param int    width    - width of flash object
 * @param int    height   - height of flash object
 * @param string playlist - url to playlist.xml
 * @param Emv.Coverflow.Config - config object
 */
Emv.Coverflow = function(playerId, width, heigth, playlist, config){
    // 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 swf file.
    // Can be set via FlashplayerControlObject.setSWF()
    this.swf = "coverflow.swf";

    // Configuration XML Url.
    if('undefined' == typeof(config)){
        this.config = new Emv.Coverflow.Config();
    }
    else{
        this.config = config;
    }

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

    // Do we have an initial Clip XML to start the player with?
    this.playlistUrl = playlist;

    this.callback;

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

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


    // Set the flashplayer domain.
    // full domain (http://my.domain.de/ - with slash at the end)
    // Effects only before FlashplayerControlObject.draw().
    this.setDomain = function(domain)
    {
        this.fpDomain = domain;
    };

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

    this.setCallback = function(cb)
    {
        this.callback = cb;
    };

    this.getCallback = function()
    {
        if('undefined' == typeof this.callback)
        {
            this.callback = new Emv.Coverflow.Callback();
        }
        return this.callback;
    };

    /**
     * setting coverflow config data
     * Effects only before FlashplayerControlObject.draw().
     * @param Emv.Coverflow.Config
     */
    this.setConfig = function(config)
    {
        this.config = config;
    };

    /**
     * Set player playlist Url.
     * Effects only before FlashplayerControlObject.draw().
     * @param String url
     */
    this.setPlaylistUrl = function(url)
    {
        this.playlistUrl = 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.playlist         = this.playlistUrl;
        flashvars.playerId         = this.id;
        //parasm from config

        flashvars.planeWidth           = this.config.planeWidth;
        flashvars.planeHeight          = this.config.planeHeight;
        flashvars.numPlane             = this.config.numPlane;
        flashvars.planeAngle           = this.config.planeAngle;
        flashvars.planeSeparation      = this.config.planeSeparation;
        flashvars.planeOffset          = this.config.planeOffset;
        //offset
        flashvars.offSetY              = this.config.offSetY;
        flashvars.offSetZ              = this.config.offSetZ;
        //reflection settings
        flashvars.showReflections      = this.config.showReflections;
        flashvars.backgroundImage      = this.config.backgroundImage;
        flashvars.backgroundColor      = this.config.backgroundColor;
        flashvars.reflectionLength     = this.config.reflectionLength;
        flashvars.reflectionAlphaStart = this.config.reflectionAlphaStart;
        flashvars.reflectionAlphaEnd   = this.config.reflectionAlphaEnd;

        // 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);
    };

    /**
     * used for fullpages - Emv.Videode.Callback.Fullpage
     * to show last selected image via html button
     */
    this.showLastEventImage = function()
    {
        this.callback.showImageForLastEvent();
    };


    // 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];
        }
    };


    // Player callback "loadPlaylist".
    this.swfCall_LoadPlaylist = function(xml)
    {
        try
        {
            this.getMovie().loadPlaylist(xml);
        }
        catch (err)
        {
            console.log('Exception in Mediacenter.Coverflow.swfCall_LoadPlaylist: ' + err);
        }
    };

    // Player callback "playpause".
    this.showNext = function()
    {
        try
        {
            this.getMovie().showNext();
        }
        catch (err)
        {
            console.log('Exception in Mediacenter.Coverflow.showNext: ' + err);
        }
    };

    // Player callback "playpause".
    this.showPrevious = function()
    {
        try
        {
            this.getMovie().showPrevious();
        }
        catch (err)
        {
            console.log('Exception in Mediacenter.Coverflow.showPrevious: ' + err);
        }
    };

    // Handle the player events.
    this.handleEvent = function(event)
    {
        var cb = this.getCallback();
        if (event.name == Emv.Coverflow.Event._IMAGE_LOADED)
        {
            cb.imageLoaded(event);
        }
        else if (event.name == Emv.Coverflow.Event._IMAGE_CLICKED)
        {
            cb.imageClicked(event);
        }
    };
};

/**
 * basic Coverflow Config
 * DO NOT CHANGE THOSE VALUES HERE!
 */
Emv.Coverflow.Config = function()
{
    //images within coverflow are shown boxed
    /**
     * max width of boxed image
     * @param int
     */
    this.planeWidth         = 529;
    /**
     * max height of boxed image
     * @param int
     */
    this.planeHeight        = 280;
    /**
     * number of visible images
     * @param int
     */
    this.numPlane           = 7;
    /**
     * angle of playlist images
     * @param int
     */
    this.planeAngle         = 60;
    /**
     * gap between playlist images
     * @param int
     */
    this.planeSeparation    = 80;

    /**
     * gap between shown image and first left/right playlist item
     * @param int
     */
    this.planeOffset        = 0;
    /**
     * moves whole playlist up
     * @param int
     */
    this.offSetY            = 20;

    /**
     * plane separation on z-axis (depth)
     * @param int
     */
    this.offSetZ            = 450;
    /**
     * flag, if images reflect
     * @param boolean
     */
    this.showReflections    =  true;

    /**
     * pixel length of the reflection+
     * @param int
     */
    this.reflectionLength       =  100;
    /**
     * beginning alpha of reflection
     * @param double
     */
    this.reflectionAlphaStart   =  0;
    /**
     * ending alpha of reflection (should those vars not be flipped)
     * @param double
     */
    this.reflectionAlphaEnd     =  0.4;
    /**
     * background image - take care that reflections does not work with gradients (within reflection range)
     * always use single color as background for reflections
     * @param string url
     */
    this.backgroundImage    = null;
    /**
     * background color is used to calculate reflections against those color
     * @param string hex color string
     */
    this.backgroundColor    = 0xffffff;

};

/**
 * callback object for handling coverflow event - overwrite this class to implement
 * service specific (or object specific) event handling
 */
Emv.Coverflow.Callback = function()
{
    imageClicked = function(event)
    {
       alert ('imageClicked');
    };

    imageLoaded = function(event)
    {
        alert('imageLoaded');
    };
};

Emv.Coverflow.RttObserver = function(rttSubelement) {

    this.id = "CoverflowRttObserver";
    this.rttSubelement = rttSubelement;

    this.getId = function() {

        return this.id;
    };

    this.handleEvent = function(event) {

        if (typeof(this.rttSubelement) !== "undefined" &&
            (event.name == Emv.Coverflow.Event._ROTATE_CLICKED_LEFT ||
             event.name == Emv.Coverflow.Event._ROTATE_CLICKED_RIGHT)) {

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

Emv.Coverflow.Event = {
    _IMAGE_LOADED        : "imageLoaded",
    _IMAGE_CLICKED       : "imageClicked",
    _ROTATE_CLICKED_LEFT : "rotateClickedLeft",
    _ROTATE_CLICKED_RIGHT: "rotateClickedRight"
};
