/*! NV Fonts v2.1 <http://nvinteractive.co.nz>
	Copyright (c) NV Interactive
	
	References:
		swfobject.js
		utilities.2.0.js
		jquery-1.2.6.js		
		
	Release Notes:
		2.1
		-- rewrote for jQuery
*/


var fontBasePath = "nvfonts/";
var nvfontsEnabled = true;

var fonts = new Array(
{
 	selector: ".home_panel .content h3, #newsticker h3",
	skip: true // Use to override flash replace
	},
{
 	selector: "#wireframe_header h1",
	skip: true // Use to override flash replace
	}
,
{
 	selector: "#photo_widget h3",
	css: "div {font-family: __standard; font-size: 18px; color: #000000; text-align: right; letter-spacing: 0px; leading: 0px;}",
	fontFile: "eurostile_std.swf",
	textThickness: 0
	}
,
{
 	selector: "h1",
	css: "div {font-family: __standard; font-size: 26px; color: #04101b; letter-spacing: 0px;}",
	fontFile: "eurostile_bld_itc.swf",
	textThickness: 50,
	textThickness: 0,
	skip: false,
	defaultInteraction: true
	}
,
{
 	selector: "#home_search h2",
	css: "div {font-family: __standard; font-size: 18px; color: #ffffff; letter-spacing: 0px; leading: 0px;}",
	fontFile: "eurostile_bld_itc.swf",
	textThickness: 50}	

,
{
 	selector: "h2",
	css: "div {font-family: __standard; font-size: 20px; color: #04101b; letter-spacing: 0px; leading: 0px;}",
	fontFile: "eurostile_bld_itc.swf",
	textThickness: 50}	

,	
{
 	selector: "#sidebar h3",
	css: "div {font-family: __standard; font-size: 18px; color: #000000; letter-spacing: 0px; leading: 0px;}",
	fontFile: "eurostile_std.swf",
	textThickness: 0
	}
	
,

{
 	selector: ".home_panel .heading h3",
	css: "div {font-family: __standard; font-size: 12px; color: #ffffff; letter-spacing: 0px; leading: 0px; vertical-align: top;}",
	fontFile: "eurostile_std.swf",
	textThickness: -200
}	
,
{
 	selector: "h3",
	css: "div {font-family: __standard; font-size: 16px; color: #37393B; letter-spacing: 0px; leading: 0px;}",
	fontFile: "eurostile_std.swf",
	textThickness: 50
}	
,
	
	
{
 	selector: "#menu a strong",
	css: "div {font-family: __standard; font-size: 12px; color: #ffffff; letter-spacing: 0px; leading: 0px; vertical-align: center; }",
	cssOver: "div {font-family: __standard; font-size: 12px; color: #ffffff; letter-spacing: 0px; leading: 0px; vertical-align: center;}",	
	fontFile: "eurostile_std.swf",
	textThickness: -200,
	textSharpness: 200,
	defaultInteraction: false
}
);

