//Libraries we're using:
//  jQuery 1.3.2
//  jQuery.easing 1.3
//  jQuery.backgroundPosition 1.1
//  jQuery.Tools 1.1.0 [tools.tabs-1.0.2, tools.tabs.slideshow-1.0.0, tools.tooltip-1.1.0, tools.overlay-1.1.0, tools.overlay.apple-1.0.0, tools.expose-1.0.4]

var map_original_position_array = new Array();
var map_new_position_array = new Array();


$(document).ready(function(){

  preloadImages();

  if($("#localeimagemap"))
    setupGlobe("#localeimagemap","#selectedlocaledetails");


  //home page animations

  $("#homenavleft").hover(
    function() {
      $("#homeimage").animate({"backgroundPosition": "(0px 0px)"}, { "duration": 1200, "easing": "easeOutBack" });
      $("#homeimage > p").animate({"backgroundPosition": "(0px 284px)"}, { "duration": 1200, "easing": "easeOutBack" });
      $("#homenavleftover").fadeIn("normal");      
    },
    function() { }
  );

  $("#homenavleftover").hover(
    function() { },
    function() {
      $("#homenavleftover").fadeOut("normal");
    }
  );

  $("#homenavright").hover(
    function() {
      $("#homeimage").animate({"backgroundPosition": "(200px 0px)"}, { "duration": 1200, "easing": "easeOutBack" });
      $("#homeimage > p").animate({"backgroundPosition": "(200px 284px)"}, { "duration": 1200, "easing": "easeOutBack" });
      $("#homenavrightover").fadeIn("normal");
    },
    function() { }
  );

  $("#homenavrightover").hover(
    function() { },
    function() {
      $("#homenavrightover").fadeOut("normal");
    }
  );


  $("#homebranding").hover(
    function() {
      $("#homeimage").animate({"backgroundPosition": "(100px 0px)"}, { "duration": 600, "easing": "easeOutQuad" });
      $("#homeimage > p").animate({"backgroundPosition": "(100px 284px)"}, { "duration": 600, "easing": "easeOutQuad" });
    },
    function() { }
  );
  
  
  
  //prepare the survey overlay
  $("a[rel]").overlay({ 
 
    expose: '#344e8e', 
    effect: 'apple', 
    
    onBeforeLoad: function() {
      this.getContent().find("div.wrap").load(this.getTrigger().attr("href") + "&overlay=1", "", prepareOverlayHandlers); 
    }
  });  


  //prepare "read more" links
  $("a.moreinfolink").click(function(e) {
    e.preventDefault();
    $(".moreinfo").slideToggle();
  });

});


function prepareOverlayHandlers() {

  if($("#localeimagemapoverlay"))
    setupGlobe("#localeimagemapoverlay","#selectedlocaledetailsoverlay");


  $("#surveyform").submit(function(e) {
    e.preventDefault();
    
    $.post("index.php?mod=survey&overlay=1", $("#surveyform").serialize(), function(postresult) { $("#surveyoverlay > div.wrap").html(postresult); prepareOverlayHandlers(); }, "html");
  });  
}


function prepareMapOverlays(e) {

  map_original_position_array[e] = new Array();
  map_new_position_array[e] = new Array();

  //gather map contact summary positions
  $(e + " .mapcontact").each(function() {

    var new_left, new_top;
    var this_summary_id = e + " #" + this.id + "summary";
    var summary_margin = 10;
  
    var map_width = parseInt($(e).css("width").replace("px", ""));
    var map_height = parseInt($(e).css("height").replace("px", ""));

    var current_left = parseInt($(this_summary_id).css("left").replace("px", ""));
    var current_top = parseInt($(this_summary_id).css("top").replace("px", ""));
    var current_width = parseInt($(this_summary_id).css("width").replace("px", ""));
    var current_height = parseInt($(this_summary_id).css("minHeight").replace("px", ""));

    if(current_left <= (map_width / 2)) {
      current_left = current_left + summary_margin;
      new_left = current_left + summary_margin;
    } else {
      current_left = current_left - (current_width + summary_margin);
      new_left = current_left - summary_margin;
    }

    $(this_summary_id).css({"left": current_left + "px"});


    if(current_top <= (map_height / 2)) {
      current_top = current_top + summary_margin;
      new_top = current_top + summary_margin;
    } else {
      current_top = current_top - (current_height + summary_margin);
      new_top = current_top - summary_margin;
    }

    $(this_summary_id).css({"top": current_top + "px"});

    map_original_position_array[e][this.id] = {'left':current_left, 'top':current_top};
    map_new_position_array[e][this.id] = {'left':new_left, 'top':new_top};
  });
}


function addMapHovers(e) {
 
   //apply the animation to show/hide the map contact summaries on hover
  $(e + " .mapcontact").hover(
    function() {
      var new_left = map_new_position_array[e][this.id]['left'];
      var new_top = map_new_position_array[e][this.id]['top'];

      $(e + " #" + this.id + "summary").show();
      $(e + " #" + this.id + "summary").animate({"left": new_left + "px", "top": new_top + "px"}, { "duration": 100, "easing": "easeOutQuad" });
    },
    function() {
      var new_left = map_original_position_array[e][this.id]['left'];
      var new_top = map_original_position_array[e][this.id]['top'];

      $(e + " #" + this.id + "summary").hide();
      $(e + " #" + this.id + "summary").animate({"left": new_left + "px", "top": new_top + "px"}, { "duration": 100, "easing": "easeOutQuad" });
    }
  );

}


function addLocaleClickHandlers(e,d) {
  //if any star on the globe is clicked then gather the conditions from the href and use jquery to load into the DOM
  //this allows us to load the contact details via an AJAX call and give visual feedback
  $(e + " .mapcontact").click(function(el) {
    el.preventDefault(); //prevent usual click action
    
    //break apart href of link
    locale_id_split = $(this).attr("href").split("#locale");
    
    //fade out bloglist and then load new data into the parent div
    if($(d + " .localedata"))
      $(d + " .localedata").fadeTo("normal",0.3);
    else if($(d + " .localeshortdata"))
      $(d + " .localeshortdata").fadeTo("normal",0.3);
    
    if(e == "#localeimagemapoverlay")
      $(d).load('localeselect.php?locale_id=' + encodeURIComponent(locale_id_split[1]) + '&g=1&s=1', function() { });    
    else
      $(d).load('localeselect.php?locale_id=' + encodeURIComponent(locale_id_split[1]) + '&g=1', function() { });
    
    //choose the related country to the selected contact if a country selection menu is in the document.
    if($("#country"))
      $("#country").val($(e + " #" + this.id + " em").html());

    if($("#countryoverlay"))
      $("#countryoverlay").val($(e + " #" + this.id + " em").html());
  });
}


function setupGlobe(e,d) {
  prepareMapOverlays(e);
  addMapHovers(e);
  addLocaleClickHandlers(e,d);
}


function preloadImages() {

  //preload images for the globe / locale selection  
  globe_preload0 = new Image(1,1);
  globe_preload0.src = 'images/globe800.png';
  globe_preload1 = new Image(1,1);
  globe_preload1.src = 'images/globeoverlay.png';
  globe_preload2 = new Image(1,1);
  globe_preload2.src = 'images/contactborder.png';
  globe_preload3 = new Image(1,1);
  globe_preload3.src = 'images/globestar.png';
  globe_preload4 = new Image(1,1);
  globe_preload4.src = 'images/globestar-hover.png';

}