/*
Copyright 2006-2007 Wellspring Technologies, LLC
All Rights Reserved
Unauthorized Use Prohibited
*/
var fontSizeControl_INTERNAL_targetNode;
var fontSizeControl_INTERNAL_sizeStep;
var fontSizeControl_INTERNAL_fontSizeRegExp = '^([0-9.]*)([pxtem]*)$';
var fontSizeControl_INTERNAL_cookieName = 'fontSizeControl.lastUsed';
var fontSizeControl_INTERNAL_showVisualFeedback = true;
function fontControlSetVisualFeedback(bDisable) {
	fontSizeControl_INTERNAL_showVisualFeedback = bDisable;
}

function createFontSizeControl(fontNode, label, step, placementNode, smallerDivContent, largerDivContent, extraContent) {
	//Set the global variable for the target of the font size adjustment
	fontSizeControl_INTERNAL_targetNode = fontNode;
	//If no inline font size, make the body's inline font size the style's font size
	var findFontSizeNode = fontNode
	var bFound = false;
	var fontSize = getCookie(fontSizeControl_INTERNAL_cookieName);
	while(!bFound && findFontSizeNode.parentNode) {
		if(findFontSizeNode.style.fontSize) {
			fontNode.style.fontSize = (fontSize)?fontSize:findFontSizeNode.style.fontSize;
			bFound = true;
		} else if(getStyle(findFontSizeNode, 'fontSize', 'font-size')) {
			fontNode.style.fontSize = (fontSize)?fontSize:getStyle(findFontSizeNode, 'fontSize', 'font-size');
			bFound = true;
		} else {
			findFontSizeNode = findFontSizeNode.parentNode;
		}
	}
	//Set the global step size based on input
	fontSizeControl_INTERNAL_sizeStep = (step > 0)?step:1;
	//Create container div for the control
	var containerDiv = document.createElement('div');
		containerDiv.className = 'fontSizeControlContainer';
	//Build what will be a 'down button' & assign events
	var smallerDiv = document.createElement('div');
		smallerDiv.className = 'fontSizeControlSmallerDiv fontSizeControlMouseUp fontSizeControlMouseOut';
		smallerDiv.onmouseover = fontSizeControlMouseOver;
		smallerDiv.onmouseout = fontSizeControlMouseOut;
		smallerDiv.onmousedown = fontSizeControlMouseDown;
		smallerDiv.onmouseup = fontSizeControlMouseUp;
		smallerDiv.onclick = fontSizeControlDownOnclick;
		smallerDiv.innerHTML = (smallerDivContent)?smallerDivContent:' ';
	//Build what will be an 'up button' & assign events
	var largerDiv = document.createElement('div');
		largerDiv.className = 'fontSizeControlLargerDiv fontSizeControlMouseUp fontSizeControlMouseOut';
		largerDiv.onmouseover = fontSizeControlMouseOver;
		largerDiv.onmouseout = fontSizeControlMouseOut;
		largerDiv.onmousedown = fontSizeControlMouseDown;
		largerDiv.onmouseup = fontSizeControlMouseUp;
		largerDiv.onclick = fontSizeControlUpOnclick
		largerDiv.innerHTML = (largerDivContent)?largerDivContent:' ';
	//Create a label
	var labelDiv = document.createElement('div');
		labelDiv.className = 'fontSizeControlLabel';
	//Allow room for extra content - this may prove to be unnecessary
	var extraDiv = document.createElement('div');
		extraDiv.className = 'fontSizeControlExtra';
		(extraContent)?extraDiv.innerHTML = extraContent:null;
	//Put all our new elements in containerDiv
	containerDiv.appendChild(smallerDiv);
	containerDiv.appendChild(largerDiv);
	containerDiv.appendChild(labelDiv);
	containerDiv.appendChild(extraDiv);
	(placementNode)?placementNode.appendChild(containerDiv):null;
	return containerDiv;
}

function fontSizeControlMouseOver(e) {
	e = (e)?e:window.event;
	currentTarget = (e.currentTarget)?e.currentTarget:e.srcElement;
	(currentTarget.className.search(/fontSizeControlMouseOver/i) < 0 && fontSizeControl_INTERNAL_showVisualFeedback)?currentTarget.className += ' fontSizeControlMouseOver':null;
	(fontSizeControl_INTERNAL_showVisualFeedback)?currentTarget.className = currentTarget.className.replace(/\s*fontSizeControlMouseOut/i, ''):null;
}

