Function.prototype.bind = function (object) {
	
	var func = this;
	return function () {
		func.call(object);
	}

}

var call = function (callback) {
	
	if (typeof(callback) == 'function') {
		callback();
	}

}


var menu = {

	topMenu : null,
	bottomMenus : null,
	block_id : null,
	freezeTop : null,
	freezeBottom : null,
	
	blockTMP : false,
	
	currentBlock : 0,

	init : function () {
		
		var context = this;
		
		this.topMenu = $('#menuHead TABLE');
		this.bottomMenus = $('#submenuHead .inner');
		
		this.rollBackTopMenu();
		this.rollBackBottomMenu();
		
		this.initHover();
		
	},

	/**
	 * Инициализация меню клике
	 */
	initHover : function () {
		
		var context = this;
		
		$('a', this.topMenu).click( function () {
			
			if ($(this).is('.current') || context.blockTMP == true) {
				return false;	
			}
			context.blockTMP = true;

			var block_id = /item_(\d)/.exec(this.className)[1];
			
			$('#menuHead .current', this.topMenu).removeClass('current');
			$(this).addClass('current');
			$(this).next('div').addClass('current');
			
			context.hideBottomMenu(
				function () {
					
					$('.submenuHead-' + block_id).fadeIn(250, function () {
						context.blockTMP = false;
					});
				}
			);
			
		
			return false;
			
		});
		
	},
	
	setCurrentBlock : function (block_id) {
		
		var context = this;

		if (this.block_id == block_id) {
			return;
		}
		
		this.block_id = block_id;
		this.addCookie();
		
		var process = function (no_hide_bottom) {
			
			context.rollBackTopMenu();

			if(context.block_id) {
			
				// Разбираемся с верхим меню
				$('#menuHead TD A').hide();
				
				$('.current', context.topMenu).removeClass('current');

				var firstHTML = $('.item_' + context.block_id).parent().html();
				
				$('TD:first', context.topMenu).html(firstHTML);
				
				$('TD:first A', context.topMenu).show();
				$('TD:first A', context.topMenu).addClass('current');
				$('TD:first A', context.topMenu).next('div').addClass('current');

				// Теперь на разборки подменю
				var test = function () {

					$('#submenuHead DIV.inner').removeClass('current');
					$('#submenuHead DIV.inner').removeClass('first');
					
					var submenu = $('.submenuHead-' + context.block_id, $('#fullmenu'));
					submenu.addClass('first');
					//submenu.addClass('current');
					
					var td = $('td', submenu);
					
					if (($(td[0]).html() != ' ')) {			// а вдруг уже отсортированы
						
						// Сортируем ячейки в таблице
						switch(context.block_id) {
							case 2:
									$(td[1]).html($(td[0]).html());
									$(td[0]).html(' ')
								break;
							case 3:
									$(td[2]).html($(td[1]).html());
									$(td[1]).html($(td[0]).html());
									$(td[0]).html(' ')
								break;
						}

					}
					
					submenu.fadeIn(500);
					context.textHeadDown();

				}
				if (no_hide_bottom) {
					test();
				}
				else {
					context.hideBottomMenu(test);
				}
				

				
			}
			else {
				
				// Откатываемся на первоначальное меню
				context.rollBackBottomMenu();
				context.textHeadUp();
				
			}

			context.showTopMenu();
			context.initHover();
			
		}
		
		if ($('#shortmenu').css('display') == 'block') {
			
			
			$('#menuHead_short TABLE').animate(
				{
					top: $('#menuHead_short').height()
					
				}, 500, 
				function () {
					
					context.rollBackTopMenu();
					context.rollBackBottomMenu();
					context.hideBottomMenu();
					
					$('#submenuHead_short').fadeOut(500, function () {
						
						$('#shortmenu').hide();
						
						context.topMenu.css('top', '80px');
						$('#fullmenu').show();

						//context.hideBottomMenu();
						process(true);

						
					});
					
					
				}
			);
			
		}
		else {

			// Прячем верхнее меню
			this.hideTopMenu(process);

		}
		
		
	},
	
	textHeadUp : function () {
		return;
		$('.textHead').animate({
			top: 0
		}, 500);
	},
	
	textHeadDown : function () {
		return;
		$('.textHead').animate({
			top: 80
		}, 500);
	},
	
	rollBackTopMenu : function () {
		
		if (this.freezeTop == null) {
			
			if (_main) {
				$('#menuHead .current').removeClass('current');
				$('#menuHead td:first a').addClass('current');
				$('#menuHead td:first div').addClass('current');
			}

			this.freezeTop = this.topMenu.html();
			return;
		}
		
		this.topMenu.html(this.freezeTop);
		
	},
	
	rollBackBottomMenu : function () {
		
		if (this.freezeBottom == null) {
			
			if (_main) {
				$('#submenuHead .current').removeClass('current');
				$('#submenuHead .inner:first').addClass('current');
			}
			
			this.freezeBottom = $('#submenuHead').html();
			return;
		}
			
		$('#submenuHead').html(this.freezeBottom);
		
	},
	
	addCookie : function () {

		$.cookie('bank', this.block_id, {expires : 255});
		slide.SetBlock(this.block_id - 1);
		slide.SetBlockRand();
		
	},
	
	showBlock : function (blockId) {
		
	},

	hideTopMenu : function (callback) {

		this.topMenu.animate(
			{
				top: this.topMenu.height()
				
			}, 500, 
			function () {
				call(callback);
			}
		);
		
	},
	
	showTopMenu : function (callback) {

		this.topMenu.animate({
			top: 0
			
		}, 500);
		
	},
	
	hideBottomMenu : function (callback) {

		var context = this;
		this.bottomMenus = $('.inner', $('#submenuHead'));
		
		this.bottomMenus.each(function () {
				
			if ('block' == $(this).css('display')) {

				$(this).fadeOut(200, function () {
					call(callback);
				});

			}

		});
		
	},
	
	showBottomMenu : function (callback) {

		this.bottomMenus.fadeOut(5000);
		
	}
	
};


$(document).ready(function () {
	menu.init();
});
