(function ($) {
    jQuery.fn.autotarget = function (options) {
        var opts = $.extend({},
        $.fn.autotarget.defaults, options);
        var $this = $(this);
        $this.data('opts', opts);
        return this.each(function (i, e) {
            if (!jQuery().ajaxSubmit) {
                _load_missing_js("/js/jquery.form.js");
            }
            var $t = jQuery(this);
            if (jQuery().ajaxSubmit) {
                
                jQuery(e).unbind('submit.autotarget');
                jQuery(e).bind('submit.autotarget', function (event) {
                    event.preventDefault();
                    var $test = $t.attr('id').split('_', 12);
					
                    var $UniqID = $test[$test.length - 1];
                    var target_holder = jQuery('#' + opts.target_prefix + $UniqID);
           //console.log(target_holder);
                    $t.find(":submit").attr('disabled', 'disabled');
                    $(this).ajaxSubmit({
                        target: target_holder,
                        success: function (responseText, statusText) { 
								
						//						alert('status: ' + statusText + '\n\nresponseText: \n' + responseText);
                            $t.find(":submit").removeAttr("disabled");
                            if (!jQuery().jumpmenu) {
                                debug('jumpmenu Missing.');
                                _load_missing_js("/js/jquery.jumpmenu.js");
                            } else {
									//debug('jumpmenu Is Here.');
									}					
                            $('.vocal_transcript').jumpmenu();
                        }
                    });
                    return false;
                });
            } else {
                //debug('ajaxSubmit Missing.');
            }
        })
    } //defaults
    jQuery.fn.autotarget.defaults = {
        target_prefix: 'holder_',
        hideLabel: 'hide'
    }; //private part


    function _bussy_box($obj) {
        $obj.html('<h2 style="color:green;text-align:center;">Processing<img src="../images/indicator.gif" alt="" border="0"></h2>');
    }

    function _load_missing_js($file_name) {
        $.getScript($file_name, function () {
            return true;
        });
    }

    function debug($obj) {
        if (window.console && window.console.log) {
            window.console.log('sendinfo debug: Start');
            window.console.log($obj);
            window.console.log('sendinfo debug: End');
        } else alert($obj);
    };
})(jQuery);