function fontSizeControlMouseOut(e) {
	e = (e)?e:window.event;
	currentTarget = (e.currentTarget)?e.currentTarget:e.srcElement;
	(currentTarget.className.search(/fontSizeControlMouseOut/i) < 0 && fontSizeControl_INTERNAL_showVisualFeedback)?currentTarget.className += ' fontSizeControlMouseOut':null;
	(fontSizeControl_INTERNAL_showVisualFeedback)?currentTarget.className = currentTarget.className.replace(/\s*fontSizeControlMouseOver/i, ''):null;
}

function fontSizeControlMouseDown(e) {
	e = (e)?e:window.event;
	currentTarget = (e.currentTarget)?e.currentTarget:e.srcElement;
	(currentTarget.className.search(/fontSizeControlMouseDown/i) < 0 && fontSizeControl_INTERNAL_showVisualFeedback)?currentTarget.className += ' fontSizeControlMouseDown':null;
	(fontSizeControl_INTERNAL_showVisualFeedback)?currentTarget.className = currentTarget.className.replace(/\s*fontSizeControlMouseUp/i, ''):null;
}

function fontSizeControlMouseUp(e) {
	e = (e)?e:window.event;
	currentTarget = (e.currentTarget)?e.currentTarget:e.srcElement;
	(currentTarget.className.search(/fontSizeControlMouseUp/i) < 0 && fontSizeControl_INTERNAL_showVisualFeedback)?currentTarget.className += ' fontSizeControlMouseUp':null;
	(fontSizeControl_INTERNAL_showVisualFeedback)?currentTarget.className = currentTarget.className.replace(/\s*fontSizeControlMouseDown/i, ''):null;
}

function fontSizeControlDownOnclick(e) {
	e = (e)?e:window.event;
	currentTarget = (e.currentTarget)?e.currentTarget:e.srcElement;
	var sizeRegExp = new RegExp(fontSizeControl_INTERNAL_fontSizeRegExp, 'i');
	var parts = sizeRegExp.exec(fontSizeControl_INTERNAL_targetNode.style.fontSize);
	if(parts[1] != 0) {
		fontSizeControl_INTERNAL_targetNode.style.fontSize = (parseInt(parts[1]) - fontSizeControl_INTERNAL_sizeStep) + parts[2];
	}
	setCookie(fontSizeControl_INTERNAL_cookieName, fontSizeControl_INTERNAL_targetNode.style.fontSize);
}

function fontSizeControlUpOnclick(e) {
	e = (e)?e:window.event;
	currentTarget = (e.currentTarget)?e.currentTarget:e.srcElement;
	var sizeRegExp = new RegExp(fontSizeControl_INTERNAL_fontSizeRegExp, 'i');
	var parts = sizeRegExp.exec(fontSizeControl_INTERNAL_targetNode.style.fontSize);
	fontSizeControl_INTERNAL_targetNode.style.fontSize = (parseInt(parts[1]) + fontSizeControl_INTERNAL_sizeStep) + parts[2];
	setCookie(fontSizeControl_INTERNAL_cookieName, fontSizeControl_INTERNAL_targetNode.style.fontSize);
}

function getStyle(node, cssProperty, GeckoProperty) {
	if (node.currentStyle) {//if IE5+
		return node.currentStyle[cssProperty];
	} else if (window.getComputedStyle) { //if NS6+
		if(node.nodeType == 1) {
			var elstyle= document.defaultView.getComputedStyle(node, "");
			var propToUse = (GeckoProperty)?GeckoProperty:cssProperty;
			return elstyle.getPropertyValue(propToUse);
		} else {
			return null;
		}
	}
}

if(!window.setCookie) {
	function setCookie(name, value, expires, path, domain, secure) {
	  var curCookie = name + "=" + escape(value) +
	      ((expires) ? "; expires=" + expires.toGMTString() : "") +
	      ((path) ? "; path=" + path : "") +
	      ((domain) ? "; domain=" + domain : "") +
	      ((secure) ? "; secure" : "");
	  document.cookie = curCookie;
	}
}

if(!window.getCookie) {
	function getCookie(name) {
	  var dc = document.cookie;
	  var prefix = name + "=";
	  var begin = dc.indexOf("; " + prefix);
	  if (begin == -1) {
	    begin = dc.indexOf(prefix);
	    if (begin != 0) return null;
	  } else
	    begin += 2;
	  var end = document.cookie.indexOf(";", begin);
	  if (end == -1)
	    end = dc.length;
	  return unescape(dc.substring(begin + prefix.length, end));
	}
}

