
var navigation = {
	init: function(){
		this.oMenu = (new oMenu()).init($(".information .menu ul"));
	}
}


function oMenu(){}
oMenu.prototype = {
	aItems: new Array(),
	aClones: new Array(),
	aSpeed: new Array(),
	toRemove: new Array(),

	is: {
		drag: false,
		clicked: false,
		toclick: true,
		animate: false,
		moveall: false,
		notAni: false,
		loading: false,
		corrected: false,
		played: false,
		withsound: true
	},

	_hli: 53,
	
	_a: function(iy){
		return iy-this.d.top;
	},
	_startdrag: function(oEvt){
		oEvt.preventDefault();
		$(document.body).bind("selectstart", function(evt){
			evt.preventDefault();
			return false;
		});
	},
	_enddrag: function(){
		$(document.body).unbind("selectstart");
	},
	
	init: function(ptr){

		this.ptr = $(ptr);
		this.oParent = this.ptr.parent();
		
		this.prepare();
		this.events();
		this.checkscreen();
		
		return this;
	},
	prepare: function(){
		var oThis = this;

		if(this.is.withsound && $.browser.safari){
			swfobject.embedSWF("/sound.swf", "MenuSound", "0", "0", "9.0.0", null, null, { allowScriptAccess: "always" });
			this.menusound = swfobject.getObjectById("MenuSound");
		}

		this.d = {
			width: this.oParent.width(),
			height: this.oParent.height(),
			top: this.oParent.offset().top
		}
		
		var _c = 0;
		var _t = 0;
		this.ptr.find("li").each(function(i){
			oThis.aItems.push($(this).css({ position: "absolute", top: _t, left: 10, width: 245 }));
			_t += oThis._hli;
			if($(this).is(".selected")){
				_c = i;
				oThis.current = $(this);
			}
		})
		this.jh = _t;
		
		setInterval(function(){
			if(oThis.is.drag)
				oThis.checkSpeed();
		}, 1)
		
		var _t = this.getCurrentTop()-_c*this._hli;
		this.curt = _t;
		
		this.ptr.css({ top: _t });
		this.aClones.push({ ptr: this.ptr, cy: 0, t: _t, e: { t: false, b: false } });
		ajaxPages.addCurrent(oThis.current.find("a"));
		
	},
	events: function(){
		var oThis = this;
				
		$(document).mousemove(function(evt){
			if(oThis.is.drag){
				$(document.body).focus();
				oThis.onmove(evt);
				return false;
			}
		}).mouseup(function(evt){
			oThis.onmouseup(evt);
		}).bind("dragstart", function(){
			return false;
		})
		

		this.oParent.mousedown(function(evt){
			oThis.onmousedown(evt);
		}).mouseup(function(evt){
			oThis.onmouseup(evt);
		});
		
		this.ptr.find("a").click(function(){
			var link = $(this);
			oThis.is.clicked = true;
			if(!oThis.is.toclick){
				oThis.is.toclick = true;
				return false;
			}
			
			var t = (oThis.d.height/2)-Math.ceil(oThis._hli/2) - (link.parents("li").offset().top - oThis.d.top);
			if(t > 0) t +=7
			else t -= 7;
			oThis.animate(t, 800, "easeInOutCirc", function(){
				this.correct();
			});
			return false;
		}).mousedown(function(oEvt){
			oThis.onmousedown(oEvt);
			$(document.body).focus();
			return false;
		})
		.bind("dragstart", function(){
			return false;
		})
		
	},
	
	checksound: function(bool){
		if(this.is.withsound){
			if(Math.abs(Math.abs(this.curt) - Math.abs(this.aClones[0].t)) > 53)
				this.is.played = true;
		
			if((this.is.played || bool) && this.menusound && typeof this.menusound.play != 'undefined'){
				this.menusound.play();
				this.is.played = false;
				this.curt = this.aClones[0].t;
			}
		}
	},
	
	onmove: function(evt){
		this.is.toclick = false;
		this.d.cy = evt.clientY;
		
		for (var i=0; i < this.aClones.length; i++) {
			var c = this.aClones[i];
			var cy = this._a(evt.clientY-c.cy);
			c.ptr.css({ top: cy });
			c.t = cy;
		};
		
		if(!$.browser.safari)
			this.checkSpeed();
			
		this.checksound();
		
		this.checkscreen(evt);
	},
	onmouseup: function(evt){
		var oThis = this;
		this.is.drag = false;
		this._enddrag(evt);
		
		setTimeout(function(){
			if(!oThis.is.clicked && !oThis.is.toclick){
				oThis.is.toclick = true;
			}
		}, 50)
		
		this.moveAll(this.getSpeed(), evt);
		this.aSpeed = new Array();
		
	},
	onmousedown: function(evt){
		if(!this.is.notAni){
			if(this.tAnim){
				this.tAnim.stop();
				delete this.tAnim;
				this.is.animate = false;
			}
			
			this.curt = this.aClones[0].t;
			
			this.checkEdges();

			this.aSpeed = new Array();
			this.is.drag = true;
			this.is.clicked = false;
			this.is.moveall = false;

			this.d.top = this.oParent.offset().top;
			for (var i=0; i < this.aClones.length; i++) {
				this.aClones[i].cy = this._a(evt.clientY-this._a(this.aClones[i].ptr.offset().top));
			};

			this._startdrag(evt);
		}
	},
	
	moveAll: function(speed, evt){
		var oThis = this;
		
		
		if(!this.is.animate && !this.is.moveall){
			this.is.animate = true;
			this.is.moveall = true;

			if((speed < 5 && speed > 0) || (speed > -5 && speed < 0)) speed = 0;
			speed *= 5;
			
			if(speed && speed != 0){
				this.animate(speed, 800, "easeOutQuart", function(){
					this.is.animate = false;
					this.correct();
				})
			} else {
				this.is.animate = false;
				this.correct();
			}
		}
	},
	animate: function(speed, duration, easing, callback){
		var oThis = this;
		this.tAnim = new Animate(function(pos, prev){
			var now = Math.round(speed*pos - speed*prev);
			for (var i=0; i < oThis.aClones.length; i++) {
				var _t = oThis.aClones[i].t + now;
				oThis.aClones[i].ptr.css({ top: _t });
				oThis.aClones[i].t = _t;
			};
			oThis.checksound();
			oThis.checkscreen();
		}, duration, easing, function(){
			delete oThis.tAnim;
			callback.apply(oThis);
		});
	},
	correct: function(){
		var oThis = this;
		var limit = {
			top: (oThis.d.height/2)-(Math.ceil(oThis._hli/2)+2),
			bottom: (oThis.d.height/2)+Math.ceil(oThis._hli/2)
		}
		var c;
		var t;
		$(this.oParent).find("li").each(function(i){
			var li = $(this);
			var it = li.offset().top-oThis.d.top+3;
			var ib = it+oThis._hli+4;
			
			if(it < limit.top && ib > limit.top && ib-limit.top > oThis._hli/2){
				t = it-3;
				oThis.new_current = li;
			}
			
			if(it < limit.bottom && ib > limit.bottom && limit.bottom - it > oThis._hli/2){
				t = it-3;
				oThis.new_current = li;
			}
		})
		var d = limit.top - t;
		if(d != 0){
			this.is.notAni = true;
			this.moveTo(d, function(){
				oThis.checksound(true);
				oThis.is.notAni = false;
				oThis.loadNewPage();
			})
		} else {
			this.loadNewPage();
		}
			
	},
	moveTo: function(d, callback){
		var oThis = this;
		var _d = this.getRelativeString(d);
		for (var i=0; i < this.aClones.length; i++) {
			
			this.aClones[i].t += d;
			this.aClones[i].ptr.animate({ top: _d }, 200, "easeOutQuad", callback);
		};
	},
	loadNewPage: function(){
		if(!this.is.loading){
			this.is.loading = true;
			
			if(this.current && this.new_current && this.current.find("a").attr("href") != this.new_current.find("a").attr("href")){
				this.current = this.new_current;
				delete this.new_current;
				this.is.loading = false;
				
				// Loading new page here
				ajaxPages.load(this.current.find("a"));
			} else {
				this.is.loading = false;
			}
		}
	},
	
	getCurrentTop: function(){
		return (this.d.height/2)-(Math.ceil(this._hli/2)+2);
	},
	getRelativeString: function(i){
		var _s = "+=";
		if(i < 0){ _s = "-="; i *= -1; }
		return _s + i;
	},
	getblock: function(t){
		for (var i=0; i < this.aClones.length; i++) {
			var o = this.aClones[i]
			if(o.t == t) return true;
		};
		return false;
	},
	newblock: function(t, oEvt){
		var nb = this.aClones[0].ptr.clone(true).appendTo(this.oParent).css({ top: t });

		var cy=0;
		if(oEvt) cy = oEvt.clientY-nb.offset().top;

		this.aClones.push({ ptr: nb, cy: cy, t: t, e: { t: false, b: false } });
		this.checkEdges();
	},

	checkEdges: function(){
		for (var i=0; i < this.aClones.length; i++) {
			var c = this.aClones[i];
			var ot = c.t;
			var ob = ot + this.jh;

			if(this.getblock(ot-this.jh)) c.e.t = true;
			else c.e.t = false;

			if(this.getblock(ob)) c.e.b = true;
			else c.e.b = false;
		};
	},
	checkscreen: function(oEvt){
		var repeat = false;
		var nb = false;

		for (var i=this.aClones.length-1; i >= 0; i--) {

			var c = this.aClones[i];
			var ot = c.t;
			var ob = ot + this.jh;

			if(!c.e.t && ot > -10){
				this.newblock(ot-this.jh, oEvt);
				nb = true;
			}
			
			if(!c.e.b && ob < this.d.height+30){
				this.newblock(ob, oEvt);
				nb = true;
			}

			if((!c.e.b && ot >= this.d.height+30) || (!c.e.t && ob <= -10)){
				this.toRemove.push(i);
			}

		};

		if(this.toRemove.length > 0){
			for (var i=0; i < this.toRemove.length; i++) {
				this.aClones[this.toRemove[i]].ptr.remove();
				this.aClones.splice(this.toRemove[i], 1);
			};
			this.toRemove = new Array();
			this.checkEdges();
		}

		if(nb)
			this.checkEdges();

	},
	
	checkSpeed: function(){
		if(this.d.c - this.aSpeed[this.aSpeed.length-1] != 0)
			this.aSpeed.push(this.d.cy);

		if(this.aSpeed.length > 2)
			this.aSpeed.splice(0, 1);
	},
	getSpeed: function(){
		if(this.aSpeed.length > 1)
			return this.aSpeed[1]-this.aSpeed[0];
		else
			return 0;
	}
}

