(function($) {

	/**
	 * Applies Cufon to all widgets on the page. Used for late-loading widgets
	 * that miss the initial application of cufon styles. 
	 */
	$.BNZ.replaceWidgetFonts = function(){
		var h1Gradient = $.BNZ.standardGradient;

		Cufon
			.set('fontFamily', 'Serrano Pro Black')
				.replace('div.widget h1:not(.ezone-content, .nocufon)', { color: h1Gradient })
				.replace('div.widget .widget-rate, div.widget .phone-number, div.widget .black', { color: $.BNZ.standardGradient })
				.replace('#ezone .ezone-content div.widget h1')
			.set('fontFamily', 'Serrano Pro Black Italic')
				.replace('div.widget h2:not(.white)', { color: $.BNZ.standardGradient })
				.replace('div.widget h2.white, div.widget .black-italic, div.widget .footnote-marker')
			.set('fontFamily', 'Serrano Pro Bold')
				.replace('div.widget h3.icon, div.widget .serrano-bold')
			.set('fontFamily', 'Serrano Pro Bold Italic')
				.replace(' #sidebar-right .download-guide h3, div.widget .widget-description, div.widget .bold-italic')
				.replace(' div.widget h3.widget-description', { color: $.BNZ.standardGradient });
	};

	/**
	 * Ajax loader.
	 * 
	 * Requests a feed when it's needed. Subsequent uses of that feed via
	 * the ajaxLoader object will use the retrieved version on the client's
	 * computer.
	 * 
	 * Usage:
	 * 1. create ajaxLoader object
	 * 2. create callback function(s)
	 * 2. [optional] create error handling function
	 * 3. register callback function with jQuery object(s)
	 * 4. call loadFeed()
	 * 
	 * E.G:
	 * 
	 * var loader = new $.BNZ.ajaxLoader('/path/to/feed');
	 * 
	 * var aFunction = new Function(xmlFeed, anObj){ 
	 * 						var textFromFeed = xmlFeed.find('name').text();
	 * 						anObj.find('span.to-fill').html(textFromFeed); 
	 * 					};
	 * var anErrorHandlingFunction = newFunction(anObj){ anObj.hide(); };
	 * var anObj = $('#objectToManipulate');
	 * 
	 * loader.registerCallback(aFunction, anObj, anErrorHandlingFunction);
	 * 
	 * loader.loadFeed();
	 * 
	 */

	$.BNZ.ajaxLoader = function(feedUri) {
		var that = this;
		this._callBack_functions = new Array();
		this._callBack_objects = new Array();
		this._callBack_error_functions = new Array();
		this._xml = null;
		this._feedUri = feedUri;
		
		this.size = function(){ return this._callBack_objects.length; };

		this.registerCallback = function(fctCallback, objJQuery, fctOnError){
			if(that._xml == null){
				that._callBack_functions.push(fctCallback);
				that._callBack_error_functions.push(fctOnError);
				that._callBack_objects.push(objJQuery);
			} else {
				that.processCallback(fctCallback, objJQuery, fctOnError, that._xml);
			}
		};
		
		this.processCallback = function(fctCallback, objJQuery, fctOnError, xml){
			if (typeof xml == 'string' && xml == 'ERROR' && fctOnError != null) {
				fctOnError(objJQuery);
			} else {
				fctCallback($(xml), objJQuery);
			}
		};

		this.loadFeed = function(noDelay){
		
			//       add a small delay if called without arguments.
			//       This would occur when the page is first loaded
			//       and allows a faster time to first impression
			//       on some browsers.
			//       A bit of a hack, but needed to prevent shotgun
			//       surgery.
			var that = this;
			if(noDelay == null){ 
				window.setTimeout(function(){that.loadFeed(true);}, 2000);
				return;
			}
			
			$.ajax({
				type: "GET",
				url: that._feedUri,
				dataType: "xml",
				success: function(xml) {
					that._xml = xml;
					
					var len = that._callBack_objects.length;
					
					for(var i = 0; i < len; i++ ){
						var objJQuery = that._callBack_objects.pop();
						var theFunction = that._callBack_functions.pop();
						var theErrorFunction = that._callBack_error_functions.pop();
						that.processCallback(theFunction, objJQuery, theErrorFunction, that._xml);
					}
					
					$.BNZ.replaceWidgetFonts();

				},
				error: function(){
					that._xml = "ERROR";
					while(that._callBack_objects.length != 0){
						var objJQuery = that._callBack_objects.pop();
						var theFunction = that._callBack_functions.pop();
						var theErrorFunction = that._callBack_error_functions.pop();
						that.processCallback(theFunction, objJQuery, theErrorFunction, that._xml);
					}
					
					$.BNZ.replaceWidgetFonts();
				}
			});
		};
		
	};

	$.fn.randomIdCounter = 0;
	$.fn.assignRandomId = function(){
		$(this).each( function(){

			if($(this).attr('id') != "") return this;
			$(this).attr('id', "RANDOM_ID_" + $.fn.randomIdCounter++);
		});
		return this;
	};
	
	formatFxRate = function (rate) {
		if(rate == "") return "";
		else return rate;	
	};
	
	formatRate = function (rate,format) {
		if(rate == "") return "Error";
		
		if (isNumeric(rate)) return rate + format;
		else return rate;
	};
				
	isNumeric = function(input){
		return (input - 0) == input && input.length > 0;
	};

})(jQuery);

