function remove(_this) {
	$(_this).parent().remove();
}

function loadArea(hidden_a) {
	$('#provinces').html(hidden_a);

	$('#provinces').find('input').each(function(x) {
		$(this).attr({id:'area'+(x+1)});
		$(this).next().attr({'for':'area'+(x+1)});
		$(this).bind('click', function(){getAreas(this)});
	})

	$('.now_selected_area').html($('#current_place').html());
	$('.now_selected_area').find('input').each(function() {			
		if($(this).attr('type') == 'hidden' && $(this).attr('ref') != undefined) {
			$(this).after('<span><input id="place_' +this.value+ '" type="checkbox" value="' +this.value+ '" onclick="resetCurrentArea(this);" checked="checked" /><label class="hand" for="place_' +this.value+ '">' +$(this).attr('ref')+ '</label></span>');
			$(this).remove();
		}
	});

	return $('#provinces').html();

}

function removeCurrentArea(_this) {
	$('.now_selected_area,#current_place').find('input').each(function() {	
		
		if(this.value == _this.value) {
			$(this).parent().remove();
			$(_this).next().removeClass('highline_this');
		}

	});

//	$('.now_selected_area').html($('#current_place').html());
//alert($.trim($('.now_selected_area').html()).length);

	if($.trim($('.now_selected_area').html()) =='') {
//		alert(1);
//		$('#choose_work_place').val('不限');
		if($('#choose_work_place').attr('type')) {
			$('#choose_work_place').val('不限');
		}
		else {
			$('#choose_work_place').text('不限');
		}
//		$('#current_place').html('<span><input type="hidden" value="0" name="work_place[]" /></span>');
		$('#current_place').empty();
	}
}

function resetCurrentArea(_this) {
	$('#provinces').find('input[parent="' +_this.value+ '"]').each(function() {
		this.disabled = false;
	});

	$('#provinces').find('input').each(function() {
		if(this.value == _this.value) {
			this.checked = false;
			$(this).next().removeClass('highline_this');
		}
//		$(_this).parent().remove();
	});

	removeCurrentArea(_this);	

	//		alert($('.now_selected_area').html());
//	if($('.now_selected_area').html() =='') {
//		$('#choose_work_place').val('不限');
//		$('#current_place').html('<input type="hidden" value="0" name="work_place[]" />');
//	}
}

function setCurrentArea(_this, parent) {
	if(parent) {
//		alert(1);
//		$('.now_selected_area').empty();
		$('#provinces').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_area').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) {
//				if($('#choose_work_place').attr(
//alert($('#choose_work_place').attr('type'));
				if($('#choose_work_place').attr('type')) {
					$('#choose_work_place').val($(_this).attr('name'));
				}
				else {
					$('#choose_work_place').text($(_this).attr('name'));
				}

				$('#current_place').html('<span><input type="hidden" value="' +_this.value+ '" name="work_place[]" /></span>');
				$('.now_selected_area').html('<span><input name="" value="' +_this.value+ '" id="now_area_' +_this.value+ '" type="checkbox" checked="checked" onclick="resetCurrentArea(this);" /><label class="hand" for="now_area_' +_this.value+ '">' +$(_this).attr('name')+ '</label></span>');
				$('#place_box').hide();
				//$('.toHide').toggle();
			} else {
					$('#provinces').find('input').each(function() {
						if(_this.value == $(this).attr('parent')) {
							this.disabled = true;
							this.checked = false;
						}
					});
				$('.now_selected_area').append('<span><input value="' +_this.value+ '" id="now_area_' +_this.value+ '" type="checkbox" checked="checked" onclick="resetCurrentArea(this);" /><label class="hand" for="now_area_' +_this.value+ '">' +$(_this).attr('name')+ '</label></span>');			
		
//				$('#current_place').append('<span><input parent="' +$(_this).attr('parent')+ '" type="checkbox" value="' +_this.value+ '" name="work_place[]" id="business_' +_this.value+ '" checked="checked" onclick="remove(this);resetCurrentArea(this);removeCurrentArea(this);" /><label class="hand" for="business_' +_this.value+ '">' +$(_this).attr('name')+ '</label></sapn>');

				$('#current_place').html($('.now_selected_area').html());

				$('#current_place').find('input').each(function() {	
						$(this).attr('name', 'work_place[]');
				});
			}

		}
