function toggle_row(row) {
	$('#'+ row).toggle();
}


$.fn.categories = function()
{
	var $container = this,
		anchors = $(' > .curents', this),
		select = $(' > .cats select', this),
		category = $('> .cats [name="category"]', this);
		loading = $(' > .i-loading', this);

	$(select).change(catChange);
	$(' > a', anchors).click(setSelect);

	function catChange()
	{
		if ($(this).val() < 1)
			return false;

		$(this).hide();
		loading.show();

		$.ajax({
			type: 'POST',
			url: conf.vir_path + 'index.php?m=get_ajax&p=get_cats',
			data: 'id=' + $(this).val(),
			success: successGet,
			error: function(responseText)
			{
				alert("Error connection! Please try latter.");
				loading.hide();
			}
		});
	}


	function update(items) 
	{
		select.html('');
		select.append("<option value=\"0\" selected=\"selected\">Select Category</option>");
		$.each(items, function() {
			select.append("<option value=\"" + this.value + "\">" + this.name + "</option>");
		});
		select.show();
	}

	function successGet(responseText)
	{
		var res = responseText ? eval('(' + responseText + ')') : '';
		if (res && res.title)
		{
			anchors.append("<a href=\"javascript:void(0);\" rel=\"" + res.owner_id + "\">" + res.title + "</a>");
			if (res.items.length > 0)
			{
				anchors.append("<span>&nbsp;&nbsp;&raquo;&nbsp;&nbsp;</span>");
				update(res.items);
			}

			$(category).val(res.category_id);
			$(' > a', anchors).click(setSelect);
		}
		else {
			alert("Error connection! Please try latter.");
		}

		loading.hide();
	}

	function setSelect()
	{
		select.hide();
		loading.show();
		$(this).nextAll().remove();
		$(this).remove();

		$.ajax({
			type: 'POST',
			url: conf.vir_path + 'index.php?m=get_ajax&p=get_cats',
			cache: false,
			data: "id=" + $(this).attr('rel'),
			success: function (responseText)
			{
				var res = responseText ? eval('(' + responseText + ')') : '';
				if (res && res.items.length > 0) {
					update(res.items);
				}
				else {
					alert("Error connection! Please try latter.");
				}
				loading.hide();
			},
			error:function()
			{
				alert("Error connection! Please try latter.");
				loading.hide();
			}
		});
	}
}



jQuery.fn.highlight = function(words, settings) {
	var highlight_settings = {split: ' ', className: 'highlight', caseSensitive: false};
	highlight_settings = jQuery.extend(highlight_settings, settings);
 
	function wordsHighlight(node, words) {
		if (node.nodeType == 3) {
		    var regexp = new RegExp("(" + words.replace(highlight_settings.split, "|") +")", (highlight_settings.caseSensitive ? "" : "i") + "g");
		    var str = jQuery('<div></div>').text(node.data).html();
		    jQuery(node).replaceWith(str.replace(regexp, '<span class="' + highlight_settings.className + '">$1</span>'));
		} else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {
			jQuery(node).contents().each(function() {
				wordsHighlight(this, words);
			});
		}
	}
 
	return this.each(function() {
	    wordsHighlight(this, words);
	});
};
 
jQuery.highlightHtml = function (html, words, settings) {
	return jQuery('<div>' + html + '</div>').highlight(words, settings).html();
}
