﻿// SpinSix
// jQuery colorboxForm Plugin
// @version	0.10
// @author	Nicholas Boll
// @copy	Spinsix 2010

; (function($) {

    var 
	defaults = {
	    debug: true,
	    replaceContent: false
	},
	publicMethod,
	colorboxForm = 'colorboxForm';

    publicMethod = $.fn[colorboxForm] = function(options) {
        // check if an element is selected
        if (!this.length) {
            log('colorboxForm: skipping submit process - no element selected');
            return this;
        }

        // check prerequisites
        if (!$.fn.colorbox && !$.fn.ajaxSubmit) {
            log('jquery.colorbox and jquery.form are required');
            return this;
        }

        var opts = $.extend({}, defaults, options);

        this.colorbox(opts);
        $(document).bind('cbox_complete', init);

        return this;
    };

    // parent init function
    function init() {
        $('#cboxContent .close').bind('click', clickClose);
        $('#cboxContent form').bind('submit', submitForm);
        $('#cboxContent input[type=submit]').attr('onclick', ''); // damn WebForms
        //$.fn.colorbox.resize();
    };

    /*
    *	Events
    */

    // close the colorbox
    clickClose = function(e) {
        $.fn.colorbox.close();
        e.preventDefault();
    };

    submitForm = function(e) {
        // find the submit button and make sure it is included
        $submit = $('#cboxContent form').find('input[type=submit],input[type=image]');
        $('#cboxContent form').append('<input type="hidden" name="'+$submit.attr('name')+'" value="'+$submit.val()+'" />');
        $(this).ajaxSubmit({
            beforeSubmit: submitFormBefore,
            success: submitFormSuccess,
            error: submitFormError
        });

        e.preventDefault();
    };

    submitFormBefore = function(formData) {
        $('#cboxLoadingOverlay,#cboxLoadingGraphic').fadeIn();
    };

    submitFormError = function(xhr, status, thrownError) {
        log('Status: ' + xhr.status + ', Error: ' + status);
    };

    submitFormSuccess = function(responseText, statusText) {
        // clean the HTML
        var div = document.createElement('div');
        div.innerHTML = responseText;
        $content = $('#content', div);

        if ($content.length == 0) $content = $(div);
        $wrapper = $('<div />').append($content);

        // check for validation errors or success confirmation
        if ($('.validation-summary-errors,.field-validation-error,.validation-summary-success', $wrapper).length != 0) {
            $('#cboxLoadedContent').html($content.html());
            //$.fn.colorbox.resize();
            $('#cboxLoadingOverlay,#cboxLoadingGraphic').fadeOut();
            $(document).trigger('cbox_complete');
        }
        else if ($('a.redirect', $content).length != 0) {
            window.location = $('a.redirect', $content).attr('href');
        }
        else {
            log(defaults.replaceContent);
            $.fn.colorbox.close();
        }
    };

    // helper fn for console logging
    // set $.fn.colorboxForm.debug to true to enable debug logging
    function log() {
        var args = Array.prototype.slice.call(arguments);
        args[0] = '[jquery.' + colorboxForm + '] ' + args[0];
        if (window.console && window.console.log)
            window.console.log(args);
    };

})(jQuery);
