var IPOXMenu, ipoxmenu;

IPOXMenu = function ($) {
	var that = this;
	
	this.init = function () {
		this.build_products_menu();
		this.build_products_switch();
	};
	
	this.build_products_menu = function () {
		var container, top, bottom, lang = LANG, selectors, routes;
		
		selectors = CATEGORIES.products_menu;
		routes = ROUTES.products;
		
		container = $('#menu-item-' + selectors[lang] + ' > ul.sub-menu');
		container.addClass('ipox-products-submenu');
		$(document.body).append(container);
		
		top = document.createElement('li');
		container.append(top);
		$(top).addClass('ipox-submenu-top');
		
		bottom = document.createElement('li');
		container.append(bottom);
		$(bottom).addClass('ipox-submenu-bottom');
		
		/* disable the main links (categories) */
		$('.ipox-products-submenu > li > a').each(function () {
			$(this).attr('href', '');
			$(this).click(function (event) {
				event.preventDefault();
			});
		});
		
		$('.ipox-products-submenu > li a').each(function () {
			var href = $(this).attr('href');
			if (href.length > 0) {
				$(this).attr('href', href.replace('category/' + routes[lang] + '/', routes[lang] + '\/#'));
			}
		});
		
		$('#menu-item-' + selectors[lang]).mouseover(function () {
			container.addClass('active');
			container.css({
				left: $('#menu-item-' + selectors[lang]).offset().left - 9 + 'px',
				top: $('#menu-item-' + selectors[lang]).offset().top + $('#menu-item-' + selectors[lang]).height() + 'px'
			});
			$(document).mousemove(function (event) {
				if ($(event.target).closest('#menu-item-' + selectors[lang]).length === 0 && $(event.target).closest('.ipox-products-submenu').length === 0) {
					container.removeClass('active');
					$(document).unbind('mousemove');
				}
			});
		});
	};
	this.build_products_switch = function () {
		var lang = LANG, routes;
		
		routes = ROUTES.products;
				
		/* disable the main links (categories) */
		$('#product-navigation > ul > li > a').each(function () {
			var href = $(this).attr('href');
			if (href.length > 0) {
				$(this).attr('href', href.replace('category/' + routes[lang] + '/', routes[lang] + '\/#'));
			}
		});
		
		$('#product-navigation > ul > li a').each(function () {
			var href = $(this).attr('href');
			if (href.length > 0) {
				$(this).attr('href', href.replace('category/' + routes[lang] + '/', routes[lang] + '\/#'));
			}
		});
	};
	
	$(document).ready(function () {
		that.init();
	});
};
ipoxmenu = new IPOXMenu(jQuery);

