var _=function(obj){
	if(typeof obj=="string" && document.getElementById) obj=document.getElementById(obj);
	return obj;
}


//Element styles functions
var CSS=function(obj){
	return {
		//convert js style property to css property (zIndex -> z-index)
		js2css:function(prop){
			return prop.replace(/([A-Z])/g,"-$1").toLowerCase();
		},
		//get style|styles value from css element (arguments=[string|hash|array])
		get:function(prop){
			if(typeof prop=="string"){
				if(obj.currentStyle) return obj.currentStyle[prop];
				if(window.getComputedStyle) return window.getComputedStyle(obj,null).getPropertyValue(this.js2css(prop));
			}
			else if(prop){
				var style={};
				for(var i in prop){
					if(prop.length) i=prop[i]; //get prop if array
					style[i]=this.get(i);
				}
				return style;
			}
			else return 0;
		},
		//set new styles to element & return old styles (arguments=[hash])
		set:function(hash){
			var style={};
			for(var i in hash){
				style[i]=this.get(i);
				obj.style[i]=hash[i];
			}
			return style;
		},
		//copy style|styles from obj to anoter element & return this styles (arguments=[string|array])
		copy:function(prop,to){
			if(typeof prop=="string")
				return (to.style[prop]=this.get(prop));
			else if(prop){
				var style=this.get(prop);
				for(var i in style)
					to.style[i]=style[i];
				return style;
			}
			return null;
		},
		//check current element style|styles (arguments=[hash])
		check:function(hash){
			for(var i in hash)
				if(hash[i]!=this.get(i))
					return false;
			return true;
		}
	}
};





(Smooth=function(opt){this.init(opt)}).prototype={
	init:function(opt){
		//init values & default styles
		this.opt=opt;
		this.obj=opt.obj || this.obj;
		this.prop=opt.prop || this.prop;
		this.from=opt.from || 0;
		this.to=opt.to || 100;
		this.inversion=opt.inversion || (opt.to!=undefined);
	},
	go:function(){
		this.start(this.from, this.to);
	},
	back:function(){
		this.start(this.to, this.from);
	},
	start:function(from, to){
		if(this.tm) this.end(1);
		this.d=from<to?1:-1; //direction
		this.onstart(this.d);
		this.startPos=this.curPos=from;
		this.endPos=to;
		this.inc=(this.d<0 && this.inversion)?4:(this.endPos-this.startPos)/2*this.d; //increment
		this.play(); 
	},
	play:function(){
		var _this=this;
		this.inc=Math.round(this.inc-(this.inc/3)*(this.inversion?this.d:1)); //formula
		this.curPos+=this.inc*this.d;
		if(this.endPos*this.d<this.curPos*this.d) return this.end();
		this.obj.style[this.prop]=this.curPos+"px";	
		this.tm=setTimeout(function(){_this.play()},10);
	},
	end:function(false_start){
		clearTimeout(this.tm);
		this.tm=null;
		this.curPos=this.endPos;
		this.obj.style[this.prop]=this.curPos+"px";
		if(!false_start) this.onend(this.d);
		return false;
	},
	onstart:function(){},
	onend:function(){}
};



var SlideModule={
	init:function(obj){
		//init smooth
		obj.old_styles=CSS(obj).get(["position","visibility","display","overflow", "height", "paddingTop", "paddingBottom"]);
		obj.smooth=new Smooth({"obj":obj, prop:"height", to:this.checkHeight(obj)});
		obj.smooth.onstart=function(d){  obj.a.className='over'; };
		obj.smooth.onend=function(d){ if(d>0){ CSS(obj).set({height:"auto", overflow:obj.old_styles.overflow}); obj.a.className='over';} else{ CSS(obj).set({display:"none"}); obj.a.className='';} };
	},
	checkHeight:function(obj){
		CSS(obj).set({position:"absolute", visibility:"hidden", display:"block", overflow:"visible", height:"auto", paddingTop:0, paddingBottom:0});
		var h=obj.offsetHeight;
		CSS(obj).set(obj.old_styles);
		return h;
	},
	toogle:function(obj){
		if(obj.isShow==undefined) obj.isShow=(obj.style.display=="block");
		this[obj.isShow?"hide":"show"](obj);
		obj.isShow=!obj.isShow;
		return false;
	},
	show:function(obj){
		this.play(obj, 1);
	},
	hide:function(obj){
		this.play(obj, -1);
	},
	play:function(obj, d){
		if(!obj.smooth) this.init(obj);
		else obj.smooth.to=this.checkHeight(obj);
		CSS(obj).set({display:"block", overflow:"hidden"});
		obj.smooth[d>0?"go":"back"]();
	}
};


function init_mail(id){
	_(id).innerHTML=_(id).lang+"@"+_(id).innerHTML;
	_(id).href="mailto:"+_(id).innerHTML;
}


function init_menu(id){
	var tags =_(id).getElementsByTagName("i");
	for (i=0; i < tags.length; i++) {
		tag=tags[i].parentNode.parentNode;
		tag.block=tag.getElementsByTagName("ul")[0];
		if(!tag.block) continue;
		tag.block.a=tags[i].parentNode;
		tag.onmouseover=function() {
			var _this=this;
			this.isOver=1;
			if(this.isOpen) return false;
			setTimeout(function(){if(!_this.isOpen && _this.isOver){ SlideModule.toogle(_this.block); _this.isOpen=1}},10);
		}
		tag.onmouseout=function() {
			var _this=this;
			this.isOver=0;
			if(!this.isOpen) return false;
			setTimeout(function(){if(_this.isOpen && !_this.isOver){ SlideModule.toogle(_this.block); _this.isOpen=0}},10);
		}
	}
}


document.onload=function(){
	init_menu("nav");
}

