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

function QuickLinkEditor(iconsURL, categoriesURL, staticHTMLURL, renderTo, languages){
	
		var quickLinkEditorEditorTitleText = getLanguageForElement("quickLinkEditorEditorTitleText", selectedLangId);
		var navMenuDestinationLableText = getLanguageForElement("navMenuDestinationLableText", selectedLangId);
		var productsEditorDisplayOrderLableText = getLanguageForElement("productsEditorDisplayOrderLableText", selectedLangId);
		

		var config = {
			globalParameters: {
				renderTo: renderTo,
				formTitle: quickLinkEditorEditorTitleText,
				categoriesURL: '',
				iconsURL: iconsURL,
				defaultIconImage: './img/icons/blank-48x48.gif',
				languages: languages
			},
			navigationTreeConfig: {				
				treeID: 'tree',
				treeRootNode: 'All_quick_links',
				treeRequestDataURL: 'XmlGMServlet?action_type=quick_link_actions&action=get_quick_links_names',
				treeDoubleClickCategoryFunction: Ext.emtpyFn, //function that will be executed on category double click
				treeDoubleClickItemFunction: function(node, tree, e){ //function that will be executed on item double click
					var scope = tree.scope;
					ajaxRequestQuickLinkItem(node.attributes.nodeId, scope);
				}
			},
			itemEditor: {
				displayCategory : false,
				specificComponents:{
						edit_menu_itemDestinationCombo: new GmSoft.components.ComboSelector(
															{
														  		comboId: 'edit_menu_itemsDestinationCombo',
														  		label: navMenuDestinationLableText,
														  		queryField: 'file',
														  		mappingElement: 'url',
														  		queryURL: staticHTMLURL,
														  		functionToExecOnSelection: Ext.emptyFn,
																comboWidth: 250,
														  		scope: this 
															}),
						itemOrder: getNumberFieldComponent('quickLink_order',productsEditorDisplayOrderLableText,'quickLink_order',250),
						colorPicker: new GmSoft.components.ColorSelector({defaultValue: '993300'})
				},			
				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);
					//update the color
					var color = findDirectTextNodeValue(xmlDoc.getElementsByTagName('color')[0]);
					components.colorPicker.setColor(color);
				},
				send: function(image, category, content, specificComponents, scope){
					//get specific component values
					var destination = specificComponents.edit_menu_itemDestinationCombo.getCombo().getValue();				
					var order = specificComponents.itemOrder.getValue();
					var color = specificComponents.colorPicker.getColor();
					//send the request				
					ajaxSendNewQuickLinkItem(image, destination, content, order, color, 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();
					var color = specificComponents.colorPicker.getColor();
					//request update
					ajaxSendUpdateQuickLinkItem(id,image, destination, content, order, color, scope);				
				},
				remove: function(selectedItem, specificComponents, scope){
					ajaxSendDeleteQuickLinkItem(selectedItem, scope);
				}				
			},
			categoryEditor: {
				specificComponents:{},
				updateComponentsFromXML: Ext.EmtpyFn,
				send: Ext.EmtpyFn,
				update: Ext.EmtpyFn,
				remove: Ext.EmtpyFn
			}
		};
		var the_component = new genericCateogryItemEditor(config);
		//hide all not required elements
		//hide categories
		the_component.edit_menu_itemsForm.remove(the_component.selectMenuItemsIcon,false);
		the_component.edit_menu_itemsForm.doLayout(false);
		the_component.edit_menu_itemsForm.render(true);
		//hide edit categories tab
		the_component.tabPanel.remove(the_component.edit_menu_categoriesForm);
		the_component.border.doLayout(false);
		return navEditor = the_component;
}

//save the quick link
function ajaxSendNewQuickLinkItem(image, destination, content, order, color, thisScope){
	ajaxGenericCall('XmlGMServlet?action_type=quick_link_actions&action=add_quick_link',
					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,
					{
							image: image,
							destination: destination, 
							content: content,
							order: order,
							color: color
					}
	);			
}

//save the quick link
function ajaxSendUpdateQuickLinkItem(id, image, destination, content, order, color, thisScope){
	ajaxGenericCall('XmlGMServlet?action_type=quick_link_actions&action=update_quick_link',
					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,
						image: image, 
						destination: destination, 
						content: content,
						order: order,
						color: color
					}
	);			
}

function ajaxSendDeleteQuickLinkItem(id,thisScope){
	ajaxGenericCall('XmlGMServlet?action_type=quick_link_actions&action=delete_quick_link',
					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.disableUpdateDeleteItemButtons();			
						}					
					},
					thisScope,
					{
						id: id
					}
	);
}

//Request quick link information
function ajaxRequestQuickLinkItem(id,thisScope){
	ajaxGenericCall('XmlGMServlet?action_type=quick_link_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,
					{
						id:id
					}
	);			
}


