/**
 * 
 */

if (!window.Goodahead) {
    var Goodahead = Class.create();
}

Goodahead.Morph = Class.create(Effect.Base, {
    tempOptins : {},
    initialize: function(element) {
      this.element = $(element);
      if (!this.element) throw(Effect._elementDoesNotExistError);
      var options = Object.extend({
        style: { }
      }, arguments[1] || { });

      if (!Object.isString(options.style)) this.style = $H(options.style);
      else {
        if (options.style.include(':'))
          this.style = options.style.parseStyle();
        else {
          this.element.addClassName(options.style);
          this.style = $H(this.element.getStyles());
          this.element.removeClassName(options.style);
          var css = this.element.getStyles();
          this.style = this.style.reject(function(style) {
            return style.value == css[style.key];
          });
          options.afterFinishInternal = function(effect) {
            effect.element.addClassName(effect.options.style);
            effect.transforms.each(function(transform) {
              effect.element.style[transform.style] = '';
            });
          };
        }
      }
      this.tempOptins = options;
      this.start(options);
    },

    setup: function(){
      function parseColor(color){
        if (!color || ['rgba(0, 0, 0, 0)','transparent'].include(color)) color = '#ffffff';
        color = color.parseColor();
        return $R(0,2).map(function(i){
          return parseInt( color.slice(i*2+1,i*2+3), 16 );
        });
      }
      this.transforms = this.style.map(function(pair){
        var property = pair[0], value = pair[1], unit = null;

        if (value.parseColor('#zzzzzz') != '#zzzzzz') {
          value = value.parseColor();
          unit  = 'color';
        } else if (property == 'opacity') {
          value = parseFloat(value);
          if (Prototype.Browser.IE && (!this.element.currentStyle.hasLayout))
            this.element.setStyle({zoom: 1});
        } else if (Element.CSS_LENGTH.test(value)) {
            var components = value.match(/^([\+\-]?[0-9\.]+)(.*)$/);
            value = parseFloat(components[1]);
            unit = (components.length == 3) ? components[2] : null;
        }

        var originalValue = this.element.getStyle(property);
        return {
          style: property.camelize(),
          originalValue: unit=='color' ? parseColor(originalValue) : parseFloat(originalValue || 0),
          targetValue: unit=='color' ? parseColor(value) : value,
          unit: unit
        };
      }.bind(this)).reject(function(transform){
        return (
          (transform.originalValue == transform.targetValue) ||
          (
            transform.unit != 'color' &&
            (isNaN(transform.originalValue) || isNaN(transform.targetValue))
          )
        );
      });
    },

    update: function(position) {
      var style = { }, transform, i = this.transforms.length;
      while(i--)
        style[(transform = this.transforms[i]).style] =
          transform.unit=='color' ? '#'+
            (Math.round(transform.originalValue[0]+
              (transform.targetValue[0]-transform.originalValue[0])*position)).toColorPart() +
            (Math.round(transform.originalValue[1]+
              (transform.targetValue[1]-transform.originalValue[1])*position)).toColorPart() +
            (Math.round(transform.originalValue[2]+
              (transform.targetValue[2]-transform.originalValue[2])*position)).toColorPart() :
          (transform.originalValue +
            (transform.targetValue - transform.originalValue) * position).toFixed(3) +
              (transform.unit === null ? '' : transform.unit);
      this.element.setStyle(style, true);
    }
  });

Goodahead.Accordion = Class.create({
    element : null,
    options : {},
    effectParalel : null,
    initialize : function (element, params) {
        this.element = $(element);
        if (!this.element) throw ('Not set element');

        var options = Object.extend({
            'defaultWidth' : 50,
        },params || {});
        this.options = options;
        var els = this.element.select('li.apanel');
        var index = 0;
        els.each(function(el){
            el.origWidth = el.getWidth();
            el.indexId = index;
            index++;
            if (options['openItem']==null || options['openItem']=='undefined' || options['openItem']!=index) {
                el.setStyle('width:'+this.options['defaultWidth']+'px');
            }
            Event.observe(el,'mouseenter',this.enter.bind(this));
        }.bind(this));
        if (options['control']!=null && options['control']!='undefined') {
            var control = $(options['control']);
            var conEls = control.select('a.apanel-control');
            var index = 0;
            conEls.each(function(el){
                el.indexItem = index;
                index++;
                Event.observe(el,'click',this.controlClick.bind(this));
            }.bind(this));
        }
    },
    controlClick : function (event) {
        var element = event.element();
        var els = this.element.select('li.apanel');
        var el = els[element.indexItem];
        this.enter(el);
    },
    enter : function (event) {
        var element = 0;

        try {
            element = event.element();
            console.log(element);
            if (element==null || element=='undefined') {
                element = event;
            }
        } catch (e) {
            element = event;
        }
        if (!element.hasClassName('apanel')) {
            element = element.up('.apanel');
        }

        if (this.effectParalel) {
            this.effectParalel.cancel();
        }

        var els = this.element.select('li.apanel');
        var mas = [];
        els.each(function(el){
            var gaIndex = -1;
            if (element.indexId != null && element.indexId!='undefined') {
                gaIndex = element.indexId;
            }
            if (el.indexId != gaIndex && el.getWidth()!=this.options['defaultWidth']) {
                mas.push(new Goodahead.Morph(el,{sync:true,style:'width:'+this.options['defaultWidth']+'px'}));
            }
        }.bind(this));
        mas.push(new Goodahead.Morph(element,{sync:true,style:'width:'+element.origWidth+'px'}));
        this.effectParalel = new Effect.Parallel(mas);
    }
});
