// Global 
var browser=navigator.appName
var b_version=navigator.appVersion
var version=parseFloat(b_version)
var aspectRatio="default";
function insertVLC() {
  if (browser=="Microsoft Internet Explorer") {
    document.write('<object classid="clsid:9BE31822-FDAD-461B-AD51-BE1D1C159921" codebase="http://downloads.videolan.org/pub/videolan/vlc/latest/win32/axvlc.cab#Version=0,8,6,0" width="500" height="300" id="tv" name="tv" events="True">');
  } else {
    document.write('<object type="application/x-vlc-plugin" id="tv" name="tv" height="300" width="500" target="" progid="VideoLAN.VLCPlugin.2">');
  }
  document.write('<param name="Src" value="" />');
  document.write('<param name="MRL" value="" />');
  document.write('<param name="ShowDisplay" value="True" />');
  document.write('<param name="AutoLoop" value="False" />');
  document.write('<param name="AutoPlay" value="False" />');
  document.write('</object>');
}
//Returns a reference to the instance of the VLC plugin
function getVLC(name) {
  if (window.document[name]){
    return window.document[name];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1) {
    if (document.embeds && document.embeds[name])
      return document.embeds[name]; 
  } else {
    return document.getElementById(name);
  }
}
//Retrieves the filename from the actual file input and sends it to the fake one
function updateFilename(newfilename) {
	document.getElementById('filename').value=newfilename;
	var lastslashposition=-1
	if(newfilename.indexOf('\\')==-1) {
  	lastslashposition=newfilename.lastIndexOf('/')+1;
	} else {
  	lastslashposition=newfilename.lastIndexOf('\\')+1;
	}
	var lastdotposition=newfilename.lastIndexOf('.');
	var trimmedfilename=newfilename.substring(lastslashposition,lastdotposition);
	document.getElementById('displayfilename').innerHTML=trimmedfilename;
}
//Catches all actions and sends them to the right place
function monkey(action) {
  var tv=getVLC("tv");
  if(tv) {
    switch(action) {
      case 'fullscreen'   :tv.video.toggleFullscreen(); break;
      case 'volume_up'    :tv.audio.volume += 10;break;
      case 'volume_down'  :tv.audio.volume -= 10;break;
      case 'mute'         :tv.audio.toggleMute();break;
      default             :sendAction(action);break;
    }
    document.getElementById("volume_mask").style.width=tv.audio.volume+"%";
  }
}
//Sets Aspect Ratio
function aspect(ratio) {
  var tv = getVLC("tv");
  if(tv) {
    if( tv.input.state && tv.input.hasVout ) {
        tv.video.aspectRatio = ratio;
    }
    aspectRatio = ratio;
  }
};
//Clears Playlist object, adds new file, prepares to play
function queue(targetURL) {
  var tv = getVLC("tv");
  var options = new Array(":aspect-ratio="+aspectRatio);
  if(tv) {
    tv.playlist.items.clear();
    while( tv.playlist.items.count > 0 ) {
    }
    var itemId = tv.playlist.add(targetURL, null, options);
    if( itemId != -1 ) {
        tv.log.verbosity = 1;
        tv.log.messages.clear();
    } else {
        tv.log.verbosity = -1;
        alert("cannot play at the moment !");
    }
    tv.playlist.stop();
  }
}  
//Processes actions received from chat connection
function handleReceiveAction(action) {
  var tv=getVLC("tv");
  if(tv) {
    switch(action) {
      case 'play'    :tv.playlist.play();break;
      case 'pause'   :if( tv.playlist.isPlaying ) {tv.playlist.togglePause();};break;
      case 'stop'    :tv.playlist.stop();break;
      case 'back1'   :tv.input.time=tv.input.time-60000;break;
      case 'fore1'   :tv.input.time=tv.input.time+60000;break;
      case 'back5'   :tv.input.time=tv.input.time-300000;break;
      case 'fore5'   :tv.input.time=tv.input.time+300000;break;
    }
  }
}
function playBeep() {
  var tv=getVLC("tv");
  if(tv) {
    if(tv.playlist.isPlaying) {
      if(tv.video.fullscreen) {
        var audiobox=document.getElementById('audiobox');
        if(audiobox.checked) {
          var audionotification=document.getElementById('audionotification');
          audionotification.innerHTML='<object type=\'audio/x-wav\' data=\'/blip3.wav\' width=\'1\' height=\'1\'><param name=\'src\' value=\'/blip3.wav\'><param name=\'autoplay\' value=\'true\'><param name=\'autoStart\' value=\'1\'></object>';
        }
      }
    }
  }
}
function sendInvite(invitee,inviter,roomname,roomtitle) {
  var timestamp = new Date().getTime();
  var inviteReq = getXmlHttpRequestObject();
  if (inviteReq) {
  	if (inviteReq.readyState == 4 || inviteReq.readyState == 0) {
  		inviteReq.open("GET", "/includes/php/sendinvite.php?invitee=" + invitee.value + "&inviter=" + inviter.value + "&roomname=" + roomname + "&roomtitle=" + roomtitle + "&nocache=" + timestamp);
  		inviteReq.onreadystatechange = doneInvite; 
  		inviteReq.send(null);
  	}			
	}
}
function toggleInvite() {
  var inviteform=document.getElementById('inviteform');
  if(inviteform.style.display=='block') {
    hideInvite();
  } else {
    showInvite();
  }
}
function showInvite() {
  var inviteform=document.getElementById('inviteform');
  inviteform.style.display='block';
}
function hideInvite() {
  var inviteform=document.getElementById('inviteform');
  inviteform.style.display='none';
}
function doneInvite() {
  var inviteformdone=document.getElementById('inviteformdone');
  inviteformdone.style.display='block';
  var invitee=document.getElementById('invitee');
  inviteformdone.value='';
}
function notDoneInvite() {
  var inviteformdone=document.getElementById('inviteformdone');
  inviteformdone.style.display='none';
}
function toggleRoomTitle() {
  var inviteform=document.getElementById('roomtitle');
  if(inviteform.style.display=='block') {
    hideRoomTitle();
  } else {
    showRoomTitle();
  }
}
function showRoomTitle() {
  var inviteform=document.getElementById('roomtitle');
  inviteform.style.display='block';
}
function hideRoomTitle() {
  var inviteform=document.getElementById('roomtitle');
  inviteform.style.display='none';
}
var readyToGo=false;
function parentCall(){
  readyToGo=true;
  // var win = window.parent; 
  // if(win) {
  //   alert('win1');
  //   alert(win.windowtype);
  // }
  // if(!win || win == window) {
  //   win = window.opener; 
  //   alert('win==window');
  // }
//return win.document.getElementById('tree-id');
//  win.alertTest(); 
  // top.convert(document.getElementById('filename').value);
};