/* Text Resizer */

if (!window.TextResizer) {

	var TextResizer = function() {
		this.minLimit = 1;
		this.defaultSize = 2;
		this.currentSize = parseInt(this.getCookie()) || this.defaultSize;
		this.maxLimit = 5;
		this.link = document.getElementById('textResizerStylesheet');
		
		// Set starting stylesheet
		if (this.currentSize != this.defaultSize) {
			this.setStylesheet();
		}
	
	}

	
	TextResizer.prototype.init = function() {
		this.header = document.getElementById('header');
		this.textResizerDown = document.getElementById('textResizerDown');
		this.textResizerUp = document.getElementById('textResizerUp');
		
		var oThis = this;
		
		
		// Add event handlers
		YAHOO.util.Event.addListener(this.textResizerDown, 'mouseup', function(e) {
			oThis.decrease(e);
		});
		YAHOO.util.Event.addListener(this.textResizerDown, 'mouseover', function(e) {
			oThis.mouseover(e);
		});
		YAHOO.util.Event.addListener(this.textResizerDown, 'mouseout', function(e) {
			oThis.mouseout(e);
		});
		
		YAHOO.util.Event.addListener(this.textResizerUp, 'mouseup', function(e) {
			oThis.increase(e);
		});
		
		YAHOO.util.Event.addListener(this.textResizerUp, 'mouseover', function(e) {
			oThis.mouseover(e);
		});
		YAHOO.util.Event.addListener(this.textResizerUp, 'mouseout', function(e) {
			oThis.mouseout(e);
		});
		
		
		// preload hover states
		this.textResizerDownHover = document.createElement('div');
		YAHOO.util.Dom.addClass(this.textResizerDownHover, 'textResizerDown hover');
		this.header.appendChild(this.textResizerDownHover);
		
		this.textResizerUpHover = document.createElement('div');
		YAHOO.util.Dom.addClass(this.textResizerUpHover, 'textResizerUp hover');
		this.header.appendChild(this.textResizerUpHover);
	}
	
	
	// EVENT HANDLERS
	TextResizer.prototype.mouseover = function(e) {
		var target = YAHOO.util.Event.getTarget(e);

		// Just add a class for quick hover action
		YAHOO.util.Dom.addClass(target, 'hover');
	}
	
	TextResizer.prototype.mouseout = function(e) {
		var target = YAHOO.util.Event.getTarget(e);

		// Just remove a class for quick hover action
		YAHOO.util.Dom.removeClass(target, 'hover');
	}
	TextResizer.prototype.increase = function(e) {
		if (this.currentSize < this.maxLimit) {
			this.currentSize++;	
			
			this.setCookie();
			this.setStylesheet();
		}
	}
	TextResizer.prototype.decrease = function(e) {
		if (this.currentSize > this.minLimit) {
			this.currentSize--;	
			
			this.setCookie();
			this.setStylesheet();
		}
	}
	
	
	// UTILITIES
	TextResizer.prototype.setStylesheet = function() {
		this.link.href = '../styles/textSize' + this.currentSize + '.css';
	}
	
	
	
	
	// COOKIES
	TextResizer.prototype.getCookie = function() {
		return jimAuld.utils.cookieLib.get('textSize');
	}
	TextResizer.prototype.setCookie = function() {
		jimAuld.utils.cookieLib.set('textSize', this.currentSize, 720);
	}


	// Instantiate
	window.textResizer = new TextResizer();

	YAHOO.util.Event.addListener(window, 'load', function() {
		window.textResizer.init();
	});
	
}
