var IMAGES_DIR = "http://www.trymasak.my/images/";

$(document).ready(function() {
	//drop down menu
	$("ul#menubar").simplemenu();
	//for login
	 $(".signin").click(function(e) {
        e.preventDefault();
        $("p#signin_menu").toggle();
        $(".signin").toggleClass("menu-open");
    });

    $("p#signin_menu").mouseup(function() {
        return false
    });
    $(document).mouseup(function(e) {
        if($(e.target).parent("a.signin").length==0) {
            $(".signin").removeClass("menu-open");
            $("p#signin_menu").hide();
        }
    });
	//end of login	
});

function toggleDetails(what){
	if (what=="advanced-search-form"){
		$("#advanced-search-form-container").slideToggle();
		$("#search-advanced-button-holder").hide();
		$("#search-by-food-form").css("border-bottom", "0px");
		//$("#latest-search-container").height(540); 
		$('#latest-search-container').animate({height: '540px',}, 500 );
	}
	if (what=="advanced-search-form-close"){
		$("#advanced-search-form-container").slideToggle();				
		setTimeout("redisplayAdvanced();",500);
		//setTimeout('$("#latest-search-container").height(440)',500); 
		 $('#latest-search-container').animate({height: '440px',}, 500 );
	}	
	if (what=="forgotPassword"){
		$("#forgot-password-container").slideToggle();
		if ($('#signin_menu').height()==180){
			$('#signin_menu').animate({height: '205px',}, 500 );
		}else{
			$('#signin_menu').animate({height: '180px',}, 500 );
		}
		
	}
}

function login(){
	
	var pathname = "http://"+document.location.hostname;
	var loginpassword = $('#loginpassword').val();
	var loginemail = $('#loginemail').val();
	$("#signin-form-loading-container").html('<center><img src="'+IMAGES_DIR+'/loading/loading3.gif" alt="Signing in.." title="Signing in.."><\/center><center>Signin in..<\/center>');
	$.post(pathname+"/ajax-functions/template.php", {action: "LOGIN", loginemail: loginemail, loginpassword: loginpassword}, 
	function (data){
		$("#signin-form-loading-container").html(data);
		var isSuccess = data.search("success");
		if (isSuccess > 1){
			var temp = data.indexOf('|');
			var temp2 = data.indexOf('^');
			var temp3 = data.indexOf('*');
			var newMsgCount = data.substring(temp+1,temp2);
			var isNewShout = data.substring(temp2+1,temp3);
			membersFunctions(newMsgCount,isNewShout);	//calls the functions to load registered members' functions
		}
	}
	,"html");
}

function checkEnter(e,caller){ //e is event object passed from function invocation
	var characterCode //literal character code will be stored in this variable

	if(e && e.which){ //if which property of event object is supported (NN4)
		e = e
		characterCode = e.which //character code is contained in NN4's which property
	}else{
		e = event
		characterCode = e.keyCode //character code is contained in IE's keyCode property
	}

	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		//document.forms[0].submit() //submit the form
		if (caller == "headerLogin"){
			login();
			return false;
		}else if (caller=="searchRecipeByFood"){
			searchRecipeByFood();
			return false;
		}else if (caller=="searchRecipeByIngredients"){
			searchRecipeByIngredients();
			return false;
		}else if (caller=="sendShoutsGame"){
			sendShouts();
			return false;
		}else if (caller=="searchMembersRecipes"){
			searchMembersRecipes();
			return false;
		}else if (caller=="searchAllRecipes"){
			searchAllRecipes();
			return false;
		}else if (caller=="searchTryMasakRecipes"){
			searchTryMasakRecipes();
			return false;
		}
	}
}

function clearField(fieldName){
	var temp = "#"+fieldName;
	if (fieldName == "searchIngredients"){
		if ($(temp).val() == "Contoh: Ayam, Bawang, Timun"){
			$(temp).val("");
		}
	}else if (fieldName == "searchRecipe"){	
		if ($(temp).val() == "Nak masak apa hari ini?"){
			$(temp).val("");
		}
	}else if (fieldName == "searchFood"){	
		if ($(temp).val() == "Nak masak apa hari ini?"){
			$(temp).val("");
		}
	}else if (fieldName == "shoutMessage"){	
		if ($(temp).val() == "Anda tengah buat apa?"){
			$(temp).val("");
		}
	}else if (fieldName == "txtSearchGlossary"){	
		if ($(temp).val() == "Maklumat Apa Yang Anda Cari?"){
			$(temp).val("");
		}
	}else if (fieldName == "facebook"){	
		if ($(temp).val() == "Contoh: http://www.facebook.com/abdmuhaimin"){
			$(temp).val("");
		}
	}else{
		$(temp).val("");
	}

}

function resetPassword(){
	var pathname = "http://"+document.location.hostname;
	var forgotPasswordEmail = $('#forgotPassword').val();
	$("#forgot-password-container").html('<center><img src="'+IMAGES_DIR+'/loading/loading3.gif"> E-mailing your new password..<\/center>');
	
	$.post(pathname + "/ajax-functions/template.php", {action: "FORGOTPASS", forgotPasswordEmail: forgotPasswordEmail}, 
	function (data){
		$("#forgot-password-container").html(data);
	}
	,"html");
	
}

function wopen(url, name, w, h){
	w += 32;
	h += 96;
	var win = window.open(url,name,'width=' + w + ', height=' + h + ', ' + 'location=no, menubar=no, ' + 'status=no, toolbar=no, scrollbars=yes, resizable=no');
	win.resizeTo(w, h);
	win.focus();
}

function toggleHeaderIOS(){
	$("#header-ios").slideToggle();
}
