/*
   inserts an image of australia with state rollovers
   images from 'Set 2': http://commons.wikimedia.org/wiki/Maps_of_Australia
   
   Usage:
   <div class="map"></div>
   $('div.map').mapify();
   
   dependencies: jquery.cookie.js 
*/
(function($){
   $.fn.mapify = function (urls, domain) {
      // the links of the <area>'s can be overridden
      var settings = {
         sa: '/',
         wa: '/',
         nt: '/',
         qld: '/',
         act: '/',
         nsw: '/',
         vic: '/',
         tas: '/'
      };
      jQuery.extend(settings, urls || {});
      
      jQuery.data(this[0], 'mapify_config', {
         'urls': settings,
         'cookieDomain': domain
      });
      
      // fill the container with the image and imagemap
      if($('#mappify').length == 0){
         $('body').append('\
            <div id="mappify">\
               <div id="map-overlay"></div>\
               <div id="map-box">\
                  <div id="map-content">\
                     <h1>Please pick your location to continue.</h1>\
                     <img src="/images/map/aus.png" alt="Map of Australia" usemap="#ausMap" />\
                     <map name="ausMap">\
                        <area shape="poly" coords="232,221,378,221,370,384,236,300" href="javascript:$(this).goto(\'' + settings.sa + '#loc=sa\')" alt="South Australia" data-img="/images/map/sa.png" />\
                        <area shape="poly" coords="236,300,78,365,30,187,196,50,225,73" href="javascript:$(this).goto(\'' + settings.wa + '#loc=qa\')" alt="Western Australia" data-img="/images/map/wa.png" />\
                        <area shape="poly" coords="225,73,241,16,334,22,346,88,342,221,232,221" href="javascript:$(this).goto(\'' + settings.nt + '#loc=nt\')" alt="Northern Territory" data-img="/images/map/nt.png" />\
                        <area shape="poly" coords="346,88,406,11,535,216,525,263,512,263,489,273,492,265,475,263,470,267,376,262,378,221,342,221" href="javascript:$(this).goto(\'' + settings.qld + '#loc=qld\')" alt="Queensland" data-img="/images/map/qld.png" />\
                        <area shape="poly" coords="452,346,474,345,471,365,452,365" href="javascript:$(this).goto(\'' + settings.act + '#loc=act\')" alt="Australian Capital Territory" data-img="/images/map/act.png" />\
                        <area shape="poly" coords="525,263,512,263,489,273,492,265,475,263,470,267,376,262,373,327,413,357,449,360,452,372,467,383,515,310" href="javascript:$(this).goto(\'' + settings.nsw + '#loc=nsw\')" alt="New South Wales" data-img="/images/map/nsw.png" />\
                        <area shape="poly" coords="373,327,413,357,449,360,452,372,467,383,430,402,396,397,370,384" href="javascript:$(this).goto(\'' + settings.vic + '#loc=vic\')" alt="Victoria" data-img="/images/map/vic.png" />\
                        <area shape="poly" coords="406,420,454,423,445,469,410,466" href="javascript:$(this).goto(\'' + settings.tas + '#loc=tas\')" alt="Tasmania" data-img="/images/map/tas.png" />\
                     </map>\
                  </div>\
               </div>\
            </div>\
         ');
      }
      
      var img = $('#mappify').find('img');
      var areas = $('#mappify').find('area');
      
      // preload images
      var pre = [];
      areas.each(function(idx, e){
         pre.push(new Image());
         pre[pre.length - 1].src = $(e).data('img');
      });
      
      // on mouseover, switch the image
      areas.mouseover(function(e){
         $(img).attr('src', $(e.target || e.srcElement).data('img'));
      })
      
      var pageSize = ___getPageSize();
      var pageScroll = ___getPageScroll();
      $('#map-overlay').css({
         width: pageSize[0],
         height: pageSize[1]
      });
      $('#map-box').css({
         top: pageScroll[1] + (pageSize[3] / 10),
         left: pageScroll[0]
      });
      
      $('#mappify').hide();
      $('#mappify').fadeIn();
      
      /* code lifted from jquery-lightbox */
      function ___getPageSize() {
         var xScroll, yScroll;
         if (window.innerHeight && window.scrollMaxY) {   
            xScroll = window.innerWidth + window.scrollMaxX;
            yScroll = window.innerHeight + window.scrollMaxY;
         } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
            xScroll = document.body.scrollWidth;
            yScroll = document.body.scrollHeight;
         } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
            xScroll = document.body.offsetWidth;
            yScroll = document.body.offsetHeight;
         }
         var windowWidth, windowHeight;
         if (self.innerHeight) {   // all except Explorer
            if(document.documentElement.clientWidth){
               windowWidth = document.documentElement.clientWidth; 
            } else {
               windowWidth = self.innerWidth;
            }
            windowHeight = self.innerHeight;
         } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
            windowWidth = document.documentElement.clientWidth;
            windowHeight = document.documentElement.clientHeight;
         } else if (document.body) { // other Explorers
            windowWidth = document.body.clientWidth;
            windowHeight = document.body.clientHeight;
         }   
         // for small pages with total height less then height of the viewport
         if(yScroll < windowHeight){
            pageHeight = windowHeight;
         } else { 
            pageHeight = yScroll;
         }
         // for small pages with total width less then width of the viewport
         if(xScroll < windowWidth){   
            pageWidth = xScroll;      
         } else {
            pageWidth = windowWidth;
         }
         arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
         return arrayPageSize;
      };
      function ___getPageScroll() {
         var xScroll, yScroll;
         if (self.pageYOffset) {
            yScroll = self.pageYOffset;
            xScroll = self.pageXOffset;
         } else if (document.documentElement && document.documentElement.scrollTop) {    // Explorer 6 Strict
            yScroll = document.documentElement.scrollTop;
            xScroll = document.documentElement.scrollLeft;
         } else if (document.body) {// all other Explorers
            yScroll = document.body.scrollTop;
            xScroll = document.body.scrollLeft;   
         }
         arrayPageScroll = new Array(xScroll,yScroll);
         return arrayPageScroll;
      };
   }
   $.fn.goto = function (url) {
      var config = jQuery.data($(this)[0], 'mapify_config');
      // a link on the map was clicked.
      //If we clicked on a link to the current site just hide the popup
      var urlLoc = url.replace('http://', '').split('/');
      if(urlLoc.length > 0) urlLoc = urlLoc[0];
      var urlHash = url.split('#loc=');
      if(urlHash.length > 0) urlHash = urlHash[urlHash.length - 1];
      var currentLoc = window.location.hostname.replace('http://', '').split('/');
      if(currentLoc.length > 0) currentLoc = currentLoc[0];
      
      if(urlLoc && urlHash && currentLoc){
         if(urlLoc.match(':')) urlLoc = urlLoc.split(':')[0];
         var currentLocWWW = currentLoc.replace('www.', '');
         if(urlLoc == currentLoc || urlLoc == currentLocWWW)
            $.cookie('lecornu-location', urlHash, { path: '/', domain: config.cookieDomain, expires: 365 });
         else
            window.location = url;
      }
      $('#mappify').fadeOut();
   }
})(jQuery);

