/// ******************************************************************************************************************
/// ExtendedPlayer

Type.registerNamespace('ExtendedPlayer');

ExtendedPlayer.Player = function(domElement) {
    ExtendedPlayer.Player.initializeBase(this, [domElement]);      
}
ExtendedPlayer.Player.prototype =  {
    xamlInitialize: function() {    
        ExtendedPlayer.Player.callBaseMethod(this, 'xamlInitialize');  
        //this._volumeControls = new ExtendedPlayer.MouseOverControl(this.get_element(), "VolumeHolder");
        //this._timelineControls = new ExtendedPlayer.MouseOverControl(this.get_element(), "TimelineHolder");        
        //this._playlistControls = new ExtendedPlayer.MouseOverControl(this.get_element(), "Playlist");        
        this._toggleControlsControl = new ExtendedPlayer.ToggleControlsControl(this.get_element());
         this.PlaylistButton = new ExtendedPlayer.PlaylistButton(this.get_element());
         this.ListThumbButton1 = new ExtendedPlayer.ListThumbButton(this, this.get_element(),"PlayListButton0");
         this.ListThumbButton2 = new ExtendedPlayer.ListThumbButton(this, this.get_element(),"PlayListButton1");
         this.ListThumbButton3 = new ExtendedPlayer.ListThumbButton(this, this.get_element(),"PlayListButton2");
    },
    
    xamlDispose: function() {
        if (this._volumeControls) this._volumeControls.dispose();
        if (this._timelineControls) this._timelineControls.dispose();
        if (this._toggleControlsControl) this._toggleControlsControl.dispose();
        if (this.PlaylistButton) this.PlaylistButton.dispose();
        if (this.ListThumbButton1) this.ListThumbButton1.dispose();
        if (this.ListThumbButton2) this.ListThumbButton2.dispose();
        if (this.ListThumbButton3) this.ListThumbButton3.dispose();
        
        this._volumeControls = null;
        this._timelineControls = null;
        this._toggleControlsControl = null;
        this.PlaylistButton = null;
        this.ListThumbButton1 = null;
        this.ListThumbButton2 = null;
        this.ListThumbButton3 = null;
        
	    ExtendedPlayer.Player.callBaseMethod(this, 'xamlDispose');    				
    }
    
    
}   
ExtendedPlayer.Player.registerClass("ExtendedPlayer.Player",EmePlayer.Player);

//Mouse Control Class that handles all mouseover hitarea for fading in controls or chapterlists 1in the player.
ExtendedPlayer.MouseOverControl = function(host, nameElement) {
    // plays animations on mouse enter/leave
    this._element = host.content.findName(nameElement);
    this._t1 = this._element.addEventListener("mouseEnter", Function.createDelegate(this, this._mouseEnter));
    this._t2 = this._element.addEventListener("mouseLeave", Function.createDelegate(this, this._mouseLeave));
    this._enter = host.content.findName(nameElement + "_MouseEnter");
    this._leave = host.content.findName(nameElement + "_MouseLeave");
}
ExtendedPlayer.MouseOverControl.prototype = {
    dispose: function() {
        this._element.removeEventListener("mouseEnter", this._t1);
        this._element.removeEventListener("mouseLeave", this._t2);
        this._enter = null;
        this._leave = null;
        this._element = null;
    },
    _mouseEnter: function() {
        this._enter.begin();
    },
    _mouseLeave: function() {
        this._leave.begin();
    }
}
ExtendedPlayer.MouseOverControl.registerClass("ExtendedPlayer.MouseOverControl");

//Controls the fade of the control panel for the player.
ExtendedPlayer.ToggleControlsControl = function(host){
    this._chapterArea = host.content.findName('ChapterArea');
    this._playerControls = host.content.findName('PlayerControls');
    this._sbShow = host.content.findName('PlayerControls_Show');
    this._sbHide = host.content.findName('PlayerControls_Hide');
    this._sbTimer = host.content.findName('PlayerControls_HideTimer');
    this._controlsVisible = true; // set to false if controls should be invisible by default
    
    //Events used when control area is to be hidden.  This player does not have this enabled
    
    //this._t1 = this._sbTimer.addEventListener("Completed", Function.createDelegate(this, this._controlsAreaStartHide));
    //this._t2 = this._sbHide.addEventListener("Completed", Function.createDelegate(this, this._controlsAreaHidden));
    //this._t3 = this._sbShow.addEventListener("Completed", Function.createDelegate(this, this._controlsAreaShown));
    //this._t4 = this._playerControls.addEventListener("mouseEnter", Function.createDelegate(this, this._mouseEnter));
    //this._t5 = this._playerControls.addEventListener("mouseLeave", Function.createDelegate(this, this._mouseLeave));
}
ExtendedPlayer.ToggleControlsControl.prototype={ 
    _controlsAreaStartHide: function() {
        if (this._controlsVisible) this._sbHide.begin();
    },
    _controlsAreaShown: function() {
        this._controlsVisible = true;
    },
    _controlsAreaHidden: function() {
        this._controlsVisible = false;
        this._chapterArea.opacity = 0;
    },
    _mouseEnter: function() {
        this._sbTimer.stop();  
        if (!this._controlsVisible) {
            this._sbShow.begin();
            this._chapterArea.opacity = 0;           
        }            
    },
    _mouseLeave: function() {
        this._sbTimer.begin();
    },
    dispose: function() {
        //this._sbTimer.removeEventListener("Completed", this._t1);
        //this._sbHide.removeEventListener("Completed", this._t2);
        //this._sbShow.removeEventListener("Completed", this._t3);
        //this._playerControls.removeEventListener("mouseEnter", this._t4);
        //this._playerControls.removeEventListener("mouseLeave", this._t5);
        this._chapterArea = null;
        this._playerControls = null;
        this._sbShow = null;
        this._sbHide = null;
        this._sbTimer = null;
    }
}
ExtendedPlayer.ToggleControlsControl.registerClass('ExtendedPlayer.ToggleControlsControl');

