(function($) {

    $.jsfId = function(id) {
        var escapedId = '#' + id.replace(/([:\[\]\.])/g, '\\$1');
        return escapedId;
    }

    $.fn.labelOver = function(overClass) {
        return this.each(function() {
            var label = jQuery(this);
            var f = label.attr('for');
            if (f) {
                var input = jQuery('#' + f.replace(/([:\[\]\.])/g, '\\$1'));

				// make sure the parent is positioned
				$('#'+f).parent().css("position","relative");

                //var offset = input.offset();
                //label.css({ left: offset.left, top: offset.top });

                this.hide = function() {
                    label.css({ textIndent: -10000 });
                };

                this.show = function() {
                    if (input.val() == '') label.css({ textIndent: 0 });
                };

                // handlers
                input.focus(this.hide);
                input.blur(this.show);
                label.addClass(overClass).click(function() {
                    input.focus();
                });
                if (input.val() != '') this.hide();
            }
        });
    };

    $.fn.refreshLabelOver = function() {
        return this.each(function() {
            var label = jQuery(this);
            var f = label.attr('for');
            if (f) {
                var input = jQuery('#' + f.replace(/([:\[\]\.])/g, '\\$1'));

                this.hide = function() {
                    label.css({ textIndent: -10000 })
                }
                this.show = function() {
                    if (input.val() == '') label.css({ textIndent: 0 })
                }

                if (input.val() != '') this.hide();
            }
        })
    }

    $.fn.disable = function() {
        return this.each(function() {
            if (typeof this.disabled != "undefined") this.disabled = true;
        })
    }

    $.fn.enable = function() {
        return this.each(function() {
            if (typeof this.disabled != "undefined") this.disabled = false;
        })
    }

    $.fn.swfAjaxAction = function(options) {
        var settings = $.extend({
            url: document.URL,
            sourceId: null,
            formId: null,
            processIds: null,
            ajaxSource: null
        }, options || {});

        var formData = $($.jsfId(settings.formId)).serialize();
        var extraData = "&ajaxSource=" + encodeURIComponent(settings.ajaxSource);
        extraData += "&processIds=" + encodeURIComponent(settings.processIds);

        $.ajax({
            url: settings.url,
            type: 'POST',
            data: formData + extraData,
            dataType: 'html',
            beforeSend: function(xhr) {
                xhr.setRequestHeader("Accept", "text/html;type=ajax");
            },
            success: function(data) {
                $(data).each(function() {
                    var content = $(this).html();
                    $($.jsfId(this.id)).enable().html(content);
                });
            },
            error: function(xhr) {
                $("<ul class=\"messages\"><li class=\"error\">"+xhr.status+' '+xhr.statusText+"</li></ul>")
                        .insertBefore($.jsfId(settings.formId));
            }
        });
        return this;
    }
})(jQuery);
$(function() {
	$('label.over').labelOver('over');

	//all hover and click logic for buttons
	$(".rp-button:not(.ui-state-disabled)")
	.hover(
		function(){ 
			$(this).addClass("ui-state-hover"); 
		},
		function(){ 
			$(this).removeClass("ui-state-hover"); 
		}
	)
	.mousedown(function(){
			$(this).parents('.rp-buttonset-single:first').find(".rp-button.ui-state-active").removeClass("ui-state-active");
			if( $(this).is('.ui-state-active.rp-button-toggleable, .rp-buttonset-multi .ui-state-active') ){ $(this).removeClass("ui-state-active"); }
			else { $(this).addClass("ui-state-active"); }	
	})
	.mouseup(function(){
		if(! $(this).is('.rp-button-toggleable, .rp-buttonset-single .rp-button,  .rp-buttonset-multi .rp-button') ){
			$(this).removeClass("ui-state-active");
		}
	});


});
