/**
 * This file contains javascript functionality for the 'Delta Orionis' theme.
 * 
 * created: 01. Jul 2007
 *      by: Johannes Ruthenberg
 * encoding: UTF-8 [Proof: Ω]
 */

/**
 * Ensure that jQuery plays nicely with prototype and other libraries.
 * Use '$jq' instead of '$'!
 */
var $jq = jQuery.noConflict();

/**
 * The following is jQuery functionality that will be executed as soon as the
 * DOM is ready (roughly equal to onload).
 */
jQuery(function($) {
  // remove spam protection on mailto links
  removeSpamProtection();
  // round the corners of the info and disclaimer box
  makeRoundCorners();
  // make sidebar elements hideable
  toggleSidebarElements();
  // add the dead link icons
  addDeadlinkIcon();
});


/**
 * Mail address are entered as my [punkt] name &#38; domain [punkt] tld for
 * spam protection reasons. This function removes these modifications for the
 * benefit of real visitors.
 */
function removeSpamProtection() {
  // get all mailto links
  $jq("a.limailto").each(function() {
    this.firstChild.data = this.firstChild.data.replace(/%20/g, " ").replace(/ @ /g, "@").replace(/ \[punkt\] /g, ".");
    this.href = this.href.replace(/%20/g, " ").replace(/ @ /g, "@").replace(/ \[punkt\] /g, ".");
  });
}


/**
 * Adds the rounded corners to the info and disclaimer box.
 */
function makeRoundCorners() {
  $jq('.infobox_inner').corner({
    tl: {radius: 12},
    tr: false,
    bl: false,
    br: {radius: 12},
    antiAlias: true,
    autoPad: true});
  $jq(".infobox_tab_bar").each(function() {
    $jq(this).css({marginTop: "11px"});
  });
  $jq(".infobox_inner").each(function() {
    var marginTop = $jq(this).css("marginTop");
    marginTop = (parseInt(marginTop) + 12) + "px";
    $jq(this).css({marginTop: marginTop});
    var marginBottom = $jq(this).css("marginBottom");
    marginBottom = (parseInt(marginBottom) + 12) + "px";
    $jq(this).css({marginBottom: marginBottom});
  });
  $jq('.disclaimer_inner').corner({
    tl: {radius: 12},
    tr: false,
    bl: false,
    br: {radius: 12},
    antiAlias: true,
    autoPad: true});
  $jq(".disclaimer_inner").each(function() {
    var marginTop = $jq(this).css("marginTop");
    marginTop = (parseInt(marginTop) + 12) + "px";
    $jq(this).css({marginTop: marginTop});
    var marginBottom = $jq(this).css("marginBottom");
    marginBottom = (parseInt(marginBottom) + 12) + "px";
    $jq(this).css({marginBottom: marginBottom});
  });
}


/**
 * Controls the hide-and-show effect on the sidebar elements.
 */
function toggleSidebarElements() {
  // add the icons
  $jq(".sidebar ul h3").each(function() {
    $jq(this).prepend('<img src="' + dor_template_path + 'img/icon_dreieck_up.png" width="12" height="8" alt="" title="Element einklappen" class="toggleIcon" />');
  });
  
  // add the hover and click effects
  $jq(".sidebar h3 .toggleIcon").each(function() {
    // add the hover effect (icon changing)
    $jq(this).hover(function(){
      if (this.src == dor_template_path + 'img/icon_dreieck_up.png') {
        this.src = dor_template_path + 'img/icon_dreieck_up_hover.png';
      } else {
        this.src = dor_template_path + 'img/icon_dreieck_down_hover.png';
      }
    }, function(){
      if (this.src == dor_template_path + 'img/icon_dreieck_up_hover.png') {
        this.src = dor_template_path + 'img/icon_dreieck_up.png';
      } else {
        this.src = dor_template_path + 'img/icon_dreieck_down.png';
      }
    });
    
    // add the click effect
    $jq(this).click(function() {
      // hide or show all lists (ul) in the same parent li node as the h3 element
      var parentLiNode = this.parentNode.parentNode;
      // fix for recent posts
      if (parentLiNode.className == 'tile_header') {
        parentLiNode = parentLiNode.nextSibling; // = div class="tile_body"
      }
      $jq(parentLiNode).children('ul').toggle();
      // hide or show all lists (ol) in the same parent li node as the h3 element
      $jq(parentLiNode).children('ol').toggle();
      // hide or show all forms in the same parent li node as the h3 element
      $jq(parentLiNode).children('form').toggle();
      // hide or show the tag cloud in the same parent li node as the h3 element
      $jq(parentLiNode).children('.st-tag-cloud').toggle();
      // flip the icon
      if (this.src == dor_template_path + 'img/icon_dreieck_up_hover.png') {
        this.src = dor_template_path + 'img/icon_dreieck_down_hover.png';
        this.title = 'Element ausklappen';
      } else {
        this.src = dor_template_path + 'img/icon_dreieck_up_hover.png';
        this.title = 'Element einklappen';
      }
    });
  });
}


/**
 * Inits an event handler so that pressing the escape key will close the
 * current window. Call this in the onload handler of a popup window.
 */
function initEscapeKey() {
  document.onkeypress = function esc(e) {  
    if (typeof(e) == "undefined") {
      e = event;
    }
    if (e.keyCode == 27) {
      self.close();
    }
  }
}


/**
 * Adds a 'report dead link' icon on each link list entry.
 */
function addDeadlinkIcon() {
  // get all link list entries and add the icon
  $jq("ul.linklist li ul li").each(doAddDeadLinkIcon);
  $jq(".link-summarizer-loop ul li").each(doAddDeadLinkIcon);
  // add the hover handler
  $jq(".deadlinkicon img").hover(function(){
    this.src = dor_template_path + 'img/deadlink.png';
  }, function(){
    this.src = dor_template_path + 'img/deadlink_fade.png';
  });
}


/**
 * Worker function for adding a deadlink icon to a link in a list entry.
 */
function doAddDeadLinkIcon() {
  var linkUri = $jq(this).children('a')[0].getAttribute('href');
  if ((linkUri != null) && (linkUri.length > 0) && (linkUri.substring(0, 1) != "#")) {
    var toInsert = '<a href="http://johannes-ruthenberg.de/kontakt?wpcf_msg=' + encodeURIComponent('Bitte überprüfe den Link "' + linkUri + '" auf der Seite "' + window.location.href + '".') + '" class="liimagelink deadlinkicon">'
      + '<img src="' + dor_template_path + 'img/deadlink_fade.png" alt="" title="einen Link melden"'
      + ' width="15" height="12" class="deadlink"></a>';
    $jq(this).append(toInsert);
  }
}
