var lastMapAction = {
  action: '',
  data: ''
};

function setLastMapAction(action, data) {
  lastMapAction = {
    action: action,
    data: data
  };
}

function setLastMapActionToDefault() {
  lastMapAction = {
    action: '',
    data: ''
  };
}

function getLastMapAction() {
  return lastMapAction;
}


var imxSearchPois = new Array();

function setImxSearchPois(listId) {
  imxSearchPois = new Array();
  setLastMapActionToDefault();

  // get the identifier of the tourist information if present
  var touristInformation = $('#shadwords-location a.map').attr('rel');
  if (touristInformation) {
    imxSearchPois.push(touristInformation);
  }

  // get the shadwords offers if present
  $('#shadwords-offers a.map').each(function() {
    imxSearchPois.push($(this).attr('rel'));
  });

  // and at last set the pois from the current list
  $('#' + listId + ' a.map').each(function() {
    imxSearchPois.push($(this).attr('rel'));
  });

  if (typeof getSWF('mapEmbedSmall').setPOIs == 'function') {
    initMapSmall();
  }
  if (typeof getSWF('mapEmbedLarge').setPOIs == 'function') {
    initMapLarge();
  }
}

function getImxSearchPois() {
  return imxSearchPois;
}


/**
 * Gets the flash file for use with the api.
 *
 * @param string swfName
 * @return object
 */
function getSWF(swfName) {
  if (navigator.appName.indexOf('Microsoft') != -1) {
    return window[swfName];
  } else {
    return document[swfName];
  }
}

function showPoi(id) {
  if ($('#alta4_open, #alta4_closed').height() == 0) {
    $('a.map_opener').trigger('click');
  }

  setLastMapAction('showPoi', id);
  if (typeof getSWF('mapEmbedLarge').setPOIs == 'function') {
    initMapLarge();
  }

  scrollTo($('#keyvisual'));
}

/**
 * Used for debugging with Firebug.
 * Do not use console.log() directly because this could make problems shen Firebug is not activated!
 *
 * @param mixed msg
 */
function logInConsole(msg) {
  if (typeof(console) == 'object') {
    console.log(msg);
  }
}

$(document).ready(function() {
  /**
   * Opens the maxi map
   */
  $('a.map_opener').click(function() {
    if ($('#alta4_open, #alta4_closed').height() == 0) {
      setLastMapAction('open', '');
      
      $('#map_container').show();

      $('#alta4_closed, #alta4_open').animate({
        height: '581px'
      }, 750, '', function() {
        });
    }

    return false;
  });

  /**
   * Closes the maximap
   */
  $('#alta4_closed a, #alta4_open a').click(function () {
    setLastMapAction('close', '');

    $('#alta4_closed, #alta4_open').animate({
      height: '0px'
    }, 750, '', function() {
      $('#map_container').hide();
    });

    return false;
  });
});

/**
 * With this it is possible to open the map from within a link in the content of the page.
 */
function enlargeMapHomepageWithLayers() {
  $('#map_teaser a.map_opener').trigger('click');
}


/**
 * Callback function to detect scrolling over the map and handle it
 *
 * @param event
 */
function handleWheel(event) {

  //alert('handle Wheel');

  var app = getSWF('mapEmbedLarge');
  if (app) {
    var a = findPos(app);
    //alert(a[0]);

    if( !event ) {
      if( window.event ) {
        //Internet Explorer
        event = window.event;
      } else {
        //total failure, we have no way of referencing the event
        return;
      }
    }


    var xcoord;
    var ycoord;


    if(typeof(event.pageX) == 'number') {

      xcoord = event.pageX;
      ycoord = event.pageY;

    } else if(typeof(event.clientX) == 'number'){

      //Internet Explorer and older browsers
      //other browsers provide this, but follow the pageX/Y branch
      xcoord = event.clientX;
      ycoord = event.clientY;
      var badOldBrowser = ( window.navigator.userAgent.indexOf( 'Opera' ) + 1 ) ||
      ( window.ScriptEngine && ScriptEngine().indexOf( 'InScript' ) + 1 ) ||
      ( navigator.vendor == 'KDE' );

      if( !badOldBrowser ) {
        if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
          //IE 4, 5 & 6 (in non-standards compliant mode)
          xcoord += document.body.scrollLeft;
          ycoord += document.body.scrollTop;
        } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
          //IE 6 (in standards compliant mode)
          xcoord += document.documentElement.scrollLeft;
          ycoord += document.documentElement.scrollTop;
        }
      }

    }
    if(xcoord){

      if(xcoord-a[0]>0 && xcoord-a[0]<app.width && ycoord-a[1]>0&&ycoord-a[1]<app.height){
        try{
          event.preventDefault();
        }catch(error) {

        }
        event.returnValue = false;
      }

      var delta = 0;
      if (event.wheelDelta) { /* IE/Opera. */
        delta = event.wheelDelta/120;
        /** In Opera 9, delta differs in sign as compared to IE.
*/
        if (window.opera)
          delta = -delta;
      } else if (event.detail) { /** Mozilla case. */
        /** In Mozilla, sign of delta is different than in IE.
* Also, delta is multiple of 3.
*/
        delta = -event.detail/3;
      }


      var o = {
        x: xcoord - a[0],
        y: ycoord - a[1],
        delta: delta,
        ctrlKey: event.ctrlKey,
        altKey: event.altKey,
        shiftKey: event.shiftKey
      };

      try {
        app.handleWheel(o);
      } catch (e) {
      // suppress error silently
      }
    }

  }
}

/**
 * Returns the current position of the given object
 *
 * @param obj object
 * @return array
 */
function findPos(obj)
{
  var curleft = curtop = 0;
  if (obj.offsetParent)
  {
    do
    {
      curleft += obj.offsetLeft;
      curtop += obj.offsetTop;
    }
    while (obj = obj.offsetParent);
  }
  return [curleft,curtop];
}