// Copyright (c) 2007 Michael Schreckenberg (http://www.markt8.de)

  
  var AJAX_URL = './ajax.php';
  
  /*
   * Common Window class
   * handles (no idea so far...;)
   */
  var Win = Class.create();
  Win.prototype = {
  	initialize: function () {
      this.debug        = 'DEBUG';
      this.defDuration  = 0.6;
    },
    
    loadURL: function (url) {
      document.location.href = url;
    },
    
    fadeInContent: function (element, type, id, hide, mark) {
      this.element = element;
      $(this.element).innerHTML = '';
      if(hide) {
        $(this.element).hide();
        if(this.objectExists('ArticleBackground') && $('ArticleBackground').style.display != "none") {
          $('ArticleBackground').style.display = "none";
        }
      }
      if(mark) {
        this.resetItems();
        this.highlightItem('Article'+id);
      }
      if(!type) type = 'page';
      var ajax  = new Ajax.Updater(this.element, AJAX_URL, {   
                    parameters:   {type: type, id: id},
                    evalScripts:  true
                  });
    },
    
    fadeInCentered: function (element, type, id) {
      var ajax  = new Ajax.Updater(element, AJAX_URL, {   
                    parameters:   {type: type, id: id},
                    evalScripts:  true
                  });
    },
    
    blindDown: function (element, duration) {
      if(!duration > 0) duration = this.defDuration;
      Effect.BlindDown(element);
    },
    
    appear: function (element, duration) {
      if(!duration > 0) duration = this.defDuration;
      Effect.Appear(element, { duration: duration } );
    },
    
    resetItems: function (obj) {
      $$('ul.Articles li a').each( function(e) { 
        e.className = '';
      });
    },
    
    highlightItem: function (mark) {
      $(mark).className = 'active';
    },
    
    fade: function (element, duration) {
      if(!duration > 0) duration = this.defDuration;
      Effect.Fade(element, { duration: duration } );
    },
    
    toggleDisplay: function (element) {
      this.element   = element;
      if(Element.getStyle(this.element, "display") != "block") {
        $(this.element).show();
      } else {
        $(this.element).hide();
      }
    },
    
    shade: function () {
      Event.observe('Shader', 'click', this.unshade.bindAsEventListener(this));
      Effect.Appear('Shader', { from: 0, to: 0.8 });
    },
    
    unshade: function () {
      if(this.objectExists('ArticleAjaxCentered') && $('ArticleAjaxCentered').style.display != "none") {
        $('ArticleAjaxCentered').innerHTML = '';
        $('ArticleAjaxCentered').style.display = "none";
      }
      Effect.Fade('Shader');
    },
    
    loadContent: function (element, type, id, params) {
      if(!type) type = 'page';
      var ajax  = new Ajax.Updater(element, AJAX_URL, {   
                    parameters:   {type: type, id: id, params: params},
                    evalScripts:  true
                  });
    },
    
    debug: function (string) {
      $(this.debug).innerHTML = string;
      Effect.Appear(this.debug);
    },
    
    objectExists: function(object) {
      if(Object.inspect($(object)) != "null") {
        return true;
      }
      return false;
    },
    
    setCursor: function(c) {
      $('MAIN').setStyle({   
        cursor: c 
        });
    }
    
  };
  
  
  /*
   * Button Class
   * handles highlighting of buttons etc.
   */
  var Button = Class.create();
  Button.prototype = {
  	initialize: function () {
      // nothing yet
    },
    
    findObj: function (n, d) {
      var p,i,x;  if(!d) d=document; 
      if((p=n.indexOf("?"))>0&&parent.frames.length) {
        d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
      }
      if(!(x=d[n])&&d.all) x=d.all[n]; 
      for(i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
      for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=this.findObj(n,d.layers[i].document);
      if(!x && d.getElementById) {
        x=d.getElementById(n); 
        
      }
      return x;
    },
    
    swap: function() {
      var i, j=0, x, a = this.swap.arguments; 
      this.Images = new Array;
      for(i=0; i<(a.length-2); i+=3) {
        if((x=this.findObj(a[i])) != null) {
          
          this.Images[j++] = x; 
          if(!x.oSrc) {
            x.oSrc = x.src; 
            x.src  = a[i+2];
          }
        }
      }
    },
    
    restore: function() {
      var i, x, a=this.Images; 
      for(i=0; a && i < a.length && (x=a[i]) && x.oSrc; i++) {
        x.src=x.oSrc; x.oSrc=null;
      }
    },
    
    preload: function() {
      var d=document; 
      if(d.images){ 
        if(!d.M8_p) d.M8_p=new Array();
        var i,j=d.M8_p.length,a=M8_preloadImages.arguments; 
        for(i=0; i<a.length; i++) {
          if (a[i].indexOf("#")!=0) { 
            d.M8_p[j]=new Image; d.M8_p[j++].src=a[i];
          }
        }
      }
    }
    
  };
  
  
  /*
   * fader class
   * handles agbs etc.
   */
  var Footer = Class.create();
  Footer.prototype = {
  	initialize: function () {
      this.element  = 'FAQS';
      this.textBox  = 'FAQText';
      this.tab      = 'FAQTab';
    },
    
    fadeIn: function (id, page) {
      var e    = 'FAQ_' + id;
      this.activate(e);
      $$('select').each( function(e) { 
        e.style.visibility = 'hidden';
      });
      var ajax =  new Ajax.Updater(this.textBox, AJAX_URL, {   
                    parameters:   {id: page, article_id: id},
                    evalScripts:  true,
                    onComplete:   new Effect.BlindDown(this.element)
                  });
    },
    
    fadeOut: function () {
      new Effect.BlindUp(this.element);
      $$('select').each( function(e) { 
        e.style.visibility = 'visible';
      });
    },
    
    fadeOutListener: function (event) {
      var element = Event.findElement(event, 'a'); 
      if(typeof(element.id) == 'undefined') {
        this.fadeOut();
      }
    },
    
    loadContent: function (id, page) {
      var e    = 'FAQ_' + id;
      this.activate(e);
      var ajax =  new Ajax.Updater(this.textBox, AJAX_URL, {   
                    parameters:   {id: page, article_id: id},
                    evalScripts:  true
                  });
    },
    
    activate: function(e, state) {
      this.deactivate(e);
      if(state) {
        $(e).toggleClassName(state);
      } else if($(e).className == 'active') {
        $(e).toggleClassName('default');
      } else {
        $(e).toggleClassName('active');
      }
    },
    
    deactivate: function(e) {
      p = $(this.tab);
      for (var i=0; i < p.childNodes.length; i++) {
        if(typeof(p.childNodes[i].tagName) != 'undefined') {
          p.childNodes[i].childNodes[0].className = 'default';
        }
      }
    },
    
    showInspiration: function() {
      if(Element.getStyle('Inspiration', 'display') == 'none') {
        Effect.Appear('Shader', { duration: 1.0, from: 0.0, to: 0.7 } );
        Effect.BlindDown('Inspiration', {duration: 0.5});
      } else {
        Effect.Fade('Shader', {duration: 0.5});
        Effect.BlindUp('Inspiration', {duration: 0.5});
      }
    },
    
    bindEvents: function() {
      if(this.objectExists(this.element)) {
        Event.observe(this.element, 'click', this.fadeOutListener.bindAsEventListener(this));
        Event.observe('ShaderHandler', 'mouseover', this.showInspiration.bindAsEventListener(this));
      }
    },
    
    objectExists: function(object) {
      if(Object.inspect($(object)) != "null") {
        return true;
      }
      return false;
    }
    
  };
  
   
  /**
   * Leonardo Storefinder V 2
   * 
   */
  var StoreFinder = Class.create();
  StoreFinder.prototype = {
    initialize: function (l) {
      this.element  = null;
      this.language = l;
      this.filter   = 0;
      this.loadCountries();
    },
    
    // load countries
    loadCountries: function () {
      //$('StoreTowns').innerHTML   = "";
      //$('StoreEntries').innerHTML = "";
      //$('StoreDetails').innerHTML = "";
      this.load('countries', 'StoreCountries');
    },
    
    // 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/storefinder.php', {   
        parameters:   {type: t, id: id, filter: this.filter, language: this.language, country: this.country},
        evalScripts:  true
      });
    },
    
    // search towns
    searchTowns: function (k) {
      var ajax  = new Ajax.Updater($('StoreTowns'), BASE_URL+'soap/storefinder.php', {   
        parameters:   {type: 'towns', id: k, search: 1, filter: this.filter},
        evalScripts:  true
      });
    },
    
    // search stores
    searchStores: function (k) {
      var ajax  = new Ajax.Updater($('StoreEntries'), BASE_URL+'soap/storefinder.php', {   
        parameters:   {type: 'stores', id: k, search: 1, filter: this.filter},
        evalScripts:  true
      });
    },
    
    // search
    search: function() {
      //$('StoreTowns').innerHTML   = "";
      //$('StoreEntries').innerHTML = "";
      $('StoreDetails').innerHTML = "";
      var str = this;
      var k   = document.forms['StoreFinderForm'].elements['key'].value;
      new Ajax.Request(BASE_URL+'soap/storefinder.php', {   
        method:     'get',
        parameters: {type: 'search', id: k, filter: this.filter},
        onSuccess: function(transport){
          var r = transport.responseText;
          var d = r.evalJSON();
          if(!d.success) {
            alert(d.message);
          } else {
            str.searchTowns(k);
            str.searchStores(k);
            str.loadDetails(d.record.id);
          }
        } 
      }); 
      return false;
    },
    
    setFilter: function (filter, item) {
      this.filter = filter;
    },
    
    activate: function (e) {
      e.siblings().each(function(e){ e.className = 'default' });
      e.className = 'active';
    },
        
    initSlider: function() {
      var str     = this;
      this.hMax   = Element.getHeight("StoreTownMask");
      this.hCur   = Element.getHeight("StoreTownEntries");
      oPos        = Element.getStyle("StoreTownEntries", "top");
      this.pStart = 1*(oPos.slice(0,(oPos.length)-2));
      // debug $('DEBUG').innerHTML = 'H-Max: ' + this.hMax + ' ,H-Entries: ' + this.hCur;
      // max => entries
      if(this.hMax >= this.hCur) {
        $("SliderBar").hide();
      } else {
        this.hSlb = Element.getHeight("SliderBar");
        this.hSlh = Math.floor(this.hSlb*(this.hMax/this.hCur));
        Element.setStyle("SliderHandle",{height: (this.hSlh)+"px"});
        Element.show("SliderBar");
        new Control.Slider('SliderHandle','SliderBar',{axis: "vertical", onSlide: storeFinder.slide});
      }
    },
    
    slide: function(v) {
      if(isNaN(v)) {
        return;
      }
      var str = storeFinder;
      p       = Math.floor(-(v*(str.hCur-str.hMax))+str.pStart);
      $('DEBUG').innerHTML = 'slide: '+ v + ' ,H-Max: ' + str.hMax + ' ,H-Entries: ' + str.hCur + ' ,Top: ' + p + ' ,Start: ' + str.hSlb;
      Element.setStyle("StoreTownEntries",{top: (p)+"px"});
    }
    
  };
  
  
  /*
   * Leonardo Company Start
   * 
   */
  var Company = Class.create();
  Company.prototype = {
    initialize: function () {
      this.url      = null;
      this.pElement = $('CompanyStart');
  	  this.pElement.childElements().each( function(s) { 
        Effect.Appear(s, {duration:1, to:0.7});
      });
    },
    
    click: function (element, url) {
      this.url = url;
      var i = 0;
      this.pElement.childElements().each( function(s) {
        if(s.id != element) {
          i++;
          Effect.Fade(s, {duration: 2, to:0.1 });
        }
      });
      this.jump(i);
    },
    
    highlight: function (element) {
      //Effect.Highlight(element, {startcolor:'#ff99ff', endcolor:'#999999'});
      //Effect.Pulsate(element, { duration:10, from:0.7, pulses:10 });
      //Effect.Opacity(element, { from:0.5, to:1.0, duration:1.0 });
      new Effect.Opacity(element, {duration:0.3, from:0.7, to:1.0});
    },
    
    blur: function (element) {
      //Effect.Highlight(element, {startcolor:'#ff99ff', endcolor:'#999999'});
      //Effect.Pulsate(element, { duration:10, from:0.7, pulses:10 });
      new Effect.Opacity(element, {duration:0.3, to:0.7});
    },
    
    jump: function (i) {
      if(i==4) {
        document.location.href = this.url;
        //alert(i);
      }
    }
    
  };  
  

  /*
   * Leonardo Events
   * 
   */
  var Events = Class.create();
  Events.prototype = {
  	initialize: function () {
    },
    
    showCat: function (id) {
      this.element   = 'EventCatContent';
      $(this.element).innerHTML = '';
      $(this.element).hide();
      this.resetItems();
      this.highlight('Cat'+id);
      var ajax  = new Ajax.Updater(this.element, AJAX_URL, {   
                    parameters:   {type: 'eventCat', id: id},
                    evalScripts:  true
                  });
    },
    
    resetItems: function () {
      $$('#EventCatHeadlines a').each( function(e) { 
        e.className = '';
      });
    },
    
    highlight: function (mark) {
      $(mark).className = 'active';
    },
    
    showList: function (id, page, order) {
      this.element  = 'EventList';
      if($('EventListBackGroung').style.display != 'none') {
        Effect.Fade('EventListBackGroung');
        $('EventSelector').hide();
        Effect.BlindUp(this.element);
      } else {
        Effect.Appear('EventListBackGroung', { from:0, to:0.5 });
        var ajax      = new Ajax.Updater(this.element, AJAX_URL, {   
                          parameters:   {type: 'eventList', id: id, page: page, order: order},
                          evalScripts:  true
                        });
      }
    },
    
    reloadList: function (id, page, order) {
      this.element  = 'EventList';
      var ajax      = new Ajax.Updater(this.element, AJAX_URL, {   
                          parameters:   {type: 'eventList', id: id, page: page, order: order},
                          evalScripts:  true
                        });
    }
    
  };  
  

  
  /*
   * FormHandler
   * handles everything about forms
   */
  var FormHandler = Class.create();
  FormHandler.prototype = {
  	initialize: function () {
    },
    
    openAttribute: function (element, type, id, attr) {
      var ajax  = new Ajax.Updater(element, './ajax.php', {
                    asynchronous: true,
                    parameters:   {type: type, id: id, attributeID: attr}
                  });
    },
    
    clipBoard: function (formID) {
      var ajax  = new Ajax.Request('./ajax.php', {
                    parameters: $(formID).serialize(true)
                  }); 
    },
    
    submit: function (formID) {
      $(formID).submit();
    },
    
    loadUrl: function (url) {
      alert(url);
      document.location.href = url;
    },
    
    // f[key] => key
    getIndex: function (v) {
      var r = v.split("[");
      return ( r[1].substring(0, (r[1].length-1)) );
    },
    
    doPostBack: function(form, method, action, update, onFailure) {
      params      = $(form).serialize(true);
      var ajax    = new Ajax.Request($(form).action,   {
                      method:       (method) ? method : 'post',
                      parameters:   params,
                      evalScripts:  true,
                      onLoading:    scr.setCursor('wait'),
                      onComplete:   scr.setCursor('default'),
                      onSuccess:    function(transport) {
                                      //(update) ? $(update).innerHTML = transport.responseXML : '';
                                      eval(transport.responseText);
                                    },
                      onFailure:    function(){ 
                                      alert('Something went wrong...');
                                    }
                    });
     }
    
  };
 
  
  /*
   * Lexikon
   * 
   */  
  var Lex = Class.create();
  Lex.prototype = {
  	initialize: function () {
      var page = null;
    },
    fadeInContent: function (element, type, id, page) {
      this.element   = element;
      $(this.element).innerHTML = '';
      $(this.element).hide();
      $('ArticleBackground').style.display = "none";
      var ajax  = new Ajax.Updater(this.element, AJAX_URL, {   
                    parameters:   {type: type, id: id, page: this.page},
                    evalScripts:  true
                  });
    },
    highlight: function (id, article) {
      this.article  = article;
      var element   = id;
      $$('div.Alphabet ul li a').each( function(e) { 
        if(e.id == element) {
          e.className = 'active';
        } else {
          e.className = '';
        }
      });
    },
    next: function () {
      this.fadeInContent('ArticleAjax', 'glasslexikon', this.nextID);
    },
    prev: function () {
      this.fadeInContent('ArticleAjax', 'glasslexikon', this.prevID);
    }
  };  
  

// END