;(function($) {

$.fn.loyalistttf = function(options, f1, f2) {
	
    if (typeof options == 'function') {
        f2 = f1;
        f1 = options;
        options = {};
    }
    
    return this.each(function() {

        var o = getSettings(this, options);

        // save any  existing id and class
        var id = this.id ? (' id="'+this.id+'"') : '';
        var cls = o.cls ? (' class="' + o.cls + '"') : '';

        // build query string
		var q = '?text=' + encodeURIComponent(o.caption);
		q = q + '&font=' + o.font;
		q = q + '&size=' + o.size;
		if (o.padding)
			q = q + '&padding=' + o.padding;
        if (o.angle)
            q = q + '&angle=' + o.angle;
        if (o.fgColor)
            q = q + '&fgcolor=' + o.fgColor;
        if (o.bgColor)
            q = q + '&bgcolor=' + o.bgColor;
        if (o.width)
            q = q + '&width=' + o.width;
		
		// Build new img element
        var imgt = '<img' + id + cls;
        $.each(o.attrs, function(i, p) {
        	imgt += ' ' + i + '="' + p + '"';
        });
        
        if (this.alt && this.alt != '')
        	imgt += ' alt="' + this.alt.replace(/"/g, '&quot;') + '"';
        else if (o.addAlt)
        	imgt += ' alt="' + o.caption.replace(/"/g, '&quot;').replace(/\|/g, " ") + '"';
        
        if (this.title && this.title != '')
        	imgt += ' title="' + this.title.replace(/"/g, '&quot;') + '"';
        else if (o.addTitle)
        	imgt += ' title="' + o.caption.replace(/"/g, '&quot;').replace(/\|/g, " ") + '"';
        
        imgt += '>';
        
		// Create new img element
        var i = $(imgt);

        // Set img src
	    i.attr('src', o.src + q);

	    // Replace existing element with new img elements
        $(this).after(i).remove();

        // post-conversion callback, passes original element, new div element and fully populated options
        if (typeof f2 == 'function') f2(this, i[0], o);

    });
};

// global defautls; override as needed
$.fn.loyalistttf.defaults = {
    font:			'handsean',
    fsize:			24,
    bgColor:		'ffffff', // background color
    fgColor:		'000000',
    src:			'/wp-content/themes/loyalist/text_to_image.php',
    angle:          0
};


//
//  everything below here is private
//


// flatten all possible options: global defaults, meta, option obj
function getSettings(el, options) {
    options = options || {};
    var $el = $(el);
    var cls = el.className || '';

    // support metadata plugin (v1.0 and v2.0)
    var meta = $.metadata ? $el.metadata() : $.meta ? $el.data() : {};
    meta = meta || {};
    var w = meta.width  || parseInt(((cls.match(/w:(\d+)/)||[])[1]||0));
    var h = meta.height || parseInt(((cls.match(/h:(\d+)/)||[])[1]||0));

    if (w) meta.width  = w;
    if (h) meta.height = h;
    if (cls) meta.cls = cls;

    var a = $.fn.loyalistttf.defaults;
    var b = options;
    var c = meta;

    var p = { params: { bgColor: options.bgColor || $.fn.loyalistttf.defaults.bgColor } };
    var opts = $.extend({}, a, b, c);
    $.each(['attrs'], function(i,o) {
        opts[o] = $.extend({}, p[o] || {}, a[o] || {}, b[o] || {}, c[o] || {});
    });

    if (typeof opts.caption == 'undefined') opts.caption = $el.text();

    // make sure we have a source!
    opts.src = opts.src || $el.attr('href') || $el.attr('src') || 'unknown';
    return opts;
};


})(jQuery);

