var Menu = Class.create({
	defVBg : [2500,1767],
	scrollerDim : {},
	scrollerPos : {},
	containerDim: {},
	container : null,
	timer : {
		vertical : false,
		horizontal : false
	},
	pasScroll : {
		vertical : null,
		horizontal : null
	},
	queueScroll : {
		vertical : false,
		horizontal : false
	},
	autoScroll : false,
	bgUrl : null,
	imgLoader : null,
	imgLoaderTimer : null,
	imgD :0,
	initialize : function(c){
		this.container = c.down(1);
//		this.imgLoader = new Element('img');
//		$(document.body).insert(this.imgLoader);
//		this.imgLoader.hide();
		this.loadInterface();
		this.loadEvent();
	},
	scrollVertical : function(){
		window.clearTimeout(this.timer.vertical);
		var pas = parseInt(this.pasScroll.vertical/3);
		var mT = parseFloat(this.container.getStyle('marginTop'));
		var m = mT - pas;
		var d = this.containerDim.height-this.scrollerDim.height+20

		if(this.containerDim.height<this.scrollerDim.height)
			this.queueScroll.vertical = false;
		else if(m <= -d){
			this.container.setStyle({marginTop : -d+'px'});
			this.queueScroll.vertical = false;
		} else if (m>=0){
			this.container.setStyle({marginTop : 0+'px'});
			this.queueScroll.vertical = false;
		} else {
			this.container.setStyle({marginTop : m+'px'});
			this.queueScroll.vertical = true;
			if(this.autoScroll)
				this.timer.vertical = window.setTimeout(this.scrollVertical.bind(this), 10);
		}
	},
	scrollHorizontal : function(){
		window.clearTimeout(this.timer.horizontal);
		var pas = parseInt(this.pasScroll.horizontal/3);
		var mT = parseFloat(this.container.getStyle('marginLeft'));
		var m = mT - pas;
		var d = this.containerDim.width-this.scrollerDim.width+20;

		if(this.containerDim.width<this.scrollerDim.width)
			this.queueScroll.horizontal = false;
		else if(m <= -d){
			this.container.setStyle({marginLeft : -d+'px'});
			this.queueScroll.horizontal = false;
		} else if (m>=0){
			this.container.setStyle({marginLeft : 0+'px'});
			this.queueScroll.horizontal = false;
		} else {
			this.container.setStyle({marginLeft : m+'px'});
			this.queueScroll.horizontal = true;
			if(this.autoScroll)
				this.timer.horizontal = window.setTimeout(this.scrollHorizontal.bind(this), 10);
		}
	},
	overScroll : function(p){
		window.clearTimeout(this.timer);
		this.autoScroll = true;

		if(p[0]<35 || p[0]>65 || p[1]<35 || p[1]>65){
			if(p[0]<35 || p[0]>65){

				this.pasScroll.horizontal = p[0]-50;
				this.pasScroll.horizontal += (this.pasScroll.horizontal > 0) ? -15 : 15;

				if(!this.queueScroll.horizontal)
					this.scrollHorizontal();

			}

			if(p[1]<35 || p[1]>65){

				this.pasScroll.vertical = p[1]-50;
				this.pasScroll.vertical += (this.pasScroll.vertical > 0) ? -15 : 15;

				if(!this.queueScroll.vertical)
					this.scrollVertical();

			}
		} else
			this.outScroll();
	},
	outScroll : function(){
		this.queueScroll = { vertical : false, horizontal : false};
		this.autoScroll = false;
		window.clearTimeout(this.timer.vertical);
		window.clearTimeout(this.timer.horizontal);
	},
	useScroll : function(e){
		if(e.clientX > this.scrollerPos.left && e.clientX < (this.scrollerPos.left+this.scrollerDim.width) &&
				e.clientY > this.scrollerPos.top && e.clientY < (this.scrollerPos.top+this.scrollerDim.height)){
			this.overScroll([((e.clientX-this.scrollerPos.left)/this.scrollerDim.width)*100, ((e.clientY-this.scrollerPos.top)/this.scrollerDim.height)*100]);
		} else
			this.outScroll()
	},
	scrollVMouse : function(e){
		this.pasScroll.vertical = (e.detail > 0 || e.wheelDelta <0) ? 160 : -160;
		this.scrollVertical();
		this.outScroll();
	},
	scrollHMouse : function(e){
		this.pasScroll.horizontal = (e.detail > 0 || e.wheelDelta <0) ? 160 : -160;
		this.scrollHorizontal();
		this.outScroll();
	},
	loadInterface : function(){
		this.scrollerDim = this.container.ancestors()[0].getDimensions();
		this.scrollerPos = this.container.ancestors()[0].viewportOffset();
		this.containerDim = this.container.getDimensions();
		var w = (self.innerWidth || document.documentElement.clientWidth || document.body.clientWidth || 1300);
		var h = (self.innerHeight || document.documentElement.clientHeight || document.body.clientHeight || 979);
		var r = this.defVBg[0]/this.defVBg[1];
		var R = w/h;
		var u = $(document.body).getStyle('backgroundImage').gsub(/\:[0-9]*\./, ':'+parseInt(((R>=r) ? w : (h*r)))+'.');
		$(document.body).setStyle({width:w+'px', height:h+'px'});
//		this.bgUrl = u.substr(4, u.length-5)
//		this.imgLoader.src = this.bgUrl;
//		this.loadBg();
	},
	loadBg : function(){
//		if(this.imgLoader.getWidth() != this.imgD){
//			$(document.body).setStyle({backgroundImage : 'url('+this.bgUrl+')'});
//			window.clearTimeout(this.imgLoaderTimer);
//			this.imgD = this.imgLoader.getWidth();
//		} else if($(document.body).getStyle('backgroundImage') != 'url('+this.bgUrl+')')
//			this.imgLoaderTimer = window.setTimeout(this.loadBg.bind(this), 1000);
	},
	emptyFunction : function(){
		return;
	},
	loadEvent : function(){
		window.onresize = this.loadInterface.bindAsEventListener(this);
		document.observe('mousemove', this.useScroll.bindAsEventListener(this));
		var mouseScroller = this[((this.containerDim.height>this.scrollerDim.height) ? 'scrollVMouse' : ((this.containerDim.width>this.scrollerDim.width) ? 'scrollHMouse' : 'emptyFunction' ))].bindAsEventListener(this);
		document.observe('DOMMouseScroll', mouseScroller);
		document.observe('mousewheel', mouseScroller);
	}
});

document.observe('dom:loaded', function(){
	new Menu($('page'));
});

