/**
 * @file   posttype.js
 * @date   2009.11.19
 * @update 2009.11.19
 * @author ticktack
 */
 
$(function () {
	var ellevel1 = $('#typeLevel1');
	var ellevel2 = $('#typeLevel2');
	var ellevel3 = $('#typeLevel3');
	var ellevel4 = $('#typeLevel4');
	var ellevel5 = $('#typeLevel5');
	
	//顶层类型事件响应
	ellevel1.change(function (event) {
		var type = $(this).val();
		
		typestr.typeLevel1Value = type;
		typestr.typeLevel1Name = $(this).children(':selected').text();
		
		typestr.typeLevel2Name = '';
		typestr.typeLevel2Value = '';
		
		typestr.typeLevel3Name = '';
		typestr.typeLevel3Value = '';
		
		typestr.typeLevel4Name = '';
		typestr.typeLevel4Value = '';
		
		typestr.typeLevel5Name = '';
		typestr.typeLevel5Value = '';
		
		//当顶层类型更新时，更新子类型
		fillRelatedTypeLevel2(type);
	});

	fillTopLevelTypes();
	
	//第二层类型事件响应
	ellevel2.change(function (event) {
		var type = $(this).val();
		
		typestr.typeLevel2Value = type;
		typestr.typeLevel2Name = $(this).children(':selected').text();
		
		typestr.typeLevel3Name = '';
		typestr.typeLevel3Value = '';
		
		typestr.typeLevel4Name = '';
		typestr.typeLevel4Value = '';
		
		typestr.typeLevel5Name = '';
		typestr.typeLevel5Value = '';
		
		fillRelatedTypeLevel2Plus(type, ellevel3, 3);
	});
	ellevel2.empty();
	ellevel2.hide();
	$('#arrow2').hide();
	
	//第三层类型事件响应	
	ellevel3.change(function (event) {
		var type = $(this).val();
		
		typestr.typeLevel3Value = type;
		typestr.typeLevel3Name = $(this).children(':selected').text();
		typestr.typeLevel4Name = '';
		typestr.typeLevel4Value = '';
		typestr.typeLevel5Name = '';
		typestr.typeLevel5Value = '';
		fillRelatedTypeLevel2Plus(type, ellevel4, 4);
	});
	
	ellevel3.empty();
	ellevel3.hide();
	$('#arrow3').hide();
	//第四层类型事件响应	
	ellevel4.change(function (event) {
		var type = $(this).val();
		typestr.typeLevel4Value = type;
		typestr.typeLevel4Name = $(this).children(':selected').text();
		typestr.typeLevel5Value = '';
		typestr.typeLevel5Name = '';
		fillRelatedTypeLevel2Plus(type, ellevel5, 5);
	});
	ellevel4.empty();
	ellevel4.hide();
	$('#arrow4').hide();
	ellevel5.change(function (event) {
		var type = $(this).val();
		typestr.typeLevel5Value = type;
		typestr.typeLevel5Name = $(this).children(':selected').text();
	});
	ellevel5.empty();
	ellevel5.hide();
	$('#typeInfo').hide();
	$('#changeTypeLabel').show();
});

function fillTopLevelTypes() {
	var eltemp;
	var ellevel1 = $('#typeLevel1');
	
	for (var i = 0, limit = typelevel1.length/2; i < limit; i++) {
		eltemp = $('<option></option>');
		var index = i * 2;
		eltemp.val(typelevel1[index]);
		eltemp.text(typelevel1[index+1]);
		ellevel1.append(eltemp);		
	}
}

function fillRelatedTypeLevel2(type) {
	var eltemp;
	var ellevel2 = $('#typeLevel2');

	var typelevel2related = typelevel2[type];
	
	$('#arrow1').show();
	ellevel2.empty();
	ellevel2.show();
	
	for (var i = 0, limit = typelevel2related.length/2; i < limit; i++) {
		eltemp = $('<option></option>');
		var index = i * 2;
		eltemp.val(typelevel2related[index]);
		eltemp.text(typelevel2related[index+1]);
		ellevel2.append(eltemp);
	}
	
	$('#arrow2').hide();
	$('#typeLevel3').empty();
	$('#typeLevel3').hide();
	
	$('#arrow3').hide();
	$('#typeLevel4').empty();
	$('#typeLevel4').hide();
	
	$('#arrow4').hide();
	$('#typeLevel5').empty();
	$('#typeLevel5').hide();
}

//填充三层或以上的问题类型列表
function fillRelatedTypeLevel2Plus(type, element, level) {
	//发起异步请求
	var uri = '/type/relatedSubTypes.action?typeName=' + type;
	
	$.get(uri, function (data) {
		element.empty();
		
		switch (level) {
			case 3:
			$('#arrow3').hide();
			$('#typeLevel4').hide();
			$('#arrow4').hide();
			$('#typeLevel5').hide();
			break;
			case 4:
			$('#arrow4').hide();
			$('#typeLevel5').hide();
			break;
		}
		
		if (data == '') {
			switch (level) {
				case 3:
				$('#arrow2').hide();
				typestr.typeLevel3Name = '';
				typestr.typeLevel3Value = '';
				break;
				case 4:
				$('#arrow3').hide();
				typestr.typeLevel4Name = '';
				typestr.typeLevel4Value = '';
				break;
				case 5:
				$('#arrow4').hide();
				typestr.typeLevel5Name = '';
				typestr.typeLevel5Value = '';
				break;
			}
			
			element.hide();
		}
		else {
			var types = data.split(',');
			var eltemp;
			var index = 0;
			
			switch (level) {
				case 3:
				$('#arrow2').show();
				break;
				case 4:
				$('#arrow3').show();
				break;
				case 5:
				$('#arrow4').show();
				break;
			}
		
			element.empty();
			element.show();
		
			for (var i = 0, limit = types.length/2; i < limit; i++) {
				eltemp = $('<option></option>');
		
				index = i * 2;
				eltemp.val(types[index]);
				eltemp.text(types[index+1]);
				element.append(eltemp);
			}
		}
	});
}

