
/******************************************************************************
 *
 *	PROJECT: Flynax Classifieds Software
 *	VERSION: 4.0
 *	LISENSE: http://www.flynax.com/license-agreement.html
 *	PRODUCT: General Classifieds
 *	
 *	FILE: CROSSED.JS
 *
 *	This script is a commercial software and any kind of using it must be 
 *	coordinate with Flynax Owners Team and be agree to Flynax License Agreement
 *
 *	This block may not be removed from this file or any other files with out 
 *	permission of Flynax respective owners.
 *
 *	Copyrights Flynax Classifieds Software | 2012
 *	http://www.flynax.com/
 *
 ******************************************************************************/

crossCount = plans[selected_plan_id] ? plans[selected_plan_id]['Cross'] : false;
crossTotal = 0;
xajaxFix = false;

var crossedTree = function(clear, first){
	this.inputs = $('#crossed_tree input[name="crossed_categories[]"]');
	this.printInfo = function(){
		$('#cc_number').html(crossCount);
		if( crossTotal > 0)
		{
			$('#crossed_selected').fadeIn();
		}
		else
		{
			$('#crossed_selected').fadeOut();
		}
		
		if ( crossCount == 0 )
		{
			$('#cc_text').hide();
			$('#cc_text_denied').show();
			
			$('#crossed_tree input:not(.disabled)').attr('disabled', true);
		}
		else if ( crossCount == 1 )
		{
			$('#cc_text_denied').hide();
			$('#cc_text').show();
			
			$('#crossed_tree input:not(.disabled,.system)').attr('disabled', false);
		}
	};
	this.remove = function(){
		$('li.tmp_crossed_select img.remove').unbind('click').click(function(){
			var id = $(this).parent().attr('id').replace('tmp_', '');

			$('li#'+id+'>label>input').attr('checked', false);
			$(this).parent().remove();
			crossCount++;
			crossTotal--;
			base.printInfo();
		});
	};
	
	/* reset environment */
	if ( clear )
	{
		$('#crossed_tree input[name="crossed_categories[]"]').attr({checked: false});
		$('#crossed_tree input[name="crossed_categories[]"]:not(.system)').attr({disabled: false,class: false});
	}
	
	/* unbind events */
	$(inputs).unbind('click');
	
	/* call default methods */
	this.printInfo();
	
	/* define referent to class self */
	var base = this;
	
	/* open sub-level checkboxes handler */
	if ( crossCount == 0 )
	{
		$('#cc_text').hide();
		$('#cc_text_denied').show();
		
		$('#crossed_tree input:not(.disabled)').attr('disabled', true);
	}
	
	/* click handler */
	$(inputs).click(function(){
		var id = $(this).parent().parent().attr('id');
		
		if ( $(this).is(':checked') )
		{
			crossCount--;
			crossTotal++;
			$(this).addClass('disabled');
			
			var html = '<li class="tmp_crossed_select" id="tmp_'+id+'"><a href="'+$(this).next().next().attr('href')+'" target="_blank">'+$(this).next().html()+'</a> <img src="'+rlConfig['tpl_base']+'img/blank.gif" class="remove" alt="" title="'+ lang['delete'] +'" /></li>';
			$('#crossed_selected').append(html);
			base.remove();
		}
		else
		{
			crossCount++;
			crossTotal--;
			$(this).removeClass('disabled');
			
			$('#crossed_selected li#tmp_'+id).remove();
		}
		
		base.printInfo();
	});
	
	/* post handler */
	if ( ca_post.length > 0 )
	{
		if ( cc_parentPoints.length > 0 )
		{
			for ( var i=0; i<cc_parentPoints.length; i++ )
			{
				$('#tree_cat_'+cc_parentPoints[0]+'>img:first').trigger('click');
			}
			cc_parentPoints.splice(0, 1);
		}
		else
		{
			for ( var i=0; i<ca_post.length; i++ )
			{
				$('#tree_cat_'+ca_post[i]+'>label>input').attr('checked', true).trigger('click');
				if ( !first )
				{
					$('#tree_cat_'+ca_post[i]+'>label>input').attr('checked', true);
				}
			}
			ca_post = false;
		}
	}
}

$(document).ready(function(){
	//crossedTree(true, true);
	
	$('#crossed_button').click(function(){
		var value = parseInt($('input[name=crossed_done]').val());
		
		if ( !value )
		{
			$('input[name=crossed_done]').val(1);
			$('#crossed_tree').slideUp();
			$(this).val(lang['manage']);
		}
		else
		{
			$('input[name=crossed_done]').val(0);
			$('#crossed_tree').slideDown();
			$(this).val(lang['done']);
			$('#crossed_tree span.tree_loader').hide();
		}
	});
});