//Jsir menu written by mohammad esmaeilpour
if(!window.JsirMenu) {
	Element.extend({
		moveChildTo: function(e) {
			while (this.childNodes.length > 0) {
				e.appendChild(this.childNodes[0]);
			}
			return this;
		}
	});
	var JsirMenu = new Class({
		options: {
			'container': null,
			'direction': 'ltr',
			'type': 'horizental', //vertical or horizental
			'transition': 'none', //none, clip, fade, custom
			'className': 'JsirMenu',
			'hideDelay': 200
		},
		initialize: function(options) {
			this.setOptions(options);
			this.container = $(options.container);
			window.onload = (this.init.bind(this));
		},
	
		init: function() {
			var c = this.options.className;
			if(!$(this.container.parentNode).hasClass(c)) {
				var container = new Element('div');
				this.container.addClass(c).moveChildTo(container);
				this.container = container.inject(this.container);
			}
	
			this.container.className = '';
	
			[this.options.type, this.options.direction].each(function(c) {
				this.container.addClass(c);
			}, this);
	
			this.timers = {};
			this.trans = {};
			this.build();
		},
	
		reBuild: function(options) {
			this.options = $extend(this.options, options);
			this.init();
		},
	
		build: function() {
			var previous, root;
			
			$A(this.container.getElementsByTagName('ul')).each(function(item, i) {
				if(!i) return (root = item);
				
				var LI = $(item.parentNode)
				LI.setAttribute('id', 'jm_' + i);
				
				//if this is a first level submenu
				if (LI.parentNode == root) {
					switch(this.options.type) {
						case 'vertical':
							if(this.options.direction == 'ltr') {
								item.style.left = 0;
							} else {
								item.style.left = '-' + (item.offsetWidth - LI.offsetWidth) + "px";	
							}
							item.style.top = LI.offsetHeight + "px";
						break;
						case 'horizental':
						default:
							if(this.options.direction == 'ltr') {
								item.style.left = item.parentNode.offsetWidth + "px";
							} else {
								item.style.left = '-' + item.offsetWidth + "px";
							}
							item.style.top = 0;
						break;
					}
					
					LI.getElementsByTagName("a")[0].className = "main";
				} else {
					if(this.options.direction == 'ltr') {
						item.style.left = (previous.getElementsByTagName("a")[0].offsetWidth) + "px";
					} else {
						if(document.all && !window.XMLHttpRequest) {
							item.style.left = '-' + (previous.getElementsByTagName("a")[0].offsetWidth + 80) + "px";
						} else {
							item.style.left = '-' + (previous.getElementsByTagName("a")[0].offsetWidth) + "px";
						}
					}
					LI.getElementsByTagName("a")[0].className = "sub";
				}
				
				var scope = this;
				LI.onmouseover = function(e) { scope.onOver(e, LI); };
				LI.onmouseout = function(e) { scope.onOut(e, LI); };
			    
			    previous = item;
			    
			}, this);
	
			this.options.transition = this.getTransition(this.options.transition);
		},
		
		onOver: function(e, LI) {
			e = e || window.event;
			var element = (e.target || e.srcElement);
			
			do {
				if(element && element.nodeName && element.nodeName.toUpperCase() == 'LI') {
					window.clearInterval( this.timers[element.id] );
				}
				element = element.parentNode;
				if(element == this.container) break;
			} while(true);
			
			try {
				var UL = LI.getElementsByTagName("ul")[0];
				UL.style.visibility = "visible";
				if(LI.id in this.trans) return;
				this.trans[ LI.id ] = true;
				new this.options.transition($(UL), this);
			} catch(e) {};
		},
		
		onOut: function(e, LI) {
			var scope = this, timer = setTimeout(function() {
				var UL = LI.getElementsByTagName("ul")[0];
				UL.style.visibility = "hidden";
				delete scope.trans[ LI.id ];
			}, this.options.hideDelay);
			this.timers[ LI.id ] = timer;
		},
	
		getTransition: function(trn) {
			if((typeof trn == 'string') && ['clip', 'fade'].contains(trn.toLowerCase())) {
				trn = trn.substr(0, 1).toUpperCase() + trn.substr(1).toLowerCase();
				return eval('JMTransition' + trn);
			} else if(!(trn instanceof Object)) return JMTransitionNone;
			return trn;
		}
	});
	
	JsirMenu.implement(new Events, new Options);
	
	var JMTransitionNone = new Class({
		initialize: function(element, menu) {
			//no body
		}
	});
	
	var JMTransitionCommon = new Class({
		initialize: function(from, to) {
			this.from = from;
			this.to = to;
			this.point = from;
			this.start();
		},
		start: function() {
			this.set(this.from);
			this.timer = window.setInterval(this.transition.bind(this), 20);
		},
		transition: function() {
			if(this.point == this.to) {
				this.fireEvent('onEnd');
				return window.clearInterval(this.timer);
			}
			var d = (this.to-this.point)/4;
			this.point += d > 0 ? Math.ceil(d) : Math.floor(d);
			this.set(this.point);
		},
		set: function(p) {
			this.fireEvent('onSet', p);
		}
	});
	
	JMTransitionCommon.implement(new Events);
	
	var JMTransitionClip = JMTransitionCommon.extend({
		initialize: function(element, menu) {
			element.setStyle('clip', "rect(0px, 0px, 0px, 0px)");
			this.addEvent('onEnd', function() {
				element.style.clip = "inherit";
			});
			var w = element.offsetWidth;
			this.addEvent('onSet', function(p) {
				with(element.style) {
					clip = "rect(0px, "+ w +"px, "+ p +"px, 0px)";
				}
			});
			this.parent(0, element.offsetHeight);
		}
	});
	
	var JMTransitionFade = JMTransitionCommon.extend({
		initialize: function(element, menu) {
			element.alpha(0);
			this.addEvent('onEnd', function() {
				with(element.style) {
					opacity = 'inherit';
					filter	= 'inherit';
				}
			});
			this.addEvent('onSet', function(p) {
				with(element.style) {
					opacity = p/100;
					filter	= 'alpha(Opacity='+p+')';
				}
			});
			this.parent(0, 100);
		}
	});
	
}

