(function(){
	function getScript(url, callback) {
		// borrowed from jQuery ajax
		var head = document.getElementsByTagName("head")[0];
		var script = document.createElement("script");
		var done = false;
		var callback = callback || function(){}
		script.onload = script.onreadystatechange = function(){
			if ( !done && (!this.readyState || this.readyState == "loaded" || this.readyState == "complete") ) {
				done = true;
				callback();
			}
		};	
		script.src = url;
		head.appendChild(script);
	}
	
    var ajaxBaseUrl = (typeof window.propellerBaseUrl == 'undefined') ? 'http://www.propeller.com/ajax/api.syndicate/' : window.propellerBaseUrl;
    var cdnBaseUrl = (typeof window.propellerCdn == 'undefined') ? 'http://www.propeller.com/public/' : window.propellerCdn;
    var $ = (typeof jQuery == 'undefined') ? null : jQuery;
    var autoLoad = typeof propellerAutoLoad == 'undefined' || propellerAutoLoad;
    var initCalled = false;
    var _devId;
    var _a;
   
    var PropellerAPI = window.PropellerAPI = {
        init : function() {
    		if (initCalled) { return; }
    		initCalled = true;
            if (!$) {
				// revert previous script.aculo.us, etc
				$ = jQuery.noConflict(true);
            }
            if (autoLoad) {
            	$(PropellerAPI.loadWidgets);
            }
        },
        loadWidgets : function(devId, a) {
        	if (!initCalled) {
        		_devId = devId;
        		_a = a;
        		setTimeout('PropellerAPI.loadWidgets();', 100);
        		return;
        	}
        	if (_devId && _a) { // override with saved values
        		devId = _devId;
        		a = _a;
        	}
            PropellerAPI.stories = [];
            $('.propeller-story').each(function() {
    		    var story = {
    		        url: this.href || window.location,
    		        title: this.text || '',
    		        description: this.title || '',
    		        tags: this.rel || ''
    		        
    		    };
    		    PropellerAPI.stories.push(story);
    		});
    		
		    // get story badges via JSONP
            data = {
            	format: typeof window.propellerVoteWidgetFormat == 'undefined' ? 'large' : window.propellerVoteWidgetFormat,
        		widgets: $.toJSON(PropellerAPI.stories),
        		auth: typeof window._sns_isLoggedIn != 'undefined' && window._sns_isLoggedIn ? 'aol' : ''
        	}
            if (devId && a) {
            	$.extend(data, { devId:devId, a:a });
            }
		    $.getJSON(ajaxBaseUrl+'get_vote_widgets/?c=?', data, function(result) {
	            $('head').append('<link rel="stylesheet" href="' + cdnBaseUrl + 'css/api-widgets.css" />');
		        for (i=0; i<result.length; i++) {
		            widget = result[i];
		            e = $(".propeller-story[href='"+widget.url+"']")[0]
        		    $(e).replaceWith(widget.html);
    	        }
		    });
        },
        propStory : function(id) {
            var options = {
                dataType: 'json',
                success: function(obj, respStatus, $form) {
                    if (obj.statusCode == 301 || obj.statusCode == 302) {
                        window.location = obj.location;
                        $form.find('a').html(obj.statusText);
                    } else {
                    	$form.replaceWith(obj.formHtml || obj.statusText);
                    }
                    return false;
                }
            }
            e = $('#propeller-story-'+id+' form');
            e.ajaxSubmit(options);
        },
        submitStory : function(id) { 
            e = $('#propeller-story-'+id+' form');
            e[0].submit();
        }
    }

    if ($) {
    	$.ajax({ url: "http://o.aolcdn.com/propeller/js/jqueryplugins/jquery.form.js", 
    		type: "GET", dataType: 'script', cache: true });
    	$.ajax({ url: "http://o.aolcdn.com/propeller/js/json.js",
			type: "GET", success: PropellerAPI.init, dataType: 'script', cache: true });
    } else {
    	getScript("http://o.aolcdn.com/propeller/js/jquery-1.3.2.min.js", function() {
            getScript("http://o.aolcdn.com/propeller/js/jqueryplugins/jquery.form.js");
            getScript("http://o.aolcdn.com/propeller/js/json.js", PropellerAPI.init);
    	});
    }
}());
