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.initFrontEnd();
                break;
                	
			default:
				break;
		}
	}
	
    this.initFrontEnd = function() {
        
        $("#mainmenu").corner("5px");
            
    }
    
	this.initAdminMainMenuView = function() {
		if (systemBrowserName != "ie"){
			$(".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');
			}
		);
	}
	
	// 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() {
		if (systemBrowserName != "ie"){
			$(".grgAdminButton").corner("3px");
		}
	}
	
	this.cancelAddEditMenu = function() {
		window.location = '?app=mainmenu';
	}
	
	this.saveAddEditMenu = function() {
		
		var options = null;
		
		var parentmenuid = $("#parentmenuid :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();
		
		// 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}, 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);
		});
		
	}
}	
