/**
	Cufon startup helper.
	
	This script detects whether the computer has the defined font for the element to be replaced.
	If not, the text occurence is replaced with Cufon.
	
	Depends on:
		jquery.js (v. 1.4.2)
		cufon-yui.js (v. 1.09)
		jquery.fontavailable-1.1.js (v. 1.1)
		
	Usage:
		$.specialFontOptions(options)
			- Calling this function is optional
		Parameters:
			1. options = {
				forceRun: true/false, (force Cufon.now() function wheter there is something to replace or not)
				forceCufon: true/false (force Cufon to replace fonts whether the font is available or not)
			}
		$.specialFont(elementSelector, options);
		Parameters:
			1. elementSelector = jQuery form for the element's selector
			2. options = Additional font options for the Cufon's replace-function, will be ignored if the font isn't replaced.
			Font family is read straight from the CSS.
	
	@licence MIT Licence
	@author Antti Hukkanen, Huge Solutions
	@version 0.1
*/
(function() {

// CufonHelper
var CufonHelper = {
	
	fontElements: [],
	fontOptions: [],
	runReplacement: false,
	forceCufon: false, // For testing
	
	init: function(options) {
		if (typeof(options.forceRun) != "undefined") {
			this.runReplacement = eval(options.forceRun) == true;
		} else if (typeof(options.forceCufon) != "undefined") {
			this.forceCufon = eval(options.forceCufon) == true;
		}
	},
	
	registerElement: function(selector, options) {
		this.fontElements.push(selector);
		if (typeof(options) == "undefined" || options == null) {
			options = {};
		}
		this.fontOptions.push(options);
	},
	
	replaceFonts: function() {
		for (var i in this.fontElements) {
			var selector = this.fontElements[i];
			var options = this.fontOptions[i];
			
			var currentFontElem = jQuery(selector);
			if (currentFontElem.length > 0) {
				var currentFont = currentFontElem.css('font-family');
				var fontInUse = true;
				if (typeof(options.fontFamily) != "undefined" && options.fontFamily != currentFont) {
					currentFont = options.fontFamily;
					fontInUse = false;
				} else {
					options.fontFamily = currentFont;
				}
				if (this.forceCufon || !$.fontAvailable(currentFont)) {
					// Replacing missing fonts with Cufon
					Cufon.replace(selector, options);
					this.runReplacement = true;
				} else if (!fontInUse) {
					$(selector).css('font-family', currentFont);
				}
			}
		}
	},
	
	runCufon: function() {
		if (this.runReplacement) {
			Cufon.now();
		}
	}
	
};

// jQuery functions
jQuery.specialFontOptions = function(options) {
	CufonHelper.init(options);
}

jQuery.specialFont = function(selector, options) {
	if ($.isArray(selector)) {
		for (var i in selector) {
			var sel = selector[i];
			CufonHelper.registerElement(sel, options);
		}
	} else {
		CufonHelper.registerElement(selector, options);
	}
}

// Startup
$(document).ready(function() {
	CufonHelper.replaceFonts();
	//CufonHelper.runCufon();
});

})();
