/**
 * @author albert.sole
 * THIS FILE CONTAINS ALL THE COMPONENTS THAT WEB INTERFACE REQUIRE TO 
 * - RENDER THE NAVIGATION MENU EDITOR
 */ 

function NavMenuEditor(menuTreeURL, iconsURL, categoriesURL, staticHTMLURL, renderTo, languages){
		//i18n texts
		var navMenuEditorEditorTitleText = getLanguageForElement("navMenuEditorEditorTitleText", selectedLangId);
		var navMenuDestinationLableText = getLanguageForElement("navMenuDestinationLableText", selectedLangId);
		var navMenuDisplayOrderLableText = getLanguageForElement("navMenuDisplayOrderLableText", selectedLangId);
	
		var config = {
			globalParameters: {
				renderTo: renderTo,
				formTitle: navMenuEditorEditorTitleText,
				categoriesURL: categoriesURL,
				iconsURL: iconsURL,
				defaultIconImage: './img/icons/blank-48x48.gif',	
				languages: languages,
				addDestination: true
			},
			navigationTreeConfig: {
				treeID: 'tree',
				treeRootNode: 'RootMenu',
				treeRequestDataURL: menuTreeURL,
				treeDoubleClickCategoryFunction: function(node, tree, e){ //function that will be executed on category double click
					var scope = tree.scope;
					ajaxRequestMenuCategory(node.attributes.nodeId, scope);
				},
				treeDoubleClickItemFunction: function(node, tree, e){ //function that will be executed on item double click
					var scope = tree.scope;
					ajaxRequestMenuItem(node.attributes.nodeId, scope);
				}
			},
			itemEditor: {
				specificComponents:{
						/*edit_menu_itemDestinationCombo: new GmSoft.components.ComboSelector(
															{
														  		label: navMenuDestinationLableText,
														  		queryField: 'file',
														  		mappingElement: 'url',
														  		queryURL: staticHTMLURL,
														  		functionToExecOnSelection: Ext.emptyFn,
														  		scope: this 
															}),*/
						itemOrder: getNumberFieldComponent('item_order',navMenuDisplayOrderLableText,'item_order',250)
				},			
				updateComponentsFromXML: function(xmlDoc, components){
					//select the destination value from the combo
					//var destination = findDirectTextNodeValue(xmlDoc.getElementsByTagName("destination")[0]);
					//components.edit_menu_itemDestinationCombo.setComboValue(destination);
					//read and load the order field
					var order = findDirectTextNodeValue(xmlDoc.getElementsByTagName('order')[0]);
					components.itemOrder.setValue(order);					
				},
				send: function(image, category, content, specificComponents, scope){
					//get specific component values
					//var destination = specificComponents.edit_menu_itemDestinationCombo.getCombo().getValue();				
					var order = specificComponents.itemOrder.getValue();
					//send the request				
					//ajaxSendNewNavMenuItem(image, destination, category, content, order, scope);
					ajaxSendNewNavMenuItem(image, category, content, order, scope);
				},
				update: function(id,image, category, content, specificComponents, scope){
						//get specific component values
						//var destination = specificComponents.edit_menu_itemDestinationCombo.getCombo().getValue();	
						var order = specificComponents.itemOrder.getValue();
						//request update
						//ajaxSendUpdateNavMenuItem(id,image, destination, category, content, order, scope);				
						ajaxSendUpdateNavMenuItem(id,image, category, content, order, scope);
				},
				remove: function(selectedItem, specificComponents, scope){
					ajaxSendDeleteMenuItem(selectedItem, scope);
				}				
			},
			categoryEditor: {
				specificComponents:{				
						categoryOrder: getNumberFieldComponent('category_order',navMenuDisplayOrderLableText,'category_order',250)
				},
				updateComponentsFromXML: function(xmlDoc, components){
						var order = findDirectTextNodeValue(xmlDoc.getElementsByTagName('order')[0]);
						components.categoryOrder.setValue(order);					
				},
				send: function(image, content, specificComponents, scope){
					var order = specificComponents.categoryOrder.getValue();
					//send ajax request	
					ajaxSendNewNavMenuCategory(image, content, order, scope);				
				},
				update: function(id,image, content, specificComponents, scope){
						var order = specificComponents.categoryOrder.getValue();
						//send ajax request
						ajaxSendUpdateNavMenuCategory(id,image, content, order, scope);			
				},
				remove: function(selectedItem, specificComponents, scope){
						ajaxSendDeleteMenuCategory(selectedItem, scope);
				}
			}
		};
		return navEditor = new genericCateogryItemEditor(config);
}

//AJAX SPECIFIC CALLS
//add new category
function ajaxSendNewNavMenuCategory(image, content, order, thisScope){
	ajaxGenericCall('XmlGMServlet?action_type=nav_menu_actions&action=add_category',
					function(conn, response, options){
						var failure = lookForXMLFailureResponse(conn.responseXML);
						if(failure != null){	//in case there was an error show it
							showErrorDialog('ERROR!!',failure);
						}else{
							var scope = response.scope;
							scope.edit_menu_itemsCategoryChooser.reload();
							var result = getResponseText(conn.responseXML);
							showInfoDialog('INFO', result);	
							//update the navTree
							scope.showNewsTree.loadTree();
						}						
					},
					thisScope,
					{
						lang: selectedLangId,
						image: image,
						content: content,
						order: order					
					}
	);				
}
//update category
function ajaxSendUpdateNavMenuCategory(id, image, content, order, thisScope){
	ajaxGenericCall('XmlGMServlet?action_type=nav_menu_actions&action=update_category',
					function(conn, response, options){
						var failure = lookForXMLFailureResponse(conn.responseXML);
						if(failure != null){	//in case there was an error show it
							showErrorDialog('ERROR!!',failure);
						}else{
							var scope = response.scope;
							scope.edit_menu_itemsCategoryChooser.reload();
							var result = getResponseText(conn.responseXML);
							showInfoDialog('INFO', result);
							//update the navTree
							scope.showNewsTree.loadTree();
							scope.disableUpdateDeleteCategoryButtons();
						}						
					},
					thisScope,
					{
						id: id,
						lang: selectedLangId,
						image: image,
						content: content,
						order: order
					}
	);				
}

