// Allows console to alert in IE
if( !console ) { var console = { log: function( msg ) { alert( msg ); } } }

var framework = 'jquery';

$(document).ready(function() {
	setPrintLink.init();
	textSizer.init();
	assignPopUps.init();
	
	$('.headerContainer .navigationContainer a').bind( 'click', function() {
		this.blur();
	});
});

// Adds inline log chaining
jQuery.fn.log = function ( msg ) {
	console.log( "%s: %o", msg, this );
	return this;
};

var textSizer = {
	defaultSize: 'default',

	init: function() {
		textSizer.checkForCookie();

		$('#textSizeUtility a').bind( 'click', function() {
			$(this).blur();
			var size = $(this).attr( 'title' );
			ICON.util.cookie.set( 'UserTextSize', size, { expires: 365 } );

			if( $.browser.msie ) {
				window.location.reload();
			} else {
				location = document.URL;
			}
			return false;
		});
	},

	checkForCookie: function() {
		if( ICON.util.cookie.get( 'UserTextSize' ) != null ) {
			textSizer.defaultSize = ICON.util.cookie.get( 'UserTextSize' );
			textSizer.writeStyleSheet( textSizer.defaultSize );

			var textSizerLink = $('#textSizeUtility a:first');
			if( ICON.util.cookie.get( 'UserTextSize' ) == 'default' ) {
				textSizerLink.text( 'Enlarge Font' );
				textSizerLink.attr( 'title', 'large' );
			} else {
				textSizerLink.text( 'Reduce Font' );
				textSizerLink.attr( 'title', 'default' );
			}
		} else {
			ICON.util.cookie.set( 'UserTextSize', textSizer.defaultSize, { expires: 365 } );
			textSizer.writeStyleSheet( textSizer.defaultSize );
		}
	},
	
	writeStyleSheet: function( theSheetSize ) {
		if( $.browser.msie ) {
			$('#injectedCss').remove();
			$( 'head' ).append('<link id="injectedCss" rel="stylesheet" type="text/css" href="/assets/css/' + theSheetSize + '.css" media="screen" title="' + theSheetSize + '" />');
		} else {
			$( 'head' ).append('<style id="injectedCss" type="text/css">@import url("/assets/css/' + theSheetSize + '.css") screen;</style>');
		}
	}
}

var setPrintLink = {
	init: function() {
		$('#printPageLink').click(function() {
			printWindow = window.open( this.href, null, 'height=600, width=800, location=1, menubar=1, resizable=1, scrollbars=1, status=1, titlebar=1, toolbar=1' );
			return false;
		});
	}
}

var assignPopUps = {
	init: function() {
		var frxPop = '';
		$('.isPopUp').bind( 'click', function() {
			if( !frxPop.closed && frxPop.location ) {
				frxPop.location.href = this.href;
			} else {
				frxPop = window.open( this.href, null, "height=400, width=400, location=0, menubar=0, resizable=1, scrollbars=1, status=1, titlebar=1, toolbar=0" );
			}
			if( window.focus ) { frxPop.focus(); }
			return false;
		});
	}
}

/*
 * Utility functions
 */

var ICON = {};
ICON.util = {};

ICON.util.cookie = {
	get: function ( name ) {
		var cookieValue = null;
		if ( document.cookie && document.cookie != '' ) {
			var cookies = document.cookie.split( ';' );
			for ( var i = 0; i < cookies.length; i++ ) {
				var cookie = ICON.util.trim( cookies[i] );
				// 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;
	},

	set: function ( name, value, options ) {
		if ( typeof value != 'undefined' ) {
			options = options || {};
			if ( value === null ) {
				value = '';
				options.expires = -1;
			}
			var expires = '';
			if ( options.expires && ( typeof options.expires == 'number' || options.expires.toUTCString ) ) {
				var date;
				if ( typeof options.expires == 'number' ) {
					date = new Date();
					date.setTime( date.getTime() + ( options.expires * 24 * 60 * 60 * 1000 ) );
				} else {
					date = options.expires;
				}
			}
			expires = '; expires=' + date.toUTCString();
			
			document.cookie = name + '=' + encodeURIComponent( value ) + expires +
		    ( ( options.path ) ? '; path=' + ( options.path ) : '; path=/') +
		    ( ( options.domain ) ? '; domain=' + ( options.domain ) : '') +
		    ( ( options.secure ) ? '; secure' : '');
		}
	}
};


ICON.util.trim = function( str ) {
	if( 'jquery' == framework.toLowerCase() ){
		return jQuery.trim( str );
	} else if( 'mootools' == framework.toLowerCase() ){
		return str.trim();
	}
}

