/**
  * Initializes a new scroll control.
  * Usage:
  * var myScroller = new ScrollControl($('content'), {}, $('scrollUpBtn'), $('scrollDownBtn'));
  *
  * @param {HTMLelement} contentElement which contains the content which shall be scrolled
  * @param {Object}    optional, you can adjust different settings here
  * @param {HTMLelement} scrollUpBtn a button which can be clicked to scroll up
  * @param {HTMLelement} scrollDownBtn a button which can be clicked to scroll down
  */

var ScrollControl = new Class({
  options: {
    'wheelStepSize': 15, //amount of pixels that the content is scrolled on mousewheel event
    'scrollStepSize': 20 //amount of pixels that the content is scrolled on button click
  },
  initialize: function(contentElement, options, scrollUpBtn, scrollDownBtn) {
    this.setOptions(options);

    this.contentElement = contentElement;
    this.scrollUpBtn = scrollUpBtn;
    this.scrollDownBtn = scrollDownBtn;

    var contentHeight = this.contentElement.getSize().y;
    this.currentStep = 0;
    this.scrollHeight = this.contentElement.getScrollSize().y - contentHeight;

    if($defined(this.scrollUpBtn)) {
      this.scrollUpBtn.addEvents({
        'mousedown': this.startScrolling.bind(this, 'up'),
        'mouseup': this.stopScrolling.bind(this),
        'mouseout': this.stopScrolling.bind(this)
      });
    }
    if($defined(this.scrollDownBtn)) {
      this.scrollDownBtn.addEvents({
        'mousedown': this.startScrolling.bind(this, 'down'),
        'mouseup': this.stopScrolling.bind(this),
        'mouseout': this.stopScrolling.bind(this)
      });
    }

    this.contentElement.addEvent('mousewheel', function(e){
      e = new Event(e).stop();
      if(e.wheel >= 0) {
        this.doWheelUp(e);
      }
      if(e.wheel < 0) {
        this.doWheelDown(e);
      }
    }.bind(this));
  },
  

  doWheelUp: function(e) {
    new Event(e).stop();
    this.scrollUpBtn.tween('opacity', [0.65,1]);
    this.scrollUp(this.options.wheelStepSize);
  },
  
  doWheelDown: function(e) {
    new Event(e).stop();
    this.scrollDownBtn.tween('opacity', [0.65,1]);
    this.scrollDown(this.options.wheelStepSize);
  },
  
  startScrolling: function(mode) {
    if(mode == 'up') {
      this.scrollIntervall = this.scrollUp.periodical(50, this, this.options.scrollStepSize);
    } else {
      this.scrollIntervall = this.scrollDown.periodical(50, this, this.options.scrollStepSize);
    }
  },
  
  stopScrolling: function() {
    $clear(this.scrollIntervall);
  },
  
  refresh: function(step) {
    if(step == this.currentStep) {
      return;
    }
    step = Math.round(step.toInt().limit(0, this.scrollHeight));
    this.currentStep = step;
    this.contentElement.scrollTo(0,step);
  },

  scrollUp: function(amount) {
    this.refresh(this.currentStep - amount);
  },

  scrollDown: function(amount) {
    this.refresh(this.currentStep + amount);
  }
});
ScrollControl.implement(new Options);



/**
 * SqueezeBox - Expandable Lightbox
 *
 * Allows to open various content as modal,
 * centered and animated box.
 *
 * Dependencies: MooTools 1.2 trunk (04/2008)
 *
 * Inspired by
 *  ... Lokesh Dhakar - The original Lightbox v2
 *
 * @version   1.1 rc2
 *
 * @license   MIT-style license
 * @author    Harald Kirschner <mail [at] digitarald.de>
 * @copyright Author
 */
