/**
 * Scripts by Paul Hertz for ejumpcut.org
 *
 * Uses jquery.cookies.2.1.0 library by James Auldridge (see http://code.google.com/p/cookies/).
 * Pages must all be on same server for cookies to be effective. 
 *
 */

/**
 * Check that cookies are enabled, return true if they are, false if they aren't.
 * @return   Boolean
 */
function cookiesAreEnabled() {
	return jaaulde.utils.cookies.test();
}

/**
 * @param c_name   name of cookie
 * @param value    value of cookie
 * @param options  options for cookie (see http://code.google.com/p/cookies/wiki/Documentation)
 *                 If no options are set, cookie expires when session expires (usually when 
 *                 user quits the browser, if state is not saved)
 */
function setCookie(c_name, value, options) {
	jaaulde.utils.cookies.set(c_name, value, options);
}

/**
 * Set a cookie to expire after a time specified in seconds. 
 * @param c_name   name of cookie
 * @param value    value of cookie (needs to have a value other than "")
 * @param secs     seconds until expiration
 */
function setTimeCookie(c_name, value, secs) {
	var opts = { hoursToLive: secs/3600.0 }
	jaaulde.utils.cookies.set(c_name, value, opts);
}

/**
 * Get the value of a cookie.
 * @return value of cookie c_name, null if it isn't available.
 */
function getCookie(c_name) {
	return jaaulde.utils.cookies.get(c_name);
}

/**
 * Delete a cookie.
 * @param c_name   name of cookie to delete, only works if all options and paths are identical.
 */
function deleteCookie(c_name) {
	alert("deleting cookie: " + c_name);
	jaaulde.utils.cookies.del(c_name);
}

/**
 * When called in a header script, before a page fully loads, will redirect to a different page
 * if conditions are not met. First checks that cookie c_name has the specified value. Optionally 
 * checks a second cookie, timeCookie. The first cookie is typically a session cookie, that may be 
 * cached in a saved session. The second one should be a cookie with an expiration date, 
 * which browsers evidently do not cache with a saved session.
 *
 * @param c_name   name of cookie, probably a session cookie
 * @param value    value of cookie required to continue loading page
 * @param page     page to redirect to if cookie does not exist or does not have required value
 * @param timeCookie   another cookie, typically one that expires at a fixed time (optional)
 */
function checkCookies(c_name, value, page, timeCookie) {
	if (!cookiesAreEnabled()) {
		alert("Cookies must be enabled in your browser before you can navigate to the requested page.");
	}
	permission = getCookie(c_name);
	if (null != permission && "" != permission) {
		// the cookie exists and has a value
		if (value == permission) {
			if (null != timeCookie && null == getCookie(timeCookie)) {
				// optional timeCookie was requested but its value is null (expired)
				window.location.assign(page);
			}
			// value equals permission and the timeCookie, if requested, is good
			// so do nothing (stay on the page, don't bounce to another)
		}
		else {
			// value doesn't match permission
			window.location.assign(page);
		}
	}
	else {
		// cookie requested by c_name is either missing or set to empty string
		window.location.assign(page);
	}
}

/**
 * Set a cookie and go to a page. Cookie will expire when user quits browser. 
 * @param result   a boolean value, true or false
 * @param c_name   name of cookie to set to 1 if result is true, otherwise set to 0
 * @param page     an absolute or relative address of a page
 */
function setAndGo(result, c_name, page) {
	if (true == result) {
		setCookie(c_name, 1);
	} 
	else {
		setCookie(c_name, 0)
	}
	window.location.assign(page);
}

/**
 * Delete a cookie after a specified number of seconds have elapsed using a timer.
 * @param c_name    name of the cookie
 * @param seconds   number of seconds to wait
 */
function deleteAfterInterval(c_name, seconds) {
	var t = setTimeout("deleteCookie('" + c_name + "')", seconds * 1000);
}

