window.addEvent('domready', function(){
 new MooMenu($E('ul', 'menuT'), {
  bgiframe: true, delay: 500,
  animate: {
   props: ['opacity', 'height'],
   opts: {duration:600, fps: 100, transition: Fx.Transitions.Quad.easeOut}
  },
  swapimage: true,
  suffix: '_f2'
 });
 
 /*
 if(window.ie6 || window.ie7){
	var zIndexNumber = 1000;
	$$('div').each(function(el,i){
		var pos=el.getStyle("position");
		if (pos == "relative" || pos == "absolute" || pos == "fixed") {
			el.setStyle('z-index',zIndexNumber);
			zIndexNumber -= 10;		
		}
	});
 };
 */
});

var MooMenu = new Class({
    options: {
        bgiframe: true,
        hoverClass: 'over',
        delay: 500,
        animate: {
            props: ['opacity', 'height'],
            opts: Class.empty
        },
       swapimage: false,
       suffix: '_f2'
    },
    initialize: function(el, options){
        this.setOptions(options);
        this.element = $(el);
        this.element.getElements('li').each(function(el){
            el.addEvents({
                'mouseover': this.over.bind(this, el),
                'mouseout': this.out.bind(this, el)
            })
        }, this);
        this.current = this.element.getElement('li.this');
        
        if (this.options.swapimage) {
         this.imgPathes = [];
         this.element.getChildren('li').each(function(item){
            var img=$E('img',item);
            var src=img.getProperty('src');
            var ext = src.substring(src.lastIndexOf('.'));
            this.imgPathes.push(src);
            this.imgPathes.push(src.replace(ext,this.options.suffix + ext));                     
         }.bind(this));
         var preloadedImages = new Asset.images(this.imgPathes);
         
         //current
         if (this.current) {
          var span=$E('a',this.current);
          if (span) span.addClass(this.options.hoverClass);
          var img=$E('img',this.current);
          if (img) img.src=ToggleFileSuffix(img.getProperty('src'),this.options.suffix,true);
         }
        }
    },
    over: function(el){
        $clear(el.sfTimer);
        if (!el.hasClass(this.options.hoverClass)) {
            el.addClass(this.options.hoverClass);
            var ul = el.getElement('ul');
            if (ul) {
                if (this.options.bgiframe) ul.bgiframe({opacity: false});
                ul.animate(this.options.animate);
            }
            el.getSiblings().each(function(ele){
                ele.removeClass(this.options.hoverClass);
                if (!ele.hasClass('this')) {
                 var span=$E('a',ele);
                 if (span) span.removeClass(this.options.hoverClass);             
                 var img=$E('img',ele);
                 if (img) img.src=ToggleFileSuffix(img.getProperty('src'),this.options.suffix,false);
                }
            }, this);
            this.swapImage(el);
        }
    },
    out: function(el){
        var ul = el.getElement('ul');
        var delay = 0;
        if (ul) delay = this.options.delay;
        el.sfTimer = (function(){
            el.removeClass(this.options.hoverClass);
            var iframe = el.getElement('iframe');
            if (iframe) iframe.remove();
            this.swapImageRestore(el);
        }).delay(delay, this);
    },
    swapImage: function(el) {
     if (!el.hasClass('this')) {
      var span=$E('a',el);
      if (span) span.addClass(this.options.hoverClass);       
      var img=$E('img',el);
      if (img) img.src=ToggleFileSuffix(img.getProperty('src'),this.options.suffix,true);
     }
    },
    swapImageRestore: function(el) {
     if (!el.hasClass('this')) {
      var span=$E('span',el);
      if (span) span.removeClass(this.options.hoverClass);             
      var img=$E('img',el);
      if (img) img.src=ToggleFileSuffix(img.getProperty('src'),this.options.suffix,false);
     }
    }
});
MooMenu.implement(new Options);

Element.extend({
    animate: function(obj){
        if (!this.Fx) {
            this.Fx = this.effects(obj.opts);
            this.now = this.getStyles.apply(this, obj.props);
            this.FxEmpty = {};
            for (var i in this.now) 
                this.FxEmpty[i] = 0;
        }
        if (obj.props.contains('height') || obj.props.contains('width')) {
            this.setStyle('overflow', 'hidden');
            this.getParents('ul').each(function(el){
                el.setStyle('overflow', 'visible');
            })
        }
        this.Fx.set(this.FxEmpty).start(this.now);
    },
    getParents: function(expr){
        var matched = [];
        var cur = this.getParent();
        while (cur && cur !== document) {
            if (cur.getTag().test(expr)) 
                matched.push(cur);
            cur = cur.getParent();
        }
        return matched;
    },
    getSiblings: function(){
        var children = this.getParent().getChildren();
        children.splice(children.indexOf(this), 1);
        return children;
    }
});

if (!$defined(Element.bgiframe)) {
   Element.extend({
       bgiframe: function(styles){
           if (window.ie6) {
               if (!this.getElement('iframe.bgiframe')) {
                   styles = styles || {};
                   var ifsrc = $pick(styles.src, "javascript:'<html><body style=\"background-color:transparent\"></body></html>'");
                   delete styles.src;
                   var ifopac = $pick(styles.opacity, true);
                   delete styles.opacity;
                   (new Element('iframe', {
                       'class': 'bgiframe',
                       'allowTransparency': 'true',
                       'scrolling': 'no',
                       frameborder: 0,
                       tabindex: -1,
                       src: ifsrc,
                       styles: $merge({
                           top: -this.getStyle('borderTopWidth').toInt(),
                           left: -this.getStyle('borderLeftWidth').toInt(),
                           width: this.offsetWidth,
                           height: this.offsetHeight
                       }, styles, {
                           display: 'block',
                           position: 'absolute',
                           zIndex: -1,
                           filter: ifopac ? "Alpha(Opacity='0')" : ''
                       })
                   })).injectBefore(this.getFirst());
               }
           }
           return this;
       }
   });
}

function ToggleFileSuffix(src,suffix,yesno) {
 var suffext = src.lastIndexOf(suffix + '.')>0?src.substring(src.lastIndexOf(suffix + '.')):'';
 var ext = src.substring(src.lastIndexOf('.'));
 if (suffext=='') {
  if (yesno) src=src.replace(ext,suffix + ext);
 } else {
  if (!yesno) src=src.replace(suffext,ext);
 }   
 return src;
};