var SqueezeBox = {

  presets: {
    size: {x: 600, y: 450},
    sizeLoading: {x: 200, y: 150},
    marginInner: {x: 20, y: 20},
    marginImage: {x: 50, y: 75},
    handler: false,
    target: null,
    closable: true,
    closeBtn: true,
    zIndex: 65555,
    overlayOpacity: 0.7,
    classWindow: '',
    classOverlay: '',
    overlayFx: {},
    resizeFx: {},
    contentFx: {},
    parse: false, // 'rel'
    parseSecure: false,
    ajaxOptions: {},
    onOpen: $empty,
    onClose: $empty,
    onUpdate: $empty,
    onResize: $empty,
    onMove: $empty,
    onShow: $empty,
    onHide: $empty
  },

  initialize: function(presets) {
    if (this.options) return this;
    this.presets = $merge(this.presets, presets);
    this.options = {};
    this.setOptions(this.presets).build();
    this.bound = {
      window: this.reposition.bind(this, [null]),
      scroll: this.checkTarget.bind(this),
      close: this.close.bind(this),
      key: this.onKey.bind(this)
    };
    this.isOpen = this.isLoading = false;
    return this;
  },

  build: function() {
    this.overlay = new Element('div', {
      id: 'sbox-overlay',
      styles: {display: 'none', zIndex: this.options.zIndex}
    });
    this.content = new Element('div', {id: 'sbox-content'});
    this.closeBtn = new Element('a', {id: 'sbox-btn-close', href: '#'});
    this.win = new Element('div', {
      id: 'sbox-window',
      styles: {display: 'none', zIndex: this.options.zIndex + 2}
    }).adopt(this.closeBtn, this.content);
    this.fx = {
      overlay: new Fx.Tween(this.overlay, $merge({
        property: 'opacity',
        onStart: Events.prototype.clearChain,
        duration: 250,
        link: 'cancel'
      }, this.options.overlayFx)).set(0),
      win: new Fx.Morph(this.win, $merge({
        onStart: Events.prototype.clearChain,
        unit: 'px',
        duration: 750,
        transition: Fx.Transitions.Quint.easeOut,
        link: 'cancel',
        unit: 'px'
      }, this.options.resizeFx)),
      content: new Fx.Tween(this.content, $merge({
        property: 'opacity',
        duration: 250,
        link: 'cancel'
      }, this.options.contentFx)).set(0)
    };
    $(document.body).adopt(this.overlay, this.win);
  },

  assign: function(to, options) {
    return to.addEvent('click', function() {
      return !SqueezeBox.fromElement(this, options);
    });
  },

  fromElement: function(from, options) {
    this.initialize();
    if (this.element) this.trash();
    this.element = $(from);
    this.setOptions($merge(this.presets, options || {}));
    if (this.element && this.options.parse) {
      var obj = this.element.getProperty(this.options.parse);
      if (obj && (obj = JSON.decode(obj, this.options.parseSecure))) this.setOptions(obj);
    }
    this.assignOptions();
    this.url = ((this.element) ? (this.options.url || this.element.get('href')) : from) || '';
    var handler = this.options.handler;
    if (handler) return this.setContent(handler, this.parsers[handler].call(this, true));
    var ret = false;
    this.parsers.some(function(parser, key) {
      var content = parser.call(this);
      if (content) {
        ret = this.setContent(key, content);
        return true;
      }
      return false;
    }, this);
    return ret;
  },

  assignOptions: function() {
    this.overlay.set('class', this.options.classOverlay);
    this.win.set('class', this.options.classWindow);
    if (Browser.Engine.trident4) this.win.addClass('sbox-window-ie6');
  },

  close: function(e) {
    var stoppable = ($type(e) == 'event');
    if (stoppable) e.stop();
    if (!this.isOpen || (stoppable && !$lambda(this.options.closable).call(this, e))) return this;
    this.fx.overlay.start(0).chain(this.toggleOverlay.bind(this));
    this.win.setStyle('display', 'none');
    this.trash();
    this.toggleListeners();
    this.isOpen = false;
    this.fireEvent('onClose', [this.content]);
    return this;
  },

  trash: function() {
    this.element = this.asset = null;
    this.options = {};
    this.removeEvents().setOptions(this.presets).callChain();
  },

  onError: function() {
    this.asset = null;
    this.setContent('string', 'Error during loading');
  },

  setContent: function(handler, content) {
    if (!this.handlers[handler]) return false;
    this.content.className = 'sbox-content-' + handler;
    this.applyTimer = this.applyContent.delay(this.fx.overlay.options.duration, this, this.handlers[handler].call(this, content));
    if (this.overlay.retrieve('opacity')) return this;
    this.toggleOverlay(true);
    this.fx.overlay.start(this.options.overlayOpacity);
    return this.reposition();
  },

  applyContent: function(content, size) {
    this.applyTimer = $clear(this.applyTimer);
    this.hideContent();
    if (!content) {
      this.toggleLoading(true);
    } else {
      if (this.isLoading) this.toggleLoading(false);
      this.fireEvent('onUpdate', [this.content], 20);
    }
    this.content.empty();
    if (['string', 'array', false].contains($type(content))) this.content.set('html', content || '');
    else this.content.adopt(content);
    this.callChain();
    if (!this.isOpen) {
      this.toggleListeners(true);
      this.resize(size, true);
      this.isOpen = true;
      this.fireEvent('onOpen', [this.content]);
    } else {
      this.resize(size);
    }
  },

  resize: function(size, instantly) {
    var box = document.getSize(), scroll = document.getScroll();
    this.size = $merge((this.isLoading) ? this.options.sizeLoading : this.options.size, size);
    var to = {
      width: this.size.x,
      height: this.size.y,
      left: (scroll.x + (box.x - this.size.x - this.options.marginInner.x) / 2).toInt(),
      top: (scroll.y + (box.y - this.size.y - this.options.marginInner.y) / 2).toInt()
    };
    $clear(this.showTimer || null);
    this.hideContent();
    if (!instantly) {
      this.fx.win.start(to).chain(this.showContent.bind(this));
    } else {
      this.win.setStyles(to).setStyle('display', '');
      this.showTimer = this.showContent.delay(50, this);
    }
    return this.reposition();
  },

  toggleListeners: function(state) {
    var fn = (state) ? 'addEvent' : 'removeEvent';
    this.closeBtn[fn]('click', this.bound.close);
    this.overlay[fn]('click', this.bound.close);
    document[fn]('keydown', this.bound.key)[fn]('mousewheel', this.bound.scroll);
    window[fn]('resize', this.bound.window)[fn]('scroll', this.bound.window);
  },

  toggleLoading: function(state) {
    this.isLoading = state;
    this.win[(state) ? 'addClass' : 'removeClass']('sbox-loading');
    if (state) this.fireEvent('onLoading', [this.win]);
  },

  toggleOverlay: function(state) {
    this.overlay.setStyle('display', (state) ? '' : 'none');
    $(document.body)[(state) ? 'addClass' : 'removeClass']('body-overlayed');
  },

  showContent: function() {
    if (this.content.get('opacity')) this.fireEvent('onShow', [this.win]);
    this.fx.content.start(1);
  },

  hideContent: function() {
    if (!this.content.get('opacity')) this.fireEvent('onHide', [this.win]);
    this.fx.content.set(0);
  },

  onKey: function(e) {
    switch (e.key) {
      case 'esc': this.close(e);
      case 'up': case 'down': return false;
    }
  },

  checkTarget: function(e) {
    return this.content.hasChild(e.target);
  },

  reposition: function() {
    var size = document.getSize(), scroll = document.getScroll();
    this.overlay.setStyles({
      left: scroll.x + 'px',
      top: scroll.y + 'px',
      width: size.x + 'px',
      height: size.y + 'px'
    });
    this.win.setStyles({
      left: (scroll.x + (size.x - this.win.offsetWidth) / 2).toInt() + 'px',
      top: (scroll.y + (size.y - this.win.offsetHeight) / 2).toInt() + 'px'
    });
    return this.fireEvent('onMove', [this.overlay, this.win]);
  },

  removeEvents: function(type){
    if (!this.$events) return this;
    if (!type) this.$events = null;
    else if (this.$events[type]) this.$events[type] = null;
    return this;
  },

  extend: function(properties) {
    return $extend(this, properties);
  },

  handlers: new Hash(),

  parsers: new Hash()

};

