/* Unhide email addresses */
$("a.email").each(function () {
  $(this).attr("href", emaildecrypt($(this).attr("href")));
  $(this).html(emaildecrypt($(this).html()));
});

function emaildecrypt(email) {
  return email.replace(/ *\[at\] */, "@").replace(/ *\[dot\] */, ".");
}

function video_init(anchor, gallery_name){
    if (anchor.attr("title").substr(0,22)=="http://www.youtube.com"){
      var youtube_url = anchor.attr("title");
      if (youtube_url.substr(23,8) == "watch?v=") {
        youtube_url = "http://www.youtube.com/v/" + youtube_url.substr(31);
      }
      anchor.attr("rel","shadowbox[" + gallery_name + "];width=600;height=490");
      anchor.attr("href",youtube_url);
    }
}


function addGalleryLink() {
  var galleryName;
  var galleryLink;
  if ($("#gallery-name").html() != null) {
    $("#gallery-name").remove();
  }
  if (Shadowbox.getCurrent().title.substr(0,22)=="http://www.youtube.com"){
    galleryName = "Video&nbsp;Gallery";
    galleryLink = "/whats-hot/video-clips";
  } else {
    galleryName = "Image&nbsp;Gallery";
    galleryLink = "/vibrant-niue/hiapo-gallery";
  }
  $("#sb-info-inner").append("<div id='gallery-name'><a href='" + galleryLink + "'>" + galleryName + "</a></div>");
}


jQuery(document).ready(function($){
  /* Enable youtube videos */
  $("#ngg-images-4 a").each(function () {
    video_init($(this), "menu-videos")
  });

  $(".ngg-gallery-thumbnail a").each( function () {
    video_init($(this), "video-gallery")
  });

  Shadowbox.init({
    language: 'en',
    "onOpen": function(args) {addGalleryLink();}
  });

  /* Sub menu logic */
  $("ul.children").each(function () {
    if (!$(this).children("li").hasClass("current_page_item")){
      $(this).css("height", 0);
    }
  });
  
  $("ul.children").each(function (par) {
    $(this).parent().hover(
      function (overPar) {
        var ul = $(this).children("ul.children");
        ul.stop(false,false);

        var currentHeight = ul.height();
        ul.css("height", "");
        var targetHeight = ul.height();
        ul.css("height", currentHeight + "px");
        var duration = (targetHeight - currentHeight)*20;
        
        ul.stop(false,true).animate({height: targetHeight}, duration);
      },
      function (outPar) {
        if (!$(this).children("ul").children("li").hasClass("current_page_item")) {
          var ul = $(this).children("ul.children");
          ul.stop(false,false);

          var currentHeight = ul.height();
          var duration = currentHeight*20;

          ul.animate({height: 0}, duration);
        }
      });
  });

  if ($("#map_cancas") != null) {
    $("#map_cancas").css("background-color", "");
    $("#map_cancas").html("");
    var map = new GMap2(document.getElementById("map_canvas"));
    map.setCenter(new GLatLng(-19.051734,-169.867172), 11);
    map.setUIToDefault();
  }
});
