// Change this to make the trial optional or compulsory.
var requiresConfirm = true;

var confirmUrl = "confirm.asp";
var contentUrl = "content.asp";
var controlUrl = "control.asp";

var g_fIsSP2 = (window.navigator.userAgent.indexOf("SV1") != -1);

var dialogue = requiresConfirm && getVariable("participant", "participant", null) == null;
var participant = !requiresConfirm || getVariable("participant", "participant", null) == "yes";

var bHasLoadedRawflow = false;
var sInitialChannelId = "";
var iInitialBitrate = 0;
var iPort = 0;


function isRawflowEnabled(port) {
  return is.ie && !isMac && port != 0;
}

function doInitialCheck() {
  // An awful duplication of code.
  sChannelId = getVariable("netradio_channel", "kanal", sDefaultChannelId);
  var sAlternativeChannelId = oldChannelId();
  if(sAlternativeChannelId != null && sAlternativeChannelId.length > 0) {
    sChannelId = sAlternativeChannelId;
  }
  iBitrate = getVariable("netradio_bitrate", "kbps", iDefaultBitrate);

  // Display the confirm dialogue if necessary.
  if(isRawflowEnabled(getChannelPort()) && dialogue) {
    location.replace(confirmUrl + location.search);
  }

  sInitialChannelId = sChannelId;
  iInitialBitrate = iBitrate;
}

doInitialCheck();

function isParticipant() {
  if(dialogue) {
    window.onbeforeunload = null;
    location.href = confirmUrl + "?kanal=" + sChannelId;
  }
  return participant;
}

function playRawflowChannel() {
  var port = getChannelPort();

  if(is.ie && !isMac) {
    oPlayer.stop();
  }

  if(!isRawflowEnabled(port) || !isParticipant()) {
    cancelRawflow();
    oPlayer.open(getChannelUrl());
  } else {
    setBufferingText("Vent venligst");
    if(g_fIsSP2 && !bHasLoadedRawflow && sChannelId != sInitialChannelId) {
      // Refresh the page under XP SP2 so that the control will install properly after the Information Bar blocks it.
      window.onbeforeunload = null;
      window.location.href = contentUrl + "?kanal=" + sChannelId;
    } else {
	  top.frames["rawflowframe"].location.href = controlUrl + "?channelId=" + sChannelId + "&bitrate=" + iBitrate + "&port=" + port;
      bHasLoadedRawflow = true;
      return true;
    }
  }
  return false;
}

function _reloadChannel() {
  playChannel(sChannelId);
}

function cancelRawflow() {
	top.frames["rawflowframe"].location.href = "about:blank";
}

function _controlLoaded() {
  setBufferingText("Åbner kanal");
}

function _callback(worked) {
  if(!worked) {
    if(g_fIsSP2) {
      // Make sure the on close dialogue is turned off under XP SP2 if the control is blocked,
      // because it might have been the Information Bar that blocked it, and the user must be
      // given the opportunity to accept the control and refresh the page without being annoyed.
      window.onbeforeunload = null;
    } else {
      // Don't delete the cookie under XP SP2 because if we do, the information dialogue will
      // appear again if the user asks to install the control through the Information Bar.
      deleteCookie("participant");
    }

    participant = false;
    playRawflowChannel();
  }
  removeBufferingText();
  updateChannelDetails();
}
