var video = new videoControl()

function videoControl(  )
{
    var activeVideo         = 'slidecontainer';
    var videopreviewimage   = 'videopreviewimage';
    var videosource         = 'videosource';
    var farright            = '900'; 
    var farleft             = '-900'; 
    var slidestatus         = false;
    var livediv             = false;
    this.videosequence      = 0;

    this.videos2display     = 6;
    this.icons2display      = 10;
    this.videodisplaystart  = 0;

    this.click = function ( oNode )
    {
        document.getElementById( "video_preview" ).style.display = 'none';
        document.getElementById( "video" ).style.display = 'block';

        document.getElementById('video').innerHTML = '<object id="silverlightobject" data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="540" height="300" ><param name="source" value="/media/silverlight/Mediaplayer.xap"/>                                                <param name="background" value="black" />                        <param name="minRuntimeVersion" value="2.0.31005.0" />                        <param name="autoUpgrade" value="true" />                        <param name="initParams" id=\'videosource\' value="' + xmlc.aVideo[this.videosequence] + '" />           <param value="pluginLoaded" name="pluginLoaded"/>' + 
                            '<table  width="514" height="288" border="0" background="/media/images/bg.png">'+
                            '<tr>'+
                            '<td align="left"></td><td align="right"><a href="http://www.sport2media.com" target="new"><img src="/media/silverlight/images/s2mlogo.png" width="121" height="29" alt="" style="margin-right:10px; margin-top:25px" border="0"></a></td></tr><tr><td colspan="2" valign="top">' +
                            '<div class="tekstwhite">Voor een optimale videobeleving moet  Microsoft Silverlight op de pc worden ge&iuml;nstalleerd.'+
                            '<p>'+ 
                            'Microsoft Silverlight is net als Flash een simpele, kleine <a href="http://go.microsoft.com/fwlink/?LinkID=124807" target="new">download</a> die geen gevaar vormt voor de pc.'+
                            '<p>'+
                            'Norton Security en vergelijkbare software accepteren Microsoft Silverlight probleemloos.'+
                            '<p>'+
                            'Hulp nodig bij de installatie? Bekijk <a onClick="javascript:xlaAFMlaunch();" href="#">hier de FAQ</a> of mail naar: <a href="mailto:helpdesk@sport2media.com">helpdesk@sport2media.com</a></div>'+
                            '</td>'+
                        '</tr>'+
                        '<tr>'+
                        '    <td colspan="2" align="center" valign="middle">'+
                        '    <a href="http://go.microsoft.com/fwlink/?LinkID=124807" target="new"><img src="/media/images/installbutton.png" width="317" height="44" alt="" border="0"></a></td>'+
                        '</tr>'+
                    '</table>' +
                       '</object>';

    }

    this.reset = function ( )
    {
        document.getElementById( "video_preview" ).style.display = 'block';
        document.getElementById( "video" ).style.display = 'none';
        document.getElementById( 'video' ).innerHTML = '';
    }

    this.setNewVideo = function ( direction )
    {
        if ( document.getElementById('videoicons') )
        {
            var divID = document.getElementById('videoicons');
            var allImgs = divID.getElementsByTagName('img');
            for(var i=0; i<allImgs.length; i++){
                  allImgs[i].src = '/media/images/thub_icon.gif';
            }
        }
        if ( document.getElementById('videothumbs') )
        {
            var divID = document.getElementById('videothumbs');
            var allImgs = divID.getElementsByTagName('img');
            for(var i=0; i<allImgs.length; i++)
            {
                  if ( allImgs[i].id != 'videopopprev' && allImgs[i].id != 'videopopnext' )
                  {
                      allImgs[i].className = 'icon';
                  }                  
            }
        }

        if ( direction == 'next' )
        {
            if ( document.getElementById('videothumbs') )
            {
                if( xmlc.aVideo[(this.videosequence+1)]==undefined || !document.getElementById( 'icon' + ( this.videosequence+1 ) ) )
                    this.videosequence = 0;
                else
                    this.videosequence = this.videosequence + 1;
            }
            else
            {
                if( xmlc.aVideo[(this.videosequence+1)]==undefined )
                    this.videosequence = 0;
                else
                    this.videosequence = this.videosequence + 1;
            }

        }
        if ( direction == 'previous' )
        {
            if ( this.videosequence == 0 )
            {
                if ( document.getElementById('videothumbs') )
                {
                    if( ( xmlc.aVideo.length - 1 ) > 5 )
                        this.videosequence = 5;
                    else
                        this.videosequence = xmlc.aVideo.length - 1;
                }
                else
                {
                    /* this.videosequence = xmlc.aVideo.length - 1;*/
                    this.videosequence = xmlc.aVideo.length - 1;
                }
            }
            else
            {
                if( xmlc.aVideo[(this.videosequence-1)]==undefined )
                    this.videosequence = xmlc.aVideo.length - 1;
                else
                    this.videosequence = this.videosequence - 1;
            }

        }
        document.getElementById(videopreviewimage).src = xmlc.aScreens[ this.videosequence ];
        
        /* Home */
        if ( xmlc.aType[ this.videosequence ] == 'video' && document.getElementById('homeinfo') )
        {
            getHomeInfo( xmlc.aPid[ this.videosequence ] );
        }
        /* Live */
        if ( xmlc.aType[ this.videosequence ] == 'live' && document.getElementById('homeinfo') )
        {
            getLiveInfo( xmlc.aPid[ this.videosequence ] );
        }

/*        if ( document.getElementById('homeinfo') )
        {
            getHomeInfo( xmlc.aPid[ this.videosequence ] );
        }
        else if( document.getElementById('liveinfo') )
        {
            getLiveInfo( xmlc.aPid[ this.videosequence ] );
        }
        else
        { */

            if ( document.getElementById('liveplayerinfo') )
            {
                getLivePlayerInfo( xmlc.aPid[ this.videosequence ] );
                document.title = 'Sport.nl - Live - ' + xmlc.aTitle[ this.videosequence ];
                getLiveAds( xmlc.aPid[ this.videosequence ] );
            }
            if ( document.getElementById('videoinfo') )
            {
                getVideoInfo( xmlc.aPid[ this.videosequence ] );
                getAds( xmlc.aPid[ this.videosequence ] );
                document.title = 'Sport.nl - Video - ' + xmlc.aTitle[ this.videosequence ];
            }
            
        //}

        if ( document.getElementById('videoicons') )
        {
            document.getElementById( 'icon' + this.videosequence ).src = '/media/images/thub_icon_active.gif';
        }
        /* Home */

        if ( document.getElementById('videothumbs') )
        {

            document.getElementById( 'icon' + this.videosequence ).className = 'icon previds';
        }

    }
    
    this.init = function ( )
    {
        this.setNewVideo( );

        if ( document.getElementById('videothumbs') )
        {
            var divID = document.getElementById('videothumbs');
            divID.innerHTML = '';
            if ( this.videodisplaystart > 0 )
            {
                divID.innerHTML += '<img class="videopop" id="videopopprev" src="/media/images/gfx/back.png" height="19" width="9" alt="" onclick="video.populateprev();" />';
            }
            for( var i=0; i<this.videos2display; i++ )
            {
                if ( i < xmlc.aSmallScreens.length )
                {
                    if ( i == 0 )
                    {
                        z = this.videos2display - 1;
                    }
                    else
                    {
                        z = i - 1;
                    }
                    divID.innerHTML += '<img class="icon" src="'+xmlc.aSmallScreens[i]+'" id="icon'+i+'" alt="" onclick="video.videosequence='+z+';video.next();" />';
                }
            }
            if ( eval( this.videodisplaystart + this.videos2display ) < eval( xmlc.totalvideos ))
            {
                divID.innerHTML += '<img class="videopop" id="videopopnext" src="/media/images/gfx/next.png" height="19" width="9" alt="" onclick="video.populatenext();" />';
            }
            /* set first video highlighted */
            document.getElementById( 'icon' + this.videosequence ).className = 'icon previds';
        }
        else
        {
            var divID = document.getElementById('videoicons');
            divID.innerHTML = '';
            for ( var i=0; i<this.icons2display; i++ )
            {
                if ( i < xmlc.aSmallScreens.length )
                {
                    if ( i == 0 )
                    {
                        z = this.icons2display - 1;
                        divID.innerHTML += '<img class="icon" src="/media/images/thub_icon_active.gif" id="icon'+i+'" alt="" onclick="video.videosequence='+z+';video.next();" width="31" />';
                    }
                    else
                    {
                        z = i - 1;
                        divID.innerHTML += '<img class="icon" src="/media/images/thub_icon.gif" id="icon'+i+'" alt="" onclick="video.videosequence='+z+';video.next();" width="31" />';
                    }
                }
                
            }
        }
        

        document.getElementById('pulldownwrapper').style.top = '0px';
    }

    this.populatenext = function ( )
    {
        this.videodisplaystart = this.videodisplaystart+this.videos2display;
        xmlc.init('', this.videos2display, this.videodisplaystart );
        video.init();
    }

    this.populateprev = function ( )
    {
        if ( eval( this.videodisplaystart - this.videos2display ) < 0 )
        {
            this.videodisplaystart = 0;
        }
        else
        {
            this.videodisplaystart = this.videodisplaystart-this.videos2display;
        }
        xmlc.init('', this.videos2display, this.videodisplaystart );
        video.init();
    }

    this.next = function ( )
    {
        if ( slidestatus == false )
        {
            slidestatus = true;
            this.reset();
            var myVideo = new Fx.Tween(activeVideo);
            myVideo.start('left',farright).chain(
                function()
                { 
                    myVideo.set('left',farleft);
                    document.getElementById(videopreviewimage).src = '/media/images/blank.gif';
                    video.setNewVideo( 'next' );
                    this.start('left','0').chain(
                        function()
                        {
                            slidestatus = false;
                        }
                    );
                    
                }
            );
        }
    }

    this.previous = function ( )
    {
        if ( slidestatus == false )
        {
            slidestatus = true;
            this.reset();
            var myVideo = new Fx.Tween(activeVideo);
            myVideo.start('left',farleft).chain(
                function()
                { 
                    myVideo.set('left',farright);
                    document.getElementById(videopreviewimage).src = '/media/images/blank.gif';
                    video.setNewVideo( 'previous' );
                    this.start('left','0').chain(
                        function()
                        {
                            
                            slidestatus = false;
                        }
                    );
                    
                }
            );
        }
    }

}