SqueezeBox.extend(new Events($empty)).extend(new Options($empty)).extend(new Chain($empty));

SqueezeBox.parsers.extend({

  image: function(preset) {
    return (preset || (/\.(?:jpg|png|gif)$/i).test(this.url)) ? this.url : false;
  },

  clone: function(preset) {
    if ($(this.options.target)) return $(this.options.target);
    if (this.element && !this.element.parentNode) return this.element;
    var bits = this.url.match(/#([\w-]+)$/);
    return (bits) ? $(bits[1]) : (preset ? this.element : false);
  },

  ajax: function(preset) {
    return (preset || (this.url && !(/^(?:javascript|#)/i).test(this.url))) ? this.url : false;
  },

  iframe: function(preset) {
    return (preset || this.url) ? this.url : false;
  },

  string: function(preset) {
    return true;
  }
});

SqueezeBox.handlers.extend({

  image: function(url) {
    var size, tmp = new Image();
    this.asset = null;
    tmp.onload = tmp.onabort = tmp.onerror = (function() {
      tmp.onload = tmp.onabort = tmp.onerror = null;
      if (!tmp.width) {
        this.onError.delay(10, this);
        return;
      }
      var box = document.getSize();
      box.x -= this.options.marginImage.x;
      box.y -= this.options.marginImage.y;
      size = {x: tmp.width, y: tmp.height};
      for (var i = 2; i--;) {
        if (size.x > box.x) {
          size.y *= box.x / size.x;
          size.x = box.x;
        } else if (size.y > box.y) {
          size.x *= box.y / size.y;
          size.y = box.y;
        }
      }
      size.x = size.x.toInt();
      size.y = size.y.toInt();
      this.asset = $(tmp);
      tmp = null;
      this.asset.setProperties({width: size.x, height: size.y});
      this.applyContent(this.asset, size);
    }).bind(this);
    tmp.src = url;
    if (tmp && tmp.onload && tmp.complete) tmp.onload();
    return (this.asset) ? [this.asset, size] : null;
  },

  clone: function(el) {
    return el.clone();
  },

  adopt: $arguments(0),

  ajax: function(url) {
    this.asset = new Request.HTML($merge({
      method: 'get'
    }, this.options.ajaxOptions)).addEvents({
      onSuccess: function(resp) {
        this.applyContent(resp);
        this.asset = null;
      }.bind(this),
      onFailure: this.onError.bind(this)
    });
    this.asset.send.delay(10, this.asset, [{url: url}]);
  },

  iframe: function(url) {
    return new Element('iframe', $merge({
      src: url,
      frameBorder: 0,
      width: this.options.size.x,
      height: this.options.size.y
    }, this.options.iframeOptions));
  },

  string: function(str) {
    return str;
  }

});

SqueezeBox.handlers.url = SqueezeBox.handlers.ajax;
SqueezeBox.parsers.url = SqueezeBox.parsers.ajax;
SqueezeBox.parsers.adopt = SqueezeBox.parsers.clone;



var Ticker = new Class({
  options: {
    speed: 1000,
    delay: 5000,
    onComplete: Class.empty,
    onStart: Class.empty
  },
  initialize: function(container,items,options){
    this.setOptions(options);
    this.el = $(container);
    this.items = items;
    var w = 0;
    this.items.each(function(li,index) {
      w += li.getSize().x
    });
    this.el.setStyles({
      position: 'relative',
      top: 0,
      left: 0,
      width: w
    });
    this.periodical =
    this.fx = new Fx.Morph(this.el,{duration:this.options.speed,transition: Fx.Transitions.Cubic.easeInOut,onComplete:function() {
      var i = (this.current==0)?this.items.length:this.current;
      this.items[i-1].injectInside(this.el);
      this.el.setStyle('left',0);
    }.bind(this)});
    this.current = 0;
    this.next.bind(this).delay(this.options.delay);
  },
  next: function() {
    this.current++;
    if (this.current >= this.items.length) this.current = 0;
    this.fx.start({
      top: this.items[this.current].offsetTop,
      left: -this.items[this.current].offsetLeft
    });
    this.next.bind(this).delay(this.options.delay);
  }
});
Ticker.implement(new Options);


var MooSelect = new Class({
  Implements: [Events, Options],
  options: {
    container: false,
    baseClass : 'cust-sel',
    offset: 0
  },
  source : false,
  selected : false,
  _select : false,
  current : false,
  selectedOption : false,
  dropDown : false,
  optionsContainer : false,
  hiddenInput : false,

  initialize: function(options){
    this.setOptions(options);
    if ( !this.options.container ) return;
    this.selected = false;
    this.source = $(this.options.container).getElement('select');
    this.buildFrameWork();
    $(this.source).getElements('option').each( this.addOption, this );
    $(this.options.container).set('html','');
    this._select.injectInside($(this.options.container));
    this.bindEvents();
  },

  buildFrameWork : function() {
    this._select = new Element('div').addClass( this.options.baseClass );
    this.current = new Element('div').addClass('selected').injectInside($(this._select));
    this.selectedOption = new Element('div').addClass('selected-option').injectInside($(this.current));
    this.dropDown = new Element('div').addClass('dropdown').injectInside($(this.current));
    new Element('div').addClass('clear').injectInside($(this._select));
    this.optionsContainer = new Element('div').addClass('options-container').injectInside($(this._select));
    this.optionsContainer.setStyle('margin-top',-this.options.offset+'px');

    this.hiddenInput = new Element('input').setProperties({
      type  : 'hidden',
      name  : this.source.getProperty('name')
    }).injectInside($(this._select));
  },

  bindEvents : function() {
    document.addEvent('click', function(e) {
      if ($defined(this.optionsContainer)) {
        if ( this.optionsContainer.getStyle('display') == 'block') { this.onDropDown(); }
      }
    }.bind(this));
    $(this.options.container).addEvent( 'click', function(e) { new Event(e).stop(); } );
    this.current.addEvent('click', this.onDropDown.bindWithEvent(this) );
  },

  addOption: function( option ){
    var o = new Element('div').addClass('option').setProperty('value',option.value);
    if ( option.disabled ) { o.addClass('disabled') } else {
      o.addEvents( {
        'click': this.onOptionClick.bindWithEvent(this),
        'mouseout': this.onOptionMouseout.bindWithEvent(this),
        'mouseover': this.onOptionMouseover.bindWithEvent(this)
      })
    }
    if ( $defined(option.getProperty('class')) && $chk(option.getProperty('class')) )
      o.addClass(option.getProperty('class'));
    if ( option.selected ) {
      if ( this.selected) this.selected.removeClass('selected');
      this.selected = o;
      o.addClass('selected');
      this.selectedOption.set('text',option.text);
      this.hiddenInput.setProperty('value',option.value);
    }
    o.set('text',option.text);
    o.inject($(this.optionsContainer),'bottom');
  },

  onDropDown : function( e ) {
      if ( this.optionsContainer.getStyle('display') == 'block') {
        this.optionsContainer.setStyle('display','none');
      } else {
        this.optionsContainer.setStyle('display','block');
        this.selected.addClass('selected');
      }
  },

  onOptionClick : function(e) {
    var event = new Event(e);
    if ( this.selected != event.target ) {
      this.selected.removeClass('selected');
      event.target.addClass('selected');
      this.selected = event.target;
      this.selectedOption.set('text',this.selected.get('text'));
      this.hiddenInput.setProperty('value',this.selected.getProperty('value'));
     }
    this.onDropDown();
  },

  onOptionMouseover : function(e){
    var event = new Event(e);
    this.selected.removeClass('selected');
    event.target.addClass('selected');
  },

  onOptionMouseout : function(e){
    var event = new Event(e);
    event.target.removeClass('selected');
  }
});


var MooBar = new Class({
  Implements: [Events, Options],
  options: {
    ignoreMouse: false,
    horizontal: false
  },
  initialize: function(content,contentheight,options) {
    this.setOptions(options);
    this.mooscrollbar = new Element('div',{
      'class': 'moobar'
    }).injectBefore($(content));
    this.mooknob = new Element('div',{
      'class': 'mooknob'
    }).injectInside(this.mooscrollbar);
    this.mooscrollbar.setStyles({
      'height': contentheight
    });
    var steps = (this.options.horizontal?(content.getScrollSize().x - content.getSize().x):(content.getScrollSize().y - content.getSize().y));
    var slider = new Slider(this.mooscrollbar, this.mooknob, {
      steps: steps,
      mode: (this.options.horizontal?'horizontal':'vertical'),
      onChange: function(step){
        // Scrolls the content element in x or y direction.
        var x = (this.options.horizontal?step:0);
        var y = (this.options.horizontal?0:step);
        content.scrollTo(x,y);
      }
    }).set(0);
    if( !(this.options.ignoreMouse) ) {
      // Scroll the content element when the mousewheel is used within the 
      // content or the scrollbar element.
      $$(content, this.mooscrollbar).addEvent('mousewheel', function(e){
        e = new Event(e).stop();
        var step = slider.step - e.wheel * 30;
        slider.set(step);
      });
    }
    // Stops the handle dragging process when the mouse leaves the document body.
    $(document.body).addEvent('mouseleave',function(){
      slider.drag.stop();
    });

    return this.mooscrollbar;
  }
});