/**
* Version: 1.0 Alpha-1 
* Build Date: 14-Oct-2009
* Copyright (c) 2009, VML. (http://www.vml.com/). All rights reserved.
*/
//requires date.js: http://code.google.com/p/datejs/
//requires jquery.js and a ScriptManager
//usage: $('selector').formatter({type, format, handlerUrl});
//examples: $('.someClass').formatter( {type:'date'});
//examples: $('.someClass').formatter( {type:'number'});
//examples: $('.someClass').formatter( {format:'my special format {0:c}'});
//examples: $('.someClass').formatter( {handlerUrl:'MyHandler.aspx'});

var formatter_defaultDateFormat = "MMM. d, yyyy";
var formatter_cache = new Array();
var formatter_handler_elements = new Array();

(function($) {
    $.fn.formatter = function(p) {

        if (!String.localeFormat) {
            alert("jquery.formatter requires a ScriptManager, consult documentation");
        }

        p = $.extend({
            type: false,
            format: formatter_defaultDateFormat,
            handlerUrl: false,
            cacheResults: true
        }, p);

        return this.each
			(
				function() {
				    var locElement = $(this);
				    var result = null;
				    p.text = escape(locElement.text());
				    var cacheKey = p.text + p.format;

				    if (locElement.attr("type")) p.type = locElement.attr("type");
				    if (locElement.attr("format")) p.format = locElement.attr("format");

				    if (formatter_cache[cacheKey] != null) {
				        result = formatter_cache[cacheKey];
				    }
				    else if (p.handlerUrl) {
				        //ajax call to server
				        //jQuery.get(url, [data], [callback], [type])
				        if (p.cacheResults) {
				            if (formatter_handler_elements[cacheKey] != null) {
				                formatter_handler_elements[cacheKey].push(locElement);
				                return;
				            }
				            formatter_handler_elements[cacheKey] = new Array();
				        }

				        $.get(p.handlerUrl, p, function(data) {
				            //var xmlParser = /result>(.*?)<\/result/;
				            //var html = data.match(xmlParser)[1];
				            locElement.text(data);
				            if (p.cacheResults) {
				                formatter_cache[cacheKey] = data;
				                for (var i = 0; i < formatter_handler_elements[cacheKey].length; i++) {
				                    formatter_handler_elements[cacheKey][i].text(data);
				                }
				                formatter_handler_elements[cacheKey] = null;
				            }
				        });
				        return;
				    }
				    else if (p.type == "date") {
				    var date = Date.parseInvariant(locElement.text(), "MM/dd/yyyy hh:mm:ss tt", "MM/dd/yyyy", "yyyy-MM-dd", "yyyy-MM-ddTHH:mm:ss", "dd/MM/yyyy HH:mm:ss", "dd/MM/yyyy HH.mm.ss", "dd.MM.yyyy HH:mm:ss", "ddd, MMM dd, yyyy H:mm:ss tt", "ddd MMM d yyyy HH:mm:ss zzz", "d");
				        if (date != null) {
				            result = date.getFullYear() > 1970 ? date.localeFormat(p.format) : "";
				        }
				    }
				    else if (p.type == "number" || p.type == "currency") {
				        var number = locElement.text().replace(/[^0-9\.]/, ""); //strip out non numeric components
				        result = (p.type == "number") ? number.localeFormat(p.format) : String.localeFormat("{0:c}", Number.parseInvariant(number));
				    }
				    else {
				        result = String.localeFormat(p.format, locElement.text());
				    }
				    if (result !== null) {
				        locElement.text(result);
				        if (p.cacheResults) formatter_cache[cacheKey] = result;
				    }
				}
			);

    };

})(jQuery);
