/**
 *  Oxford University Press - Online Resource Center - Scripts
 *
 *  This script file will cointains the javascript methods related to ORClayout & some basic widgets like text resize.
 */

  	/**
  	 * Declare constants here
  	 */

	//Set the  font sizes for text resize widget
	var FONT_SIZE = new Array();
	FONT_SIZE["SMALL"] = 10;
	FONT_SIZE["MEDIUM"] = 11;
	FONT_SIZE["LARGE"] = 12;

	var RESIZE_TEXT_COOKIE_NAME = "textSize";
	var RESIZE_TEXT_SELECTED_COLOR  = "#E1E1E1";

	var SEARCH_INPUT_DEFAULT_TEXT = "Search Online Resource Centres";

  	/**
  	 * This function will set the default text for the given search input element.
  	 *
  	 * @param {Object} inputElem - The input element
  	 */
	function setSearchDefaultText(inputElem) {
		var calssName = "blue";

		//If the class name of the element is blue remove the value and class name
  		if (inputElem.className == calssName) {
			inputElem.value = "";
			inputElem.className = "";
		}
		else {
			//If the input value is empty add the class name and default text, else remove the class name
			if (inputElem.value == "") {
				inputElem.value = SEARCH_INPUT_DEFAULT_TEXT;
				inputElem.className = calssName;
			}
			else {
				inputElem.className = "";
			}
		}
	}

	/**
	 * This function will resize the page text.
	 * It will store the size selected in a cookie and will use it across the pages
	 *
	 * @param {Object} size
	 */
	function resizeText(size) {
		//If the size is given, store the size value in a cooke and change the font size
		if(size) {
			size = FONT_SIZE[size.toUpperCase()];
			createCookie(RESIZE_TEXT_COOKIE_NAME, size, 365);
			document.body.style.fontSize = size + "px";
		}
		else {
			//If the size is not given, check the size in cookie. If cookie has size value, resize the
			//text with cookie value
			var textSizeCookie = readCookie(RESIZE_TEXT_COOKIE_NAME);
			size = textSizeCookie;
			if(textSizeCookie != null) {
				document.body.style.fontSize = textSizeCookie + "px";
			}
		}

		//Changhe the background style of the text resize elements to show which one is selected.
		var textSmall = document.getElementById("textSizeSmall");
		var textMedium = document.getElementById("textSizeMedium");
		var textLarge = document.getElementById("textSizeLarge");
		var noColor = "";

		if(size == FONT_SIZE["LARGE"]) {
			textLarge.style.backgroundColor = RESIZE_TEXT_SELECTED_COLOR;
			textMedium.style.backgroundColor = noColor;
			textSmall.style.backgroundColor = noColor;
		}
		else if(size == FONT_SIZE["MEDIUM"]) {
			textMedium.style.backgroundColor = RESIZE_TEXT_SELECTED_COLOR;
			textLarge.style.backgroundColor = noColor;
			textSmall.style.backgroundColor = noColor;
		}
		else {
			textSmall.style.backgroundColor = RESIZE_TEXT_SELECTED_COLOR;
			textMedium.style.backgroundColor = noColor;
			textLarge.style.backgroundColor = noColor;
		}
	}

  	/**
  	 * This function will help us to create a cookie
  	 *
  	 * @param {Object} name - The name of the cookie
  	 * @param {Object} value - The value of the cookie
  	 * @param {Object} days - The expiry days
  	 */
	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=/";
	}

	/**
	 * This function will help us to read the cookie
	 *
	 * @param {Object} name - The cookie name
	 */
	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;
	}


