/**
 * 选择学科中选项卡切换效果
 * 
 * @param list
 *            ，con
 * @param target ，
 *            tab
 * @param index ，
 *            对象编号，
 * @return
 */

var J = jQuery.noConflict();

function showSubject(list, target, index) {
	// alert(list+'***************'+ target+'***************'+ index);
	J('.sideTab_cnt div').hide();
	J(".sideTab_cnt :checkbox[checked='true']").attr('checked', false);// 切换选卡，取消以前所选择项
	J('#' + list + '_' + target + '_' + index).show();
	J(".hover").attr('class', '');
	J('#' + target + index).attr('class', 'hover');
}

function selectLanguage(o) {
	J('.rad_selected').attr('class', 'rad_non');
	J(o).parent().parent().attr('class', 'rad_selected');
}

/**
 * 展示类型城市下的弹出框
 * 
 * @param target
 *            选择的元素的对象
 * @return
 */
function selectCity(target) {

	J("table[id^='city']").hide();// 让所有城市表格隐藏
	J("table[id^='city'] :checkbox").attr('checked', false);// 清除以前的勾选
	J(".rad_selected1").attr("class", "rad_non");// 让被选城市突出恢复
	J(target).parent("div").parent("div").attr("class", "rad_selected1");// 突出radio的背景
	J('#' + J(target).val()).show();// 显示城市列表
}

/*******************************************************************************
 * 选择学历搜索条件收提交表单
 * 
 * @return
 */
function submitSearch(type) {
	var subject = J(".sideTab_cnt :checkbox[checked='true']").map(function() {
		return J(this).val()
	}).get().join('-');// 获取被选择的program的值
	if (subject != '') {

		window.location.href = "degreeSearch.php?subject=" + subject
				+ '&degreeType=' + type;

	} else {
		alert("Please Choose a Subject!");
		J(":checkbox[value='0_0']").focus();// 上提焦点
	}
}

/**
 * 提交组下拉菜单的值
 * 
 * @return
 */
function submitSelect() {
	var url = J('#s_form').attr('action');// 获取表单原始值

	// alert(url);
	// alert("&" + J('#s_form').serialize());

	url += "&" + J('#s_form').serialize();// 获取表单的值
	window.location.href = url + '&s_new=1';
}

/**
 * 按照以前搜索条件初始化选择框
 * 
 * @param select
 *            之前选择的数据
 * @return
 */
function installSelect(select) {

	if (select.length != 0) {
		J
				.each(select, function(i, n) {

					if (n != "" && n != null) {
						J("select[name='" + i + "']").val(n);// 改变下拉菜单首项名称

						if (i == 's_date') {
							// J("select[name='" + i + "']
						// option[value='']").text(
						// 'Other Starting Dates>>');// 改变下拉菜单首项名称
						//					

						J("select[name='" + i + "'] option[value='']").remove();
						J("select[name='" + i + "']")
								.append(

										'<optgroup label=""><option value="">Other Starting Dates>></option></optgroup>');

					} else if (i == 's_city') {
						// J("select[name='" + i + "'] option[value='']").text(
						// ' Other Cities>>');// 改变下拉菜单首项名称

						J("select[name='" + i + "'] option[value='']").remove();
						J("select[name='" + i + "']")
								.append(

								'<optgroup label=""><option value="">Other Cities>></option></optgroup>');
					} else if (i == 's_fee') {
						// J("select[name='" + i + "'] option[value='']").text(
						// ' Other Tuition Fees >>');// 改变下拉菜单首项名称

						J("select[name='" + i + "'] option[value='']").remove();
						J("select[name='" + i + "']")
								.append(

								'<optgroup label=""><option value="">Other Tuition Fees>></option></optgroup>');
					}
				}

			}	);
	}
}
/**
 * 选择比较课程
 * 
 * @param id
 *            课程id
 * @param name
 *            课程名
 * @param school
 *            课程所在学校
 * @return
 */
function compireProgram(id, name, school, aid, time,key) {

	if (!J('#' + id).parent().is("div")) {
		var div = '<div class=\"addto\"><div class=\"addto_l\"><input id="'
				+ id
				+ '"onclick="selectPg(this)" type=\"checkbox\" value=\"'
				+ id
				+ '" name=\"program\"></div><div class=\"addto_r\"><a href=\"http://school.cucas.edu.cn/HomePage/'
				+ aid
				+ '/'
				+ time
				+ '/Program_'
				+ id
				+ '.shtml\" target="_blank">'
				+ name
				+ '</a></div><div class="addto1"><a href="http://school.cucas.edu.cn/HomePage/'
				+ aid

				+ '">' + school + '</a></div></div>';
		J('#s_box').before(div);
		if (J('#s_box2').css('display') == 'none') {
			J('#s_box').show();
		}

		J.get(
				'visiterRecord.php?act=2&key=degree&program=' +  encodeURIComponent(div) + '&id='
						+ id+'&m_key='+key, function(msg) {
				});

	} else {
		alert("The one you choose is exist!");
	}
}
/**
 * 决定比较栏下三个按钮可用
 * 
 * @return
 */
function selectPg(target) {

	var sl = J(".fp_courselist :checkbox[checked='true']").length;
	if (sl > 0) {
		J('#s_box').hide();
		J('#s_box2').show();
	} else {
		J('#s_box').show();
		J('#s_box2').hide();
	}
	
	var id = J(target).attr('id');

	if(J(":checkbox[checked='true'][id='"+id+"']").parent().is('div')){
		J(target).attr('CHECKED',true);
	}else{
		J(target).removeAttr('CHECKED');
	}
	
	var div = '<div class="addto">'+J(target).parent('div').parent('div').html()+'</div>';
	
	
	
	J.get(
			'visiterRecord.php?act=2&key=degree&program=' +  encodeURIComponent(div) + '&id='
					+ id, function(msg) {
			});

}
/**
 * 选择并进行课程比较
 * 
 * @return
 */
