//Axinom GmbH - 2008
if (!window.Axinom)
	window.Axinom = {};

Axinom.CanvasPlayer = function(pPlayer) 
{
    this.Player = pPlayer;
    this.isMediaOpened = false;
    this.isControlPanelEnabled = false;
    this.MediaElementPlayerScreen = null;
    this.TextBlockProgressbarValue = null;
    this.timeStampOfLastClickOnCanvasPlayerScreen = null;
}

Axinom.CanvasPlayer.prototype =
{
	handleLoad: function(control, userContext, rootElement) 
	{	    
		this.control = control;
        var host = control; 
        
        this.LogicCheck();
        this.InitControls(rootElement);	
        control.content.onfullScreenChange = this.onFullScreenChanged; 		    
	},
	LogicCheck: function()
	{
	    if(this.Player.AutoDownload == false && this.Player.AutoPlay == true)
	    {
	        this.Player.AutoDownload = true;
	    }
	},
	InitControls: function(rootElement)
	{
		this.TextBlockProgressbarValue = rootElement.findName("TextBlockProgressbarValue");
		this.canvasTimer = rootElement.findName("CanvasTimer");
		
		this.InitCanvasMain(rootElement);
	},
	InitCanvasMain: function(rootElement)
	{
		this.CanvasMain = rootElement.findName("CanvasMain");
		this.CanvasMain.addEventListener("MouseLeave", Silverlight.createDelegate(this, this.CanvasMain_MouseLeave));
		this.CanvasMain.addEventListener("MouseEnter", Silverlight.createDelegate(this, this.CanvasMain_MouseEnter));

		this.InitCanvasPlayerScreen(rootElement);
		this.InitCanvasPanel(rootElement);
		this.InitCanvasInfo(rootElement);
	},
	InitCanvasPlayerScreen: function(rootElement)
	{
		this.CanvasPlayerScreen = rootElement.findName("CanvasPlayerScreen");	
		this.CanvasPlayerScreen["Width"] = this.CanvasMain["Width"];
		this.CanvasPlayerScreen["Height"] = this.CanvasMain["Height"];
		this.CanvasPlayerScreen.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.CanvasPlayerScreen_MouseLeftButtonUp));	
		
		this.RectangleBlackScreen = rootElement.findName("RectangleBlackScreen");	
		this.RectangleBlackScreen["Width"] = this.CanvasPlayerScreen["Width"];	
	    this.RectangleBlackScreen["Height"] =  this.CanvasPlayerScreen["Height"];
	    
		this.ImageFixedImage = rootElement.findName("ImageFixedImage");
		this.ImageFixedImage["Width"] = this.CanvasPlayerScreen["Width"];	
	    this.ImageFixedImage["Height"] =  this.CanvasPlayerScreen["Height"]
		this.ImageFixedImage["Stretch"] = this.Player.FixedImage.Stretch;
		if(this.Player.FixedImage.Source != null)
		{
		    this.ImageFixedImage["Source"] = this.Player.FixedImage.Source;
		}
		
		this.MediaElementPlayerScreen = rootElement.findName("MediaElementPlayerScreen");
		this.MediaElementPlayerScreen["Width"] = this.CanvasPlayerScreen["Width"];	
	    this.MediaElementPlayerScreen["Height"] =  this.CanvasPlayerScreen["Height"];
		this.MediaElementPlayerScreen["Stretch"] = this.Player.Medium.Stretch;		
		this.MediaElementPlayerScreen.addEventListener("DownloadProgressChanged", Silverlight.createDelegate(this, this.MediaElementPlayerScreen_MediaDownloadChanged));
		this.MediaElementPlayerScreen.addEventListener("MediaOpened", Silverlight.createDelegate(this, this.MediaElementPlayerScreen_MediaOpened));
		this.MediaElementPlayerScreen.addEventListener("MediaEnded", Silverlight.createDelegate(this, this.MediaElementPlayerScreen_MediaEnded));
		if(this.Player.AutoDownload == true)
		{
		    this.MediaElementPlayerScreen["Source"] = this.Player.Medium.Source;
		}
	},
	InitCanvasInfo: function(rootElement)
	{
		this.CanvasInfo = rootElement.findName("CanvasInfo");
		this.CanvasInfo.addEventListener("MouseLeave", Silverlight.createDelegate(this, this.CanvasInfo_MouseLeave));
		this.CanvasInfo["Width"] = this.CanvasPanel["Width"];
	    this.CanvasInfo["Height"] =  this.CanvasMain["Height"] - 18;
		
		this.RectangleInfoBackground = rootElement.findName("RectangleInfoBackground");
		this.RectangleInfoBackground["Width"] = this.CanvasInfo["Width"];
	    this.RectangleInfoBackground["Height"] =  this.CanvasInfo["Height"];
	    
		this.TextBlockInfoTitle = rootElement.findName("TextBlockInfoTitle");
	    this.TextBlockInfoTitle["Width"] =  this.CanvasInfo["Width"] - (2 * 8);
        this.TextBlockInfoTitle["Text"] = this.Player.Info.Title;
        
		this.TextBlockInfoText = rootElement.findName("TextBlockInfoText");	
	    this.TextBlockInfoText["Width"] =  this.CanvasInfo["Width"] - (2 * 8);
        this.TextBlockInfoText["Text"] = this.Player.Info.Text;        
        
	    this.CanvasInfo["Visibility"] =  "Collapsed";
	},
	InitCanvasPanel: function(rootElement)
	{
		this.CanvasPanel = rootElement.findName("CanvasPanel");
		this.CanvasPanel.addEventListener("MouseMove", Silverlight.createDelegate(this, this.CanvasPanel_MouseMove));
		this.CanvasPanel.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.CanvasPanel_MouseLeftButtonUp));	
		this.CanvasPanel["Canvas.Top"] = this.CanvasMain["Height"] - (this.CanvasPanel["Height"] + 10);
		this.CanvasPanel["Width"] = this.CanvasMain["Width"] - (2 * 8);	
				
		this.RectanglePanelBackground = rootElement.findName("RectanglePanelBackground");
		this.RectanglePanelBackground["Width"] = this.CanvasPanel["Width"];
		
		this.RectanglePanelForeground = rootElement.findName("RectanglePanelForeground");
		this.RectanglePanelForeground["Width"] = this.CanvasPanel["Width"];
	    if(this.Player.AutoDownload == false)
	    {
	        this.RectanglePanelForeground["Visibility"] = "Collapsed";
	    }
	    
		this.RectangleProgressTime = rootElement.findName("RectangleProgressTime");		
		this.RectangleProgressTime.addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.RectangleProgressTime_MouseLeftButtonDown));
		
		this.InitCanvasProgressTime(rootElement);
		this.InitCanvasButtons(rootElement);
		this.InitCanvasVolume(rootElement);
		
		this.CanvasPanel["Opacity"] = 0;
	},
	InitCanvasProgressTime: function(rootElement)
	{
		this.CanvasProgressbarDownload = rootElement.findName("CanvasProgressbarDownload");
		this.CanvasProgressbarDownload["Width"] = this.CanvasPanel["Width"] - (2 * 8);
		
		this.RectangleProgressBackground = rootElement.findName("RectangleProgressBackground");		
		this.RectangleProgressBackground["Width"] = this.CanvasProgressbarDownload["Width"];
	},
	InitCanvasButtons: function(rootElement)
	{
		this.CanvasButtons = rootElement.findName("CanvasButtons");
		this.CanvasButtons["Canvas.Left"] = (this.CanvasPanel["Width"] - this.CanvasButtons["Width"]) / 2;
		
		this.CanvasButtonPlayPause = rootElement.findName("CanvasButtonPlayPause");
		this.CanvasButtonPlayPause.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.CanvasButtonPlayPause_MouseLeftButtonUp));
		
		this.CanvasButtonStop = rootElement.findName("CanvasButtonStop");		
		this.CanvasButtonStop.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.CanvasButtonStop_MouseLeftButtonUp));
		
		this.CanvasButtonFullScreen = rootElement.findName("CanvasButtonFullScreen");		
		this.CanvasButtonFullScreen.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.CanvasButtonFullScreen_MouseLeftButtonUp));
        
		this.CanvasButtonInfo = rootElement.findName("CanvasButtonInfo");
		this.CanvasButtonInfo.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.ButtonInfo_MouseLeftButtonUp));
	},
	InitCanvasVolume: function(rootElement)
	{
		this.CanvasVolume = rootElement.findName("CanvasVolume");
		this.CanvasVolume["Canvas.Left"] = this.CanvasPanel["Width"] - this.CanvasVolume["Width"] - 8;
		
		this.CanvasButtonVolumeDecrease = rootElement.findName("CanvasButtonVolumeDecrease");		
		this.CanvasButtonVolumeDecrease.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.CanvasButtonVolumeDecrease_MouseLeftButtonUp));
		
		this.CanvasButtonVolumeIncrease = rootElement.findName("CanvasButtonVolumeIncrease");		
		this.CanvasButtonVolumeIncrease.addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.CanvasButtonVolumeIncrease_MouseButtonLeftUp));
		
	},
	CanvasInfo_MouseLeave: function(sender, args)
	{
	    if(_isInfoShown == true)
	    {
	        sender.findName("CanvasInfo_Hide").begin();
            _isInfoShown = false;  
        }
	},
	ButtonInfo_MouseLeftButtonUp: function(sender, args)
	{
	    if(_isInfoShown == true)
	    {
	        sender.findName("CanvasInfo_Hide").begin()
	        _isInfoShown = false;    
	    }
	    else
	    {
    	    this.CanvasInfo["Visibility"] =  "Visible";
	        sender.findName("CanvasInfo_Show").begin()
	        _isInfoShown = true;
	    }
	},
	DoPlayPause: function(sender, args)
	{
	    if(this.Player.Medium.Type == "video")
	    {
	        this.RectangleBlackScreen["Visibility"] = "Collapsed";
		    this.ImageFixedImage["Visibility"] = "Collapsed";
		}
	    
	    if(_isButtonPlay == true)
	    {	    
	        if(this.Player.AutoDownload == false && this.isMediaOpened == false)
	        {
		        this.MediaElementPlayerScreen["Source"] = this.Player.Medium.Source;
		    }
		    else
		    {
		        _doTick = true;
	            _isButtonPlay = false;
    		    
	            sender.findName("ButtonPlay_Switch").begin();
	            sender.findName("StoryboardTimer").begin();
	            if(this.MediaElementPlayerScreen.position.seconds < this.Player.Medium.StartPosition)
	            {
                    var position = this.MediaElementPlayerScreen.position;
                    position.seconds = this.Player.Medium.StartPosition;
                    this.MediaElementPlayerScreen.position = position;
                }
    	        
		        //this.ImageFixedImage["Visibility"] = "Collapsed";
	            this.MediaElementPlayerScreen.Play();
	        }
	    }
	    else
	    {
	        _doTick = false;
	        _isButtonPlay = true;
	        
	        sender.findName("ButtonPause_Switch").begin();
	        this.MediaElementPlayerScreen.Pause();
	    }	
	},
	CanvasPlayerScreen_MouseLeftButtonUp: function(sender, args)
	{	
	    if(this.timeStampOfLastClickOnCanvasPlayerScreen != null && (new Date()).getTime() - (this.timeStampOfLastClickOnCanvasPlayerScreen.getTime()) <= 1000)
	    {
	        this.DoFullScreenChange(sender, args);
	        this.timeStampOfLastClickOnCanvasPlayerScreen = null;
	    }
	    else
	    {
	        this.timeStampOfLastClickOnCanvasPlayerScreen = new Date();
	    }
	},
	CanvasButtonPlayPause_MouseLeftButtonUp: function(sender, args)
	{	
	    this.DoPlayPause(sender, args);
	},
	CanvasButtonStop_MouseLeftButtonUp: function(sender, args)
	{	
	    this.RectangleBlackScreen["Visibility"] = "Visible";
	    if(_isButtonPlay != true)
	    {
	        _isButtonPlay = true;
	        
	        sender.findName("ButtonPause_Switch").begin();
	    }
	    _doTick = false;	    
	    this.MediaElementPlayerScreen.Stop();
	    	    
        var position = this.MediaElementPlayerScreen.position;
        position.seconds = this.Player.Medium.StartPosition;
        this.MediaElementPlayerScreen.position = position;
        this.MediaElementPlayerScreen.Pause();
        
	    this.RectangleBlackScreen["Visibility"] = "Collapsed";
		this.ImageFixedImage["Visibility"] = "Visible";
	},
	CanvasButtonFullScreen_MouseLeftButtonUp: function(sender, args)
	{
	    this.DoFullScreenChange(sender, args); 
	},
	DoFullScreenChange: function(sender, args)
	{
	    var silverlightPlugin = sender.getHost();
        silverlightPlugin.content.fullScreen = !silverlightPlugin.content.fullScreen;  
	},
    onFullScreenChanged: function(sender, args)
    {    
        var silverlightPlugin = sender.getHost();
        var canvasPanel = sender.findName("CanvasPanel");
        var canvasMain = sender.findName("CanvasMain");
        var canvasPlayerScreen = sender.findName("CanvasPlayerScreen");
        var mediaElementPlayerScreen = sender.findName("MediaElementPlayerScreen");
        var rectangleBlackScreen = sender.findName("RectangleBlackScreen");
        var imageFixedImage = sender.findName("ImageFixedImage");
        
        if (silverlightPlugin.content.fullScreen == true)
        {
            canvasPanel.opacity = 0;
            canvasMain.width = silverlightPlugin.content.actualWidth;
            canvasMain.height = silverlightPlugin.content.actualHeight; 
            mediaElementPlayerScreen.width = silverlightPlugin.content.actualWidth;
            mediaElementPlayerScreen.height = silverlightPlugin.content.actualHeight;  
            mediaElementPlayerScreen["canvas.top"] = 0;
            mediaElementPlayerScreen["canvas.left"] = 0;
            canvasPanel["Visibility"] = "Collapsed";
            _isFullScreen = true;
		    rectangleBlackScreen["Width"] = silverlightPlugin.content.actualWidth;
		    rectangleBlackScreen["Height"] = silverlightPlugin.content.actualHeight;
		    imageFixedImage["Width"] = silverlightPlugin.content.actualWidth;
		    imageFixedImage["Height"] = silverlightPlugin.content.actualHeight;
        }
        else 
        {
            canvasPanel.opacity = 1; 
            canvasMain.width = silverlightPlugin.content.actualWidth;
            canvasMain.height = silverlightPlugin.content.actualHeight;
            canvasMain["Width"] = canvasPlayerScreen["Width"];
            canvasMain["Height"] = canvasPlayerScreen["Height"];
		    rectangleBlackScreen["Width"] = canvasPlayerScreen["Width"];
		    rectangleBlackScreen["Height"] = canvasPlayerScreen["Height"];
		    imageFixedImage["Width"] = canvasPlayerScreen["Width"];
		    imageFixedImage["Height"] = canvasPlayerScreen["Height"];
		    canvasPlayerScreen["Width"] = canvasPlayerScreen["Width"];
		    canvasPlayerScreen["Height"] = canvasPlayerScreen["Height"];
            mediaElementPlayerScreen.width = canvasPlayerScreen.width;
            mediaElementPlayerScreen.height = canvasPlayerScreen.height;
            mediaElementPlayerScreen["canvas.top"] = 0;
            mediaElementPlayerScreen["canvas.left"] = 0;
            canvasPanel["Visibility"] = "Visible";
            _isFullScreen = false;
        }
        
    },
    MediaElementPlayerScreen_MediaDownloadChanged: function(sender, args)
    {
        _downloadProgress = sender.downloadProgress;
        var width = Math.floor(_downloadProgress * (this.CanvasProgressbarDownload["Width"] - (2 * _interspaceProgressbarDownload))); 
        sender.findName("RectangleProgressbarDownload").width = width;
        
        var startPositionInPercent = this.Player.Medium.StartPosition / this.MediaElementPlayerScreen.NaturalDuration.Seconds;
        
        if((this.Player.Medium.StartPosition + 5/* 5sec. buffer */) < ((_downloadProgress * this.MediaElementPlayerScreen.NaturalDuration.Seconds)) && this.isControlPanelEnabled == false)
        {
            var position = this.MediaElementPlayerScreen.position;
            position.seconds = this.Player.Medium.StartPosition;
            this.MediaElementPlayerScreen.position = position;
            this.MediaElementPlayerScreen.Pause();
            
            this.RectanglePanelForeground = sender.findName("RectanglePanelForeground");
            this.RectanglePanelForeground["Visibility"] = "Collapsed";
            
	        if(this.Player.Medium.Type == "video")
	        {
                this.RectangleBlackScreen["Visibility"] = "Collapsed";
            }
            this.isControlPanelEnabled = true;
            
            if(this.Player.AutoPlay == true || this.Player.AutoDownload == false)
            {
		        _doTick = true;
	            _isButtonPlay = false;
    		    
	            sender.findName("ButtonPlay_Switch").begin();
	            sender.findName("StoryboardTimer").begin();
	            if(this.MediaElementPlayerScreen.position.seconds < this.Player.Medium.StartPosition)
	            {
                    var position = this.MediaElementPlayerScreen.position;
                    position.seconds = this.Player.Medium.StartPosition;
                    this.MediaElementPlayerScreen.position = position;
                }
    	        
	            if(this.Player.Medium.Type == "video")
	            {
		            this.ImageFixedImage["Visibility"] = "Collapsed";
		        }
	            this.MediaElementPlayerScreen.Play();
            }
        }
    },
    MediaElementPlayerScreen_MediaOpened: function(sender, args)
    {
        this.isMediaOpened = true;
        this.MediaElementPlayerScreen.opacity = 1;
    },
    MediaElementPlayerScreen_MediaEnded: function(sender, args)
    {
	    this.RectangleBlackScreen["Visibility"] = "Visible";
        this.MediaElementPlayerScreen.stop();
      
        var position = this.MediaElementPlayerScreen.position;
        position.seconds = this.Player.Medium.StartPosition;
        this.MediaElementPlayerScreen.position = position;
        this.MediaElementPlayerScreen.Pause();

        _isButtonPlay = true;        
        sender.findName("ButtonPause_Switch").begin();
	    _doTick = false;
	    	    
	    if(this.Player.Medium.Type == "video")
	    {
	        this.RectangleBlackScreen["Visibility"] = "Collapsed";
	    }
		this.ImageFixedImage["Visibility"] = "Visible";
      
      //player.play();
    },
    CanvasButtonVolumeIncrease_MouseButtonLeftUp: function(sender, args)
    {      
        if(this.MediaElementPlayerScreen["Volume"] != (_volumeUnitsMax * _volumeUnitStepSize))
        {        
            _volumeUnits++;
            this.MediaElementPlayerScreen["Volume"] = _volumeUnits * _volumeUnitStepSize;
            
            for (var i = 1; i <= _volumeUnitsMax; i++)
            {
                var controlName = "RectangleVolume" + i.toString();
                this.rectangleVolume = sender.findName(controlName);
                if(i <= _volumeUnits)
                {
                    this.rectangleVolume["Fill"] = _volumeColorActive;
                }
                else
                {
                    this.rectangleVolume["Fill"] = _volumeColorInactive;
                }
            }
        }
    },
    CanvasButtonVolumeDecrease_MouseLeftButtonUp: function(sender, args)
    {
        if(this.MediaElementPlayerScreen["Volume"] != 0)
        {
            _volumeUnits--;
            this.MediaElementPlayerScreen["Volume"] = _volumeUnits * _volumeUnitStepSize;
            
            for (var i = 1; i <= _volumeUnitsMax; i++)
            {
                var controlName = "RectangleVolume" + i.toString();
                this.rectangleVolume = sender.findName(controlName);
                if(i <= _volumeUnits)
                {
                    this.rectangleVolume["Fill"] = _volumeColorActive;
                }
                else
                {
                    this.rectangleVolume["Fill"] = _volumeColorInactive;
                }
            }
        }
    },
    RectangleProgressTime_MouseLeftButtonDown: function(sender, args)
    {
        if(this.isMediaOpened == true)
        {
            _isSliderDragged = true;
	        this.MediaElementPlayerScreen.Pause();
    	    	    
	        var mousePositionX = args.getPosition(this.CanvasPanel).x;
	        var currentLeft = this.RectangleProgressTime["Canvas.Left"];
    	    	    
	        _differenceMouseCursor = mousePositionX - (currentLeft + (this.RectangleProgressTime["Width"] / 2));
	        _doTick = false; 
    	    
	        if(_isButtonPlay == true)
	        {
	            _isButtonPlay = false;
    	        
	            sender.findName("ButtonPlay_Switch").begin();
	        }
	    }
    },
    CanvasPanel_MouseLeftButtonUp: function(sender, args)
    {    
        //alert(_isSliderDragged);
        if(_isSliderDragged == true)
        {        
//            this.RectangleProgressTime = sender.findName("RectangleProgressTime");
//            var sliderPosition = this.RectangleProgressTime["Left"] - 13;
//            var sliderPositionInPercent = sliderPosition / 305
//            var startPositionInPercent = this.Player.StartPosition / this.mediaElementPlayer.NaturalDuration.Seconds;    
//                    
//            var position = this.mediaElementPlayer.position;            
//            position.seconds = this.Player.StartPosition;
//            this.mediaElementPlayer.position = position;
//            
//            this.mediaElementPlayer.Pause();
//            this.RectanglePanelForeground = sender.findName("RectanglePanelForeground");
//            this.RectanglePanelForeground["Visibility"] = "Collapsed";
//            this.isControlPanelEnabled = true;     
        
            _isSliderDragged = false;
	        this.MediaElementPlayerScreen.Play();
    	    
		    this.canvasTimer = sender.findName("CanvasTimer");
		    this.StoryboardTimer = sender.findName("StoryboardTimer");
		    this.StoryboardTimer.begin();
		    _doTick = true;
		    
	        if(this.Player.Medium.Type == "video")
	        {
	            this.ImageFixedImage["Visibility"] = "Collapsed";
	        }
		}
    },
    CanvasMain_MouseEnter: function(sender, args)
    {
        if(_isFullScreen != true)
        {
	        sender.findName("CanvasMain_MouseEnter").begin();
	    }
    },
    CanvasMain_MouseLeave: function(sender, args)
    {
        if(_isFullScreen != true)
        {
	        sender.findName("CanvasMain_MouseLeave").begin();
	    }
    },
    CanvasPanel_MouseMove: function(sender, args)
    {
        if(_isSliderDragged == true)
        {
            var mousePositionX = args.getPosition(this.CanvasPanel).x;
        
            this.CanvasPanel = sender.findName("CanvasPanel");
            this.rectangleProgressTime = sender.findName("RectangleProgressTime");
            
            var naturalDuration = this.MediaElementPlayerScreen.naturalDuration.seconds;
            var duration = this.MediaElementPlayerScreen.position.seconds;
            var timeLineSize = this.CanvasProgressbarDownload["Width"] - (2 * _interspaceProgressbarDownload);
            var currentLeft = this.rectangleProgressTime["Canvas.Left"] - _interspaceSliderMin;            
            var rectangleProgressTimeLeft = _interspaceSliderMin;
            var sliderWidth = this.rectangleProgressTime["Width"];
            
            var startPositionInPercent = this.Player.Medium.StartPosition / this.MediaElementPlayerScreen.NaturalDuration.Seconds;
    
            if(mousePositionX - _differenceMouseCursor <= _interspaceSliderMin + (startPositionInPercent * timeLineSize))
            {
                rectangleProgressTimeLeft = (timeLineSize * startPositionInPercent) + _interspaceSliderMin - (sliderWidth / 2);
                duration = this.Player.Medium.StartPosition;
            }
            else if(mousePositionX - _differenceMouseCursor > _interspaceSliderMin + (startPositionInPercent * timeLineSize) && mousePositionX - _differenceMouseCursor < (timeLineSize * _downloadProgress) + _interspaceSliderMin)
            {
                rectangleProgressTimeLeft = mousePositionX - _differenceMouseCursor - (sliderWidth / 2);
                
                duration = ((mousePositionX - _differenceMouseCursor - _interspaceSliderMin) / timeLineSize) * naturalDuration;
            }
            else
            {
                rectangleProgressTimeLeft = (timeLineSize * _downloadProgress) + _interspaceSliderMin - (sliderWidth / 2);
                duration = Math.floor(naturalDuration * _downloadProgress);
            }
            
            this.rectangleProgressTime["Canvas.Left"] = rectangleProgressTimeLeft;
            
            var position = this.MediaElementPlayerScreen.position;
            position.seconds = duration;
            this.MediaElementPlayerScreen.position = position;         
            
            //this.TextBlockTest = sender.findName("TextBlockTest");
            //this.TextBlockTest.Text = Math.floor(duration.toString()) + "-" + Math.floor(this.mediaElementPlayer.position.seconds.toString()) + "-" + Math.floor(_downloadProgress * 100).toString();
        }
    },
    SetDuration: function(sender)
    {
        var mediaElementPlayerScreen = sender.findName("MediaElementPlayerScreen");
        var textBlockProgressbarValue = sender.findName("TextBlockProgressbarValue");
        textBlockProgressbarValue.Text = GetDuration(mediaElementPlayerScreen.naturalDuration.seconds, mediaElementPlayerScreen.position.seconds, mediaElementPlayerScreen.position.seconds);
    }
}

