/*==============================================================================================
	fontControl
	
	Requires: jquery 1.2.6
	Optional: jquery.cookies.2.0.1.js
	
	Changes the existing size of text by a fixed amount (fontControl.size)
	For use with font resizing buttons. Buttons should call fontControl.up() and fontControl.down()
	Saves changed font size to cookies so it can be applied between pages.
	
==============================================================================================*/
//$(document).ready( function(){ fontControl.init(); } );

var fontControl = {
	increment: 2, 	// increment font size is changed by
	size: 0, 		// initial font size change
	minimum: -4, 	// minimum font size change
		
	blockSelectors: ['#content'],									// list of block selectors containing text to be resized
	textSelectors: ['', 'h1', 'h2', 'p', 'li', 'div', 'td'],		// list of sub-selectors containing text to be resized
	
	blockExcluders: [],			// list of block selectors to exclude
	textExcluders: [],			// list of sub-selectors to exclude
	
	selector: [],
	excluder: [],
	
	init: function () {
		var fontBase;
		
		// Build list of block and text selectors
		for (i=0;i<this.blockSelectors.length;i++) {
			for (j=0;j<this.textSelectors.length;j++) {
				this.selector.push(this.blockSelectors[i]+' '+this.textSelectors[j]);
			}
		}
		
		// Build list of excluders
		for (i=0;i<this.blockExcluders.length;i++) {
			for (j=0;j<this.textExcluders.length;j++) {
				this.excluder.push(this.blockExcluders[i]+' '+this.textExcluders[j]);
			}
		}			
		
		
		// Change
		if($.cookies) fontBase = $.cookies.get('fontSize');
		if (fontBase) {
			this.size = parseInt(fontBase);
			if (isNaN(this.size)) { this.size = 0; }
			this.resize(this.size);
		}		
	},
	
	up: function () {
		this.resize(this.increment);
		this.size += this.increment;
		if($.cookies) { $.cookies.set('fontSize',this.size); }
	},
	
	down: function () {
		if (this.size - this.increment >= this.minimum) {
			this.resize(-this.increment);
			this.size -= this.increment;
			if($.cookies) { $.cookies.set('fontSize',this.size); }
		}
	},
	
	resize: function (sizeChange) {
		var newSizeArray = [];
		var elementsToResize = $.unique($(this.selector.join(', ')).not(this.excluder.join(', ')).get());
		sizeChange = parseInt(sizeChange);
		if (isNaN(sizeChange)) { return false; }
		
		// TRICKY: two step process prevents nested elements from be resized more than once
		$(elementsToResize).each( function() {
			newSizeArray.push(parseInt($(this).css('font-size')) + sizeChange);
		});
		
		$(elementsToResize).each( function() {
			$(this).css('font-size', newSizeArray.shift()+'px');
		});
		
	},
	
	
	printPage: function () {
		var content = '';
		$('.print').each( function(i){ content += this.innerHTML + '\r\n'; });
		if (content != '') {
			var a = window.open('','','width=800,height=700');
			a.document.open("text/html");
			a.document.write('<html><head><title>HQCC</title><link rel="stylesheet" type="text/css" href="/media/css/print.css"></head><body>')
			a.document.write(content);
			a.document.write('</body></html>');
			a.document.close();
			a.print(); 
		}
	}
	
};