(function($) {
	if($==null) $={}; if($.BNZ==null) $.BNZ={};
	$.BNZ.feeds = {
			heroRates : new $.BNZ.ajaxLoader("/XMLFeed/portal/hero/xml"),
			personalLoanRates : new $.BNZ.ajaxLoader("/XMLFeed/portal/personalloans/xml"),
			homeLoanRates : new $.BNZ.ajaxLoader("/XMLFeed/portal/homeloans/xml"),
			creditCardRates : new $.BNZ.ajaxLoader("/XMLFeed/portal/creditcards/xml"),
			transactionAccountRates : new $.BNZ.ajaxLoader("/XMLFeed/portal/transactionaccount/xml"),
			foreignCurrencyRates : new $.BNZ.ajaxLoader("/XMLFeed/portal/fx/xml"),
			foreignCurrencyCallAccountRates : new $.BNZ.ajaxLoader("/XMLFeed/portal/fccar/xml"),
			termInvestmentRates : new $.BNZ.ajaxLoader("/XMLFeed/portal/terminvestments/xml"),
			savingsCallAccountRates : new $.BNZ.ajaxLoader("/XMLFeed/portal/savingsaccounts/xml"),
			foreignCurrencyTermDepositRates : new $.BNZ.ajaxLoader("/XMLFeed/portal/fcti/xml"),
			foreignCurrencySelectRates : new $.BNZ.ajaxLoader("/XMLFeed/portal/fcs/xml"),
			unitPricesRates : new $.BNZ.ajaxLoader("/XMLFeed/portal/unitprices/xml"),
			transactionAccountRates  : new $.BNZ.ajaxLoader("/XMLFeed/portal/transactionaccounts/xml")
			};
	
	$j(document).ready(function() {
		/******************* ajaxLoader, do your thing! *******************/
		if ($.BNZ.feeds.heroRates.size() > 0) $.BNZ.feeds.heroRates.loadFeed();
		if ($.BNZ.feeds.personalLoanRates.size() > 0) $.BNZ.feeds.personalLoanRates.loadFeed();
		if ($.BNZ.feeds.homeLoanRates.size() > 0) $.BNZ.feeds.homeLoanRates.loadFeed();
		if ($.BNZ.feeds.creditCardRates.size() > 0) $.BNZ.feeds.creditCardRates.loadFeed();
		if ($.BNZ.feeds.transactionAccountRates.size() > 0) $.BNZ.feeds.transactionAccountRates.loadFeed();
		if ($.BNZ.feeds.foreignCurrencyRates.size() > 0) $.BNZ.feeds.foreignCurrencyRates.loadFeed();
		if ($.BNZ.feeds.termInvestmentRates.size() > 0) $.BNZ.feeds.termInvestmentRates.loadFeed();
		if ($.BNZ.feeds.savingsCallAccountRates._callBack_functions.length > 0) $.BNZ.feeds.savingsCallAccountRates.loadFeed();
		if ($.BNZ.feeds.transactionAccountRates._callBack_functions.length > 0) $.BNZ.feeds.transactionAccountRates.loadFeed();		
		if ($.BNZ.feeds.foreignCurrencyCallAccountRates._callBack_functions.length > 0) $.BNZ.feeds.foreignCurrencyCallAccountRates.loadFeed();		
		if ($.BNZ.feeds.foreignCurrencyTermDepositRates._callBack_functions.length > 0) $.BNZ.feeds.foreignCurrencyTermDepositRates.loadFeed();		
		if ($.BNZ.feeds.foreignCurrencySelectRates._callBack_functions.length > 0) $.BNZ.feeds.foreignCurrencySelectRates.loadFeed();	
		if ($.BNZ.feeds.unitPricesRates._callBack_functions.length > 0) $.BNZ.feeds.unitPricesRates.loadFeed();	
		
		/************************ misc housekeeping ***********************/
		$j(".number-entry-only").assignRandomId(); /* needed for autonumeric */
		$j(".number-entry-only").autoNumeric();

	});
	
	
		
	
})(jQuery);