var _isSliderDragged = false;
var _isInfoShown = false;
var _isButtonPlay = true;
var _doTick = false;
var _isFullScreen = false;
var _downloadProgress = 0;
var _differenceMouseCursor = 0;
var _volumeUnits = 3;
var _volumeUnitStepSize = 0.2;
var _volumeUnitsMax = 5;
var _solidColorBrushVolumeActive = null;
var _solidColorBrushVolumePassive = null;
var _volumeColorActive = "#FFFFFFFF";
var _volumeColorInactive = "#FFCACACA";
var _interspaceSliderMin = 17;
var _interspaceProgressbarDownload = 9;

function CanvasInfo_Hide_Completed(sender, args)
{
    (sender.findName("CanvasInfo"))["Visibility"] = "Collapsed";
}
function StoyboardTimer_Completed(sender, args)
{    
    this.Axinom.CanvasPlayer.prototype.SetDuration(sender);
    
    this.rectangleProgressTime = sender.findName("RectangleProgressTime");
    var rectangleProgressTimeNewLeft = GetRectangleProgressTimeNewLeft(sender);
    this.rectangleProgressTime["Canvas.Left"] = rectangleProgressTimeNewLeft;
    
	if(_doTick == true)
	{
	    StoryboardTimer = sender.findName("StoryboardTimer");
	    this.StoryboardTimer.begin();
	}
}
function GetRectangleProgressTimeNewLeft(sender)
{
    this.TextBlockProgressbarValue = sender.findName("TextBlockProgressbarValue");
    this.MediaElementPlayerScreen = sender.findName("MediaElementPlayerScreen");
    this.canvasProgressbarDownload = sender.findName("CanvasProgressbarDownload");
    this.rectangleProgressTime = sender.findName("RectangleProgressTime");
    
    var naturalDuration = this.MediaElementPlayerScreen.naturalDuration.seconds;
    var duration = this.MediaElementPlayerScreen.position.seconds;
    var timeLineSize = this.canvasProgressbarDownload["Width"] - (2 * _interspaceProgressbarDownload);
    
//            this.TextBlockTest = sender.findName("TextBlockTest");
//            this.TextBlockTest.Text = duration.toString();
    
    var currentDurationInPercent = duration / naturalDuration;
    var currentTimeLinePosition = Math.floor(timeLineSize * currentDurationInPercent);
    
    return currentTimeLinePosition + _interspaceSliderMin - (this.rectangleProgressTime["Width"] / 2);
}
function GetDuration(pNaturalDuration, pDuration, pMediaPosition)
{    
    var naturalDuration = GetTimeString(pNaturalDuration);
    var duration = GetTimeString(pDuration);
    
    var seconds = Math.floor(pMediaPosition);  
    var minutes = Math.floor(seconds / 60);
    var hours = Math.floor(minutes / 60);
    
    seconds = seconds - (minutes * 60);
    minutes = minutes - (hours * 60);
    
    seconds = ConvertIntoTwoChars(seconds);
    minutes = ConvertIntoTwoChars(minutes);
    hours = ConvertIntoTwoChars(hours);
    
    return  duration + " / " + naturalDuration;
}
function ConvertIntoTwoChars(number)
{
    var string;
    
    if(number.toString().length == 1)
    {
        string = "0" + number.toString();
    }
    else
    {
        string = number.toString();
    }

    return string;
}

function GetTimeString(seconds)
{
    var string;
    
    var seconds = Math.floor(seconds);  
    var minutes = Math.floor(seconds / 60);
    var hours = Math.floor(minutes / 60);
    
    seconds = seconds - (minutes * 60);
    minutes = minutes - (hours * 60);
    
    seconds = ConvertIntoTwoChars(seconds);
    minutes = ConvertIntoTwoChars(minutes);
    hours = ConvertIntoTwoChars(hours);

    string = hours + ":" + minutes + ":" + seconds;
    
    return string;
}
