function remove(_this) {
	$(_this).parent().remove();
}	

function loadBusiness(hidden_b) {
	$('#jobs_box').html(hidden_b);

	$('.now_selected_business').html($('#current_business').html());
	$('.now_selected_business').find('input').each(function() {	
			$(this).attr('name', '');
	});

	return $('#jobs_box').html();
}

function removeCurrentBusiness(_this) {
	$('.now_selected_business,#current_business').find('input').each(function() {	
		if(this.value == _this.value) {
			$(this).parent().remove();
			$(_this).next().removeClass('highline_this');
		}
	});

//	$('.now_selected_business').html($('#current_business').html());

	if($('.now_selected_business').html() =='') {
		if($('#choose_business').attr('type')) {
			$('#choose_business').val('不限');
		}
		else {
			$('#choose_business').text('不限');
		}
		$('#current_place').html('<input type="hidden" value="0" name="business_id[]" />');
	}
}

function resetCurrentBusiness(_this) {
	$('#jobs_box').find('input[parent="' +_this.value+ '"]').each(function() {
		this.disabled = false;
	});

	$('#jobs_box').find('input').each(function() {
		if(this.value == _this.value) {
			this.checked = false;
			$(this).next().removeClass('highline_this');
		}
//		$(_this).parent().remove();
	});

		removeCurrentBusiness(_this);	

	//		alert($('.now_selected_business').html());
	if($('.now_selected_business').html() =='') {
//		$('#choose_business').val('不限');
		if($('#choose_business').attr('type')) {
			$('#choose_business').val('不限');
		}
		else {
			$('#choose_business').text('不限');
		}
		$('#current_business').html('<input type="hidden" value="0" name="business_id[]" />');
	}
}

function setCurrentBusiness(_this, parent) {
	if(parent) {
//		$('.now_selected_business').empty();
		$('#jobs_box').find('input').each(function() {
			if(_this.value == $(this).attr('parent')) {
				if(_this.checked) {
					this.disabled = true;
				}
				else {
					this.disabled = false;
				}
			}
		});
	}
//	alert(place_name);
	var length = $('.now_selected_business').eq(0).find('input:checked').length;
//	alert(length);
	if(_this.checked) {
		$(_this).next().addClass('highline_this');
		if(length > SELECT_OPTIONS - 1) {
			_this.checked = false;
			$(_this).next().removeClass('highline_this');
			alert('最多只能选' +SELECT_OPTIONS+ '项');
		}
		else {
			if(SELECT_OPTIONS == 1) {
//				$('#choose_business').val($(_this).attr('name'));

		if($('#choose_business').attr('type')) {
			$('#choose_business').val($(_this).attr('name'));
		}
		else {
			$('#choose_business').text($(_this).attr('name'));
		}


				$('#current_business').html('<span><input type="hidden" value="' +_this.value+ '" name="business_id[]" /></span>');
				$('.now_selected_business').html('<span><input name="business_id[]" value="' +_this.value+ '" id="now_area_' +_this.value+ '" type="checkbox" checked="checked" onclick="resetCurrentBusiness(this);" /><label class="hand" for="now_area_' +_this.value+ '">' +$(_this).attr('name')+ '</label></span>');
				$('#business_box').hide();
				//$('.toHide').toggle();
			} else {
					$('#jobs_box').find('input').each(function() {
						if(_this.value == $(this).attr('parent')) {
							this.disabled = true;
							this.checked = false;
						}
					});
				$('.now_selected_business').append('<span><input value="' +_this.value+ '" id="now_area_' +_this.value+ '" type="checkbox" checked="checked" onclick="resetCurrentBusiness(this);" /><label class="hand" for="now_area_' +_this.value+ '">' +$(_this).attr('name')+ '</label></span>');			
		
//				$('#current_business').append('<span><input parent="' +$(_this).attr('parent')+ '" type="checkbox" value="' +_this.value+ '" name="business_id[]" id="business_' +_this.value+ '" checked="checked" onclick="remove(this);resetCurrentBusiness(this);removeCurrentBusiness(this);" /><label class="hand" for="business_' +_this.value+ '">' +$(_this).attr('name')+ '</label></sapn>');

				$('#current_business').html($('.now_selected_business').html());
				$('#current_business').find('input').each(function() {	
						$(this).attr('name', 'business_id[]');
				});
			}

		}
//		alert(_this.value);
		//$('#jobs_box').find('input')
	}
	else {
		removeCurrentBusiness(_this);
	}
}

function getBusiness(_this) {
//alert(value);
//	alert(_this.value);
//	var _this = _this;
	var _Business = $('#jobs_box');
	$.getScript(JS_PATH + 'business/' +_this.value+ '.js', function() {
//		alert(_this.value);
		var places = '';
		for(var i in br[_this.value]) {
//			alert(i);
			places += '<li><input type="checkbox" name="' +br[_this.value][i]['name']+ '" value="' +br[_this.value][i]['id']+ '" id="parent_business_' +br[_this.value][i]['id']+ '" parent="' +br[_this.value][i]['parent']+ '" onclick="getBusiness(this);" /><label class="hand" for="parent_business_' +br[_this.value][i]['id']+ '">' +br[_this.value][i]['name']+ '</label></li>';
//			alert(br[_this.value][i]['name']);
		}
		if(places != '') {
			_Business.empty();
			_Business.append('<input type="checkbox" name="' +$(_this).attr('name')+ '" value="' +_this.value+ '" parent="' +$(_this).attr('parent')+ '" id="parent_business_' +_this.value+ '" onclick="setCurrentBusiness(this, ' +_this.value+ ')" /><label class="hand" class="b" for="parent_business_' +_this.value+ '">' +$(_this).next().text()+ '</label>(选择大类则包含以下小类)').append('<ul style="margin:0 0 0 8px;">' +places+ '</ul>');
//			_Business;
		}
		else {
			setCurrentBusiness(_this);
		}
	});			
}

(function($){
$(function(){

	var hidden_b;
	if(!hidden_b) {
		hidden_b = $('#hide_business').html();
		$('#hide_business').empty();
	}
	var screen_width = screen.width;
	var box_width = $('#business_box').width();
	$.getScript(JS_PATH + 'jquery.bgiframe.min.js', function() {
		$('#business_box').bgiframe();
	});

/*
	$.getScript(JS_PATH + 'jquery.dimensions.pack.js', function() {
		var box = $('#choose_business').position();
		$('#business_box').css({left:(screen_width-box_width)/2,top:box.top/2});
	});
*/


	hidden_b = loadBusiness(hidden_b);

	$('#link_business_make_sure').click(function() {
		$('#business_box').hide();
//		$('.toHide').show();		
	});

	$('#close_jobs_box').click(function() {
		$('#business_box').hide();
//		$('.toHide').show();
	});


	$('#return_jobs_list').click(function() {
		$('#jobs_box').html(hidden_b);
	});

	$('#choose_business').click(function() {
		var scrollPos; 
		if (typeof window.pageYOffset != 'undefined') { 
			scrollPos = window.pageYOffset; 
		} 
		else if (typeof document.compatMode != 'undefined' && 
			  document.compatMode != 'BackCompat') { 
			scrollPos = document.documentElement.scrollTop; 
		} 
		else if (typeof document.body != 'undefined') { 
			scrollPos = document.body.scrollTop; 
		}
		$('#business_box').toggle().css({left:(screen_width-box_width)/2,top:scrollPos + 100});

//		$('#business_box').toggle();
	});



	$('.business').click(function() {
		//getBusiness(this);
	});
		
//
})
})(jQuery)