//Chapterlist Control
ExtendedPlayer.PlaylistButton = function (host){
    this.playListHolder = host.content.findName('Playlist');
    this.playListButton = host.content.findName('PlaylistButton');
    this.playListButton.cursor = "hand";
    this.state = "Closed";
    this.host = host;
    this.open = host.content.findName('openPlaylist');
    this.expand = host.content.findName('arrowExpand');
    this.collapse = host.content.findName('arrowCollapse');
    this.disclaimer = host.content.findName('Disclaimer');
    this.disclaimerFadeIn = host.content.findName('disclaimerFadeIn');
    this.close = host.content.findName('closePlaylist');
   // ExtendedPlayer.PlaylistButton.initializeBase(this, [player.get_wpfeControl(), nameElement, true, 1.0, 0.6] );
   this._t1 = this.playListButton.addEventListener("mouseLeftButtonUp", Function.createDelegate(this, this._onLeftButtonUp));
   this._t2 = this.open.addEventListener("Completed", Function.createDelegate(this, this._onPlaylistExpandCompleted));
    this._t3 = this.close.addEventListener("Completed", Function.createDelegate(this, this._onPlaylistCollapseCompleted));
    
}
ExtendedPlayer.PlaylistButton.prototype = {
    _onLeftButtonUp: function(sender, eventArgs) {
            if (this.state == "Closed")
            {
                this.open.begin();
                this.expand.Opacity = 0;
                this.collapse.Opacity = 0.7;
                this.disclaimer.Opacity = 0;
                this.state = "Open";
                playlistExpanded = true;
                //this.host.main.Height = "630";
                //this.host._resizePlayer(false);
            } else {
                this.close.begin();
                this.expand.Opacity = 0.7;
                this.collapse.Opacity = 0;
                this.disclaimer.Opacity = 1;
                this.state = "Closed";
                playlistExpanded = false;
                //this.host.main.Height = "550";
                //this.host._resizePlayer(false);
            }
    } ,
    _onPlaylistExpandCompleted: function() {
       
        this.disclaimer.setValue("Canvas.Top", "535");
        //this.disclaimerFadeIn.begin();
    },
    
    _onPlaylistCollapseCompleted: function() {
  
        this.disclaimer.setValue("Canvas.Top", "450");
        //this.disclaimerFadeIn.begin();
    },
    
     dispose: function() {
        this.playListButton.removeEventListener("mouseLeftButtonUp", this._t1);
        this.open.removeEventListener("Completed", this._t2);
        this.close.removeEventListener("Completed", this._t3);
        this.playListButton = null;
    }
}
ExtendedPlayer.PlaylistButton.registerClass('ExtendedPlayer.PlaylistButton');


//PlayList class for multiple videos.  A new listThumb Button needs to be instantiated per video in the playlist in the StartPlayer.js file
ExtendedPlayer.ListThumbButton = function (player, host, nameElement){
    this.player = player;
    this._element = host.content.findName(nameElement);
    this._element.cursor = "hand";
    this.elementName = nameElement;
    this._t1 = this._element.addEventListener("mouseLeftButtonUp", Function.createDelegate(this, this._onLeftButtonUp));
}
ExtendedPlayer.ListThumbButton.prototype = {
    _onLeftButtonUp: function(sender, eventArgs) {
        var num = this.elementName.substr(14);
        this.player.set_mediainfo(get_mediainfo(parseInt(num)));
    },
     dispose: function() {
        this._element.removeEventListener("mouseLeftButtonUp", this._t1);
        this._element = null;
    }
}
ExtendedPlayer.ListThumbButton.registerClass('ExtendedPlayer.ListThumbButton');