Menu = Class.create();

Menu.prototype.config_subContainerAutoShow = 0 //Charge automatiquement tous les produit d'une catégorie contenant des sous-catégorie 0|1

Menu.prototype.container = null;
Menu.prototype.subContainerTarget = null;
Menu.prototype.subContainer = null;
Menu.prototype.menuElement = {};
Menu.prototype.url = "";
Menu.prototype.lastMainCategory = 37;
Menu.prototype.lastSubCategory = 0;
Menu.prototype.lastSubSubCategory = 0;
Menu.prototype.lastSubCategoryOffsetTop = 0;

Menu.prototype.initialize = function(menuId, subMenuTargetId, subMenuId, ajaxScript, parameters)
{
	this.container = $(menuId);
	this.subContainerTarget = $(subMenuTargetId);
	this.subContainer = $(subMenuId);
	this.url = ajaxScript;
	for(option in parameters)
	{
		this.menuElement[option] = parameters[option];
	}
	this._startObservingMenu();
}

Menu.prototype._startObservingMenu = function()
{
	for(option in this.menuElement)
	{
		$("mainCategory_"+option).stopObserving("click");
		$("mainCategory_"+option).observe("click",this._onMenuElementClick.bind(this,option));
//		if(this.lastMainCategory == 0)
//			$("mainCategory_"+option).fireNativeEvent("click");
	}
}

Menu.prototype._onMenuElementClick = function(categoryId)
{
	var work = 0;
	if(this.lastMainCategory != categoryId && $("mainCategory_" + categoryId) != null){
		//mainMenu actions
		$("mainCategorySelected").innerHTML = $("mainCategory_" + categoryId).innerHTML;
		$("mainCategory_" + categoryId).style.display = "none";
		
		lastMainCategory = this.lastMainCategory;
		if(lastMainCategory != 0)
			$("mainCategory_" + lastMainCategory).style.display = "";
		this.lastMainCategory = categoryId;
		work = 1;
	}
	else
		work = 1;
	
	if(this.lastSubCategory != categoryId && $("subCategory_" + categoryId) != null){
		//subMenu actions
		lastSubCategory = this.lastSubCategory;
		if($("subCategory_" + lastSubCategory) != null){
			$("subCategory_" + lastSubCategory).removeClassName("HaveChilds");
			$("subCategory_" + lastSubCategory).removeClassName("active");
		}
		$("subCategory_" + categoryId).addClassName("active");		
		this.lastSubCategory = categoryId;
	}
	
	if(work){
		var parameters = {};
		parameters.ms = new Date();
		parameters.categoryId = categoryId;
		new Ajax.Request(this.url,{
				method : "post",
				evalJSON : "force",
				parameters : parameters,
				onSuccess : this._onSuccessGetChilds.bind(this,$("subMenuTarget"),false),
				onFailure : this._onFailureGetChilds.bind(this)
		});
		if(window.front_mgr === null)
		{
			alert("object front_mgr has disapeared from index.php");
			return false;
		}
	}
}

Menu.prototype._onSubMenuElementClick = function(categoryId)
{
	var work = 0;
	if(this.lastMainCategory != categoryId && $("mainCategory_" + categoryId) != null){
		lastMainCategory = this.lastMainCategory;
		//mainMenu actions
		$("mainCategorySelected").innerHTML = $("mainCategory_" + categoryId).innerHTML;
		$("mainCategory_" + categoryId).style.display = "none";
		if(this.lastMainCategory != 0)
			$("mainCategory_" + lastMainCategory).style.display = "";
		this.lastMainCategory = categoryId;
		work = 1;
	}
	else
		work = 1;
	
	pos = Position.positionedOffset($("subCategory_" + categoryId));
	this.lastSubCategoryOffsetTop = pos[1];
	
	if(this.lastSubCategory != categoryId && $("subCategory_" + categoryId) != null){
		//subMenu actions
		lastSubCategory = this.lastSubCategory;
		if($("subCategory_" + lastSubCategory) != null){
		$("subCategory_" + lastSubCategory).removeClassName("active");
		$("subCategory_" + lastSubCategory).removeClassName("HaveChilds");
		}
		$("subCategory_" + categoryId).addClassName("active");
		this.lastSubCategory = categoryId;
	}
	if(work){
		var parameters = {};
		parameters.ms = new Date();
		parameters.categoryId = categoryId;
		new Ajax.Request(this.url,{
				method : "post",
				evalJSON : "force",
				parameters : parameters,
				onSuccess : this._onSuccessGetChilds.bind(this,this.subContainer,true),
				onFailure : this._onFailureGetChilds.bind(this)
		});
		if(window.front_mgr === null)
		{
			alert("object front_mgr has disapeared from index.php");
			return false;
		}
		//window.cross_mgr.container.show();
		//window.cross_mgr.container.update();
	}
}