function submitType() {
	var typeName = '';
	var typeTotalName = '';
	var nameValue = '';

	if (typestr.typeLevel1Name != '') {
		typeName = typestr.typeLevel1Name;
		typeTotalName = typestr.typeLevel1Name;
		nameValue = typestr.typeLevel1Value;
	}
	
	if (typestr.typeLevel2Name != '') {
		typeName = typestr.typeLevel2Name;
		typeTotalName = typeTotalName + ' > ' + typestr.typeLevel2Name;
		nameValue = typestr.typeLevel2Value;
	}

	if (typestr.typeLevel3Name != '') {
		typeName = typestr.typeLevel3Name;
		typeTotalName = typeTotalName + ' > ' + typestr.typeLevel3Name;
		nameValue = typestr.typeLevel3Value;
	}

	if (typestr.typeLevel4Name != '') {
		typeName = typestr.typeLevel4Name;
		typeTotalName = typeTotalName + ' > ' + typestr.typeLevel4Name;
		nameValue = typestr.typeLevel4Value;
	}

	if (typestr.typeLevel5Name != '') {
		typeName = typestr.typeLevel5Name;
		typeTotalName = typeTotalName + ' > ' + typestr.typeLevel5Name;
		nameValue = typestr.typeLevel5Value;
	}
		
	hideCategory();
	
	var typePromot = $('#typePromot');
	typePromot.empty();
    //alert(typeName);
	typePromot.html('<input type = "radio" name="typeSelected" value="'+ typeName + '" checked>&nbsp;' + typeTotalName +'</input>');
}
function hideCategory() {
	$('#changeTypeLabel').show();
	$('#typeInfo').hide();
}

function changeCategory() {
	$('#typeInfo').show();
	$('#changeTypeLabel').hide();

	var typeLevel1 = $('#typeLevel1');
	typeLevel1.empty();
	typeLevel1.show();
	fillTopLevelTypes();
	
	$('#arrow1').hide();
}

       
//顶层问题类型
var typelevel1 = [
	    'youngster_child', '少年儿童',    	
	 	'young_parents', '年轻父母',  	
 	 	'teacher_gardener', '桃李园丁', 	
	 	'child_parent_exchange', '亲子互动', 	
	 	'teacher_student_exchange', '师生交流',  	
	 	'teacher_parent', '师长乐议',  	
	 	'station_management', '站务管理' 	
];

//第二层问题类型
var typelevel2 = [];

	
//少年儿童 的子类型
typelevel2['youngster_child'] = [
	'early_education', '早教(0-3岁)', 
 	'kindergarten', '幼儿园(3-6岁)', 
 	'primary_school', '小学(6-12岁)', 
 	'junior_high_school', '初中(12-15岁)', 
 	'high_school', '高中(15-18岁)'
];

//年轻父母 的子类型
typelevel2['young_parents'] = [
	'mommy_in_kitchen', '妈咪厨房', 
	'prepare_pregnant', '准备怀孕', 	
	'pregnant_mother', '好孕妈妈', 	
	'young_mother', '年轻妈妈', 
	'full_time_mommy', '全职妈咪', 
	'fathers_to_be', '准爸爸', 	
	'young_father', '年轻爸爸'
];


//桃李园丁 的子类型
typelevel2['teacher_gardener'] = [
	'preschool_teacher', '幼儿园老师', 
	'grade_teacher', '小学老师', 
	'junior_school_teacher', '初中老师', 	
	'high_school_teacher', '高中老师', 	
	'hundred_years_edu', '百年树人', 	
	'full_peaches_plums', '桃李天下', 	
	'teacher_send_word', '园丁寄语' 
];

//亲子互动 的子类型
typelevel2['child_parent_exchange'] = [
	'educate_child_properly', '教子有方',   
	'family_life', '家庭生活', 
	'basic_necessities_life', '衣食住行', 	
	'single_parent_family', '单亲家庭', 	
	'70s_parents', '70后爸妈', 	
	'80s_parents', '80后爸妈', 	
	'childcare_experience', '育儿心经', 
	'baby_name', '宝宝起名', 
	'children_thinking', '孩子心声', 
	'big_hope_son', '望子成龙', 	
	'big_hope_girl', '望女成凤' 
];

//师生交流 的子类型
typelevel2['teacher_student_exchange'] = [
	'teach_learn', '教与学',   
	'exam_oriented_edu', '应试教育', 	
	'all_around_edu', '素质教育', 
	'answer_doubts', '答疑解惑', 
	'Famous_Outstanding', '名师高徒' 
];

//师长乐议 的子类型
typelevel2['teacher_parent'] = [
	'talk_children', '说说孩子',
	'coordinate_edu', '配合教育', 
	'communicate_way', '沟通之道' 
];

//站务管理 的子类型
typelevel2['station_management'] = [

];

//类型字符串
var typestr = {
	typeLevel1Name : '',
	typeLevel1Value : '',
	
	typeLevel2Name : '',
	typeLevel2Value : '',
	
	typeLevel3Name : '',
	typeLevel3Value : '',
	
	typeLevel4Name : '',
	typeLevel4Value : '',
	
	typeLevel5Name : '',
	typeLevel5Value : ''
};
