/**
Requires jQuery
*/

this.saletype = function() {  
	getData();
	$('#saleType_toolbar').hide();
	var _data = [
			{label:'节点1',value:'1',cate:'first',isLeaf:false,children:[
				{label:'节点11',value:'11',cate:'second',isLeaf:true},
				{label:'节点12',value:'12',cate:'second',isLeaf:true},
				{label:'节点13',value:'13',cate:'second',isLeaf:false,children:[
					{label:'节点131',value:'131',cate:'third',isLeaf:true},
					{label:'节点132',value:'132',cate:'third',isLeaf:true},
				]}
			]},
			{label:'节点2',value:'2',cate:'first',isLeaf:true},
			{label:'节点3',value:'3',cate:'first',isLeaf:true},
			{label:'节点4',value:'4',cate:'first',isLeaf:true},
			{label:'节点2',value:'5',cate:'first',isLeaf:true},
			{label:'节点3',value:'6',cate:'first',isLeaf:true},
			{label:'节点2',value:'7',cate:'first',isLeaf:true},
			{label:'节点3',value:'8',cate:'first',isLeaf:true},
			{label:'节点2',value:'9',cate:'first',isLeaf:true},
			{label:'节点3',value:'10',cate:'first',isLeaf:true}
			];
	
	//generateTree('div_saleType',_data,'saleTypeDialog','checkbox',true);
	
	function getData(){
		$.ajax({
			type: "POST",
			url: $('#saleType_url').val()+'!getSaleTypeByJson.action',
			cache: false,
			dataType: "json",
			timeout: 60000,
			beforeSend: function(XMLHttpRequest){
				$('#div_saleType').empty().append('<h4>营销类型获取中</h4>');
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				$('#div_saleType').empty().append('<h4>营销类型获取失败，请点击<a id="reflushSaleType" href="javascript:void(0)">刷新</a></h4>');
				$('#reflushSaleType').bind('click',getData);
				//alert(textStatus);
			},
			complete: function(){
			},
			success: function(msg){
				if(msg.success){
					$('#div_saleType').empty();
					$('#saleType_toolbar').show();
					generateTree('div_saleType',msg.rows,'saleTypeDialog','checkbox',true);
				} else {
					$('#div_saleType').empty().append('<h4>营销类型没有数据</h4>');
				}
	      	}
		});
	
	}
	
    $('#saleTypePage').bind('keyup',openDialog).bind('mousedown',openDialog);
	
	function openDialog(){
		$("#dialog_saleType").dialog({closed:false});
		//$('#saleType_cancel').focus();
		return false;
	}
	
	$("#saleType_all").click(function() {
		$("#div_saleType > ul > li").each(function(i, n){
			$.tree.plugins.checkbox.check($(n));
		});
	});

	$("#saleType_none").click(function() {
		$("#div_saleType > ul > li").each(function(i, n){
			$.tree.plugins.checkbox.uncheck($(n));
		});
	});

	$("#saleType_toggle").click(function() {
		$("#div_saleType > ul > li").each(function(i, n){
			$.tree.plugins.checkbox.toggle($(n));
		});
	});
	
	$('#saleType_ok').click(function(){
		var _count = 0;
		var _firstValue = '';
		var _secondValue = '';
		var _thirdValue = '';
		//var _pageValue ='';
		var _separatorForFirst = '';
		var _separatorForSecond = '';
		var _separatorForThird = '';
		
		var _selected = $.tree.plugins.checkbox.get_checked();
		$(_selected).each(function(i, n) {
			var _val = $(n).attr('val');
			var _cate = $(n).attr('cate');
			_count ++ ;
			if( 'first' == _cate ){//第一级数据
				_firstValue += _separatorForFirst + _val;
				_separatorForFirst = ',';
			} else if( 'second' == _cate ){
				_secondValue += _separatorForSecond + _val;
				_separatorForSecond = ',';
			} else if( 'third' == _cate ){
				_thirdValue += _separatorForThird + _val;
				_separatorForThird = ',';
			}
			
		//	_pageValue += _separator + $(n).find('a').html().replace('<ins>&nbsp;</ins>','').replace('<INS>&nbsp;</INS>','');
		});
		//$('#saleTypePage').val(_pageValue);
		$('#saleTypePage').html('已选营销类型'+_count+'个');
		$('#saleType_firstsaname').val(_firstValue);
		$('#saleType_secondsaname').val(_secondValue);
		$('#saleType_thirdsaname').val(_thirdValue);
		
		$("#dialog_saleType").dialog({closed:true});
	});
	
	$('#saleType_cancel').click(function(){
		$("#dialog_saleType").dialog({closed:true});
	});
	
	function generateTree(targetId,data,inputName,choseType,isCascade){
		/****
		*	generate a tree which contains checkbox or radio
		*	targetId:which places the tree;
		*	data: the data to build the tree
		*	inputName: the name of input
		*	choseType:the type of input,only 'radio' and 'checkbox' works
		*	isCascade: defines whether checkbox is cascade(the prenies is choseType is in ''children'' of 'checkbox')
		****/
		var $div = $('#' + targetId);
		var _html = buildTree(data,inputName,false,choseType);
		$div.append(_html);
		
		if( 'checkbox' == choseType && isCascade){
			$('input[name='+inputName+']').click(function(){
				var _isChecked = $(this).attr('checked');
				$(this).parent().find('input').attr('checked',_isChecked);
			});
		}
		
		$('.zjl_tree_node_li_a').toggle(function(){//change the icon and hide or display children when click
			 	$(this).html('<img src="${ctx}/images/arrow_top.gif" border="0"/>').siblings('ul').show();
			 },function(){
				$(this).html('<img src="${ctx}/images/arrow_left.gif" border="0"/>').siblings('ul').hide();
			 }
		);
		
		//generate the tree
		$("#div_saleType").tree({
			ui : {
				theme_name : "checkbox",
				selected_parent_close : false
			},
			plugins : { 
				checkbox : { three_state : true }
			}
		});
	}
	
	function buildTree(data,inputName,isHide,choseType){
		var _html = '';
		if( '' != data ){
			if( isHide ){//display which deep==1 ,others hide
				_html += '<ul>';
			} else {
				_html += '<ul>';
			}
			
			try{
				$(data).each(function(i,n){
				if( n.isLeaf ){
					_html += '<li val="'+n.value+'" cate="'+n.cate+'"><a href="#"><ins>&nbsp;</ins>'+ n.label +'</a>';
				//	<li class="zjl_tree_leaf"><input type="'+choseType+'" name="'+inputName+'" value="'+n.value+'" id="'+inputName+'-'+n.value+'"/>' +
				//	'<label for="'+inputName+'-'+n.value+'">'+ n.label +'</label></li>';
				} else {
					//_html += '<li class="zjl_tree_node_li"><a href="javascript:void(0)" class="zjl_tree_node_li_a"><img src="${ctx}/images/arrow_left.gif" border="0"/></a>' +
					//'<input type="'+choseType+'" name="'+inputName+'" value="'+n.value+'" id="'+inputName+'-'+n.value+'"/>' +
					//'<label for="'+inputName+'-'+n.value+'">'+ n.label +'</label>';
					
					_html += '<li val="'+n.value+'" cate="'+n.cate+'"><a href="#"><ins>&nbsp;</ins>'+ n.label +'</a>';
					
					_html  += buildTree(n.children,inputName,true,choseType);
					_html += '</li>';
				}
				
			});
			} catch (exception) { 
		     
		    } 
			
			
			/**
			
			$(data).each(function(i,n){
				if( n.isLeaf ){
					_html += '<li class="zjl_tree_leaf"><input type="'+choseType+'" name="'+inputName+'" value="'+n.value+'" id="'+inputName+'-'+n.value+'"/>' +
					'<label for="'+inputName+'-'+n.value+'">'+ n.label +'</label></li>';
				} else {
					_html += '<li class="zjl_tree_node_li"><a href="javascript:void(0)" class="zjl_tree_node_li_a"><img src="${ctx}/images/arrow_left.gif" border="0"/></a>' +
					'<input type="'+choseType+'" name="'+inputName+'" value="'+n.value+'" id="'+inputName+'-'+n.value+'"/>' +
					'<label for="'+inputName+'-'+n.value+'">'+ n.label +'</label>';
					
					_html  += buildTree(n.children,inputName,true,choseType);
					_html += '</li>';
				}
			**/
			
			_html += '</ul>';
		}
		return _html;
	}          
    
};

jQuery(document).ready(function($){saletype();}) 