Mediacenter.Clipplayer = function(playerId, width, heigth, config, initItem)
{
//	console.log('construct clipplayer [ID: ' + playerId + ']');
    // 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/";
//    this.fpDomain = "http://www.damo.flashplayer.dev/";

    // Default Flashplayer style.
    // Can be set via FlashplayerControlObject.setStyle() to "default"|"small".
    this.style = "default";

    // Default flashplayer swf file.
    // Can be set via FlashplayerControlObject.setSWF()
    this.swf = "flashplayerV3.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;
    
    /**
     * 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()
    {
//    	console.log(typeof(this.getMovie()));
    	return !('undefined' == typeof(this.getMovie()));
    }
    
    /**
     * works only for newsFotoshows
     */
    this.hideNewsClipPlayer = function()
    {
    	var player = document.getElementById(this.id);
    	var div = document.createElement('div');
    	div.id = this.id;
    	var pare = player.parentNode;
    	pare.replaceChild(div, player);
    	
    }
    
    // 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 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 Clips und Trailer 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."));
    //    reinst_div.innerHTML = "Sollten Sie diesen Text sehen, <br> hier "+deinst_url+ " den Flash uninstaller herunter und führen sie Ihn aus. ";
    //    reinst_div.innerHTML += "Anschließend laden Sie hier: "+ install_url + " den FlashInstaller herunter und installieren Flash neu. Dann sollte dieser Text nicht mehr erscheinen und sie können wieder Videos ansehen :)";


        // --------------------------------------------------
        // 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;
        //callbacks
//        flashvars.callbackPlay  = this.callbacks.play;
//        flashvars.callbackPause = this.callbacks.pause;
        flashvars.divid         = this.id;
        flashvars.autoplay      = this.autoplay;

        // 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,
            Mediacenter.flashVersion,
            this.fpDomain + "install/expressInstall.swf",
            flashvars, params, attributes);
    };
    
    this.showPlayerDummy = function()
    {
//    	console.log('Mediacenter.Clipplayer.showPlayerDummy');
    	var containerDiv = document.getElementById(this.id);
    	containerDiv.innerHTML = '<img src="/mediathek/pics/player_mb_premium.jpg" alt="player" id="player_spacer" />';
    }

    // Handle the player events.
    // See Mediacenter.Clipplayer.Event for event definitions.
    this.handleEvent = function(event)
    {

//        console.log("Mediacenter.Clipplayer.handleEvent(): " + event.name);
        if (event.name == Mediacenter.Clipplayer.Event._LOAD_ITEM)
        {
            if ("object" == typeof(event.params.item)) {

                this.swfCall_LoadItem(event.params.item, event.params.autoplay);
            }
        }
        else if (event.name == Mediacenter.Clipplayer.Event._PLAY)
        {
            this.swfCall_Play();
        }
        else if (event.name == Mediacenter.Clipplayer.Event._PAUSE)
        {
            this.swfCall_Pause();
        }
        else if(event.name == Mediacenter.Clipplayer.Event._PLAY_PAUSE)
        {
            this.swfCall_PlayPause();
        }
        else
        {
            //console.log("Mediacenter.Clipplayer.handleEvent(): EVENT DOES NOT MATCH!");
        }
    };


    // 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];
        }
    };

    // 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)
        {
            console.log('Exception in Mediacenter.Clipplayer.resize: ' + err);
        }
    };

    // Load the player width the item. 
    this.playInitItem = function(item)
    {
        try
        {
            this.getMovie().assignInitItem(item);
        }
        catch(err)
        {
            var myErr = err;
//            console.log("Beim Versuch, das Video zu laden ist ein Fehler aufgetreten\n\n(" + err + ")");
//            alert("Beim Versuch, das Video zu laden ist ein Fehler aufgetreten\n\n(" + err + ")");
        }

        this.setInitItem(item);
    };
    this.loadInitItem = function(item)
    {

        try
        {
            this.getMovie().loadInitItem(item);
        }
        catch(err)
        {
            var myErr = err;
//            console.log("Beim Versuch, das Video zu laden ist ein Fehler aufgetreten\n\n(" + err + ")");
            //alert("Beim Versuch, das Video zu laden ist ein Fehler aufgetreten\n\n(" + 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)
		{
			console.log('Exception in Mediacenter.Clipplayer.swfCall_LoadFLV: ' + err);
		}
    };

    // Player callback "playpause".
    this.swfCall_PlayPause = function()
    {
    	try
    	{
            this.getMovie().togglePlayPause(false);
    	}
    	catch (err)
        {
            console.log('Exception in Mediacenter.Clipplayer.swfCall_PlayPause: ' + err);
        }
    };

    // Player callback "play".
    this.swfCall_Play = function()
    {
    	try
    	{
            this.getMovie().play();
    	}
        catch (err)
        {
            console.log('Exception in Mediacenter.Clipplayer.swfCall_Play: ' + err);
        }
    };

    // Player callback "pause".
    this.swfCall_Pause = function()
    {
    	try
    	{
            this.getMovie().pause();
    	}
        catch (err)
        {
            console.log('Exception in Mediacenter.Clipplayer.swfCall_Pause: ' + err);
        }
    };
};

