// GO CORE
(function($) {

    $.fn.FWButton = function(options) {

        // Save the Element
        var element = this;

        var defaults = {
            ajax: false,
            eventListener: "click",
            ajaxOptions: {
                vars: null,
                url: scriptPath,
                method: "POST",
                timeout: 5000,
                cache: false,
                loading_html: '<div class="loading"><h4>Loading...</h4></div>',
                onError: function() {
                    alert("Server Failure!");
                }
            },
            title: false

        };
        options = $.extend(defaults, options);

        // Apply CSS
        $(element).addClass("ui-widget").addClass("ui-button").addClass("ui-state-default").addClass("ui-corner-all");

        // Override Title
        if (options.title) {
            $(element).val(options.title);
        }

        // Use Ajax
        if (options.ajax) {

            // Bind Action
            $(element).live(options.eventListener, (function() {
                // Relay Options
                $(element).FWAjax({
                    vars: options.ajaxOptions.vars,
                    url: options.ajaxOptions.url,
                    method: options.ajaxOptions.method,
                    timeout: options.ajaxOptions.timeout,
                    cache: options.ajaxOptions.cache,
                    loading_html: options.ajaxOptions.loading_html,
                    onError: options.ajaxOptions.onError
                });

            }));
        }

        // Maintain Chain
        return element.each(function() {});

    };

})(jQuery);



// GO CORE
(function($) {

    $.fn.FWAjax = function(options) {

        // Save the Element
        var element = this;

        var defaults = {
            vars: null,
            url: scriptPath,
            method: "POST",
            timeout: 5000,
            cache: false,
            loading_html: '<div class="loading"><h4>Loading...</h4></div>',
			showLoading: true,
            onError: function() {
                alert("Server Failure!");
            }

        };
        options = $.extend(defaults, options);


        // Ajax Wrapper
        $.ajax({
            data: options.vars,
            type: options.method,
            url: options.url,
            timeout: options.timeout,
            cache: options.cache,
            error: function(a, c) {
                options.onError.call(a, c);
            },
            beforeSend: function(r) {
				if (options.showLoading) {
					element.each(function() {
						$(this).html(options.loading_html);
					});
				}
            },
            success: function(r) {
                // Return HTML to ajxelement
                element.each(function() {
                    $(this).html(r);
                });
            }
        });

        // Maintain Chain
        return element.each(function() {});

    };

})(jQuery);


