function initAccordin() {
	$("#accordinContent dd").hide();
	
	if($(".notopline").length <= 0)
	$("#accordinContent dt").click(function() {
		var checkElement = $(this).next();
		if ((checkElement.is("dd")) && (checkElement.is(":visible"))) {
			return false;
		}
		if ((checkElement.is("dd")) && (!checkElement.is(":visible"))) {
			$("#accordinContent dd:visible").stop().slideUp("normal");
			$("#accordinContent dd:visible").prev("dt").animate( {
				color : "black"
			}, 10).animate( {
				backgroundColor : "white"
			}, 500);
			checkElement.stop().slideDown("fast");
			$(this).css( {
				"background-color" : "#888"
			}, 10).animate( {
				color : "#888"
			}, 10).animate( {
				color : "white"
			}, 800);
			return false;
		}
	});
	else
	$("#accordinContent dt.notopline").click(function() {
		$(this).attr("class","notoplineactive");
		var checkElement = $(this).next();
		if ((checkElement.is("dd")) && (checkElement.is(":visible"))) {
			return false;
		}
		if ((checkElement.is("dd")) && (!checkElement.is(":visible"))) {
			$("#accordinContent dd:visible").stop().slideUp("normal");
			$("#accordinContent dd:visible").prev("dt").animate( {
				color : "black"
			}, 10).animate( {
				backgroundColor : "white"
			}, 500).attr("class","notopline");
			checkElement.stop().slideDown("fast");
			$(this).css( {
				"background-color" : "#888"
			}, 10).animate( {
				color : "#888"
			}, 10).animate( {
				color : "white"
			}, 800);
			return false;
		}
	});
}

function initLeftbar() {
	$("#subNavi a").click(function() {
		$("#subNavi a").removeAttr("class");
		$(this).attr("class", "active");
	});
	
	/*judge whether this is evil ie, if so, enlarge the distance */
//	if($.browser.msie) {
//		$("#subNavi li").css({"margin-bottom":"14px"});
//	}
	
	/*non-IE */
//	IF(!$.BROWSER.MSIE) {
//	$("#SUBNAVI LI").CSS({"MARGIN-BOTTOM":"4PX"});
//	}
}

function initTopNavi() {
	$(".topnavi li ul").hide();
	$(".topnavi li:last-child ul").attr("class","last");
	$(".topnavi ul li:last-child").attr("class","last");
	$(".topnavi ul li:last-child a").attr("class","last");
	
	$(".topnavi>li").hover(
			function() {
				$(this).attr("class","hover");
				$(this).children("a").attr("class","hover");
				$(this).children("ul").stop(true,true).slideDown();
			},
			function() {
				$(this).removeAttr("class");
				$(this).children("a").removeAttr("class");
				$(this).children("ul").stop(true,true).hide();
			}
			
	);
	
	$(".topnavi ul li").click(function() {
		location = $(this).children("a").attr("href");
		}
	);
	
	$(".topnavi ul li:not(:last-child)").hover(
			function() {
				$(this).attr("class","hover");
				$(this).children("a").attr("class","hover");
			},
			function() {
				$(this).removeAttr("class");
				$(this).children("a").removeAttr("class");
			}
			
	);
	
	$(".topnavi ul li:last-child").hover(
			function() {
				$(this).attr("class","lasthover");
				$(this).children("a").attr("class","lasthover");
			},
			function() {
				$(this).attr("class","last");
				$(this).children("a").attr("class","last");
			}
			
	);
}

function staticwindow(){
	$(".staticwindow div").hide();
	$("#li_1").attr("class","active");
	$("#p_1").show();
	var i = 1;
	for(i = 1;i < 14;i++){
		$("#li_"+i).css("cursor","pointer").click(
				function(){
					$(".content .contentbox ul li").removeAttr("class");
					var id = this.id;
					$(this).attr("class","active");
					$(".staticwindow div").hide();
					$("#"+this.id.replace("li","p")).show();
				});
	}
}

function initJourney(){
	var nameArr = new Array("The Journey","Day 1-6, 20-21: Beijing",
			"Day 7-9: Zhengzhou &amp; Luoyang","Day 10-12: Xi&#39;an",
			"Day 13-16: Lanzhou, Linxia, &amp; Xiahe","Day 17-20: Chengdu");
	$(".journeybox").hide();
	$("#journey0").show();
	$("#preJourney").hide();

	$(".content .heading").html(nameArr[0]).css({"color":"red"});

	$("#preJourney").click(function(){
		var num = parseInt($(".journeybox:visible").attr("id").charAt(7));
		$("#journey"+num).hide();
		num -= 1;
		$("#journey"+num).show();
		$(".content .heading").html(nameArr[num]);
		
		if(num == 0){
			$("#preJourney").hide();
		}
		$("#nextJourney").show();
	});

	$("#nextJourney").click(function(){
		var num = parseInt($(".journeybox:visible").attr("id").charAt(7));
		$("#journey"+num).hide();
		num += 1;
		$("#journey"+num).show();
		$(".content .heading").html(nameArr[num]);
		
		if(num == 5){
			$("#nextJourney").hide();
		}
		$("#preJourney").show();
	});
}

