/**
 * This file contains the functions which used by header.jsp.
 * @return
 */

/**
 * This function initializes page, called every knoodle jsp page.
 */
function initPage() {
	// Button
	$(".button").hover( function() {
		$(this).addClass('hoverButton');
		$(".button_special_preview").removeClass('hoverButton'); // in iteration 2.2, enhancement 988
	}, function() {
		$(this).removeClass('hoverButton');
		$(".button_special_preview").addClass('hoverButton'); // in iteration 2.2, enhancement 988
	});
	// set default value. // in iteration 2.2, enhancement 988
	$(".button_special_preview").addClass('hoverButton');
	// Button special preview for lesson and course. // in iteration 2.2, enhancement 988
	$(".button_special_preview").hover( 
		function() {
			// over
			$(this).removeClass('hoverButton');
			$(this).addClass('hoverButton_special_preview');
		}, 
		function() {
			// out
			$(this).removeClass('hoverButton_special_preview');
			$(this).addClass('hoverButton');
		}
	);
	// List Table
	zebraRows();

	// Field Text
	$(".text_default").each( function() {
		createTextDefault($(this));
	});
	// Field Select
	$("body").mousedown( function() {
		selectDefaultField();
	});
	$(".select_default").each( function() {
		createSelectDefault($(this));
	});
	// Input in Field Text and Field Select.
	$(".text_default_input_field").each( function() {
		$(this).blur( function() {
			blurTextDefaultField($(this));
		});
	});
	$(".text_default_input_field").each( function() {
		$(this).focus( function() {
			focusTextDefaultField($(this));
		});
	});

	$(".select_default_input").each( function() {
		$(this).click( function() {
			clickSelectDefaultField($(this));
		})
	});
	$(".select_default_right").each( function() {
		$(this).click( function() {
			clickSelectDefaultField($(this).next("div").children("input"));
		})
	});
	
	$(".timeago").timeago();
}

/**
 * This function initializes the header of each page.
 */
function initHeader() {
	// Binding keypress event to login input elements.
	$("#loginUserId").bind("keypress", function(event) {
		if (event.keyCode == 13) {
			doSignInCheck();
		}
	});

	$("#loginPassword").bind("keypress", function(event) {
		if (event.keyCode == 13) {
			doSignInCheck();
		}
	});

	initInputDefaultValue($("#header #searchFrm #mockKeyword"),"Search");
	
	initMenu();
	displaySignName();
	// $("body").addClass("homePage");
}

function initMenu() {
	$(".menuBar .menu li:last").css("border-bottom", "none");
	loadTree('categoryWidget');
	// Menu hover and menu list show/hide
	$(".operation .menuBar .menu").hover(
			function() {
				$(this).children(".menuName").addClass('hoverMenu');
				$(this).children(".menuList").show();
				if ($.browser.msie) {
					$(this).find(".fadeBackgroundImage").height(
							$(this).find(".fadeBackgroundImage").parent()
									.height());
				}
			}, function() {
				$(this).children(".menuName").removeClass('hoverMenu');
				$('#categoryWidget').hide();
				$(this).children(".menuList").hide();
			});

	// Toggle background of item in menu list.
	$(".menuList li").hover( function() {
		$(".hoveredMenuItem").removeClass('hoveredMenuItem');
		$(this).addClass('hoveredMenuItem');
	}, function() {
		$(this).removeClass('hoveredMenuItem');
	});
	
	/*
	$("#tourMenu").click( function() {
		goHelpPage('header','Tour');
	});
	*/
}

function doSignInCheck() {
	if ($('#loginUserId').val() == "") {
		alertLoginError("Please input the User ID and Password.",
				function() {
					$('#userId').focus();
				});
		return;
	}
	if ($('#loginPassword').val() == "") {
		alertLoginError("Please input the User ID and Password.",
				function() {
					$('#password').focus();
				});
		return;
	}
	if ($('#loginUserId').val() != "" && $('#loginPassword').val() != "") {
		validateLogin();
	}
}

