var lastMapAction = {
  action: '',
  data: ''
};

function setLastMapAction(action, data) {
  lastMapAction = {
    action: action,
    data: data
  };
}

function setLastMapActionToDefault() {
  lastMapAction = {
    action: '',
    data: ''
  };
}

function getLastMapAction() {
  return lastMapAction;
}


var adHocPois = new Array();

function setAdHocPois(listClass) {
  adHocPois = new Array();
  setLastMapActionToDefault();

  // and at last set the pois from the current list
  jQuery('.' + listClass + ' .mapBubble a.map').each(function() {
    var rel = jQuery(this).attr('rel');
    adHocPois.push(plannerAdHocPois[rel]);
  });

  if (typeof getSWF('mapEmbedSmall').setPOIs == 'function') {
    initMapSmall();
  }
  if (typeof getSWF('mapEmbedLarge').setPOIs == 'function') {
    initMapLarge();
  }
}

function getAdHocPois() {
  return adHocPois;
}


/**
 * 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];
  }
}

/**
 * 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);
  }
}

/**
 * With this it is possible to open the map from within a link in the content of the page.
 */
function enlargeMapHomepageWithLayers() {
  jQuery('#magnifier').trigger('click');
}

function scrollTo(selector) {
  if (typeof(selector) == 'object') {
    var targetOffset = selector.offset().top;
    jQuery('html, body').animate({
      scrollTop: targetOffset
    }, 500);
  }
}



/**
 * 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];
}
