function openURL() {
	document.location.href=globalURL;
}
function setLiveMessage (string) {
}
function stopTrigger() {
	window.clearTimeout(triggerStatusTimeout);
}
function hideAll() {
	$('#online').hide();
	$('#offline').hide();
	$('#switch').hide();
	$('#switchON').hide();
	$('#switchOFF').show();
	$('#liveMessage').hide();
	$('#java').hide();
}
function show (URL) {
  hideAll();
  globalURL = URL;
  window.setTimeout("openURL()",2000); 
}
function startProgram(){
	hideAll();
	$('#switch').hide();
	$('#switchON').hide();
	$('#switchOFF').show();
  triggerStatus ();
}
function stopProgram(){
  stopTrigger();
  $('#online').hide();
	hideAll();
	$('#switch').show();
	$('#switchON').show();
	$('#switchOFF').hide();
	if (online) {
		// stopItheora();
	}
}

function triggerStatus () {
    getOnlineStatus();
    triggerStatusTimeout = window.setTimeout("triggerStatus()",10000); 
}
function getMetaData(programId) {
  try {
    $.ajax({
      type: "GET",
      url: "./ajax_program.php",
      data: "action=get&program=" + programId,
      dataType: "json",
      success: function(json){
        if (json.error.status == false ) {
          setMetaData(json.data);
        } else {
          var newMessage = json.error.message;
          if (newMessage != liveMessage) {
            liveMessage = newMessage;
            setLiveMessage(liveMessage);
          }
        }
      },
      error:  function(msg){
        setLiveMessage(msg);
      }
    });  
   } catch (error) {
    message = "ERROR " + error;
    setLiveMessage(message);
  }
}
function setMetaData(metaData) {
  $('#programName').html(metaData.container_name);
  $('#programTitle').html(metaData.program_title);
  $('#programDescription').html(metaData.program_i18n_description);
}
function getOnlineStatus () {
  try {
    $.ajax({
      type: "GET",
      url: "./ajax_status.php",
      data: "action=get_status&mountpoint_id=4",
      dataType: "json",
      success: function(json){
        if (json.error.status == false ) {
          if (json.data.on_air == 1) {
  					online = true;
            $("#cortado").attr('width', json.data.width);
            $("#cortado").attr('height', json.data.heigth);
            $("#offline").hide();
            $("#online").show("slow");
            if (json.data.metadata_id != "") {
              getMetaData(json.data.metadata_id);
            }
          } else if (json.data.on_air == 0) {
  					online = false;
            $("#online").hide();
            $("#offline").show("slow");
          } else {
            // alert('undefined');
          }
        } else {
          setLiveMessage(json.error.message);
        }
      },
      error:  function(msg){
        setLiveMessage(msg);
      }
    });
  } catch(error) {
      setLiveMessage(error);
  }
}
function toggleFileInfo () {
	$('#fileInfo').toggle();
}
function cortadoPlay() {
  document.applets[0].doPlay();
}
function cortadoStop() {
  document.applets[0].doStop();
}
function cortadoRestart() {
  if (document.applets[0]) {
  document.applets[0].restart();
  }
}    
$(document).ready(function(){
  $('#viewWithVlc').cluetip(
     {activation: 'click', 
      sticky: true, 
      width: 350, 
      closeText: '<img src="./img/cross.png" alt="close" />',
      closePosition: 'title', 
      cluezIndex: 10000,
      positionBy: 'fixed',
      leftOffset: 300, 
      topOffset: -300 });
  $('#helpOnline').cluetip(
     {activation: 'click', 
      sticky: true, 
      width: 350, 
      closeText: '<img src="./img/cross.png" alt="close" />',
      closePosition: 'title', 
      cluezIndex: 10000,
      positionBy: 'fixed',
      leftOffset: 250, 
      topOffset: -300 });
  $('#helpOffline').cluetip(
     {activation: 'click', 
      sticky: true, 
      width: 350, 
      closeText: '<img src="./img/cross.png" alt="close" />',
      closePosition: 'title', 
      cluezIndex: 10000,
      positionBy: 'fixed',
      leftOffset: 150, 
      topOffset: -300 });
});

