/**
 * jQuery plugin providing the functionality of "load" in a syncronous way.
 *
 * sync-load is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3 of the License.
 *
 * sync-load is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * async-load; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

/**
 * jQuery syncronous load plugin
 * 
 * @author Jakob Westhoff <jakob@westhoffswelt.de>
 */
(function( $, undefined ) {
    /**
     * Plugin private function to load the requested content synchronously and
     * return it as an html string.
     *
     * @param url Url to be loaded including an optional selector
     * @param data (optional) data to be used as GET parameter if it is a
     *             string. If a key/value pair is supplied a post request
     *             with the appropriate mapping will be fired
     */
    var _syncLoad = function( url ) { // ( url, [data] )
        var data = ( typeof arguments[2] === "string" || typeof arguments[2] === "object" )
                 ? ( arguments[2] )
                 : "";

        // GET is the default request type
        var requestType = "GET";

        if ( typeof data === "object" ) {
            // We need to fire are POST request
            requestType = "POST";
            // Encode the data for the request
            data = $.param( data );
        }

        // The url might contain a selector
        var selector = null;
        var cuttingPoint;
        if ( ( cuttingPoint = url.indexOf( " " ) ) >= 0 ) {
            selector = url.slice( cuttingPoint, url.length );
            url = url.slice( 0, cuttingPoint );
        }
        
        // Fire the appropriate request
        var result = $.ajax({
            "url":  url,
            "type": requestType,
            "dataType": "html",
            "data": data,
            "async": false
        }).responseText;

        if ( !selector ) {
            // Just return the retrieved data
            return result;
        }
            
        // Create a dummy element to apply the given selector on
        var selectedElements = $('<div>')
            .html( result )
            .find( selector );

        // Simulate outerHTML
        return $('<div>')
            .append( selectedElements )
            .html();
    }

    // Register a plugin method as well as a static function
    $.fn.syncLoad = function() {
        var closure_arguments = arguments;
        $(this).each( function() {
            $(this).html( 
                _syncLoad.apply( null, closure_arguments )
            );
        });
    }

    $.syncLoad = function() {
        return _syncLoad.apply( null, arguments );
    }

})( jQuery );