function compire() {
	var sl = J(".fp_courselist :checkbox[checked='true']");
	if (sl.length > 1) {

		var plist = sl.map(function() {
			return J(this).val()
		}).get().join('-');// 获取被选择的program的值

		window.location.href = 'pcompire.php?plist=' + plist;

	} else {
		alert('The program which you choosed is less than two !');
	}
}
/**
 * 移除已选择项目
 * 
 * @return
 */
function removeProgram(key) {
	J(".fp_courselist :checkbox[checked='true']").map(
			function() {
				J(this).parent('div').parent('div').remove();
				var id = J(this).attr('id');
				J.get('visiterRecord.php?act=4&key=degree&id=' + '&id=' + id+'&m_key='+key, function(msg) {
					
				});
			});
	J('#s_box2').hide();
	if (J(".fp_courselist :checkbox").length > 0) {
		J('#s_box').show();
	}
}
/**
 * 获取比较课程id,转跳比较页，开始比较
 * 
 * @return
 */
function toCompire(type) {
	var target = J(".fp_courselist :checkbox[checked='true']");
	if (target.length >= 2) {
		var val = target.map(function() {
			return J(this).val()
		}).get().join('-');// 获取准备比较的program的值

		window.location.href = "dcompire.php?plist=" + val + '&degreeType='
				+ type;

		// window.open("dcompire.php?plist=" + val + '&degreeType='
		// + type, '', 'width=800,height=600');
	} else {
		alert('Which you choosed less than two program!');
	}

}
/**
 * 移除对比项目
 * 
 * @param id
 * @return
 */
function delProgram(id) {
	J('#' + id).prev("table").attr('class', '');
	if (J('#' + id).parent("td").children().length > 1) {
		J('#' + id).remove();
	} else {
		J('#' + id).parent("td").parent("tr").remove();
	}

}

function showDiv(target) {

	J(target).show();
}
/**
 * 显示或隐藏重新选择框
 * 
 * @param target
 *            外层id
 * @param obj
 *            外层展示层的数据(json)
 * @return
 */
function showSearch(target, obj) {

	J.each(obj, function(i, n) {
		if (i == 'pLanguage') {
			J(":radio[name='pLanguage'][value='" + n + "']").trigger("click");
		} else if (i == 'location') {
			J(":radio[name='location'][value='" + n + "']").trigger("click");
		} else if (i == 'city') {
			var val = n.split('-');
			J.each(val, function(e, f) {
				J(":checkbox[name^='city'][value='" + f + "']").attr('checked',
						true);
			})

		}
	});
	J('#' + target).toggle();
}
/**
 * 重新搜索
 * 
 * @param url
 *            原url
 * @param type
 *            搜索类型 type=1 专 本，type=2 硕博
 * @return
 */
function searchAgin(url, type) {
	var language = J(":radio[name='pLanguage'][checked='true']").val();
	var location = J(":radio[value='city_0'][checked='true']").val();
	var city = J(":checkbox[name^='city'][checked='true']").map(function() {
		return J(this).val()
	}).get().join('-');// 获取被选择的program的值
	if (location != undefined || city != '') {

		location = J(":radio[name='location'][checked='true']").val();// 获取被选中的城市类型的值
		window.location.href = "degreeSearch.php?subject=" + url + "&language="
				+ language + "&location=" + location + "&city=" + city
				+ '&degreeType=' + type;
	} else {
		alert("Please Choose a City!");
	}
}

/**
 * 按照城市对照数据生成静态html
 * 
 * @return
 */
// function testCity(){
// var target = J('.left1_el').map(
// function(){
// var name = J(this).next("div").text();
// var that = this;
// if(1){
// J.each(data,function(i,n){
// if (name == n) {
// J(that).children(":checkbox").val(i).attr('name','city_'+i);
//						
// }
// });
// }
// }
// );
// var html = J('#txg').html();
// J.post('html.php',{str:html},function(msg){
// alert(msg);
// });
// }
/**
 * 关闭提示框
 * 
 * @param target
 *            目标层的id
 * @param type
 *            执行的事件，具体参照visiterRecord.php中的case
 */
function closeAlert(target, type) {
	J.get('visiterRecord.php?act=' + type, function(msg) {
		J(target).parent().hide();
	})
}
/**
 * 打开新页面
 * 
 * @param url
 * @return
 */
function openWind(url) {
	window.open(url, '', 'width=600,height=400');
}

/**
 * 收藏课程
 * 
 * @return
 */
function saveProgram() {
	var target = J(".fp_courselist :checkbox[checked='true']").map(function() {
		return J(this).val()
	}).get().join(',');// 获取被选择的program的值
	if (target != "") {
		addC(target, 2);
	}
}

/**
 * 在搜索结果页载入成功后，恢复对比选项历史记录
 */
J(document).ready(function() {
	var sl = J(".fp_courselist :checkbox").length;
	if (sl > 0) {
		sl = J(".fp_courselist :checkbox[checked='true']").length;
		if (sl > 0) {
			J('#s_box').hide();
			J('#s_box2').show();
		} else {
			J('#s_box').show();
			J('#s_box2').hide();
		}
	}

});

/**
 * 在页面初始化后载入对比数据
 * @param url
 * @return
 */
function loadCompire(url){
	J.get(url,function(msg){
		if(msg!=''){
			J('#s_box').before(msg);
			var sl = J(".fp_courselist :checkbox[checked='true']").length;
			if (sl > 0) {
				J('#s_box').hide();
				J('#s_box2').show();
			} else {
				J('#s_box').show();
				J('#s_box2').hide();
			}
		}	
	});
}
