/*
Site: Landen auf Wasser
HTTP: www.landen-auf-wasser.de
Date: 2008-04-09
*/




/*
Audio Player - Initialize
*/
$(window).load(
  function() {
    var bodyClass = $("body").attr("class");
    if (bodyClass == "node flexinode-1") {
      law_audioPlayer_flashProxy_create();
      law_audioPlayer_remoteControl();
    }
  }
);




/*
Audio Player - Flash Proxy - Create
*/
function law_audioPlayer_flashProxy_create() {

  // CREATE NEW FLASH PROXY
  flashProxy = new FlashProxy(500, "/themes/landen_auf_wasser_advanced/swf/JavaScriptFlashGateway.swf");

};


/*
Audio Player - Post Tracklisting (XHTML) to Flash
Note: This function is called by the Flash object
*/
function law_audioPlayer() {

  // Get tracklisting
  var law_audioPlayer_tracklist = $("div.flexinode-textarea-4").find("div.form-item");

  // Access XHTML
  var law_audioPlayer_tracklist_html = law_audioPlayer_tracklist.html();

  // ========== Process XHTML - Start (by D.S.) ========== //

  // ...
  var children = law_audioPlayer_tracklist.find("li");

  // Check if Browser is IE
  var name = navigator.userAgent.toLowerCase();
  var ie = (name.indexOf("msie")>= 0);

  // Rebuild XHTML
  var output = "";

  output +="<ol>";

  for (i = 0 ; i < children.length; i++) {
    var linktext = $(children[i]).find("a").html();
    var link = $(children[i]).find("a").attr("href");
    var plaintext = $(children[i]).html();

    if (linktext != null) {
      // If not IE encode URI
      var lt = ie ? linktext : encodeURIComponent(linktext);
      output += "<li><a href='"+link+"'>"+lt+"</a>"+"</li>";
    }
    else output += "<li>"+encodeURIComponent($(children[i]).html())+"</li>";
  };

  output +="</ol>";

  // Replace Umlaute in IE
  if (ie) {
    output = output.replace(/\u00f6/g,"-oe-");
    output = output.replace(/\u00d6/g,"-Oe-");
    output = output.replace(/\u00fc/g,"-ue-");
    output = output.replace(/\u00dc/g,"-Ue-");
    output = output.replace(/\u00e4/g,"-ae-");
    output = output.replace(/\u00c4/g,"-Ae-");
    output = output.replace(/\u00df/g,"-ss-");
  };

  // ========== Process XHTML - End ========== //

  // Post XHTML to Flash
  flashProxy.call("postXML", output);

};


/*
Audio Player - Set Text below Audio Player (by D.S.)
*/
function player_set_text_and_num(t, t2) {

  player_set_tracknum(t);
  player_set_text(t2);

};

function player_set_text(t) {

  // Check if Browser is IE
  var name = navigator.userAgent.toLowerCase();
  var ie = (name.indexOf("msie")>= 0);

  // t = unescape(t);

  // Decode URI
  t = decodeURIComponent(t);

  t = t.replace(/-oe-/g,"\u00f6");
  t = t.replace(/-Oe-/g,"\u00d6");
  t = t.replace(/-ue-/g,"\u00fc");
  t = t.replace(/-Ue-/g,"\u00dc");
  t = t.replace(/-ae-/g,"\u00e4");
  t = t.replace(/-Ae-/g,"\u00c4");
  t = t.replace(/-ss-/g,"\u00df");

  t = t.replace(/<em>/g, "");
  t = t.replace(/<\/em>/g, "");
  t = t.replace(/<EM>/g, "");
  t = t.replace(/<\/EM>/g, "");

  // t = t.replace(/ö/g, "�");

  n = document.getElementById('playerText');

  while (n.hasChildNodes()) {
    n.removeChild(n.firstChild);
  };

  tn = document.createTextNode('');
  tn.data = t;

  n.appendChild(tn);

};

function player_set_tracknum(t) {

  n = document.getElementById('playerNum');

  while (n.hasChildNodes()) {
    n.removeChild(n.firstChild);
  };

  tn = document.createTextNode('');
  tn.data = t;

  n.appendChild(tn);

};


/*
Audio Player - Remote control via JS
*/
function law_audioPlayer_remoteControl() {

  // Get tracklisting
  var law_audioPlayer_remoteControl_link = $("div.flexinode-textarea-4 ol").find("a");

  // Put event on link
  law_audioPlayer_remoteControl_link.click(
    function() {
      var law_audioPlayer_remoteControl_link_url = $(this).attr("href");
      // Post URL to Flash
      flashProxy.call("postURL", law_audioPlayer_remoteControl_link_url);
      // Prevent page reload
      return false;
    }
  );

};


/*
Audio Player - Scroll Content (to Tracklisting)
*/
function law_audioPlayer_scrollContent() {

  // SCROLL TO TRACKLISTING
  var content_static_height = $("#animation").outerHeight() + $("#page-head").outerHeight();
  var content_scroll_position = $("div.flexinode-textarea-4").offset();
  var content_scroll_calculation = content_scroll_position.top - content_static_height + 5;

  //window.scrollTo(0, content_scroll_calculation);
   $.scrollTo(content_scroll_calculation, {duration:"slow"});
   //$("#page").animate({ height: 100 }, "slow");

};


/*
Audio Player - Call JavaScript from Flash at the first action
Note: This function is called by the Flash object at the first action
*/
function notifyFirstPlay() {

  law_audioPlayer_scrollContent();

};