﻿// JScript 文件
/*+---------------------------------------------------------------------------+
* 文件名称：randomPlayer.js
* 文件功能：随心听模块所用
* 文件作者：ZHOU LIU
* 创建时间：2009.4.25   14：00
* 项目名称：Usp40
*************************************
HISTORY:    Date        Author          Description
            2009-06-02  FENGXU          修改上一首下一首的播放功能,
*+--------------------------------------------------------------------------*/
var autoStart;
    function PlayerControler(controlId)
    {
        var self = this;
        this.player = document.getElementById(controlId + "$player");
        var player = this.player;
        
        var currentRingInfo = null;
        
        this.order = function()
        { 
            if(currentRingInfo)
                showPlayer(currentRingInfo.Code, playerAction.order, '随心听');
        }
        
        this.test = function()
        {
            player.Controls.CurrentPosition = player.CurrentMedia.Duration - 2;
        }
        
        var divMain = document.getElementById(controlId +"$main");  
        var rectMain = getElementRectangle(divMain);
        var volumeSilder = document.getElementById(controlId + '$volumeSilder');
        
//        setTimeout(
//            function()
//            { 
//                volumeSilder.style.left = rectMain.getX() + 100;
//                volumeSilder.style.top = rectMain.getY() + 55;
//            },
//            5000
//        );
        
        var volumeSilderBlock = document.getElementById(controlId +"$volumeSilderBlock");  
        var volumeSilder = document.getElementById(controlId +"$volumeSilder");  
        var btnPlayPause = document.getElementById(controlId+"$btnPlayPause");
        
        this.volumeSilderMousedown = false;
        
        
        var onVolumeSilderChanged = function(percentage)
        {
            //window.status=percentage;
            player.settings.volume = percentage;
            //player.volume = volumeSilder.style.width
        }
        
        var setVolumeSilder = function(value)
        {
            volumeSilderBlock.style.left = value;
            onVolumeSilderChanged((100 * parseInt(volumeSilderBlock.style.left)) / parseInt(volumeSilder.style.width));
        }
        
        this.handleVolumeSilder_mousedown = function()
        {
            this.volumeSilderMousedown = true;
            if(event.offsetX > 0 && event.srcElement == volumeSilder)
            {
                setVolumeSilder(event.offsetX);
            }
        }
        
        this.handleVolumeSilder_mouseup = function()
        {
            this.volumeSilderMousedown = false;
        }
        
        this.handleVolumeSilder_mousemove = function()
        { 
            if(this.volumeSilderMousedown && event.offsetX > 0 && event.srcElement == volumeSilder)
            { 
                setVolumeSilder(event.offsetX);
            }
        }
        
        
        var lblTime = document.getElementById(controlId + "$time");
        var divProgress =  document.getElementById(controlId + "$progress");
        var rectMain = getElementRectangle(divMain);

        this.timerRefreshInfo = window.setInterval(
            function()
            {  
                if(player.playState == /*wmppsPaused*/2 || player.playState == /*wmppsPlaying*/3) 
                { 
                    lblTime.innerText = player.controls.currentPositionString+'/'+ player.currentMedia.durationString;
//                    divProgress.style.left = (player.Controls.CurrentPosition/player.CurrentMedia.Duration) * 344 + 120; 
                }
                else
                {
                    lblTime.innerText = "00:00/00:00"; 
                    if(player.playState == /*wmppsStopped */1)
                    {
                        self.randomPlay();
                    }
                }
            },
            1000 
        );
        
        //播放暂停
        this.playPause = function()
        {
            if(!autoStart)
           {
                autoStart = true;
                setTimeout(this.randomPlay, 500);
           } 
        
            if(this.player.playState != /*wmppsPlaying*/3)
            {
                this.player.controls.play();
                btnPlayPause.src = 'images/pause.gif';
            }
            else
            {
                this.pause();
           }         
        }
        
        //暂停
        this.pause = function()
        {
            this.player.controls.Pause();
            btnPlayPause.src = 'images/play.gif';
        }
        
        
        var itemIndex = -1;
        var items = null;
        //随机播放
        this.randomPlay = function()
        {
            if(!autoStart)
                return; 
            var currentRingCode = null;
            if(currentRingInfo)
            {
                currentRingCode = currentRingInfo.Code;
            }
        
            if(items == null)
            {
                document.getElementById(controlId + "$text").innerText = "正在读取铃音,请稍候...";  
                if(typeof(ASP)!='undefined')
                {
                    items = ASP.locationcontrol_roundplayer_ascx.RandomRing().value;
                    itemIndex = -1;
                }
            }
             
            if(items)
            {
                if(itemIndex >= items.length)
                {
                    itemIndex = -1;
                }
                
                if($get('hiddenIsrandomPlay').value=="true")
                    itemIndex = Math.round(Math.random()*(items.length-1));
                else
                    itemIndex ++; 
                    
                currentRingInfo = items[itemIndex];
                
                if(currentRingInfo && currentRingInfo.Text)
                {
                    var txtText = document.getElementById(controlId + "$text");
                    txtText.title = txtText.innerText = currentRingInfo.Text; 
                    var txtAuthor = document.getElementById(controlId + "$author");
                    txtAuthor.title = txtAuthor.innerText = currentRingInfo.Author;
//                    document.getElementById(controlId + "$order").innerText = '定购';
                    
                    player.URL = currentRingInfo.Url
                    player.controls.play(); 
                    btnPlayPause.src = 'images/pause.gif';
                }
                else
                {
                    window.status = 'faild get ringInfo';
                }
            }
             

        }
        //***********************************************************************//
        //ADD BY FENGXU  2009-06-02
        //***********************************************************************//
        
        this.Play=function(operater)            //opearter == -1上一首，opearter == 1 下一首
        {
            var currentRingCode = null;
            if(currentRingInfo)
            {
                currentRingCode = currentRingInfo.Code;
            }
        
            if(items == null)
            {
                document.getElementById(controlId + "$text").innerText = "正在读取铃音,请稍候...";  
                if(ASP)
                {
                    items = ASP.locationcontrol_roundplayer_ascx.RandomRing().value;
                    itemIndex = -1;
                }
            }
             
            if(items)
            {
                if(itemIndex >= items.length)
                {
                    itemIndex = -1;
                }
                if(-1==operater)
                {
                    if(0==itemIndex || -1 ==itemIndex)
                    {
                        itemIndex = items.length-1;
                    }
                    else
                    {
                        itemIndex --;
                    }
                }
                else //if(1==operater)
                {
                    itemIndex ++;                
                }
                
                currentRingInfo = items[itemIndex];
                
                if(currentRingInfo && currentRingInfo.Text)
                {
                    var txtText = document.getElementById(controlId + "$text");
                    txtText.title = txtText.innerText = currentRingInfo.Text; 
                    var txtAuthor = document.getElementById(controlId + "$author");
                    txtAuthor.title = txtAuthor.innerText = currentRingInfo.Author;
//                    document.getElementById(controlId + "$order").innerText = '定购';
                    
                    player.URL = currentRingInfo.Url
                    player.controls.play(); 
                    btnPlayPause.src = 'images/pause.gif';
                }
                else
                {
                    window.status = 'faild get ringInfo';
                }
            }
             
        }
        
        //播放上一首    ADD BY FENGXU 2009-06-02
        this.Previous = function()
        {
            this.Play(-1);
        }        
        
        this.Next = function()
        {
            this.Play(+1);
        }
        
        setTimeout(this.randomPlay, 500);
    }
    