Mediacenter.Clipplayer.Legend = function (domid, playlistId){

    // Id of the clipinfo div container.
    this.id = domid;

    // Id of the observer object.
    this.observerObjectID;

    // The ajax request url.
    this.requestUrl = '/mediathek/clipinfo/';
    
    this.playerId;
    
    this.playlistId;
    
    this.getId = function()
    {
    	return this.id;
    };
    
    this.setPlayerId = function(id)
    {
    	this.playerId = id;
    };
    
    this.setPlaylistId = function(id)
    {
    	this.playlistId = id;
    };
    /**
     * Set id of the observer element.
     *
     * @param string id
     */
    this.setObserverObjectID = function(id)
    {
        this.observerObjectID = id;
    };

    /**
     * Set requestUrl for the ajax request.
     *
     * @param string url
     */
    this.setRequestUrl = function(url)
    {
        this.requestUrl = url;
    };

    /**
     * Handle the clipinfo events.
     * See Mediacenter.Clipplayer.Event for event definitions.
     *
     * @param FPC_ObserverEvent
     */
    this.handleEvent = function(event)
    {
//        console.log("Mediacenter.Clipplayer.Legend.handleEvent(): " + event.name);

        if (event.name == Mediacenter.Clipplayer.Event._LOAD_ITEM)
        {
            if ("object" == typeof(event.params.item)) {
                this.loadingInfosForItem(event.params.item);
            }
        }
        else if(event.name == Mediacenter.Clipplayer.Event._PLAYER_LOADED || event.name == Mediacenter.Clipplayer.Event._ASSIGNED_VIDEO)
        {
        	if('undefined' != typeof(this.playlistId))
        	{
	        	var playlist = Emv.Registry.getObject(this.playlistId);
	        	this.loadingInfosForItem(playlist.getCurrentItem());
        	}
        }
    };

    /**
     * loading infos for given item and refreshing legend HTML
     * 
     * Load/display clip information.
     *
     * @param PlaylistClipItem/PlaylistFotoshowItem item (JSON rebuild object without methods)
     */
    this.loadingInfosForItem = function(item)
    {
        if ("undefined" == typeof(item.itemType)) {

            return;
        }

        // Display loading information
        document.getElementById(this.id).innerHTML = '';
        Emv.Effects.Ajaxloader.prependLoader(this.id);


        // Call/request clip info based on item type
        var requestUrl = this.requestUrl;
        switch (item.itemType) {

            case "film":
            case "video":
            case "game":
            case "clip":

                requestUrl += item.itemType + '/' + item.itemDbId;
                break;

            default:
                return;
                break;
        }

//        console.log("ClipinfoControl.eventLoadItem(): request " + requestUrl);
        var request = YAHOO.util.Connect.asyncRequest('GET', requestUrl, this.yuiConnectionCallback);
    };

    /**
     * Yui connection request callback.
     */
    this.yuiConnectionCallback = {

        /**
         * Yui connection success.
         *
         * @param object o
         */
        success: function(o) {

//          console.log("ClipinfoControl.yuiConnectionCallback(): request success");
            clipinfoControl = o.argument[0];

            document.getElementById(clipinfoControl.id).innerHTML = o.responseText;
        },

        /**
         * Yui connection failed.
         */
        failure: function() {

          console.log("ClipinfoControl.yuiConnectionCallback(): request FAILED");
        },

        argument: [ this ]
    };
};

Mediacenter.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"
};
Mediacenter.Clipplayer.Callback = function()
{
    this.loadItem        = false;
    this.playerLoaded    = false;
    this.play            = false;
    this.pause           = false;
    this.stop            = false;
    this.playPause       = false;
    this.loadFLV         = false;
    this.playingPreroll  = false;
    this.playingVideo    = false;
    this.playingPostroll = false;
    this.done            = false;
};