/*
  Map Display
  United States Department of State, IIP/W
  Darren Krape (krapedw@state.gov)
*/

$.fn.map = function(settings) {
  settings = $.extend({
    preloader: true,
    preloaderTime: 5000
  }, settings);
  return this.each(function(i) {
    mapReturn.init(settings,this);
  });
};

var mapReturn = {
  init: function(s,p) {
  
    if(s.preloader) $.fn.preloader(s,p);

    $(p).prepend("<div class='window'><div class='windowbg'></div><div class='rwindow'><div class='inside'><a href='#' class='close'>[x] Close</a><div class='content'></div><div class='nav'><a href='#' class='button previous'><span>#</span></a><div class='status'>#</div><a href='#' class='button next'><span>#</span></a></div></div></div></div>");

    var menuHeight = $(p).find(".menu .key").height() + 35;

    $(p).find(".menu, .menu-content").css({height:menuHeight});
    $(p).find(".menu").css({marginTop:($(p).height() - menuHeight), visibility:"visible"});

    $(p).find(".elements li").each(function(i) {

      var coords = $(this).attr("id");
      var point = new Array();
      point = coords.split('y');
      var x = parseFloat(point[0].substring(1))-14;
      var y = parseFloat(point[1])-14;

      tagDetails = $(this).attr("class").split(' ');

      var tagText = $(this).attr("title");

//      if(tagDetails[1] == "tag-left" || tagDetails[1] == "tag-right")
      if($(this).hasClass("hidepoint")) var hidePoint = " hide-point"; else var hidePoint = "";
      $(p).prepend("<div class='item-point " + tagDetails[1] + hidePoint + "' ref='" + coords + "'><a href='#' class='point'>Point</a><div class='tag " + tagDetails[2] + "'><p>" + tagText + "</p></div></div>");
      $(p).find(".item-point[ref='" + coords + "']").css({marginLeft:x, marginTop:y});

      if(tagDetails[1] == "tag-left") {
        var tagLeft = $(p).find(".item-point[ref='" + coords + "'] .tag");
        var tagLeftWidth = -(tagLeft.outerWidth()) + 8;
        $(tagLeft).css({marginLeft:tagLeftWidth})
      }

    });

//Point Links for Modal Window

    $(p).find(".point, .tag").click(function() {
      var newItem = $(this).parent().attr("ref");

      currentItem = $(p).find(".window").attr("ref");

      if(!currentItem) {
        var itemContent = $(p).find(".elements li[id='" + newItem + "']").html()
        $(p).find(".window .content").append(itemContent);
      }

      swapButtons(newItem);

      if (!$(p).find(".window").is(":visible") && (currentItem == newItem))
        $(p).find(".window").fadeIn();

      if(currentItem != newItem) {
        $(p).find(".window .content").fadeOut(function() {
          var newContent = $(p).find(".elements li[id='" + newItem + "']").html();
          $(p).find(".window .content").empty().append(newContent).fadeIn();
          $(p).find(".window").attr({ref:newItem}).fadeIn();
        });
      }

      return false;
    });

//Previous Button

    $(p).find(".window .previous").click(function() {

      var currentItem = $(p).find(".window").attr("ref");
      var newItem = $(p).find(".elements li[id='" + currentItem + "']").prev().attr("id");

      if(!newItem) {
        var newContent = $(p).find(".elements li:last").html();
        var newItem = $(p).find(".elements li:last").attr("id");
      } else {
        var newContent = $(p).find(".elements li[id='" + currentItem + "']").prev().html();
      }

      $(p).find(".window .content").fadeOut(function() {
        $(p).find(".window").attr({ref:newItem});
        $(p).find(".window .content").empty().append(newContent).fadeIn();
      });

      swapButtons(newItem);

      return false;
    });

//Next Button

    $(p).find(".window .next").click(function() {

      var currentItem = $(p).find(".window").attr("ref");
      var newItem = $(p).find(".elements li[id='" + currentItem + "']").next().attr("id");

      if(!newItem) {
        var newContent = $(p).find(".elements li:first").html();
        var newItem = $(p).find(".elements li:first").next().attr("id");
      } else {
        var newContent = $(p).find(".elements li[id='" + currentItem + "']").next().html();
      }

      $(p).find(".window .content").fadeOut(function() {
        $(p).find(".window").attr({ref:newItem});
        $(p).find(".window .content").empty().append(newContent).fadeIn();
      });

      swapButtons(newItem);

      return false;
    });

//Close Button

    $(p).find(".close").click(function() {
      $(p).find(".window").fadeOut("fast");
      return false;
    });

//Menu Hide Button

    $(p).find(".menu .menu-handle").click(function() {
    
      if($.browser.msie) var tag = " .tag"; else var tag = "";
      
      var menuHeight = $(p).find(".menu .key").height() + 10;

      if($(p).find(".menu-content").hasClass("active")) {
        $(p).find(".menu-content").animate({marginTop:menuHeight}).toggleClass("active");

        $(p).find(".menu .key li").removeClass("active");
        $(p).find(".item-point" + tag).fadeTo("Fast",1);

      } else {
        $(p).find(".menu-content").animate({marginTop:"0"}).toggleClass("active");
      }

      return false;
    });

//Key Fade (onClick)

    $(p).find(".key li:not(.title)").click(function() {
    
      if($.browser.msie) var tag = " .tag"; else var tag = "";

      if(!$(this).hasClass("active")) {

        $(p).find(".key li").removeClass("active");
        $(this).addClass("active");

        var pointActive = $(this).attr("class");
        $(p).find(".item-point" + tag).fadeTo("Fast",1);
        $(p).find(".item-point:not(." + pointActive + ")" + tag).fadeTo("Fast", 0.20);

      } else {

        $(this).removeClass("active");
        $(p).find(".item-point" + tag).fadeTo("Fast",1);

      }

    });

//Swap Button Text Function

    var swapButtons = function(newItem) {

      var prevTitle = $(p).find(".elements li[id='" + newItem + "']").prev().find("h3").text();
      if(!prevTitle) var prevTitle = $(p).find(".elements li:last").find("h3").text();

      $(p).find(".window .nav .previous span").text(prevTitle);

      var nextTitle = $(p).find(".elements li[id='" + newItem + "']").next().find("h3").text();
      if(!nextTitle) var nextTitle = $(p).find(".elements li:first").find("h3").text();

      $(p).find(".window .nav .next span").text(nextTitle);

    }

  }
};

//Preloader: Displays for five seconds then fades out (not a true load timer)
$.fn.preloader = function(s,p) {
  $(p).append("<div class='preloader'><img src='i/loading_icon.gif' alt='Map is loading...' /><div class='tip'><strong>Tip:</strong> Click on a point for more information on each topic.</div></div>");
  $(p).find(".preloader .tip").fadeTo(s.preloaderTime, 1.0, function () {
    $(p).find(".menu").show();
    $(p).find(".preloader").fadeOut("slow", function() { $(this).remove(); });
  });
};

$(function() {
  $(".map").map({preloader: false});
});
