jQuery(document).ready(function () {

   var tMaps = [];
   jQuery('map.image-action-map').each(function() {
      var tImage = jQuery('img[usemap=#' + this.id + ']');
      var tMap = {
         map:jQuery(this),
         image:tImage,
         imageWidth:0,//set to 0 to force first time calculating
         initialWidth:jQuery(this).data('initial-width'),
         areas: jQuery(this).find('area').map(function(){
            return {
               shape: jQuery(this).attr('shape'),
               coords: jQuery(this).attr('coords'),
               href: jQuery(this).attr('href'),
               target: jQuery(this).attr('target'),
               onclick: jQuery(this).attr('onclick')
            }
         })
      };
      tMaps.push(tMap);
   });

   var tImageMapFixAreas = function(fMap) {
      //process situation, when an image is not loaded yet
      if(!fMap.image.get(0).complete)
         return false;
      //check if nothing is changed
      if(fMap.image.width() == fMap.imageWidth)
         return true;

      fMap.imageWidth = fMap.image.width();

      var tMultiplier = fMap.imageWidth / fMap.initialWidth;

      //create areas for new size
      fMap.map.empty();
      jQuery.each(fMap.areas, function(fNameArea, fValueArea) {
         var tArea = jQuery('<area />');

         tArea.attr('shape', fValueArea.shape).attr('href', fValueArea.href);
         if(fValueArea.target)
            tArea.attr('target', fValueArea.target);
         if(fValueArea.onclick)
            tArea.attr('onclick', fValueArea.onclick);
         tArea.attr('coords', jQuery.map(fValueArea.coords.split(','), function(fCoord) {
            return parseInt(parseInt(fCoord) * tMultiplier);
         }).join(','));

         fMap.map.append(tArea);
      });

      return true;
   };

   var tImageMapsFixAreas = function(fMaps) {
      var tMapsNotLoadedImages = [];
      jQuery.each(fMaps, function(fName, fValue) {
         if(!tImageMapFixAreas(fValue))
            tMapsNotLoadedImages.push(fValue);
      });

      if(tMapsNotLoadedImages.length > 0)
         setTimeout(function() { tImageMapsFixAreas(tMapsNotLoadedImages) }, 200);
   };

   jQuery(window).resize(function() { tImageMapsFixAreas(tMaps); });

   tImageMapsFixAreas(tMaps);
});