function initReference(){
	$(".referencebox").hide();
	$("#referencebox0").show();
	$("#preJourney").hide();

	$("#preJourney").click(function(){
		var num = parseInt($(".referencebox:visible").attr("id").charAt(12));
		$("#referencebox"+num).hide();
		num -= 1;
		$("#referencebox"+num).show();
		
		if(num == 0){
			$("#preJourney").hide();
		}
		$("#nextJourney").show();
	});

	$("#nextJourney").click(function(){
		var num = parseInt($(".referencebox:visible").attr("id").charAt(12));
		$("#referencebox"+num).hide();
		num += 1;
		$("#referencebox"+num).show();
		
		if(num == 6){
			$("#nextJourney").hide();
		}
		$("#preJourney").show();
	});
}

function initRightWords(){
	$('#rightWords').bxSlider( {
		mode : 'fade',
		speed : 1000,
		pause : 5000,
		auto : false,
		controls : true,
		auto_direction : 'right',
		next_text : 'next',
		prev_text : 'previous'
	});
	
	$('#rightWordsAuto').bxSlider( {
		mode : 'fade',
		speed : 2000,
		pause : 10000,
		auto : true,
		controls : true,
		auto_direction : 'left',
		next_text : 'next',
		prev_text : 'previous'
	});
}

function initContactUs(){
	$("#app_wouldlike_other").hide();
	
	$("#app_wouldlike").change(function(){
		selected = $("#app_wouldlike option:selected").val();
		if(selected == "other") {
			$("#app_wouldlike_other").val("Enter Here");
			$("#app_wouldlike_other").show();
		}
		else $("#app_wouldlike_other").hide();
	});	
}

function initEmailAddress(){
		var rawParam = window.location.search;
		rawParam = rawParam.substr(1,rawParam.length);
		var pairs = rawParam.split("&");
		var paramList=[];
		for(var i=0;i<pairs.length;i++){
			paramList[pairs[i].split("=")[0]]=pairs[i].split("=")[1];
		}	

	$("#emailtobefilled").html(paramList["email"]);
}
function getParam(param){
	rawParam = window.location.search;
	rawParam = rawParam.substr(1,rawParam.length);
	var pairs = rawParam.split("&");
	var paramList=[];
	for(var i=0;i<pairs.length;i++){
		paramList[pairs[i].split("=")[0]]=pairs[i].split("=")[1];
	}	
	var parameter = paramList[param];
	return parameter;
}
function initOnlineApplicationLoginError(){
	var param = getParam("errormsg");
	if(param == 1){
		$('.rightbar #loginError').html("Invalid e-mail or password");
		$('.rightbar #login_box2').height(229);
	}else if(param == 2){
		$('.rightbar #loginError').html('<a style="color:indianred;font-size:10px" href="onlineapplication/login/resendVerification.php">Your account has not been verified.</a>');
		$('.rightbar #login_box2').height(229);
	}else if(param == 3){
		$('.rightbar #loginError').html("Your account has been verified.");
		$('.rightbar #login_box2').height(229);
		$('.rightbar #loginError').css("color","green");
	}
}
$(document).ready(function() {
	$("body").css("visibility", "visible");

	initTopNavi();
	initAccordin();
	initLeftbar();
	staticwindow();
	initOnlineApplicationLoginError();
	if($(".journeybox").length > 0) initJourney();
	if($("#rightWords").length > 0 || $("#rightWordsAuto").length > 0) initRightWords();
	if($("#contactusform").length > 0) initContactUs();
	if($(".referencebox").length > 0) initReference();
	if($("#jumptoapplication").length >0) location="http://www.chinaprograms.org/onlineapplication/login/";
	if($("#jumptoPostEnrollmentLogin").length >0) location="http://www.chinaprograms.org/postenrollment/login/";
	if($("#jumptoTeachingResidentialAssistant").length >0) location="http://www.chinaprograms.org/onlineapplication/employment/assistant.php";	
	if($("#emailtobefilled").length > 0) initEmailAddress();
	
	if($('.leftnavi').height()>$('.scrollpart').height())
		$('.contentplaceholder').height('200px');
});