function getHomeInfo( iProductId )
{
    var oXML = new klib3.xml();
    oXML.onload = function()
    {
        onloadResultText( oXML.getData(), "homeinfo" );
    };
    oXML.request( "/rpc.php", "post", true, {command:"homeinfo",_format:"xml",productid:iProductId} );
}

function getLiveInfo( iProductId )
{
    var oXML = new klib3.xml();
    oXML.onload = function()
    {
        onloadResultText( oXML.getData(), "homeinfo" );
    };
    oXML.request( "/rpc.php", "post", true, {command:"liveinfo",_format:"xml",productid:iProductId} );
}

function getVideoInfo( iProductId )
{
    var oXML = new klib3.xml();
    oXML.onload = function()
    {
        onloadResultText( oXML.getData(), "videoinfo" );
    };
    oXML.request( "/rpc.php", "post", true, {command:"videoinfo",_format:"xml",productid:iProductId} );
}

function getLivePlayerInfo( iProductId )
{
    var oXML = new klib3.xml();
    oXML.onload = function()
    {
        onloadResultText( oXML.getData(), "liveplayerinfo" );
    };
    oXML.request( "/rpc.php", "post", true, {command:"liveplayerinfo",_format:"xml",productid:iProductId} );
}

function getAds( iProductId )
{
    var oXML = new klib3.xml();
    oXML.onload = function()
    {
        onloadResultText( oXML.getData(), "videoad" );
    };
    oXML.request( "/rpc.php", "post", true, {command:"videoads",_format:"xml",productid:iProductId} );
}


