
var sync_interval = 3;  // Actualize each 3 seconds
var sync_position = - sync_interval;  // Start position

var xml_path = 'http://sincro.ikunamedia.com/gobierno_en_linea/20091214/xmls/config..xml';
var media_path = 'http://sincro.ikunamedia.com/gobierno_en_linea/20091214/player/';

var player_width = 320;
var player_height = 260;

var on_demand = true;

var id_event = 4;
var id_lang = 2;

sync_offsets = new Array ();

sync_offsets [0] = {  duration: 9697   };
sync_offsets [1] = {  duration: 4935   };


for (i = 0; i < sync_offsets.length; ++ i) sync_offsets [i].offset = i ? (sync_offsets [i - 1].duration + sync_offsets [i - 1].offset) : 0; //este bucle calcula el offset de cada video a partir de su duración


// *****
//
//  Class syncronizer
//
// *****

function syncronizer (self_name)
{


    // ***  Class variables  ***
    this.my_name = self_name;  // Self reference
    this.step = 0;  // Corresponds to id_timeline in DB


    // ***  Ajax call to update contents  ***
    this.update = function (timeline)
    {
                
        if (!timeline) timeline = 0;  // This is used on-demand but not in live

        var sync_params =
        {
            id_event: id_event,
            id_lang: id_lang,
            step: this.step,
            timeline: timeline,
            rnd: Math.random ()
        }

        var this_ = eval (this.my_name);
        $.get ('_sync.php', sync_params, function (data)  // Ajax call
        {

            var result;
            try {  eval (data);  } catch (err) {  alert (err.description);  }

            if (!result.step  ||  this_.step == result.step) return;
            else
            {

                this_.step = result.step;
                this_.paint (result);

            }
        });  // $.get
    }  // this.update = function (timeline)

    
    // ***  Paint results of ajax call  ***
    this.paint = function (result)
    {

        for (key in result)  // Itering results
        {

            // Getting value and painting it
            var val = eval ('result.' + key);
            $('#el_' + key).html (val);

            // IE Patch when loading images
            val = String (val);
            val = val.split ('src="');
            if (val.length > 1)
            {

                val = val [1].split ('"');
                var img = new Image ();
                img.src = val [0];

            }  // if (val.length > 1)
        }  // for (key in result)
    }  // this.paint = function (result)


}  // function syncronizer ()



// *****
//
//  Class flash_player
//
// *****

function flash_player (self_name)
{


    // ***  Class variables  ***
    this.my_name = self_name;  // Self reference

    this.player = null;  // Player object
    this.player_id = null;  // Player ID


    // ***  Embedding player in page  ***
    this.embed = function (player, player_id, width, height, xml_path, installer, autoplug)
    {

        var flashvars =
        {
            path: xml_path,
            flashid: player_id
        }
//        if (vars) for (key in vars) eval ('flashvars.' + key + ' = ' + 

        var params =
        {
            allowscriptaccess: 'always',
            allowfullscreen: true,
            wmode: 'opaque',
            id: player_id
        };

        swfobject.embedSWF (player, player_id, width, height, '9.0.115', installer, flashvars, params, {});
        if (autoplug) this.plug (player_id);

    }  // this.embed = function (player, player_id, width, height, xml_path, installer, autoplug)


    // ***  Plug the class to a phisical player  ***
    this.plug = function (player_id)
    {

        this.player = document.getElementById (player_id);  // Trying to get flash player
        if (this.player  &&  this.player.addModelListener) this.player_id = player_id;
        else
        {

            this.player = null;
            if (this.my_name) setTimeout (this.my_name + '.plug ("' + player_id + '");', 250);
            return false;

        }
    }  // this.plug = function (player_id, update_interval)


    // ***  Seek the player to a specific item + second
    this.seek = function (item, position)
    {

        if (!this.player) return false;
        try {  this.player.seek (position, item);  }
        catch (err) {  alert ('Cannot jump to item ' + item + ', position ' + position);  }

    }  // this.seek = function (item, position)


    // ***  Add a listener to the player  ***
    this.addModelListener = function (event_name, callback_function)
    {

        if (!this.player) return false;
       // alert('event_name = ' + event_name + ' <---> callback_function = ' + callback_function); 
       try {  this.player.addModelListener (event_name, callback_function);  } catch (e) {  alert (e);  }

    }  // this.addModelListener = function (event_name, callback_function)

}



function sync_handler (data)
{
    
    if (isNaN (data.listIndex) || isNaN (data.position)) return;

    var item = data.listIndex;
    var position = parseInt (data.position);

    if (Math.abs (position - sync_position) < sync_interval) return;

    sync_position = position;
    var timeline = parseInt (sync_position + sync_offsets [item].offset);

    oSync.update (timeline);

/*

    var s = '';
    for (d in data) s += d + ': ' + eval('data.' + d) + "\n";
    s += 'realSeek: ' + realSeek + "\n";

    $('#el_message1').html (s);

*/

}  // function sync_handler (data)



var oSync = new syncronizer ('oSync');
var oPlayer = new flash_player ('oPlayer');

function player_time ()
{
   
	if (oPlayer  &&  oPlayer.player_id) oPlayer.addModelListener ('TIME', 'sync_handler');
	else setTimeout ('player_time();', 500);

}

$(document).ready(function(){

    oPlayer.embed (media_path + 'ikuna_player.swf', 'ikunaPlayer', player_width, player_height, xml_path, media_path + 'expressInstall.swf', on_demand);


});

function player_jump (flashid, time, playlistNum) {
  try {  oPlayer.seek (time, playlistNum);  } // Catching "TIME" event
  catch (err) {  alert ('Cannot jump to item ' + playlistNum);  }
}
