/**
 * Class for creating tree categories.
 * @class This is the Tree class.  
 *
 * @param {Array} conf
 *
 * @param {String} conf.id The id container element for tree
 * @param {String} conf.type The type of tree element. (Radio | Checkbox)  
 * @param {String} conf.disabled The list of disabled categories by default
 * @param {String} conf.state The list of checked categories by default
 * @param {String} conf.expanded The list of expanded categories by default
 * @param {String} conf.url The URL to script
 * @param {Boolean} conf.hideRoot Showing root category
 * @param {Boolean} expandableLocked Display the subcategories of locked category
 * @param {Array} conf.defaultCategory The array of default category
 * @param {Function} conf.callback The callback function
 *
 * TODO: 
 * Add ability to pass as defaultCategory the titles of categories
 * Add spiner
 */
esyndicat.tree = function(conf)
{
	var obj = (-1 != conf.id.indexOf('#')) ? $(conf.id) : $('#' + conf.id);
	var type = conf.type ? conf.type : 'radio';
	
	var disabled = conf.disabled ? conf.disabled.split(',') : '';
	var state = conf.state ? conf.state.split(',') : '';
	var expanded = conf.expanded ? conf.expanded.split(',') : '';

	var url = conf.url ? conf.url : 'get-categories.php';
	var hideRoot = conf.hideRoot ? conf.hideRoot : false;

	var expandableLocked = conf.expandableLocked ? conf.expandableLocked : false;

	var defaultIdCategory = conf.defaultIdCategory ? conf.defaultIdCategory : 0;

	var callback = (typeof conf.callback == 'function') ? conf.callback : function(){};

	/* prevent duplicate id if there are several tree on page */
	var idPrefix = esyndicat.getRandomLetter();
	
	if(hideRoot || defaultIdCategory)
	{
		$.ajaxSetup({async: false});

		var idCatParam = (defaultIdCategory) ? '?id=' + defaultIdCategory : '?id=0';

		$.getJSON(url + idCatParam, function(categories)
		{
			if('null' != categories)
			{
				defaultCategory = categories;
			}
		});

		$.ajaxSetup({async: true});
	}
	else
	{
		defaultCategory = conf.defaultCategory ? conf.defaultCategory : [{id: 0, title: 'ROOT', sub: true, disabled: false, checked: false}];
	}

	/**
	 *  Initialization the tree
	 */
	this.init = function()
	{
		build(obj, defaultCategory);

		/* Saving the random letter */
		obj.append('<input type="hidden" id="prefix_'+ conf.id +'" value="'+ idPrefix +'" />');
	};

	/**
	 * Building html
	 * 
	 * @param {Object} obj The container element
	 * @param {Array} categories The array of categories
	 */
	function build(obj, categories)
	{
		var html = '';

		html += '<ul class="tree">';

		for(var i = 0; i < categories.length; i++)
		{
			categories[i].locked = categories[i].locked ? categories[i].locked : false;
			categories[i].crossed = categories[i].crossed ? categories[i].crossed : false;
			categories[i].disabled = categories[i].disabled ? categories[i].disabled : esyndicat.inArray(disabled, categories[i].id);
			categories[i].checked = categories[i].checked ? categories[i].checked : esyndicat.inArray(state, categories[i].id)

			html += '<li>';
			html += '<div class="tree-col">';

			var locked = (categories[i].locked && !expandableLocked) ? false : true;
			var cls = (esyndicat.inArray(expanded, categories[i].id)) ? 'expanded' : 'collapsed';

			if(categories[i].sub && locked)
			{
				html += '<a href="#" id="c_'+ idPrefix + '_' + categories[i].id +'" class="'+ cls +'" onclick="return false;">';
				html += '<img class="tree-icon-collapsed" src="templates/'+ template_name +'/img/sp.gif" id="icon_'+ idPrefix +'_'+ categories[i].id +'" />&nbsp;';
				html += '<img class="tree-folder-collapsed" src="templates/'+template_name+'/img/sp.gif" id="folder_'+ idPrefix +'_'+categories[i].id +'" /></a>';
			}
			else
			{
				html += '<img class="tree-icon-space" src="templates/'+ template_name +'/img/sp.gif" id="icon_'+ idPrefix +'_'+ categories[i].id +'" />&nbsp;';
				html += '<img class="tree-folder-collapsed" src="templates/'+template_name+'/img/sp.gif" id="folder_'+ idPrefix +'_'+categories[i].id +'" />';
			}

			html += '<input ';
			html += 'type="'+ type +'" ';
			html += 'title="'+ categories[i].title +'" ';
			html += 'name="categories[]" ';
			html += 'value="'+ categories[i].id +'" ';
			
			if(categories[i].locked || categories[i].disabled)
			{
				html += 'disabled="disabled" ';
			}

			if(categories[i].checked)
			{
				html += 'checked="checked" ';
			}

			html += 'id="cat_'+ idPrefix + '_' + categories[i].id +'" />';

			html += '<label for="cat_'+ idPrefix + '_' + categories[i].id +'" style="cursor: pointer;">'+ categories[i].title +'</label>';

			if(categories[i].locked)
			{
				html += '<img class="tree-cat-locked" src="templates/'+template_name+'/img/sp.gif" />';
			}

			if(categories[i].crossed)
			{
				html += '<img class="tree-cat-crossed" src="templates/'+template_name+'/img/sp.gif" />';
			}

			html += '</div>';
			html += '<div id="categories_'+ idPrefix + '_' +categories[i].id +'" style="display:none;"></div>';
			html += '</li>';
		}

		html += '</ul>';

		obj.html(html);

		for(var i = 0; i < categories.length; i++)
		{
			if(esyndicat.inArray(expanded, categories[i].id))
			{
				var obj = $('#categories_' + idPrefix + '_' + categories[i].id);

				getChildren(categories[i].id);

				obj.attr('class', 'loaded');

				$('#icon_' + idPrefix + '_' + categories[i].id).attr('class', 'tree-icon-expanded');
				$('#folder_' + idPrefix + '_' + categories[i].id).attr('class', 'tree-folder-expanded');
			}

			$('#cat_' + idPrefix + '_' + categories[i].id).click(callback);

			$('#c_' + idPrefix + '_' + categories[i].id).click(function()
			{
				var obj = $(this);
				var id = obj.attr('id').replace('c_' + idPrefix + '_', '');
				var catDiv = $('#categories_' + idPrefix + '_' + id);

				if('collapsed' == obj.attr('class'))
				{
					obj.attr('class', 'expanded');

					if('loaded' == catDiv.attr('class'))
					{
						esyndicat.display(catDiv, 'show');
					}
					else
					{
						getChildren(id);
						catDiv.attr('class', 'loaded');
					}

					$('#icon_' + idPrefix + '_' + id).attr('class', 'tree-icon-expanded');
					$('#folder_' + idPrefix + '_' + id).attr('class', 'tree-folder-expanded');
				}
				else
				{
					obj.attr('class', 'collapsed');
					esyndicat.display(catDiv, 'hide');

					$('#icon_' + idPrefix + '_' + id).attr('class', 'tree-icon-collapsed');
					$('#folder_' + idPrefix + '_' + id).attr('class', 'tree-folder-collapsed');
				}

				return false;
			});
		}
	};

	/**
	 * Insert subcategories by id category 
	 * 
	 * @param {String} id id category
	 * TODO: checking id param
	 */
	function getChildren(id)
	{
		var divCategories = $('#categories_' + idPrefix + '_' + id);

		$.getJSON(url + '?id=' + id, function(categories)
		{
			if('null' != categories)
			{
				build(divCategories, categories);
				esyndicat.display(divCategories);
			}
		});
	};
}


