var acc;
Event.observe(window, 'load', function(){
	acc = new Accordion("left", "h2", "acc");
})

var LeftMenu = Class.create();
LeftMenu.prototype = {
	sectionName:'',
	initialize: function(){},
	editMenuSection: function(SectionName)
	{
		this.sectionName = SectionName;
		showOverlay();
		//bring the info about this section of menu from database;
		new Ajax.Request(Url+'index.php?action=ajaxOperation&objectToEdit=GetMenuBoxInfo', {
			method: 'post',
			parameters: 'BoxTitle='+escape(SectionName),
			onSuccess: function(transport)
			{
				$('SecondContent').update(transport.responseText)
				$('SecondContent').show();
				//hideOverlay();
			},
			onFailure: function()
			{
				alert('Something went wrong');
			}
		});
	},
	loadCategoryForm: function(CategoryParentId, CategoryId, eraseSubcategoryForm)
	{
		if(eraseSubcategoryForm)
		{
			$('SubcategoriesDiv').update('');
		}
		$('CategoriesFormDiv').update(Loader);
		//find the infos about main category
		new Ajax.Request(Url+'index.php?action=ajaxOperation&objectToEdit=loadCategoryForm', {
			method:'post',
			parameters:'CategoryId='+parseInt(CategoryId)+'&CategoryParentId='+parseInt(CategoryParentId),
			onSuccess: function(transport)
			{
				$('CategoriesFormDiv').update(transport.responseText);
			},
			onFailure: function()
			{
				alert('Something went wrong');
			}
		})
	},
	getCategoryInfo: function(categoryId)
	{
		$('CategoriesFormDiv').update('');
		$('SubcategoriesDiv').update(Loader);
		//find the infos about main category
		new Ajax.Request(Url+'index.php?action=ajaxOperation&objectToEdit=extractSubcategories', {
			method:'post',
			parameters:'CategoryId='+parseInt(categoryId),
			onSuccess: function(transport)
			{
				$('SubcategoriesDiv').update(transport.responseText);
			},
			onFailure: function()
			{
				alert('Something went wrong');
			}
		})
	},
	saveCategory: function()
	{
		if ($F('CategoryName').blank())
		{
			$('CategoryFormErrorPlacer').update('Please insert a name for Category')
			return;
		}
		$('SaveCategoryButton').disable();
		$('CategoryName').disable();
		$('SaveCategoryButton').value='Saving...';
		var Category = new Object;
		Category.Id = $F('CategoryId');
		Category.ParentId = $F('CategoryParentId');
		Category.Name=escape($F('CategoryName'));
		Category.ExternalLink = escape($F('CategoryExternalLink').strip());
		Category.ExternalLinkIsBlank = $('ExternalLinkIsBlank').checked ? true : false;
		new Ajax.Request(Url+'index.php?action=ajaxOperation&objectToEdit=saveCategory', {
			method: 'post',
			parameters: 'CategoryObject='+Object.toJSON(Category),
			onSuccess: function(transport)
			{
				if (isNaN(transport.responseText) || transport.responseText < 1)
				{
					$('CategoryFormErrorPlacer').update(transport.responseText)
				}
				else
				{
					LM.reloadCategories();
				}
			},
			onFailure: function()
			{
				alert('Something went wrong!')	
			}
		})
	},
	removeCategory: function(CategoryId)
	{
		if (confirm('Are you sure?'))
		{
			new Ajax.Request(Url+'index.php?action=ajaxOperation&objectToEdit=removeCategory', {
				method: 'post',
				parameters: 'CategoryId='+parseInt(CategoryId),
				onSuccess: function(transport)
				{
					if (transport.responseText != 'success')
					{
						alert(transport.responseText)
					}
					else
					{
						LM.reloadCategories();
					}
				}
			})
		}
	},
	reloadCategories: function()
	{
		try
		{
			new Ajax.Request(Url+'index.php?action=ajaxOperation&objectToEdit=GetMenuBoxInfo', {
				method: 'post',
				parameters: 'BoxTitle='+escape(this.sectionName),
				onSuccess: function(transport)
				{
					$('SecondContent').update(transport.responseText)
					//hideOverlay();
				},
				onFailure: function()
				{
					alert('Something went wrong');
				}
			});
		}
		catch(Ex)
		{
			alert(Ex.message)
		}
	},
	reloadMenu: function()
	{
		$('left').update(Loader);
		new Ajax.Request(Url+'index.php?action=ajaxOperation&objectToEdit=loadLeftMenu', {
			onSuccess: function(transport)
			{
				$('left').update(transport.responseText);
				acc = new Accordion("left", "h2", "acc");
			}
		});
	}
}
var LM = new LeftMenu;