var HeaderFixed = {

	init : function(){
		var me = this;
		if ($.browser.msie) {
			$('#Products').mousewheel(me.wheelScroll);
		} else 
			$(window).mousewheel(me.wheelScroll);
		$(window).resize(function(){ me.onScroll(); });
		me.ctrlInit();
		me.onScroll();
	},
	
	onScroll : function(event) {
		var height = $(document.body).height();
		var space = height-145-88-363-20; //596 + 20 scroll
		if (space < 0)
			space = 0;
		var headerspace = space * 0.44;
		if (headerspace > 121)
			headerspace = 121;
		var footerspace = space * 0.28;
		if (footerspace > 75)
			footerspace = 75;
		var centerspace;
		if (height > 596) {
			centerspace = space - headerspace - footerspace;
			centerspace = centerspace * 0.4;
		} else {
			centerspace = height - 596;
		}
		centerspace += headerspace + 145;
		
		$('#Products').css('margin-top', centerspace.toString()+'px');
		$('table.maintable').height(height - 86);
		$('#header').height(headerspace + 145);
		$('#footer').height(footerspace + 88);
		$('#fo-xren').css('bottom', (footerspace + 88).toString()+'px');
		
		footerspace = footerspace * 0.21;
		
		$('#logo').css('bottom', (footerspace + 17).toString()+'px');
		$('#he-text, #he-join, #he-info, #he-crown-big').css('bottom', footerspace.toString()+'px');
		$('#he-crown').css('bottom', (footerspace + 77).toString()+'px');
		$('#he-month').css('bottom', (footerspace + 21).toString()+'px');
	},

	wheelScroll : function(event, delta){
		delta = delta > 0 ? -1 : 1;
		document.documentElement.scrollLeft = document.documentElement.scrollLeft + (delta * 100);
		return false;
	},


	// Controls scroll
	ctrlInit : function(){
		var me = this;
		this.oCtrlLeft = $('#CtrlLeft');
		this.oCtrlRight = $('#CtrlRight');
		this.oScroll = $('#Products');

		$('#CtrlLeft, #CtrlRight').mousedown(me.ctrlScroll);
		$('#CtrlLeft, #CtrlRight').mouseout(me.ctrlScrollStop);
		$('#CtrlLeft, #CtrlRight').mouseup(me.ctrlScrollStop);

		$(window)
				.resize(function(){me.ctrlVisibility()})
				.scroll(function(){me.ctrlVisibility()});

		me.ctrlVisibility()
	},

	ctrlScroll : function(){
		var me = HeaderFixed;
		me.dir = 1;
		me.stop = 0;
		if (this.id.match(/left/i))
			me.dir = -1;

		me.ctrlScrollMove();
		me.ctrlVisibility();
	},

	ctrlScrollMove : function(){
		var me = this;
		document.documentElement.scrollLeft = document.documentElement.scrollLeft + (this.dir * 30);

		if ( !me.stop )
			this.timer = setTimeout(function(){ me.ctrlScrollMove(); },5);
	},

	ctrlScrollStop : function(){
		HeaderFixed.stop = 1;
	},

	ctrlVisibility : function(){
		var el = document.getElementById('Products');
		
		var s = document.documentElement.scrollLeft;
		var w = el.offsetWidth - document.documentElement.offsetWidth;
		
		this.oCtrlLeft.css('display', (!s ? 'none' : 'block'));
		this.oCtrlRight.css('display', ((w - s) < 4 ? 'none' : 'block'));
	}
}

$(function(){
	HeaderFixed.init();
});