//add new item
//function ajaxSendNewNavMenuItem(image, destination, category, content, order, thisScope){
function ajaxSendNewNavMenuItem(image, category, content, order, thisScope){
	ajaxGenericCall('XmlGMServlet?action_type=nav_menu_actions&action=add_link_item',
					function(conn, response, options){
						var failure = lookForXMLFailureResponse(conn.responseXML);
						if(failure != null){	//in case there was an error show it
							showErrorDialog('ERROR!!',failure);
						}else{
		   					var scope = response.scope;
			   				//scope.showNewsTree.render();
							var result = getResponseText(conn.responseXML);
							showInfoDialog('INFO', result);
							//update the navTree
							scope.showNewsTree.loadTree();
						}						
					},
					thisScope,
					{
						lang: selectedLangId,
						image: image,
						//destination: destination, 
						category: category,
						content: content,
						order: order
					}
	);			
}
//update item
//function ajaxSendUpdateNavMenuItem(id, image, destination, category, content, order, thisScope){
function ajaxSendUpdateNavMenuItem(id, image, category, content, order, thisScope){
	ajaxGenericCall('XmlGMServlet?action_type=nav_menu_actions&action=update_link_item',
					function(conn, response, options){
						var failure = lookForXMLFailureResponse(conn.responseXML);
						if(failure != null){	//in case there was an error show it
							showErrorDialog('ERROR!!',failure);
						}else{
		   					var scope = response.scope;
			   				//scope.showNewsTree.render();
							var result = getResponseText(conn.responseXML);
							showInfoDialog('INFO', result);
							//update the navTree
							scope.showNewsTree.loadTree();
							scope.disableUpdateDeleteItemButtons();
						}						
					},
					thisScope,
					{
						id: id,
						lang: selectedLangId,
						image: image, 
						//destination: destination, 
						category: category,
						content: content,
						order: order
					}
	);			
}

//Request category information
function ajaxRequestMenuCategory(id,thisScope){
	ajaxGenericCall('XmlGMServlet?action_type=nav_menu_actions&action=get_category',
					function(conn, response, options){
						var failure = lookForXMLFailureResponse(conn.responseXML);
						if(failure != null){	//in case there was an error show it
							showErrorDialog('ERROR!!',failure);
						}else{
							var scope = response.scope;
							mapCatgoryToCategoryForm(conn.responseXML,scope);
							scope.tabPanel.activate("edit_menu_categories");
							//select the element
							scope.selectedCategory = response.params.id;
							//enable update and delete buttons
							scope.enableUpdateDeleteCategoryButtons();
						}						
					},
					thisScope,
					{
						lang: selectedLangId,
						id:id
					}
	);				
}

//Request item information
function ajaxRequestMenuItem(id,thisScope){
	ajaxGenericCall('XmlGMServlet?action_type=nav_menu_actions&action=get_item',
					function(conn, response, options){
						var failure = lookForXMLFailureResponse(conn.responseXML);
						if(failure != null){	//in case there was an error show it
							showErrorDialog('ERROR!!',failure);
						}else{
							var scope = response.scope;
							mapItemToMenuItemForm(conn.responseXML,scope);
							scope.tabPanel.activate("edit_menu_items");
							//select the element
							scope.selectedItem = response.params.id;
							//enable update and delete buttons
							scope.enableUpdateDeleteItemButtons();
						}						
					},
					thisScope,
					{
						lang: selectedLangId,
						id:id
					}
	);			
}
//Delete a menu item
function ajaxSendDeleteMenuItem(id,thisScope){
	ajaxGenericCall('XmlGMServlet?action_type=nav_menu_actions&action=delete_link_item',
					function(conn, response, options){
						var failure = lookForXMLFailureResponse(conn.responseXML);
						if(failure != null){	//in case there was an error show it
							showErrorDialog('ERROR!!',failure);
						}else{
							var scope = response.scope;
							//clear all fields
							
							//update the tree	
							scope.showNewsTree.loadTree();
							var result = getResponseText(conn.responseXML);
							showInfoDialog('INFO', result);
							scope.disableUpdateDeleteItemButtons();						
						}
					},
					thisScope,
					{
						lang: selectedLangId,
						id: id
					}
	);	
}
//Delete a category
function ajaxSendDeleteMenuCategory(id,thisScope){
	ajaxGenericCall('XmlGMServlet?action_type=nav_menu_actions&action=delete_category',
					function(conn, response, options){
						var failure = lookForXMLFailureResponse(conn.responseXML);
						if(failure != null){	//in case there was an error show it
							showErrorDialog('ERROR!!',failure);
						}else{
							var scope = response.scope;
							//clear all fields
							
							//update the tree	
							scope.showNewsTree.loadTree();
							scope.edit_menu_itemsCategoryChooser.reload();
							var result = getResponseText(conn.responseXML);
							showInfoDialog('INFO', result);;
							scope.disableUpdateDeleteCategoryButtons();							
						}					
					},
					thisScope,
					{
						lang: selectedLangId,
						id: id
					}
	);
}

