var TVI24player = null;
function playerReady() {
    TVI24player = document.getElementById('TVI24playerDirecto');
    TVI24addListeners();
}

function TVI24addListeners() {
    if (TVI24player) {
        TVI24player.addModelListener("STATE", "TVI24playListener");
        TVI24player.addModelListener("TIME", "TVI24timeListener");
        TVI24player.addModelListener("ERROR", "TVI24errorListener");
    } else {
        setTimeout("TVI24addListeners()",100);
    }
}

function TVI24timeListener(obj) {
    if (obj.position>900){
        TVI24player.sendEvent("STOP");
    }
}

function TVI24errorListener(obj) {

    if (obj.message=='Stream not found: TVI24'){
        $('#TVI24playerDirecto').replaceWith('<img src="/images/directo_error_big.jpg" alt="Interrompemos a emissÃ£o por motivos relacionados com direitos sobre conteÃºdos" />');
    }
}

function TVI24playListener(obj) {
    if(obj.newstate=="PLAYING" && obj.oldstate=="PAUSED") {
        $(function(){
            // Fire off AJAX request.
            $.ajax(
            {
                // Define AJAX properties.
                method: "get",
                url: "/mediamaster/getBestServer.do",
                dataType: "html",
                timeout: (10*1000),
                cache: false,


                // Define the success method.
                success: function(server){
                    var obj = {
                        file:"TVI24",
                        autostart:"true",
                        streamer:'rtmp://'+server+'/live',
                        controlbar:"bottom",
                        type:"video"
                    };
                    TVI24player.sendEvent("LOAD",obj);
                }

            });
        });
    }
}


function TVI24createPlayer() {
   $(function(){
        //Stop Reloading page

        if (typeof(jQuery().reloadStop) == "function")  {jQuery().reloadStop();};

        // Fire off AJAX request.
        $.ajax(
        {
            // Define AJAX properties.
            method: "get",
            url: "/mediamaster/getBestServer.do",
            dataType: "html",
            timeout: (10*1000),
            cache: false,


            // Define the success method.
             success: function(server){

                    var zone_site="www_tvi24_iol_pt";
                    var account="UA-161287-28";
                    var idvideo="TVI24";
                    var duracao="0_15_00";
                    var player_video = 'player_video_live';

                    var flashvars = {
                        file:"TVI24",
                        autostart:"true",
                        streamer:'rtmp://'+server+'/live',
                        plugins:"http://www.tvi24.iol.pt/flashplayers/ova.swf,gapro-1&gapro.accountid="+account+"&gapro.trackstarts=true",
                        config:"config.html%3Fid%3D"+idvideo+"%26query%3D"+duracao+"%26divisao%3D"+zone_site+"%26player%3D"+player_video
                    };

                    var params = {
                        wmode:"transparent",
                        allowscriptaccess:"always",
                        allowfullscreen:"true"
                    };

                    var attributes = {
                        id:"TVI24playerDirecto",
                        name:"TVI24playerDirecto"
                    };
                    swfobject.embedSWF("/flashplayers/player_5_4.swf", "placeplayerDirecto", "640", "480", "9", false, flashvars, params, attributes);
            },


            // Define the error method.
            error: function( objAJAXRequest, strError ){
               alert ("Error! Type: " +  strError );
            }
        });
    });

}
TVI24createPlayer();
