/*!
 * 
 */

Ext.ns('Efa');


/**
 * 
 * @param {Object} elId
 * @param {Object} config
 */
Efa.Info = Ext.extend(Ext.util.Observable, {
  
  constructor: function(elId, config) {
    config = config || {};
    Ext.apply(this, config);
    this.container = Ext.get(elId);
    this.initEvents();
  },
  
  initEvents: function() {
    Ext.getBody().select('a.info').each(function(el){
      var el = Ext.get(el);
      el.on('click', this.showInfo, this);
    }, this);
  },
  
  showInfo: function(e, target) {
    e.preventDefault();
    var c = e.getXY();
    Ext.Ajax.request({
      url: this.url,
      success: function(response) {
        if(!this.container.isVisible()) {
          this.container.setDisplayed(true);
        }
        this.container.setTop(e.getPageY() - 400);
        this.container.child('div.wrapper').update(response.responseText);
      },
      failure: function() {
        alert('Fehler!');
      },
      params: {url: target.href},
      scope: this
    });
  },
  
  close: function() {
    this.container.child('div.wrapper').update('');
    this.container.setDisplayed(false);
  }
  
});


/**
 * Efa Map
 * 
 * @param {Object} elId
 * @param {Object} config
 */
Efa.Map = Ext.extend(Ext.util.Observable, {
    
  constructor: function(elId, config) {
    config = config || {};
    Ext.apply(this, config);
    this.enableAjax = this.enableAjax || false;
    this.container  = Ext.get(elId);
    this.map 	      = this.container.child('img');
    Efa.Map.superclass.constructor.call(this, config);
    if(this.map) this.initEvents();
	},
  
  initEvents: function() {
    //this.container.child('img').on('dblclick', this.moveTo, this);
    if (this.enableAjax) {
      this.container.select('area').each(function(el){
        el.on('click', this.centerTo, this);
      }, this);
      
    } 
  },

	moveTo: function(e) {
		var target = e.getTarget('img', 10, true);
    var ec = e.getXY();
    var oc = target.getXY();
    var coords = {x: (ec[0]-oc[0]), y: (ec[1]-oc[1])};
    var delta  = {x: coords.x - 320, y: coords.y - 240};
    var real   = {x: parseInt(this.centerX + (delta.x * this.scaleFactor)), y: parseInt(this.centerY + (delta.y * this.scaleFactor))};
    if (this.enableAjax) {
      this.loadMap(this.url + '?zoomLevel=' + this.zoomLevel + '&x=' + real.x + '&y=' + real.y);
    } else {
      document.location.href = this.url + '?zoomLevel=' + this.zoomLevel + '&x=' + real.x + '&y=' + real.y;
    }
	},
  
  centerTo: function(e, target) {
    e.preventDefault();
    this.loadMap(target.href);
  },
  
  setPoint: function(name, place, type) {
    if(this.type == "destination") {
      this.setDestination(name, place, type);
    } else {
      this.setOrigin(name, place, type);
    }
  },
  
  setCoords: function(id, type) {
    var form = Ext.get(this.form);
    form.dom.name_origin.value = "";
    form.dom.type_origin.value = "stop";
    form.dom.nameInfo_origin.value = id;
    form.dom.typeInfo_origin.value = "coord";
    form.dom.execInst.value = "verifyOnly";
    form.dom.deleteAssignedStops_origin.value = 1;
    form.dom.submit();
  },
  
  setOrigin: function(name, place, type) {
    var form = Ext.get(this.form);
    if(place) {
      form.dom.place_origin.value = place;
    }
    form.dom.name_origin.value = name;
    form.dom.type_origin.value = type || "stop";
    form.dom.typeInfo_origin.value = "stopID";
    form.dom.nameState_origin.value = 'empty';
    form.dom.execInst.value = "verifyOnly";
    form.dom.deleteAssignedStops_origin.value = 1;
    form.dom.submit();
  },
  
  setDestination: function(name, place, type) {
    var form = Ext.get(this.form);
    if(place) {
      form.dom.place_destination.value = place;
    }
    form.dom.name_destination.value = name;
    form.dom.type_destination.value = type || "stop";
    form.dom.typeInfo_destination.value = "stopID";
    form.dom.nameState_destination.value = 'empty';
    form.dom.execInst.value = "verifyOnly";
    form.dom.deleteAssignedStops_destination.value = 1;
    form.dom.submit();
  },
  
  showMap: function(type) {
    this.type = type;
    this.loadMap();
  }, 
  
  loadMap: function(url, params) {
    if(!url) url = this.url;
    Ext.Ajax.request({
      url: url,
      success: function(response) {
        if(!this.container.isVisible()) {
          this.container.setDisplayed(true);
        }
        this.container.child('div.wrapper').update(response.responseText);
        this.initEvents();
      },
      failure: function() {
        alert('Fehler!');
      },
      params: params,
      extraParams: {request: 'js'},
      scope: this
    });
  },
  
  close: function() {
    this.container.child('div.wrapper').update('');
    this.container.setDisplayed(false);
  }

});