Menu.prototype._onSubSubElementClick = function(categoryId)
{
	var work = 0;
	if(this.lastMainCategory != categoryId && $("mainCategory_" + categoryId) != null){
		lastMainCategory = this.lastMainCategory;
		//mainMenu actions
		$("mainCategorySelected").innerHTML = $("mainCategory_" + categoryId).innerHTML;
		$("mainCategory_" + categoryId).style.display = "none";
		if(this.lastMainCategory != 0)
			$("mainCategory_" + lastMainCategory).style.display = "";
		this.lastMainCategory = categoryId;
		work = 1;
	}
	else
		work = 1;
	if(this.lastSubSubCategory != categoryId && $("subSubCategory_" + categoryId) != null){
		//subMenu actions
		lastSubSubCategory = this.lastSubSubCategory;
		if ($("subSubCategory_" + lastSubSubCategory) != null && $("subSubCategory_" + lastSubSubCategory).hasClassName('active'))
			$("subSubCategory_" + lastSubSubCategory).removeClassName("active");
		$("subSubCategory_" + categoryId).addClassName("active");
		this.lastSubSubCategory = categoryId;
	}
	if(work){
		var parameters = {};
		parameters.ms = new Date();
		parameters.categoryId = categoryId;
		new Ajax.Request(this.url,{
				method : "post",
				evalJSON : "force",
				parameters : parameters,
				onSuccess : this._onSuccessGetChilds.bind(this,this.subContainer,true),
				onFailure : this._onFailureGetChilds.bind(this)
		});
		if(window.front_mgr === null)
		{
			alert("object front_mgr has disapeared from index.php");
			return false;
		}
		//window.cross_mgr.container.show();
		//window.cross_mgr.container.update();
	}
	this.subContainerTarget.hide();
}

Menu.prototype._onSuccessGetChilds = function(container, subSubMenu, xhr)
{
//	container.show();
	var cpt = 0;
	var parameters = {};
	var first = true;
	var nbOfChilds = 0;
	
	for(option in xhr.responseJSON.childs){
		if(first === true)
		{
			container.update();
			first = false;
		}
		this._insertChild(option,xhr.responseJSON.childs[option],cpt,container,subSubMenu);
		nbOfChilds++;
		if(cpt == 0){
			cpt = 1;
			parameters.categoryId = this.lastSubCategory;
		}
	}
	
	if(cpt == 0)
		if(subSubMenu)
			parameters.categoryId = this.lastSubSubCategory;
		else
			parameters.categoryId = this.lastSubCategory;
	
	if(subSubMenu && nbOfChilds > 0){
		this.subContainerTarget.show();
		done = 0;
		reductor = 0;
		SubSubMenuHeight = nbOfChilds * 21 + 28;
		while(!done){
			if((this.lastSubCategoryOffsetTop + SubSubMenuHeight - reductor) < 470){
				$('subSubMenuTarget').style.top = this.lastSubCategoryOffsetTop - 10 - reductor + "px";
				done = 1;
			}
			else
				reductor += 10;
		}
		$("subCategory_" + this.lastSubCategory).addClassName("HaveChilds");
	}
	else if(!subSubMenu && nbOfChilds == 0){
		this.lastSubSubCategory = 0;
		this.subContainerTarget.hide();
	}
	else
		this.subContainerTarget.hide();
	
	if(nbOfChilds == 0 || (subSubMenu && this.config_subContainerAutoShow == 1)){
		/*window.front_mgr.update("frontoffice/ajax/category/getProducts.php",parameters);*/ /* AJAX */
		locationRateTo = "index.php?page=list&categoryId=" + parameters.categoryId;
		this.navigateTo(locationRateTo);
		$("mainContent").setStyle({
			  backgroundImage: 'url("frontoffice/template/main/mainBg.jpg")',
			  backgroundRepeat:'repeat'
			});
	}
}

Menu.prototype.navigateTo = function(locationRateTo){
	if(!Prototype.Browser.IE)
		$(document).location.href = locationRateTo;
	else
		window.location.href = locationRateTo;
}

Menu.prototype._insertChild = function(id, category, cpt, container, subSubMenu)
{
	var element;
	var className;
	element = $('subCategory_'+id);
	if(subSubMenu){
		element = $('subSubCategory_'+id);
	}
	var submenu = id.split("_")[1];
	id = id.split("_")[0];
	
	if(element === null)
	{	
		if(subSubMenu)
		{
			element = new Element('div',{'id':'subSubCategory_'+id});
			if(this.lastSubSubCategory == id)
				className = "SubSubMenuItem active";
			else
				className = "SubSubMenuItem";
			
		}
		else
		{
			element = new Element('div',{'id':'subCategory_'+id});
			if(this.lastSubCategory == id)
				className = "SubMenuItem active";
			else
				className = "SubMenuItem";
		}
		
		Element.toggleClassName(element, className);
		element.insert(category);
		container.insert(element);
		
		element.stopObserving("click");
		if(submenu == 1)
			element.observe("click",this._onSubMenuElementClick.bind(this,id));
		else if (subSubMenu)
			element.observe("click",this._onSubSubElementClick.bind(this,id));
		else
			element.observe("click",this._onMenuElementClick.bind(this,id));
	}
	else
	{
		element.stopObserving("click");
		element.remove();
	}
}

Menu.prototype._onFailureGetChilds = function(xhr)
{
	alert(xhr.responseJSON.success);
}