//		alert(_this.value);
		//$('#provinces').find('input')
	}
	else {
		removeCurrentArea(_this);
	}
}

function getAreas(_this, parent_name, parent_id) {
//	alert(1);
//	alert($(_this).attr('parent'));
	var _Provinces = $('#provinces');
	$.getScript(JS_PATH + 'areas/' +_this.value+ '.js', function() {
		var places = '';
		for(var i in ar[_this.value]) {
//			alert(i);
//			alert(ar[_this.value][i]['name']);
			places += '<li><input type="checkbox" name="' +ar[_this.value][i]['name']+ '" value="' +ar[_this.value][i]['id']+ '" id="parent_Provinces_' +ar[_this.value][i]['id']+ '" parent="' +ar[_this.value][i]['parent']+ '" onclick="getAreas(this, \''+ar['parent_name']+'\','+ar['parent_id']+');" /><label class="hand" for="parent_Provinces_' +ar[_this.value][i]['id']+ '">' +ar[_this.value][i]['name']+ '</label></li>';
		}
		if(places != '') {
			_Provinces.empty();
//			alert(ar[$(_this).attr('parent')][_this.value]['parent_name']);
			if($(_this).attr('parent') != undefined) {
//			alert(parent_name);
			_Provinces.append('<div><input type="checkbox" name="' +parent_name+ '" value="' +parent_id+ '" id="root_parent_' +parent_id+ '" onclick="getAreas(this)" /><label class="hand" class="b" for="root_parent_' +parent_id+ '">' +parent_name+ '</label>(返回该省份)</div>');
			}
			_Provinces.append('<input type="checkbox" name="' +$(_this).attr('name')+ '" value="' +_this.value+ '" parent="' +$(_this).attr('parent')+ '" id="parent_Provinces_' +_this.value+ '" onclick="setCurrentArea(this, ' +_this.value+ ')" /><label class="hand" class="b" for="parent_Provinces_' +_this.value+ '">' +$(_this).next().text()+ '</label>(选择大类则包含以下小类)').append('<ul style="margin:0 0 0 8px;">' +places+ '</ul>');
//			_Provinces;
		}
		else {
			setCurrentArea(_this);
		}
	});			
}

function close_place_list() {
	$('#place_box').hide();
	//$('.toHide').show();
}

(function($){
$(function(){
	$.getScript(JS_PATH + 'jquery.bgiframe.min.js', function() {
			$('#place_box').bgiframe();
	});
	
	/*
	$.getScript(JS_PATH + 'jquery.dimensions.pack.js', function() {
		var box = $('#choose_work_place').position();
		$('#place_box').css({left:(screen_width-box_width)/2,top:box.top/2+30});
	});
	*/

	var screen_width = screen.width;
	var box_width = $('#place_box').width();

	var hidden_a;
	if(!hidden_a) {
		hidden_a = $('#hide_provinces').html();
	}

	hidden_a = loadArea(hidden_a);

	$('#link_area_make_sure').click(function() {
		$('#place_box').toggle();
		//$('.toHide').toggle();
	});

	$('#close_jobs_box').click(function() {
	});
	

	$('#link_return_provinces').click(function() {
		$('#provinces').html(hidden_a);
		$('#provinces').find('input').each(function(x) {
//			$(this).attr({id:'h_area'+(x+1)});
			$(this).bind('click', function(){getAreas(this)});
		})
	});

	$('#choose_work_place').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; 
		}
		$('#place_box').toggle().css({left:(screen_width-box_width)/2,top:scrollPos + 100});
	});

//
})
})(jQuery)
