// Search options
$(function() {
	if ($("#ess-optionswrap").length) {
		$("#ess-optionswrap").hide();
		
		$("#advancedSearchLink").toggle(
			function() {
				$("#advancedSearchLink").addClass("advancedSearchLinkOpen");
				$("#ess-optionswrap").slideDown(300);
			},
			function() {
				$("#advancedSearchLink").removeClass("advancedSearchLinkOpen");
				$("#ess-optionswrap").slideUp(300);
			}
		);
	}
});

// FAQ 
$(function() {
	if ($(".faq").length) {
		$(".faq dd").hide();
		
		$(".faq dt").hover(
			function() {
				$(this).addClass("hover");
			},
			function() {
				$(this).removeClass("hover");
			}
		);
		
		$(".faq dt").toggle(
			function() {
				$(this).next("dd").slideDown(300);
				$(this).addClass("open");
			},
			function() {
				$(this).next("dd").slideUp(300);
				$(this).removeClass("open");
			}
		);
	}
});

// CAPTCHA
$(function() {
	if ($("#captchahelptext").length) {
		$("#captchahelptext").hide();

		$("#captcha-help").click(function() {
			if ($("#captchahelptext").css("display") == "none") {
				$("#captchahelptext").slideDown(300);
				return false;
			} else {
				$("#captchahelptext").slideUp(300);
				return false;
			}
		});
	}
});

// Forum
$(function() {
	if ($(".contentForum").length) {
		
		$(".f-category-content").hide();
		$(".f-category-header").removeClass("open");
		
		
		if ($(".f-category").length) {
			// Restore subject state from cookie
			var cookiePrefix = "forumState_";
			
			$(".f-category-header a").each(function() {
				var cookie = getCookie(cookiePrefix + $(this).closest(".f-category-header").attr("id"));

				if (cookie != null && cookie.indexOf("open") > -1 && $(this).closest(".f-category-header").attr("class").indexOf("open") == -1) {
					$(this).closest(".f-category-header").addClass("open");
					$(this).parent().parent().find(".f-category-content").slideDown(400);
				}
			});
		}
					
		$(".f-category-header a").click(function() {
			if ($(this).parent().parent().find(".f-category-content").css("display") == "block") {
				$(this).parent().removeClass("open")
				$(this).parent().parent().find(".f-category-content").slideUp(400);
			} else {
				$(this).parent().addClass("open")
				$(this).parent().parent().find(".f-category-content").slideDown(400);
			}
			persint_SaveForumStateAsCookie();			
			return false;
		});
		
		/**
		 * This function saves the minimized/maximized state of forum subjects, 
		 *  as a cookie in the users web browser
		 *
		 **/
		function persint_SaveForumStateAsCookie() {
			var cookiePrefix = "forumState_";
			$(".f-category-header a").each(function() {	
				setCookie(  cookiePrefix + $(this).closest(".f-category-header").attr("id"), // name
							$(this).closest(".f-category-header").attr("class"), // value
							365, // expire (in days)
							"/" // path
				);
			});
		}
		
		
	}
	
	if ($("#f-new-pass-wrap").length) {
		$("#f-new-pass-wrap").hide();
		
		$("#f-new-pass-link").click(function() {
			if ($("#f-new-pass-wrap").css("display") == "none") {
				$("#f-new-pass-wrap").slideDown();
			} else {
				$("#f-new-pass-wrap").slideUp();
			}
			
			return false;
		});
	}	
});

/**
 * Deletes a cookie with name (if it exists), path is usually "/", domain is optional
 *
 **/
function deleteCookie( name, path, domain ) {
    if (getCookie(name)) {
        document.cookie = name + "=" + 
            ((path)?";path=" + path : "") +
            ((domain)?";domain=" + domain : "") + 
            ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
    }       
} // deleteCookie()
function getCookie( name ) {
    if (!document.cookie) return null;
    if (document.cookie.indexOf(name) == -1) return null;

    var allCookies = document.cookie.split(';');
    var tempCookie;
    
    for (var i=0;i < allCookies.length;i++) {
        // split apart name = value
        tempCookie = allCookies[i].split("=");
        
        if (tempCookie[0].replace(/^\s+|\s+$/g, "") == name.replace(/^\s+|\s+$/g, ""))
            return (tempCookie[1] == null)?null:unescape(tempCookie[1]);            
    } // for()

} // getCookie()

/**
 * name = name of the cookie to set
 * value = the value to set in the cookie
 * expire = time before the cookie will expire (in days)
 * path = path where the cookie will be valid (usually set to "/")
 * domain = valid domain for the cookie
 * secure = not used
 *
 * Generally, it will be enough if you provide name, value, expire and path
 **/
function setCookie( name, value, expire, path, domain, secure ) {
    var today = new Date();
    
    // if expire variable  is set, set the expire time correctly (in days instead of ms)
    if (expire) expire = expire * 1000 * 60 * 60 * 24;
    
    var expireDate = new Date( today.getTime() + expire);
    
    document.cookie = name + "=" + escape(value) + 
        ((expire)?";expires=" + expireDate.toGMTString() : "") +
        ((path)?";path=" + path : "") +
        ((domain)?";domain=" + domain : "") +
        ((secure)?";secure=" + secure : "");
}


function test(value) {
   	alert(value);	
}





