// ==== Global Settings ==== //
// ========================= //

GLOBAL_LANDING_NAME = "landing";
GLOBAL_LANDING_URL = "/";
COOKIE_LIFETIME_DAYS = 10*365;

// ==== General Utility Functions ==== //
// =================================== //

// Cookie functions (from http://www.quirksmode.org/js/cookies.html)
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

// ==== Application Utility Functions ==== //
// ======================================= //

function setPerspective(perspective) {
    // Store perspective in cookie
    createCookie("perspective", perspective, COOKIE_LIFETIME_DAYS);
    // Reset save prefs setting
    eraseCookie("dontsaveprefs");
}

function unsetPerspective() {
    // Remove perspective cookie
    eraseCookie("perspective");
    // Remember not to save preferences
    createCookie("dontsaveprefs", "dontsaveprefs", COOKIE_LIFETIME_DAYS);
}

// ==== Redirect as soon as possible ==== //
// ====================================== //

// Try to read the perspective/discipline of this page
var thisPerspective = document.URL;

// Try to read cookie values
var perspectiveCookie = readCookie("perspective");
var dontsaveprefsCookie = readCookie("dontsaveprefs");

// Redirect from global landing page if necessary
if (document.URL.match(/\/global\/eng\/pages\/(index.jsp)?$/, "i")) {
	// Is the perspective cookie set?
	if (perspectiveCookie) {
		// Redirect to perspective if set, and noredirect param wasn't passed
		if (!document.URL.match("noredirect")) {
			location.replace(perspectiveCookie);
		}
	}
}

// ==== DOM Interaction ==== //
// ========================= //

$(document).ready(function (){
	if ($("div#main").hasClass(GLOBAL_LANDING_NAME) && perspectiveCookie) return;
	
    // Update home link in breadcrumbs with cookie setting
    if (perspectiveCookie) {
        $("#breadcrumb_home").attr("href", perspectiveCookie);
    }

    // Reflect cookie settings in checkbox
    if (thisPerspective == perspectiveCookie) {
        // Check box if this page is the set perspective
        $("#rememberPerspective").attr("checked", "checked");
    }

    // Respond to check/uncheck of the remember perspective checkbox
    $("#rememberPerspective").click(function () {
        if ($("#rememberPerspective").is(':checked')) {
            setPerspective(thisPerspective);
            $("#breadcrumb_home").attr("href", thisPerspective);
        } else {
            unsetPerspective();
            $("#breadcrumb_home").attr("href", GLOBAL_LANDING_URL);
        }

        $(this).blur();
    });

    // Automatic settings
    // Should we automatically set perspective?
    if (!dontsaveprefsCookie) {
        // Is a perspective already set?
        if(!perspectiveCookie) {
            // Automatically set this perspective in cookie
            $("#rememberPerspective").attr("checked", "checked").click();
            $("#rememberPerspective").attr("checked", "checked");
        }
    }
});
