﻿// ----------------------------------------------------------------------------------------
//
//	Player v1.0
//	by Mark Riethmuller - http://www.ipinternet.com.au
//	Last Modification: 13/08/08
//      Description: Player functions and initialisation
//
// ----------------------------------------------------------------------------------------
 var $j = jQuery.noConflict();
 var section = 'Now Playing';
 if (typeof(pageid) == 'undefined') var pageid = 0; 
 if (typeof(timeid) == 'undefined') var timeid = 0; 
 if (typeof(autoplay) == 'undefined') var autoplay = 0;
 if (pageid > 1) autoplay = 1

//Wait till DOM is ready
$j(function() {

  //RUN INIT FUNCTIONS
  //*************************************
  loadProgram();  
  loadNowPlaying();
  loadPlayerTitle();
  setPlayerHTML(getPlayerHTML($j("#hdnSelectedPodcast").val(), autoplay));

  //*************************************
  
  // BIND FUNCTIONALITY TO CONTROLS 
  //*************************************
  $j("#mpNavNowPlaying").bind("click", function(e){
    loadNowPlaying();
  });  

  $j("#mpNavGallery").bind("click", function(e){
    //Retrieve the Product HTML
    //alert($j("#hdnShowPageID").val());
    $j.ajax({
      url: "/pages/ShowGallery.aspx",
      type: "POST",
      data: "PageID=" + $j("#hdnShowPageID").val(),
      async: false,
      cache: false,
      success: function(html){
        section = 'Gallery';
        $j("#featurePanel").html(html);
         fixPNGs();
      }
    })
  });
  
  $j("#mpNavAbout").bind("click", function(e){
    //Retrieve the Product HTML
    //alert("PageID=" + $j("#hdnShowPageID").val() + "&ShowID=" + $j("#hdnShowID").val());
    $j.ajax({
      url: "/pages/ShowAbout.aspx",
      type: "POST",
      data: "PageID=" + $j("#hdnShowPageID").val() + "&ShowID=" + $j("#hdnShowID").val(),
      async: false,
      cache: false,
      success: function(html){
        section = 'About';
        $j("#featurePanel").html(html);
         fixPNGs();
      }
    })
  });

  $j("#mpNavContact").bind("click", function(e){
    //Retrieve the Product HTML
    //alert($("#hdnSelectedShow").val());
    section = 'Contact';
    var html = '<iframe src="/pages/ShowContact.aspx?PageName=' + $j("#hdnShowName").val() + '" id="ContactForm" name="ContactForm" scrolling="no"></iframe>'
    $j("#featurePanel").html(html);
  });  
  
  $j("#mpNavPodcasts").bind("click", function(e){
    //Retrieve the Product HTML
    $j.ajax({
      url: "/pages/ShowPodcasts.aspx",
      type: "POST",
      data: "PageID=" + $j("#hdnShowPageID").val(),
      async: false,
      cache: false,
      success: function(html){
        $j("#featurePanel").html(html);
         fixPNGs();        
      }
    })
  });
  
  //*************************************
});  

//Functions
//*************************************

function loadPlayerTitle(){  
  $j("#playerTitle > h1").html($j("#hdnShowName").val());
  $j("#playerTitle > h2").html($j("#hdnShowTime").val());
}

function loadProgram(){  
  $j.ajax({
    url: "/pages/PlayerProgram.aspx",
    data: "pid=" + pageid + "&tid=" + timeid,
    async: false,
    cache: false,
    success: function(html){
      $j("#program").html(html);
      window.setTimeout(function() {
        loadProgram();
        loadPlayerTitle();          
        if (section = 'Now Playing') loadNowPlaying() 
      }, $j("#hdnNextRefresh").val());  
    }
  });  
}

