/* ========================== */
/*  Neptun Wasserpreis 2010   */
/*  Behaviour JS              */
/* ========================== */


$(document).ready(function() {
  init_main_nav();
  init_voting();
  external_links();
});

/**
 * Convert rel="extern" in external links to target="_blank"
 */
function external_links() {
  $("a[rel='extern']").attr('target', '_blank');
    /*.click(function() {
      window.open($(this).attr('href'), $(this).attr('target'));
      return false;
    });*/
}


/**
 * Initializes the main navigation's dropdown menu
 */
function init_main_nav() {
  if (!$('#menu > ul').length) return;
  
  $("#menu > ul").superfish({
    hoverClass: "open",
    autoArrows: false,
    disableHI: true,
    delay: 500,
    onInit: function(){
      var listitems = $(this).children('li');
      listitems.each(function() {
        var li = $(this);
        // get the text of all <a> 
        var link = li.children('a');
        var text = link.text();
        // prepare image sources
        var nav_class = link.attr('class');
        if (nav_class != "") {
          var src = "/images/ui/nav_" + nav_class + ".png";
          var src_hover = "/images/ui/nav_hover_" + nav_class + ".png";
          // prelaod hover images
          (new Image()).src = src_hover;
          
          // remove text, replace image
          link.empty();
          link
            .append('<img src="'+(li.hasClass('current') ? src_hover : src)+'" alt="'+text+'" />')
            .children('img')
            .data({src: src, src_hover: src_hover});
        }
        
        if (!li.has('ul').length) {
          link.hover(function() { show_menu(li); }, function() { hide_menu(li); } );
          link.focus(function() { show_menu(li); });
          link.blur(function() { hide_menu(li); });
        }
      });
    },
    onBeforeShow: function(){
      var li = $(this).parent();
      show_menu(li);
    },
    onHide: function(){
      $(this).each(function() {
        var li = $(this).parent();
        hide_menu(li);
      });
    }
  });
}

function show_menu(li) {
  if (!li.hasClass('current')) {
    var img = li.find('a img');
    if (img.length) {
      img.attr('src', img.data('src_hover'));
    }
  }
}

function hide_menu(li) {
  if (!li.hasClass('current')) {
    var img = li.find('a img');
    if (img.length) {
      img.attr('src', img.data('src'));
    }
  }
}


/**
 * Initializes Shadowbox for Neptun voting
 */
function init_voting() {
  if (!$('#voting').length) return;
  
  var options = {
    handleOversize:     'resize',
    handleUnsupported:  'remove',
    autoplayMovies:     true,
    autoDimensions:     true,
    counterType:        'skip',
    continuous:         false,
    animSequence:       'sync',
    resizeDuration:     '0.35',
    fadeDuration:       '0.15',
    players:            ['flv', 'html', 'iframe', 'img']
  };

  Shadowbox.init(options);
  
  // preset green highlight for selected contributions
  $('#voting input:checked').parents('div.contrib').addClass('checked');
  
  // on selecting a contribution...
  $('#voting input.vote').click(function() {
    // if more than 3 are selected, raise an alert
    if ($('#voting input.vote:checked').length > 3)
    {
      $(this).attr("checked", false);
      alert('Sie haben bereits drei Beiträge ausgewählt!');
    }
    
    // add or remove green highlight on selecting or deselecting a contribution
    if ($(this).attr("checked"))
    {
      $(this).parents("div.contrib").addClass("checked");
    }
    else
    {
      $(this).parents("div.contrib").removeClass("checked");
    }
  });
}

