
(function($) {

    $(function() {
        $('div.poll').makePollAJAX({
            url: '/AJAX/poll.aspx?voting=yes',
            trigger: '.submit'
        });
    });

    jQuery.fn.extend({
	    makePollAJAX: function(options) {
		    // Save options or set to empty, to avoid errors
		    options = options || {};
		    $.pollAJAX.saveOptions(options);
		    // Initialise
		    $(this).each(function()	{
			    // Check to see if there's form fields in there, if so AJAXify
			    if($(this).find('input, .submit').length > 0)	{
				    $.pollAJAX.AJAXify($(this));
			    }
		    });
	    }
    });

    jQuery.extend({
    	
	    pollAJAX: {
    		
		    url: '/AJAX/poll.aspx',
		    loadingOpacity: 0.6,
		    loadingClass: 'ajax_loading_white',
		    pulseColour: '#FFFFAE',
		    pulseLength: 2000,
		    trigger: 'input.button',
    		
		    saveOptions: function(options)	{
			    options.url ? this.url = options.url : null;
			    options.loadingOpacity ? this.loadingOpacity = options.loadingOpacity : null;
			    options.loadingClass ? this.loadingClass = options.loadingClass : null;
			    options.pulseColour ? this.pulseColour = options.pulseColour : null;
			    options.pulseLength ? this.pulseLength = options.pulseLength : null;
			    options.trigger ? this.trigger = options.trigger : null;
		    },
    		
		    AJAXify: function(jQobj)	{
			    // Pointer to this plugin
			    var pointer = this;
			    // Save a copy of the poll
			    pointer.poll = jQobj;
			    // Create a loading overlay
			    pointer.createLoadingOverlay(jQobj);
			    // Stop the submit button from submitting
			    $(jQobj).find(pointer.trigger).click(function()	{
				    pointer.send($(jQobj));
				    return false;
			    });
		    },
    		
		    createLoadingOverlay: function(jQobj)	{
			    // Pointer to this plugin
			    var pointer = this;
			    // Save a new overlay element that's injected into the DOM
			    pointer.loading = $('<div></div>').addClass(pointer.loadingClass).hide().appendTo($(document.body));
			    // Set the dimensions
			    pointer.loading.css({
				    'position': 'absolute',
				    'opacity': pointer.loadingOpacity
			    });
		    },
    		
		    send: function(jQobj)	{
			    // Pointer to this plugin
			    var pointer = this;
			    var formObj = $(jQobj).find('form');
			    // Only do this if one has been checked
			    if($(formObj).find('input[type=radio]:checked').length > 0)	{
				    // First let's build the data object
				    var data = $(formObj).serialize();
				    // Let's move our loading overlay over the top of the poll
				    pointer.loading.css({
					    'width': $(jQobj).outerWidth(),
					    'height': $(jQobj).outerHeight(),
					    'top': $(jQobj).offset().top + (parseInt($(jQobj).css('border-top-width')) || 0),
					    'left': $(jQobj).offset().left + (parseInt($(jQobj).css('border-left-width')) || 0)
				    }).show();
				    $.ajax({
					    url: pointer.url,
					    type: 'POST',
					    data: data,
					    success: function(html)	{
						    pointer.poll.html(html);
						    pointer.loading.css({
							    'height': $(jQobj).height() + parseInt($(jQobj).css('padding-top')) + parseInt($(jQobj).css('padding-bottom')),
							    'background': pointer.pulseColour	
						    }).animate({
							    'opacity': 0
						    }, pointer.pulseLength);
					    },
					    dataType: 'html'
				    });
			    }
		    }
    		
	    }
    	
    });
    
})(jQuery);
