/****************************************************************/
/***  SUPPORTING FUNCTIONS									  ***/
/****************************************************************/


// ---------------------------------------------------------------------
// string.trim (our own language addition)
// ---------------------------------------------------------------------
if (String.prototype.trim == null) {
	String.prototype.trim = function() {
		return this.replace(/^\s+|\s+$/, '');
	};
}


function AddClass (theNode, theClass) {
	if (theNode.className == '') {
		theNode.className = theClass;
	} else {
		theNode.className += ' ' + theClass;
	}
}

function RemoveClass (theNode, theClass) {
	var oldClass = theNode.className;
	var regExp = new RegExp('\\s?\\b'+theClass+'\\b');
	if (oldClass.match(regExp) != null) {
		theNode.className = oldClass.replace(regExp,'');
	}
}

// Show/Hide a DIV using the classes ".show" & ".hide"
function ShowHide(sDIVID)
{
	var sClass = "";
	
	switch (document.getElementById(sDIVID).className)
	{
		case "hide":
			sClass = "show";
			break;
		default:
			sClass = "hide";
			break;
	}
	document.getElementById(sDIVID).className = sClass;
}



/****************************************************************/
/***  LIBRARY FUNCTIONS										  ***/
/****************************************************************/

// Open new window in full-screen mode
function OpenFullScreenWindow(url, options) {
  var winOptions = '';
  switch (options)
  {
    case 'scroll':
      winOptions = 'directories=no, menubar=no, status=no, toolbar=no, scrollbars=yes';
      break;
      
    default:
      winOptions = 'directories=no, menubar=no, status=no, toolbar=no';
  }
  
  var newWin = window.open(url, 'newWindow', winOptions + ", height=" + screen.height + ", width=" + screen.width);
  newWin.moveTo(0,0);
  newWin.focus();
  return false;
}

// Print current page
function PrintPage()
{
	window.print();
}


// ---------------------------------------------------------------------
// Cookies
// ---------------------------------------------------------------------

 /**
 * @name arb.cookie.set
 * @desc Set a cookie
 * @param String name The name of the cookie.
 * @param String value The value of the cookie.
 * @param Hash options A set of key/value pairs for optional cookie parameters.
 * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
 * If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
 * If set to null or omitted, the cookie will be a session cookie and will not be retained
 * when the the browser exits.
 * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
 * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
 * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
 * require a secure protocol (like HTTPS).
 */
 function SetCookie (name, value, options) {
	options = options || {};
	var expires = '';

	options.expires = options.expires || 356;

	var date = new Date();
	date.setTime(date.getTime()+(options.expires*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();

	var path = options.path ? '; path=' + options.path : '';
	var domain = options.domain ? '; domain=' + options.domain : '';
	var secure = options.secure ? '; secure' : '';
	document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
 }

 /**
 * @name arb.cookie.get
 * @desc Get the value of a cookie
 * @param String name The name of the cookie.
 * @return The value of the cookie.
 * @type String
 */
 function GetCookie (name) {
	var cookieValue = null;
	if (document.cookie && document.cookie != '') {
		var cookies = document.cookie.split(';');
		for (var i = 0; i < cookies.length; i++) {
			var cookie = cookies[i].trim();
			// Does this cookie string begin with the name we want?
			if (cookie.substring(0, name.length + 1) == (name + '=')) {
				cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
				break;
			}
		}
	}
	return cookieValue;
 }

 /**
 * @name arb.cookie.remove
 * @desc Delete a cookie
 * @param String name The name of the cookie.
 */
 function DeleteCookie (name) {
	SetCookie(name, "", {expires: -1});
 }




// ---------------------------------------------------------------------
// text resizing controls
// ---------------------------------------------------------------------
$(document).ready(function(){

initFontSizeControls();
function initFontSizeControls() {
 var maxFontSize = 180; // %
 var minFontSize = 70; // %
 function incrementFontSize(sizeDelta) {
 var newSize = parseFloat(GetCookie("font-size")) + sizeDelta;
 if (newSize > minFontSize && newSize < maxFontSize) {
 setBodyFontSize(newSize);
 SetCookie("font-size", newSize, {path: "/"});
 }
 }

 function setBodyFontSize(size) {
 $(".main-content").css("font-size", size + "%");
 }

 var fontSize = GetCookie("font-size");
 var fontIncrement = 8.33; // %

 if (!fontSize) {
 SetCookie("font-size", "166", {path: "/"}); // set the default if there' no value
 fontSize = GetCookie("font-size"); // read it back to make sure cookies are supported
 }

 // check we can support this functionality
 if (window.print && fontSize) {
	$("#header-pageFunctions").append('<ul class="pageFunctions"><li id="emailControl"><span></span></li><li id="printControl"><span></span></li><li id="fontSizeDownControl"><span></span></li><li id="fontSizeUpControl" class="last"><span></span></li></ul>');

	setBodyFontSize(fontSize);
 
 // attach email handler
 $("#emailControl").click(function() {
 window.print();
 });

 // attach print handler
 $("#printControl").click(function() {
 window.print();
 });

 // attach font down handler
 $("#fontSizeDownControl").click(function() {
 incrementFontSize(-fontIncrement);
 });

 // attach font up handler
 $("#fontSizeUpControl").click(function() {
 incrementFontSize(fontIncrement);
 });

 // attach hover handler so we can style cursor with a hand on mouseover
 $("#header-pageFunctions li").hover(
 function() { AddClass(this, "hover"); },
 function() { RemoveClass(this, "hover"); }
 );
 }
}
});

