/**
 * @author gmanfredi
 * @version 0.97
 * This will replace given inputs (type "button" or "submit") with a fancy button that uses the
 * "sliding door" graphic effect.  Yet,  consider the old buttons still functioning (they're hidden).
 * Here are the interaction between original and fancy buttons:
 *   1) The "secondary" class is carried over from original to fancy for secondary button styling.
 *   2) Clicks on fancy buttons will trigger the click action on original input.  Therefore, you can attach 
 *   	click actions to the original buttons as normal.
 *   3) Disabled states: fancy button will enable/disable whenever orig button is enabled/disabled via polling
 *   4) Note: for IE6, container div may have to be cleared for buttons to show (width: 100%; overflow: hidden).
 * Note: v0.97 added feature: add class "ajax" to the original button to have the disabled state have an ajax spinner
 * Remove class "ajax" from original button to turn off ajax spinner.
 * @param {Object} options Options for creating button
 * @param {Integer} [options.prefix = "fb"] A way to define different types of buttons w class prefixes
 */
jQuery.fn.fancyButton = function(options){
	// Default options
	var opt = $.extend({
		prefix: "fb"
		}, options);
	
    return this.each(function(){
        var btn = $(this);
		
		// if button already fancied, exit
		if(btn.data("fancied")) return;
        
        // if button doesn't have an ID attribute, give it a unique one for polling for disabled state
        if (!btn.attr("id")) 
            btn.attr("id", (new Date()).getTime());
        
        // define new button - for firefox, use <div> (<a> clicks & drags in ff) and use <a> for all else
		var str = ($.browser.mozilla) ? "<div><span></span></div>"
			: "<a href='javascript:void(0)'><span></span></a>";
		
		// create button
        var fBtn = $(str).addClass(opt.prefix + "Primary").addClass("fancyButton")
			.attr("id", opt.prefix + "_" + btn.attr("id"))
			.find("span").html(btn.attr("value")).end();
        
		// if original button classed "secondary", add secondary class
		if(btn.hasClass("secondary")) fBtn.addClass(opt.prefix + "Secondary");
		
        // set ajax action for disabled buttons (original button has "ajax" then ajaxify disabled button)
		var ajaxAction = function(){
			if (btn.hasClass("ajax")) {
				$("span", fBtn).prepend("<img src='images/"+ opt.prefix +"_spinner.gif' alt='' />");
				fBtn.addClass(opt.prefix + "Ajax");
			} else {
				$("span img", fBtn).remove();
				fBtn.removeClass(opt.prefix + "Ajax");
			}
		}
		
		// set state based on current button
        if (btn.attr("disabled")) fBtn.addClass(opt.prefix + "Disabled");
        if (btn.hasClass("ajax")) ajaxAction();
        
        // route click events to old buttons unless disabled
        fBtn.click(function(){
			this.blur(); // for IE6 to blue
            if (!btn.attr("disabled")) 
                btn.click()
        });
		
		// if siblings are before the button are not floated, then float them so they remain in correct order
		// also, compensate for any changes to vertical position after floating.
		btn.prevAll().each(function(){
			var sib = $(this);
			if (sib.css("float") == "none") {
				// if position is not absolute, compensate for any vertical changes from inline to block
				if(sib.css("position") != "absolute"){
					var top = parseFloat(sib.offset().top) - parseFloat(sib.parent().offset().top)
						- parseFloat(sib.parent().css("paddingTop"));
					if($.browser.msie) top -= 3;
					var m = (parseFloat(sib.css("marginTop")) || 0);
					sib.css("marginTop", m + top);
				}
				// float left for all previous siblings
				sib.css("float", "left");
			}
		});
		
        // if buttons right aligned, add fancy button to a right floated div
        if (btn.parent().css("text-align") == "right") {
            var fbDiv = $(".fbContainer", btn.parent());
            if (!fbDiv.length){
                fbDiv = $("<div class='fbContainer'></div>")
					.appendTo(btn.parent())
					.css("float", "right");
			}
			fbDiv.append(fBtn);
        } else {
            // buttons not right aligned, so just add them after normal btn
            btn.after(fBtn).hide();
        }
        
        // hide original button and tag it "fancied" to not prevent accidental duplicate calls
        btn.hide().data("fancied", true);
        
        // now listen/poll for disabled state changes
        var disabled = btn.attr("disabled");
        var intervalID = window.setInterval(function(){
            // if not on screen anymore, stop interval & remove fancy button too if on screen
            if ($("#" + btn.attr("id")).length == 0) {
                window.clearInterval(intervalID);
                fBtn.remove();
            }
            else {
				 // if disabled attribute changed, update fancy button
                if (disabled != btn.attr("disabled")) 
                    (disabled = btn.attr("disabled")) ? fBtn.addClass(opt.prefix + "Disabled") : fBtn.removeClass(opt.prefix + "Disabled");
				
				// if not disabled yet still ajaxed, remove ajaxing
				if (!disabled && btn.hasClass("ajax")) {
					$("span img", fBtn).remove();
					btn.removeClass("ajax");
					fBtn.removeClass(opt.prefix + "Ajax");
				}
				
				// if disabled, check for ajax change
				if (disabled && (fBtn.hasClass(opt.prefix + "Ajax") != btn.hasClass("ajax"))) 
					ajaxAction();
            }
        }, 250);
        
    });
};