nvfonts = function(){

	var SAFARI_DISABLE = true;
	var FLASH_VERSION = 8;

	var fontFlashObjs;
	var checkFlashBlockerInterval;
	
	/* Hide Font html elements until we process them after domloaded 
	if(swfobject.getFlashPlayerVersion().major >= flashVersion && fonts.length > 0){
		document.write('<style type="text/css">');
		for(var g=0; g < fonts.length; g++)
			document.write(fonts[g].selector + " {visibility: hidden;} ");
		document.write('</style>');
	}*/
	
	
	/* ============ */
	/* Public Methods */
	/* ============ */
	function addFont(fontObj){
		fonts.unshift(fontObj);
	}
	
	
	function flashCallback(flashId){
		
		Debug.addLine("nvfonts.js", "flashCallback", flashId);
		
		clearInterval(checkFlashBlockerInterval);
		var flashObj = $("#" + flashId);

		Debug.addLine("nvfonts.js", "render", flashObj.data("ispreloader") );

		if(flashObj.data("ispreloader")){
			render(fontFlashObjs[flashObj.parent().attr("id")].fontObjs);
			flashObj.parent().remove();		
			return;
		}
	
		flashObj.get(0).init();
		
		if(flashObj.attr("defaultInteraction") ){
			flashObj.get(0).onmouseover = function(){this.mouseover();}
			flashObj.get(0).onmouseout = function(){this.mouseout();}
		}
	}
	
	/* ============ */
	/* Private Methods */
	/* ============ */
	
	function init(){
		
		
		if( (!nvfontsEnabled) || (SAFARI_DISABLE && jQuery.browser.safari) || (swfobject.getFlashPlayerVersion().major < FLASH_VERSION) ){
			return;
		}

		for(var j=0; j<fonts.length; j++){
			
			var font = fonts[j];
	
			font.cssOver = 				font.cssOver == undefined ? font.css : font.cssOver;
			font.textThickness = 		font.textThickness == undefined ? 0 : font.textThickness;
			font.textSharpness = 		font.textSharpness == undefined ? 0 : font.textSharpness;
			font.skip = 				font.skip == undefined ? false : font.skip;
			font.defaultInteraction = 	font.defaultInteraction == undefined ? false : font.defaultInteraction;
			font.wmode = 				font.wmode == undefined ? "transparent" : font.wmode;
						
			$(font.selector).data("font_definition", font).each(processFontObj);
						
		}
		
		//return;
	
		preload();
		//renderFontFlash();
		
		if(jQuery.browser.mozilla )checkFlashBlockerInterval = setInterval(checkFlashBlocker, 100);

	}
	
	
	function checkFlashBlocker(){
		
		Debug.addLine("nvfonts.js", "checkFlashBlocker", fontFlashObjs);
		
		if(fontFlashObjs == undefined){
			clearInterval(checkFlashBlockerInterval);
		}
		
		for(var i in fontFlashObjs){
			if( $("#" + i + " div").length > 0){
				$("#" + i).remove();
				restoreFontHtml();
				clearInterval(checkFlashBlockerInterval);
			}
		}
	}
	
	function restoreFontHtml(){
		Debug.addLine("nvfonts.js", "restoreFontHtml", "");
		
		for(var i=0; i<fonts.length; i++){			
			$(fonts[i].selector).css("visibility", "visible");
		}		
		
	}
	
	function render(fontObjs){
	
		for(var i=0; i<fontObjs.length;i++){
			var fontFlashObj = fontObjs[i];
			var el = fontFlashObj.el;
			var fontFile = fontBasePath + fontFlashObj.fontFile;
			var width = fontFlashObj.width;
			var height = fontFlashObj.height;
			var pars = fontFlashObj.pars;
			var flashObj = writeFlash(el, fontFile, width, height, pars);
			fontFlashObj.id = flashObj.id;
			flashObj.defaultInteraction = fontFlashObj.defaultInteraction;
		}
		
	}
	
	/* ================================ */
	
	function preload(){
		

		
		for(var i in fontFlashObjs){

			var fontFile = fontBasePath + fontFlashObjs[i].fontFile;
			
			var el = document.createElement("div");
			
			el.id = i;
			$(el).css({
					position: "fixed",
					top: "1px",
					left: "1px"
			});
			
			$("body").append(el);
			var pars = { wmode: "window" };
			var preLoadObj = writeFlash(el, fontFile, 1, 1, pars);
			$(preLoadObj).data("ispreloader", true);
		}
	}
	
		
	/* ================================= */
	
	function processFontObj(){
		
		font = $(this).data("font_definition");
		
		if(this.skip)return;
		if(font.skip) {
			this.skip = "skip";
			return
		}
		this.skip = "skip"; 
		
		var link = "";
		
		var l = this;
		
		while(l.tagName != "A" && l.tagName != "BODY")l = l.parentNode;
		if(l.tagName == "A")link = l.href;

		$(this).css("visibility", "visible");
		var width = $(this).width();
		var height = $(this).height();
		$(this).css("visibility", "hidden");
		
		//Hold the element open
		$(this).css("height", height);
	
		//Pull the text out of the node
		var content = "<div>" + getInnerHtml(this); + "</div>";
		
		var flashVars = "content=" + encodeURIComponent(content) +
			"&css=" +			escape(font.css) +
			"&link=" + 			encodeURIComponent(link) +
			"&css_over=" + 		escape(font.cssOver) +
			"&aa_thickness=" + 	font.textThickness +
			"&aa_sharpness=" + 	font.textSharpness;
		
		var pars = {
			flashvars: 			flashVars,
			wmode: 				font.wmode
			};
		
		var fontFlashObj = {
			el:					this,
			width:				width,
			height:				height,
			fontFile:			font.fontFile,
			defaultInteraction:	font.defaultInteraction,
			pars:				pars
			};
			
		
		//Debug.addLine("heading_replace.js", "processFontObj", flashVars);
				
		//fontFlashObjs.push(fontFlashObj);
		var fontFileId = font.fontFile.replace(".", "_");
		if(fontFlashObjs == undefined){
			fontFlashObjs = new Object();
		}
		if(fontFlashObjs[fontFileId]==undefined)fontFlashObjs[fontFileId] = new Object({fontFile: font.fontFile, fontObjs: []});
		fontFlashObjs[fontFileId].fontObjs.push(fontFlashObj);
		
	}
	
	return {
	/* Public API
	*/
	init: init,
	addFont: addFont,
	flashCallback: flashCallback
	}
	
}();

function nvfontsFlashCallback(flashid){
	Debug.addLine("nvfonts.js", "nvfontsFlashCallback", flashid);
	nvfonts.flashCallback(flashid);
}


$(document).ready(nvfonts.init);
