/*
*
* CopyRight Giacomo Lo Giusto giacomolg@gmail.com
* Mon 24 Mar 2008 16:59:05 
*
*/

MediaPlayer = function()
{
  this.so;// = new SWFObject();
  this.conf;
}
MediaPlayer.prototype.write = function ()
{
    this.so.write(this.conf['htmlid']);
}
MediaPlayer.prototype.configure = function (conf)
{

  this.conf = (conf  instanceof Object)?conf:{};
  var def = {
      htmlid: '',
      javascriptid: 'video_player',
      playersrc: 'mediaplayer.swf',
      repeat:'list',//'list'
      autostart: true,
      shuffle:false,
      width:'320',
      height:'240',
      displayheight:'222',
      src: false,
      frontcolor: '0x000000',
      backcolor: '0x0C0C0C',
      backgroundcolor: '0xFFFFFF',
      lightcolor: '0xFFFFFF',
      autoscroll: false,
      showeq: false,
      enablejs: true,
      allowfullscreen: true,
      allowscriptaccess: true,
      screencolor: '0x000000',
      callback: null,
      id: null
    };
    
    // fill in the missing user param with default values
    for (k in def)
      if ((typeof(this.conf[k]) != typeof(def[k])) && def[k] != null) 	this.conf[k] = def[k];
  
    this.so = new SWFObject(this.conf['playersrc'],this.conf['javascriptid'],this.conf['width'], this.conf['height'], '8');
  	
    this.so.addParam('allowfullscreen',(this.conf['allowfullscreen']?'true':'false'));
    this.so.addParam('allowscriptaccess',(this.conf['allowscriptaccess']?'always':'false'));
    
    this.so.addVariable('height',this.conf['height']);
    this.so.addVariable('width',this.conf['width']);
    this.so.addVariable('javascriptid',this.conf['javascriptid']);
    this.so.addVariable('displayheight',this.conf['displayheight']);
    this.so.addVariable('frontcolor',this.conf['frontcolor']);
    this.so.addVariable('backcolor',this.conf['backcolor']);
    this.so.addVariable('lightcolor',this.conf['lightcolor']);
    this.so.addVariable('screencolor',this.conf['screencolor']);
    this.so.addVariable('autostart',(this.conf['autostart']?'true':'false'));
    this.so.addVariable('showeq',(this.conf['showeq']?'true':'false'));
    this.so.addVariable('enablejs',(this.conf['enablejs']?'true':'false'));
    this.so.addVariable('shuffle',(this.conf['shuffle']?'true':'false'));
    this.so.addVariable('repeat',this.conf['repeat']);
    if(this.conf['callback']) this.so.addVariable('callback',this.conf['callback']);
    if(this.conf['id']) this.so.addVariable('id',this.conf['id']);
    if(this.conf['src']) this.so.addVariable('file',this.conf['src']);
  
   return this;
}

MediaPlayer.prototype.removeItem = function (position)
{
    var p = this.getId(this.conf.javascriptid);
    
    if(p) p.removeItem(position?position:this.track.info.position);
    else throw "No id for the player was specified!";
}

MediaPlayer.prototype.play = function(file_obj)
{
	var p = this.getId(this.conf.javascriptid);

  if(p) {
  	var html = this.getId(this.conf.htmlid)
    if(html.style.visibility == "hidden") html.style.visibility = "visible";
    var file2load = {
  										file:file_obj['file']?file_obj['file']:this.conf.src,
  										title : file_obj.title,
  										id: file_obj['id'].toString(),
                      image: file_obj['image']
  									};
    
    //html.innerHTML += "<p>"+file2load["title"]+"</p>";
    p.loadFile(file2load);
  }
  else alert("No id for the player was found!");
}

MediaPlayer.prototype.getSrc = function()
{
	return this.conf.src;
}

MediaPlayer.prototype.track_to_remove = function()
{
	return this.track.is_playlist && this.track.info.position != null && this.track.to_remove;
}

MediaPlayer.prototype.getId = function(id)
{
  if(navigator.appName.indexOf("Microsoft") != -1) {
    return window[id];
  } else {
    return document.getElementById(id);
  }
}

MediaPlayer.prototype.is_new_track = function()
{
	return this.track.info.id != null && this.track.info.id == this.track.current;
}