function getLiveAds( iProductId )
{
    var oXML = new klib3.xml();
    oXML.onload = function()
    {
        onloadResultText( oXML.getData(), "videoad" );
    };
    oXML.request( "/rpc.php", "post", true, {command:"liveads",_format:"xml",productid:iProductId} );
}

function updateCounter( )
{
    /*var oXML = new klib3.xml();
    oXML.onload = function()
    {
        //onloadResultText( oXML.getData() );
    };
    oXML.request( "/rpc.php", "post", true, {command:'updatevideocounter',_format:"xml",productid:xmlc.aPid[video.videosequence]} );*/
}

function updateLiveCounter( )
{
    var oXML = new klib3.xml();
    oXML.onload = function()
    {
        //onloadResultText( oXML.getData() );
    };
    oXML.request( "/rpc.php", "post", true, {command:'updatelivecounter',_format:"xml",productid:xmlc.aPid[video.videosequence]} );
}

function onloadResultText( oXML, sLoadDiv )
{
    var oStatus   = parseStatusResult( oXML );

	var oVideoInfo = document.getElementById( sLoadDiv );
	oVideoInfo.innerHTML = '';

    if ( oStatus )
    {
        if ( oStatus.message != 'undefined' )
        {
            oVideoInfo.innerHTML = oStatus.message;
        }
    }
}

function getSiblingByName( oNode, sName )
{

    while ( ( oNode.nodeType != 1 || ( oNode.nodeName && oNode.nodeName.toLowerCase() != sName.toLowerCase() ) ) && oNode.nextSibling )
        oNode = oNode.nextSibling;

    if ( oNode.nodeType == 1 && ( oNode.nodeName && oNode.nodeName.toLowerCase() == sName.toLowerCase() ) )
    {   
        return oNode;
    }
    return false;
}

function getNodeValue( oNode )
{
    if ( oNode )
	{
		if ( oNode.nodeType == 3 || oNode.nodeType == 4 )
			return oNode.nodeValue;
		return arguments.callee( oNode.firstChild );
	}
}

function parseStatusResult( oDataXML )
{                    
    if ( oDataXML )
    {
        var oReply = getSiblingByName( oDataXML.firstChild, "reply" );
        if ( oReply )
        {
            var sStatus = "error";
            if ( typeof oReply.attributes[ 0 ] != "undefined" && oReply.attributes[ 0 ].name.toLowerCase() == "status" )
                sStatus = oReply.attributes[ 0 ].value;
                
                
            var sMessage = getNodeValue( getSiblingByName( oReply.firstChild, "message" ) );
            
            var sContent = getNodeValue( getSiblingByName( oReply.firstChild, "content" ) );
            
            var bStatus  = sStatus == "OK";
            
            return {
                success:bStatus,
                status:sStatus,
                message:unescape( sMessage ),
                content:sContent
            };
        }
    }
    
    return false;
}
