/*
 *  THE ELECTORAL COMMISSION
 *  maps.js - Javascript functionality for Election Results Maps
 *  Author: Gwidon Walinski ( gwalinski at squiz dot pl )
 *
 */

/* 
 *  1. ToolTip jQuery Plugin 
 */
(function($){
  $.fn.tooltip = function() {
    this.each(function() {
      
      var $this = $(this);
      var title = this.title;
      if($this.attr('title') != '') {
        this.title = '';
        this.alt = '';
        $this.hover(function(e) {
          // mouse over
          $('<div id="tooltip" />')
            .appendTo('body')
            .text(title)
            .css({
              top: e.pageY + 10,
              left: e.pageX + 20
            })
            .fadeIn(350);
        }, function() {
          // mouse out
          $('#tooltip').remove();
        });  
      }
      
      $this.mousemove(function(e) {
        $('#tooltip').css({
          top: e.pageY + 10,
          left: e.pageX + 20
        });
      });
      
    });
    // returns the jQuery object to allow for chainability.
    return this;
  }
})(jQuery);

/* 
 *  2. Map Regions Highlight functionality  
 */

function mapHover(mapObject){
  var selectedRegion = $('.map-highlight:visible');
  $('map area').hover(
    function() {
      var areaID = $(this).attr('id');
      $('div.'+areaID, mapObject).show();
    }, function(){
      var areaID = $(this).attr('id');
      $('div.'+areaID, mapObject).not(selectedRegion).hide();
    }
  );

}

/*
 *  3. Documen Ready 
 */
$(document).ready(function(){
  var ukMap = $('#uk-map');
  var walesMap = $('#wales-map');
  
  // Run map highlight on UK Map
  if (ukMap) {
    $('area', ukMap).tooltip();
    mapHover(ukMap);

  }
  
  if (walesMap) {
    $('area', walesMap).tooltip();
    mapHover(walesMap);

  }

  // Run lightbox on help link
  $('a.help').each(function(){
    var urlWithBlank = $(this).attr('href') + '?SQ_DESIGN_NAME=blank';
    $(this).attr('href', urlWithBlank);
    
    $('a.help').click(function(){
      $(this).colorbox({height:450});
    });
  });


});
