
/*  =======================
    Cookie control (from PPK 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);
}

/*  =======================
    Add Disable/Enable Scripts Prefrence
    ======================= */

function setScriptPrefrence(reset) {

    var linkLocation;
	//this.containerId = "site-services";

	containerElem = setScriptPrefrence.containerElem ? setScriptPrefrence.containerElem : $("#site-services")[0];
	enableText = setScriptPrefrence.enableText ? setScriptPrefrence.enableText : "Enable Scripts";
	disableText = setScriptPrefrence.disableText ? setScriptPrefrence.disableText : "Enable Scripts";
	seperator = setScriptPrefrence.seperator ? setScriptPrefrence.seperator : "";
	position = setScriptPrefrence.position ? setScriptPrefrence.position : "before";

    // Where the link gets inserted (tries different locations)
    if (containerElem) {

		// Check preferences, and set cookie if it's not there
		var allowScripts;
		if (readCookie('allowScripts') == 'false') {
			allowScripts = false;
		} else if (readCookie('allowScripts') == 'true') {
			allowScripts = true;
		} else {
			allowScripts = true;
			createCookie('allowScripts', 'true', 30);
		}

		if (reset) {
			$("#scripts-setting").remove();
		}

		// Check the link doesn't already exist
		if ($("#scripts-setting").length == 0) {
			var location = window.location.href;
			// Strip hash
			var hash = location.indexOf("#");
			if (hash != -1) {
				location = location.substring(0, hash);
			}

			if (allowScripts == true) {
				jqLink = $("<a href='" + location + "'>"+disableText+"</a>");
			} else {
				jqLink = $("<a href='" + location + "'>"+enableText+"</a>");
			}

			if ($(containerElem).find("ul").length != 0) {
				// Add list item wrapper if theres a list in the container
				var jqWrapper = $("<li id='scripts-setting'></li>");
				jqWrapper.append(jqLink);
				if (position == "before") {
					$(containerElem).find("ul").eq(0).prepend(jqWrapper);
				} else if (position == "after") {
					$(containerElem).find("ul").eq(0).append(jqWrapper);
				}	
				
			} else if($(containerElem).is("ul")) {
				// Add list item wrapper if the container is a list
				var jqWrapper = $("<li id='scripts-setting'></li>");
				jqWrapper.append(jqLink);				
				if (position == "before") {
					$(containerElem).prepend(jqWrapper);
				} else if (position == "after") {
					$(containerElem).append(jqWrapper);
				}	

			} else {
				// Otherwise just append as it is
				jqLink.attr("id", "scripts-setting");
				if (position == "before") {
					$(containerElem).prepend(seperator).prepend(jqLink);					
				} else if (position == "after") {
					$(containerElem).append(seperator).append(jqLink);				
				}
			}

			// Change setting and reload page
			$("#scripts-setting").click(function() {
				if (readCookie('allowScripts') == 'true') {
					createCookie('allowScripts', 'false', 30);
				} else if (readCookie('allowScripts') == 'false') {
					createCookie('allowScripts', 'true', 30);
				}
			});
		}

		return(allowScripts);
	}
}


/*  =======================
    INITIALISE Scripts
    ======================= */
/*
$(function(){
	// Add "Disable Scripts" to this UL (or UL container)
    setScriptPrefrence.containerElem = "footer";
	// Change script preference link text
	//setScriptPrefrence.enableText = "Turn on slide show animation";
	//setScriptPrefrence.disableText = "Turn off slide show animation";
	//setScriptPrefrence.seperator = " | ";	
});*/