function validateLogin(id) {
	var url = ctx + '/signIn.u';
	$.ajax( {
		type :"POST",
		url :url,
		data :{userId:$('#loginUserId').val(),password:$('#loginPassword').val()},
		success : function(msg) {
			if (msg == 'true') {
				window.name = "LOGIN";
				window.location.href = ctx + "/?login=true";
			} else if (msg == 'false') {
				alertLoginError('The User ID or Password is not correct.',
						function() {
							// $('#loginUserId').focus();
					});
				return false;
			} else {
				window.location.href = ctx + "/" + msg;
			}
			return;
		},
		error : function() {
			return;
		}
	});
}

function usernameFocus() {
	$("#loginUI").hide();
	$("#loginUserId").val("").show().focus();
}

function usernameBlur() {
	var username = $("#loginUserId").val();
	if ($.trim(username) == "") {
		$("#loginUI").show();
		$("#loginUserId").hide();
	}
}

function passwordFocus() {
	$("#loginPass").hide();
	$("#loginPassword").val("").show().focus();
}

function passwordBlur() {
	var password = $("#loginPassword").val();
	if ($.trim(password) == "") {
		$("#loginPass").show();
		$("#loginPassword").hide();
	}
}

function signout() {
	chatChangePage( function() {
		var url = ctx + '/signOut.u';
		$.ajax( {
			type :"POST",
			url :url,
			data :"",
			success : function(msg) {
				window.location.href = ctx + "/";
				return;
			},
			error : function(XMLHttpRequest, textStatus, thrownError) {
				return;
			}
		});
	});
}

function displaySignName() {
	var name = GetCookie('com_skta_ulearn_username');
	if (name != "" && name != '""') {
		name = name.replace("\"", "");
		$('.loginUserInfo').show();
		$('.loginUserInfo').removeClass("hidden");
		$('.noneLogin').hide();
		$('#loginUsername').text(name);
		$('#welcomeloginName').text(name);
	} else {
		$('.loginUserInfo').hide();
		$('.noneLogin').show();
	}
}

function isLogin() {
	var name = GetCookie('com_skta_ulearn_username');
	return (name != "" && name != '""');
}

function needLogin(url) {
	if (!isLogin()) {
		WriteCookie("com_skta_ulearn_jit_url", url, 60);
		window.location.href = ctx + "/jit.u";
	}
}

function initNavigator(currentPage) {
	/*
	$("#tourMenu").click( function() {
		goHelpPage('header','Tour');
	});
	*/
	$("#tourMenu").css("cursor", "pointer");
	
	if (currentPage == "") {
		return;
	}
	$("body").addClass(currentPage);	
}

function search() {
	// return;
	chatChangePage( function() {
		keyword = $("#keyword").val();
		keyword = keyword.replace(/\%/g, "");
		if ($("#searchType").val() == 'user') {
			$("#searchFrm").attr('action', ctx + "/searchUsers.u");
		} else if ($("#searchType").val() == 'gift') {
			if (keyword == '')
				return false;
			$("#searchFrm").attr('action', ctx + "/searchGift.u");
		} else {
			$("#searchFrm").attr('action', ctx + "/search.u");
		}
		$("#searchFrm").submit();
		return true;
	});
}

function searchWithButtonHistory(page,feature) {
	var timestamp = Date.parse(new Date()); 
	var param = [
 		{ name : 'page', value : page }
		,{ name : 'feature', value : feature}
		,{ name : 'timestamp', value : timestamp}
	];				 
	$.ajax( {
    	type :"post",
    	url :ctx+"/stat/buttonHistory.u",
    	data: param,
    	success : function(msg){
		 	 search();
    	},
    	error : function() {		    
	    	 search();
		}
	});
}

function doRegister() {
	var url = ctx + '/isAbleToSignUp.u';
	$
			.ajax( {
				type :"POST",
				url :url,
				data :"",
				success : function(msg) {
					if (msg == 'true') {
						window.location.href = ctx + "/Registry.u";
						return true;
					} else {
						message = "Sorry, you must contact Knoodle customer care to create a new user account. ";
						message += "Please email your request to <a href='mailto:support@Knoodle.com' style='font-weight:normal;'>support@Knoodle.com</a>.";
						alertMessageWarning(message, '');
						return false;
					}
				},
				error : function(XMLHttpRequest, textStatus, thrownError) {
					return;
				}
			});
	return;
}