function loadNowPlaying(){
  $j.ajax({
    url: "/pages/NowPlaying.aspx",
    type: "POST",
    data: "PageID=" + $j("#hdnShowPageID").val(),    
    async: false,
    cache: false,
    success: function(html){
      section = 'Now Playing';
      $j("#featurePanel").html(html);
       fixPNGs();
    },
    error: function(e) { alert("Failure: " + e); }
  });
}
//*************************************
    
    
function getPlayerHTML(file, autoStart)
{
  if(-1 != navigator.userAgent.indexOf("MSIE"))
  {
    return '<OBJECT ID="mediaplayer" class="ieUpdateExclude" classid="clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6" width="628" height="64">'+
    '<param id="playerURL"  name="url" value="' + file + '">\n'+
    '<param name="AutoStart" value="' + (autoStart==1) + '">\n'+
    '<param name="type" value="application/x-ms-wmp">\n'+
    '<param name="ShowControls" value="true">\n'+
    '<param name="showstatusbar" value="true">\n'+
    '<param name="uiMode" value="full">\n'+
    '</OBJECT>'
  }
  else if(-1 != navigator.userAgent.indexOf("Firefox"))
  {
    return '<object type="application/x-ms-wmp" id="mediaplayer" class="ieUpdateExclude" width="628" height="64">'+
    '<param id="playerURL"  name="URL" value="' + file + '">'+
    '<param name="AutoStart" value="' + (autoStart==1) + '">'+
    '<param name="ShowTracker" value="true">'+
    '<param name="ShowControls" value="true">'+
    '<param name="ShowGotoBar" value="false">'+
    '<param name="ShowDisplay" value="false">'+
    '<param name="ShowStatusBar" value="false">'+
    '<param name="AutoSize" value="false">'+
    '<param name="StretchToFit" value="true">'+
//  '<param name="windowlessVideo" value="-1">'+
    '<a href="https://addons.mozilla.org/en-US/firefox/browse/type:7">Get the plugin Media Player for Firefox Plugin. (Plugin can be found at the bottom of the Plugins page)</a> <!--MSIE workaround-->'+
    '</object>';
  }         
}
    
function setPlayerHTML(html) 
{
  document.getElementById('MediaPlayer').innerHTML = html;
}
    
function playPodcast(file) 
{
  if (file != '') document.getElementById('MediaPlayer').innerHTML = getPlayerHTML(file, 1);
}

function stopPlayer() 
{    
  if (document.getElementById('playerURL')) {
    var url = document.getElementById('playerURL').getAttribute('value')
    document.getElementById('MediaPlayer').innerHTML = getPlayerHTML(url, 0);
  }
}
    
function getMiniPlayerHTML(file, autoStart)
{
  if(-1 != navigator.userAgent.indexOf("MSIE"))
  {
    return '<OBJECT ID="mediaplayer" class="ieUpdateExclude" classid="clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6" width="250" height="64">'+    
           '<param name="url" value="' + file + '">\n'+
           '<param name="AutoStart" value="' + (autoStart==1) + '">\n'+
           '<param name="type" value="application/x-ms-wmp">\n'+
           '<param name="ShowControls" value="true">\n'+
           '<param name="ShowStatusBar" value="false">\n'+
           '<param name="uiMode" value="mini">\n'+
           '</OBJECT>'
  }
  else if(-1 != navigator.userAgent.indexOf("Firefox"))
  {
    return '<object type="application/x-ms-wmp" id="mediaplayer" class="ieUpdateExclude" width="580" height="64">'+
    '<param name="URL" value="' + file + '">'+
    '<param name="AutoStart" value="false">'+
    '<param name="ShowTracker" value="true">'+
    '<param name="ShowControls" value="true">'+
    '<param name="ShowGotoBar" value="false">'+
    '<param name="ShowDisplay" value="false">'+
    '<param name="ShowStatusBar" value="false">'+
    '<param name="AutoSize" value="false">'+
    '<param name="StretchToFit" value="true">'+
    '<a href="https://addons.mozilla.org/en-US/firefox/browse/type:7">Get the plugin Media Player for Firefox Plugin. (Plugin can be found at the bottom of the Plugins page)</a> <!--MSIE workaround-->'+
    '</object>';
  }         
}    
    
function showImage(file) 
{ 
      var img = new Image();
      $j('#galleryImageHolder').html('<div class="loading">Loading...</div>');
  
      // wrap our new image in jQuery, then:
      $j(img)
        // once the image has loaded, execute this code
        .load(function () {
          // set the image hidden by default    
         $j(this).hide();
    
          // with the holding div #loader, apply:
          $j('#galleryImageHolder')
            // then insert our image
            .html(this);
    
          // fade our image in to create a nice effect
          $j(this).fadeIn();
      })
    
      // if there was an error loading the image, react accordingly
      .error(function () {
        // notify the user that the image could not be loaded
      })
    
      // *finally*, set the src attribute of the new image to our image
      .attr('src', file).attr('alt', file).attr('title', file);
}
    