/* Create VGR namespace */
if(typeof VGR == "undefined"){ var VGR = {}; }

/**
 * @requires jQuery
 * Enables picking various display options (font-size, line-height, etc.).
 * Uses cookies to save and get the users' options and applies them on each page.
 */
VGR.displayPreferences = function () {
	var options = {
	    // A jQuery selector matching the element(s) to apply display preferences to.
	    // The default is the body element.
		dpContainer: 'body:first',
		// Default options for the settings. The numbers match the options in the markup.
		dpDefaults: {
		    fontsize: 2,
		    lineheight: 2,
		    wordspacing: 1,
		    fontfamily: 4,
		    theme: 1
		},
		// Current settings, used to save the user's choices
		dpSettings: {
		    fontsize: 2,
		    lineheight: 2,
		    wordspacing: 1,
		    fontfamily: 4,
		    theme: 1
		},
		// ID values for the save and reset buttons
		dpSaveID: 'save',
		dpResetID: 'reset',
		// Selector for the element containing the settings page
		dpContainerElement: '#display-preferences',
		// Selector for the preview element on the settings page
		dpPreviewElement: '#preview'
	};
	/**
	* Initialization
	*/
	function init(opts) {
	    // Check for custom options
		if (VGR.displayPreferences.custom) {
		    opts = VGR.displayPreferences.custom;
		}
		// If options were supplied, apply them to the option Object.
		for (var key in opts) {
			if (options.hasOwnProperty(key)) {
				options[key] = opts[key];
			}
		}
		var currCookie;
		// Check if the current page is the settings page
		var isSettingsPage = false;
		if ($(options.dpContainerElement).length) {
		    isSettingsPage = true;
		}
	    if (isSettingsPage) {
	        options.dpContainer = options.dpPreviewElement;
        }
        // Loop through the radio groups
		for (var setting in options.dpSettings) {
		    if (isSettingsPage) {
    		    // Attach event handlers
        		$('input[type=radio][name=' + setting + ']').click(function () {
        		    $(options.dpContainer).removeClass('dp-' + this.name + '-' + options.dpSettings[this.name]);
        		    // Do not add a class name if a default setting has been selected
        		    if (this.value != options.dpDefaults[this.name]) {
        		        $(options.dpContainer).addClass('dp-' + this.name + '-' + this.value);
        		    }
        		    options.dpSettings[this.name] = this.value;
        	    });
        	}
    	    // Check for existing cookies
    	    currCookie = readCookie('dp-' + setting);
    	    if (currCookie) {
	            if (isSettingsPage) {
	                // Activate the correct radio button
    	            $('#' + setting + '-' + currCookie).click();
    	        }
    	        if (currCookie != options.dpDefaults[setting]) {
    	            // Add classes for non-default settings
    		        $(options.dpContainer).addClass('dp-' + setting + '-' + currCookie);
    		    }
    	    }
	    }
	    // Set up handlers for the save and reset buttons
	    if (isSettingsPage) {
	        $('#' + options.dpSaveID).click(saveSettings);
	        $('#' + options.dpResetID).click(resetSettings);
	    }
	}
	/**
	* Save settings
	*/
	function saveSettings() {
		for (var setting in options.dpSettings) {
		    // Erase cookies if custom value == default value
		    if (options.dpSettings[setting] == options.dpDefaults[setting]) {
		        eraseCookie('dp-' + setting);
		    }
		    else {
	            createCookie('dp-' + setting, options.dpSettings[setting], 365);
	        }
	    }
        window.history.back();
	}
	/**
	* Reset settings and erase cookies
	*/
	function resetSettings() {
		for (var setting in options.dpSettings) {
		    $('#' + setting + '-' + options.dpDefaults[setting]).click();
	    }
	}
    /* Functions for handling cookies from http://www.quirksmode.org/js/cookies.html */
    /**
    * Create a cookie
    */
    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=/";
    }
    /**
    * Read a cookie
    */
    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;
    }
    /**
    * Erase a cookie
    */
    function eraseCookie(name) {
    	createCookie(name,"",-1);
    }
    /* Return public methods */
	return {
		init: init
	};
}();

// Init on document ready
$(document).ready(function() {
	VGR.displayPreferences.init({
	    // A comma-separated list of jQuery selectors matching the elements to be affected
        //dpContainer: '#startpagecenterinnercolumn, #startpagerightinnercolumn'
	});
});