//oSource鼠标经过的对象，oTarget显示的对象
var _width;
var _height;
var _top;
var _left;
var _oTarget;
var _filterAlpha = 70;

function ShowTitleCutomer(oSource,oTarget,targetWidth,targetHeight)
{
    _width = targetWidth;
    _height = targetHeight;
    _oTarget =oTarget;
    _filterAlpha = 70;

    var parent = oSource;
    var x =parent.offsetLeft;
    var y =parent.offsetTop;
    var width = parent.offsetWidth
    while(parent =parent.offsetParent)
    {
        x = x+parent.offsetLeft;
        y = y+parent.offsetTop;
    }
    _top = y;
    _left = x;
    
    oTarget.style.position = 'absolute';
    oTarget.style.display = 'block';
    oTarget.style.left = x+'px';
    oTarget.style.top = (y)+'px';
//    oTarget.style.width = targetWidth+'px';
//    oTarget.style.height = targetHeight+'px';
    
    oTarget.style.filter  = 'Alpha(opacity='+0+')';
    OpenTarget(_width,_height,_filterAlpha);
}
function OpenTarget(width,height,alpha)
{
    if(_width>0)
        _width-=10;
    if(_height>0)
        _height -=5;
    if(_filterAlpha>0)
        _filterAlpha-=10;
        
    _oTarget.style.width = (width-_width)+'px';
    _oTarget.style.height = (height-_height)+'px';
    _oTarget.style.top = (_top-(height-_height))+'px';
    _oTarget.style.left = (_left+((_width)/2))+'px';
    _oTarget.style.filter  = 'Alpha(opacity='+(alpha-_filterAlpha)+')';
    
    if(_width>0||_height>0||_filterAlpha>0)
        setTimeout(function(){OpenTarget(width,height,alpha)},10);    
}
function CloseTarget(oTarget)
{
    oTarget.style.display = 'none';
}