//THIS FILE CONTAINS ALL THE COMPONENTS THAT WEB INTERFACE REQUIRE TO 
// - EDIT THE NEWS

//CLASS NEWS EDITOR

function NewsEditor(categoriesURL, iconsURL, menuTreeURL, renderTo, languages){
			var newsEditorEditorTitleText = getLanguageForElement("newsEditorEditorTitleText", selectedLangId);
	
			var config = {
			globalParameters: {
				renderTo: renderTo,
				formTitle: newsEditorEditorTitleText, 
				categoriesURL: categoriesURL,
				iconsURL: iconsURL,
				defaultIconImage: './img/icons/blank-48x48.gif',	
				languages: languages
			},
			navigationTreeConfig: {
				treeID: 'tree',
				treeRootNode: 'All_news',
				treeRequestDataURL: menuTreeURL,
				treeDoubleClickCategoryFunction: function(node, tree, e){ //function that will be executed on category double click
					var scope = tree.scope;
					ajaxRequestNewsCategory(node.attributes.nodeId, scope);
				},
				treeDoubleClickItemFunction: function(node, tree, e){ //function that will be executed on item double click
					var scope = tree.scope;
					ajaxRequestNewsItem(node.attributes.nodeId, scope);
				}
			},
			itemEditor: {
				specificComponents:{},			
				updateComponentsFromXML: function(xmlDoc, components){},
				send: function(image, category, content, specificComponents, scope){
					ajaxSendNewNewsItem(image, category, content, scope);				
				},
				update: function(id,image, category, content, specificComponents, scope){	
					ajaxSendUpdateNewsItem(id,image, category, content, scope);
				},
				remove: function(selectedItem, specificComponents, scope){
					ajaxSendDeleteNewsItem(selectedItem, scope);
				}				
			},
			categoryEditor: {
				specificComponents:{},
				updateComponentsFromXML: function(xmlDoc, components){},
				send: function(image, content, specificComponents, scope){	
					ajaxSendNewNewsCategory(image, content, scope);	
				},
				update: function(id,image, content, specificComponents, scope){		
					ajaxSendUpdateNewsCategory(id, image, content, scope);
				},
				remove: function(selectedItem, specificComponents, scope){
					ajaxSendDeleteNewsCategory(selectedItem, scope);						
				}
			}
		};
		return navEditor = new genericCateogryItemEditor(config);
}

/*
//add new category
function ajaxSendReadNewsItem(id,lang,thisScope){
    Ext.Ajax.request({
        url: 'XmlGMServlet?action_type=news_actions&action=get_news_item&id=' + id + '&lang=' + lang,
        success: function(conn, response, options){
			var scope = response.scope;
			mapNewsToNewsForm(conn.responseXML,scope);
        },
        failure: function(){
            showErrorDialog('Error', 'could not connect to server');
        },
        scope: thisScope	
    });
}*/

//create new news item
function ajaxSendNewNewsItem(image, category, langContent, thisScope){
	ajaxGenericCall('XmlGMServlet?action_type=news_actions&action=add_news_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;
							var result = getResponseText(conn.responseXML);
							showInfoDialog('INFO', result);
							//update the navTree
							scope.showNewsTree.loadTree();
						}
					},
					thisScope,
					{
						lang: selectedLangId,
						image: image,
						category: category,
        				content: langContent
					}
	);
}

//create new news item
function ajaxSendNewNewsCategory(image, langContent, thisScope){
	ajaxGenericCall('XmlGMServlet?action_type=news_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: langContent
					}
	);
}

//Request category information
function ajaxRequestNewsCategory(id,thisScope){
	ajaxGenericCall('XmlGMServlet?action_type=news_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 ajaxRequestNewsItem(id,thisScope){
	ajaxGenericCall('XmlGMServlet?action_type=news_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
					}
	);			
}

//create new news item
function ajaxSendDeleteNewsItem(id,thisScope){
	ajaxGenericCall('XmlGMServlet?action_type=news_actions&action=delete_news_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 ajaxSendDeleteNewsCategory(id,thisScope){
	ajaxGenericCall('XmlGMServlet?action_type=news_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;
							
							//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
					}
	);
}

//update new news item
function ajaxSendUpdateNewsItem(id, image, category, langContent, thisScope){
	ajaxGenericCall('XmlGMServlet?action_type=news_actions&action=update_news_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;
							var result = getResponseText(conn.responseXML);
							showInfoDialog('INFO', result);
							//update the navTree
							scope.showNewsTree.loadTree();
						}
					},
					thisScope,
					{
						lang: selectedLangId,
						id: id,
						image: image,
						category: category,
        				content: langContent
					}
	);
}

//update new news category
function ajaxSendUpdateNewsCategory(id, image, langContent, thisScope){
	ajaxGenericCall('XmlGMServlet?action_type=news_actions&action=update_news_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;
							var result = getResponseText(conn.responseXML);
							showInfoDialog('INFO', result);
							//update the navTree
							scope.showNewsTree.loadTree();
						}
					},
					thisScope,
					{
						lang: selectedLangId,
						id: id,
						image: image,
        				content: langContent
					}
	);
}
