mainMenuManager = null;

function mainMenuManageInit(viewName){
	mainMenuManager = new mainMenuManageSys();	
	mainMenuManager.initView(viewName);
}

function mainMenuManageSys(options){
	
	this.options 			= options;
		
	this.initView = function (viewName) {
		
		switch(viewName){
			case 'adminmenu':
				this.initAdminMainMenuView();
				break;
			
			case 'editmenu':
				this.initEditMenuView();
				break;
			
			case 'frontend':
				this.iniFrontEndView();
				break;
			
			case 'tabsview':
				this.initTabsView();
				break;
				
			default:
				break;
		}
	}
	
	this.initAdminMainMenuView = function() {
		$(".menuentry").corner("3px");
		$(".submenuentry").corner("3px");
		$(".innerSubMenuEntry").corner("3px");
		
		$(".menuentry").hover(
			function(){
				$(this).css('backgroundColor','#ffffcc');
			}, 
			function(){
				$(this).css('backgroundColor','#fff');
			}
		);
		
		$(".submenuentry").hover(
			function(){
				$(this).css('backgroundColor','#ffffcc');
			}, 
			function(){
				$(this).css('backgroundColor','#fff');
			}
		);
		
		$(".innerSubMenuEntry").hover(
			function(){
				$(this).css('backgroundColor','#ffffcc');
			}, 
			function(){
				$(this).css('backgroundColor','#fff');
			}
		);
		
	}
	
	this.initTabsView = function() {
		alert("initTabsView");
	}
	
	// mouving menu up
	this.mouveMenuUp = function(menuid) {
		var current = $("#menuHandler_"+menuid);
		current.prev('.menuHandler').before(current);
		var menuList = new Array(); 
		$('.menuHandler').each(function() { menuList.push($(this).attr('id')); });
		$.get("../apps/mainmenu/ajax.php?task=mouveMenu",{menuList:menuList.toLocaleString()}, function(data){
			$("#mainmenu-container").html(data);
			mainMenuManager.initAdminMainMenuView();
		});
	}
	
	// mouving menu dounw
	this.mouveMenuDown = function(menuid) {
		var current = $("#menuHandler_"+menuid);
		current.next('.menuHandler').after(current);
		var menuList = new Array(); 
		$('.menuHandler').each(function() { menuList.push($(this).attr('id')); });
		$.get("../apps/mainmenu/ajax.php?task=mouveMenu",{menuList:menuList.toLocaleString()}, function(data){
			$("#mainmenu-container").html(data);
			mainMenuManager.initAdminMainMenuView();
		});
	}
	
	// mouving submenu up
	this.mouveSubMenuUp = function(menuid, submenuid) {
		var current = $("#submenuHandler_"+submenuid);
		current.prev().before(current);
		var subMenuList = new Array(); 
		$('.submenu_'+menuid).each(function() { subMenuList.push($(this).attr('id')); });
		$.get("../apps/mainmenu/ajax.php?task=mouveSubMenu",{menuid:menuid, subMenuList:subMenuList.toLocaleString()}, function(data){
			$("#mainmenu-container").html(data);
			mainMenuManager.initAdminMainMenuView();
		});
	}
	
	// mouving submenu down
	this.mouveSubMenuDown = function(menuid, submenuid) {
		var current = $("#submenuHandler_"+submenuid);
		current.next().after(current);
		var subMenuList = new Array(); 
		$('.submenu_'+menuid).each(function() { subMenuList.push($(this).attr('id')); });
		$.get("../apps/mainmenu/ajax.php?task=mouveSubMenu",{menuid:menuid, subMenuList:subMenuList.toLocaleString()}, function(data){
			$("#mainmenu-container").html(data);
			mainMenuManager.initAdminMainMenuView();
		});
	}
	
	this.removeMenuEntry = function(menuid) {
		if(confirm(menuRemovedConfirmMSG)) {
			$.get("../apps/mainmenu/ajax.php?task=isEmptyMenuEntry",{menuid:menuid}, function(data){
				data = $.trim(data);
				if(data == "true") {
					$.get("../apps/mainmenu/ajax.php?task=removeMenuEntry",{menuid:menuid}, function(data){
						$("#mainmenu-container").html(data);
						mainMenuManager.initAdminMainMenuView();
						$("#editmenu-msg-container").html('<p class="okusermsg">'+menuRemovedMSG+'</p>');
						setTimeout('$("#editmenu-msg-container p").fadeOut("400")', 1000);
					});		
				} else {
					alert(notEmptyMenuMSG);
					return false;
				}
			});
		} else {
			return false;
		}
	}
	
	this.addEditMenuEntry = function(mid) {
		window.location = '?app=mainmenu&view=editmenu&mid='+mid;	
	}
	
	
	this.initEditMenuView = function() {
		$(".grgAdminButton").corner("3px");
	}
	
	this.cancelAddEditMenu = function() {
		window.location = '?app=mainmenu';
	}
	
	this.saveAddEditMenu = function() {
		
		var options = null;
		
		var parentmenuid = $("#parentmenuid :selected").val();
		var menugroupid = $("#menugroupid :selected").val();
		var menutype = $("#menutype :selected").val();
		var active = $("#active :selected").val();
		var menunames = new Array();
		$(".menudisplay").each(function(){
			menunames.push($(this).attr('id'), $(this).val());
		});
		var menuid = $("#menuid").val();
		var insert = $("#insert").val();
		var dicoid = $("#dicoid").val();
		
		var submenudisplay = $("input[name='submenudisplay']:checked").val();
		
		// managing menu type options
		switch (menutype) {
			
			// page
			case '1':
				options = $("#pageLinkOptions").serialize();
				break;
			
			// app
			case '2':
				options = $("#appLinkOptions").serialize();
				break;
				
			// external link
			case '3':
				options = $("#externalLinkOptions").serialize();
				break;
			
			// internal link	
			case '4':
				options = $("#internalLinkOptions").serialize();
				break;
				
			default:
				break;
				
		}
		
		var seoSaveData	= Array();
		$(".mainmenuSEOData").each(function (){
			var idData	= $(this).attr("id");
			var value	= $(this).val();
			seoSaveData.push("[" + idData + "][" + value + "]");
		});
		seoSaveData		= seoSaveData.join("§");
		$.get("../apps/mainmenu/ajax.php?task=saveMenuEntry", {parentmenuid:parentmenuid, menutype:menutype, menunames:menunames.toLocaleString(), menuid:menuid, insert:insert, dicoid:dicoid, options:options, seoData:seoSaveData, active:active, submenudisplay:submenudisplay, menugroupid:menugroupid}, function(data){
			data = $.trim(data);
			if(data == "success") {
				$("#editmenu-msg-container").html('<p class="okusermsg">'+menuSavedMSG+'</p>');
				setTimeout('$("#editmenu-msg-container p").fadeOut("400")', 1000);
				$("#backOrCancelButton").text('Retour');
			} else {
				$("#editmenu-msg-container").html('<p class="notokusermsg">ERROR !</p>');
				setTimeout('$("#editmenu-msg-container p").fadeOut("400")', 1000);
			}
			
		});
	}
	
	this.setMenuTypeOptions = function() {
		
		var menutype = $("#menutype :selected").val();
		$.get("../apps/mainmenu/ajax.php?task=setMenuTypeOptions", {menutype:menutype}, function(data){
			$("#app-menu-type-options-container").html(data);
		});
		
	}
	
	this.setMenuParentOptions = function() {
		//alert($("#parentmenuid :selected").val());
		if($("#parentmenuid :selected").val() != 0) {
			$("#parentmenuoption-contaier").load("../apps/mainmenu/ajax.php?task=getSubMenuDisplayOptionsPanel",{}, function(data){
				
			});
		}
	}
	
	this.iniFrontEndView = function() {
		
		function megaHoverOver(){
			if(systemBrowserName == 'ie' && systemBrowserVersion != '8.0'){
				$(this).find(".sub").stop().css('margin-left', ( $(this).width()*-1 ) + 'px' ).css('margin-top', '20px').fadeTo('fast', 1).show();	
			}else{
				$(this).find(".sub").stop().fadeTo('fast', 1).show();
			}
		}
		
		function megaHoverOut(){ 
		  $(this).find(".sub").stop().fadeTo('fast', 0, function() {
			  $(this).hide(); 
		  });
		}
	
		var config = {    
			sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
			interval: 0, // number = milliseconds for onMouseOver polling interval    
			over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
			timeout: 0, // number = milliseconds delay before onMouseOut    
			out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
		};

		$("ul#topnav li .sub").css({'opacity':'0'});
		$("ul#topnav li").hoverIntent(config);
		
	}
	
	this.updateSubMenuSelector = function() {
		var menugroupid = $("#menugroupid :selected").val();
		//alert("menugroupid = "+menugroupid);
		
		$.get("../apps/mainmenu/ajax.php?task=updateSubMenuSelector", {menugroupid:menugroupid}, function(data){
			$("#parentselector-container").html(data);
		})
	}
}	
