/**
 * @author mike
 */

  /**
   * handle leo living features
   * 
   */
  var LeoLiving = Class.create();
  LeoLiving.prototype = {
  	
    initialize: function (config) {
      if(!config) {
        config = {};
      }
      this.mainDiv       = config.mainDiv || 'LeoLiving';
      this.contentDiv    = config.contentDiv || 'LivingContent';
			this.clicked       = false;
      this.layer         = $$('#'+this.mainDiv+' div.layer')[0];
      this.layerContent  = $$('#'+this.mainDiv+' div.layer div.content')[0];
      this.content       = $(this.contentDiv);
      this.isBusy        = false;
      this.language      = config.language || 'de';
	  },
    
    toggleLayer: function (e, w, h) {
      //console.log(this.isBusy);
      if(this.isBusy) {
        return;
      }
      this.isBusy = true;
      this.link   = $(e);
      if(this.clicked && this.clicked.id != this.link.id) {
        this.resetAll();
      }
      this.clicked  = this.link;
      if(w) {
        this.layer.setStyle({width: w*1+'px'});
        this.layerContent.setStyle({height: h*1+'px'});
      }
      var pos  = Element.positionedOffset(this.link);
      var left = pos[0] - this.layer.getWidth() + this.link.getWidth() + 6;
      this.layer.setStyle({left: left+'px', top: '49px'});
      if(this.layer.visible()) {
        this.clearContent();
        this.closeLayer();
        this.ready.delay(0.5, this);
      } else {
        this.showLayer();
        this.loadContent.delay(0.4, this);
        this.ready.delay(0.5, this);
      }
    },
  
    showLayer: function () {
      Effect.Grow(this.layer, {direction: 'top-right', duration: 0.5});
      //this.layer.show();
    },
    
    closeLayer: function () {
      this.clearContent();
      Effect.Shrink(this.layer, {direction: 'top-right', duration: 0.5});
    },
    
    loadContent: function (obj) {
      var ajax = new Ajax.Request('living.php', {   
        parameters: {type: obj.link.rel, id: obj.link.id},
        evalScripts: true,
        onSuccess: function(transport) { 
          obj.content.update(transport.responseText);
          obj.showContent();
        }
      });
    },
    
    exchangeContent: function (id, type) {
      if(!type) type = 'content';
      var obj = this;
      var ajax = new Ajax.Request('living.php', {   
        parameters: {type: type, id: id},
        evalScripts: true,
        onSuccess: function(transport) { 
          obj.content.update(transport.responseText);
          obj.showContent();
        }
      });
    },
    
    showContent: function () {
      this.prepareContent();
      //Effect.BlindDown(this.content, {duration: 0.5});
      this.content.show();
    },
    
    clearContent: function (link) {
      this.content.hide();
      this.content.update('');
    },
    
    prepareContent: function () {
      var pos = Element.positionedOffset(this.layer);
      //console.log(pos);
      this.content.setStyle({
        left: (pos[0] + 30) + 'px',
        top: (pos[1] + 15) + 'px',
        width: this.layer.getWidth() - 60 + 'px',
        height: this.layer.getHeight() - 50 + 'px'
      });
    },
    
    resetAll: function () {
      this.clearContent();
      this.layer.hide();
    },
    
    ready: function (obj) {
      obj.isBusy = false;
    },
    
    remind: function(form) {
      var f = $(form)
      var p = f.serialize(true);
      var o = this;
      if(f['email'].getValue() == "" || f['title'].getValue() == "" || f['firstname'].getValue() == "" || f['lastname'].getValue() == "") {
        //alert(f['textInValid'].getValue());
        this.message(f['textInValid'].getValue());
        return false;
      }
      var ajax = new Ajax.Request('living.php',   {
        method: 'post',
        parameters: p,
        evalScripts: true,
        onLoading: scr.setCursor('wait'),
        onComplete: scr.setCursor('default'),
        onSuccess: function(transport) {
          var r = transport.responseJSON;
          if(r.success == false) {
            o.message(f['textFailure'].getValue());
          } else {
            o.message(f['textSuccess'].getValue());
            f.disable();
            $$('a.reminder')[0].hide();
          }
        },
        onFailure:    function(){ 
          o.message('Something went wrong...');
        }
      });
    },
    
    message: function(text) {
      $('FormMsg').update(text);
      return false;
    },
    
    // load towns
    loadTowns: function (id) {
      //$('StoreTowns').innerHTML   = "";
      //$('StoreEntries').innerHTML = "";
      //$('StoreDetails').innerHTML = "";
      this.country = id;
      this.load('towns', 'StoreTowns', id);
    },
    
    // load stores
    loadStores: function (id) {
      //$('StoreEntries').innerHTML = "";
      //$('StoreDetails').innerHTML = "";
      this.load('stores', 'StoreEntries', id);
    },
    
    // load details
    loadDetails: function (id) {
      $('StoreDetails').innerHTML = "";
      this.load('details', 'StoreDetails', id);
    },
    
    // load request
    load: function(t, e, id) {
      var ajax  = new Ajax.Updater($(e), BASE_URL+'soap/livingstores.php', {   
        parameters:   {type: t, id: id, filter: this.filter, language: this.language, country: this.country},
        evalScripts:  true
      });
    }
    
  };
  