/**
 * AJAX Nette Framwork plugin for jQuery
 *
 * @copyright   Copyright (c) 2009 Jan Marek
 * @license     MIT
 * @link        http://nettephp.com/cs/extras/jquery-ajax
 * @version     0.2
 */

jQuery.extend({
	nette: {
		updateSnippet: function (id, html) {
			$("#" + id).html(html);
          
		},
        
        updateCKForms: function() {
            
            /* js validace */
            $("form").each(function () {
                
                Nette.initForm($(this).get(0));

            });              
            
            /* ck editory */
            if ($('.txtContent')) {
                    $('.txtContent').each(function(index) {

                        var CurrId = $(this).attr('id');
                        var instance = CKEDITOR.instances[CurrId];
                        
                        if(instance)
                        {
                            CKEDITOR.remove(instance);
                        }                        
                        
                       if (!$(this).hasClass('ckeditorTrue')) {
                           $(this).addClass('ckeditorTrue'); 
                           CKEDITOR.replace(CurrId, ckConfig);   
                       }
                        
                    });
            }
            
        },

		success: function (payload) {
			// redirect
			if (payload.redirect) {
				window.location.href = payload.redirect;
				return;
			}

			// snippets
			if (payload.snippets) {
				for (var i in payload.snippets) {
					jQuery.nette.updateSnippet(i, payload.snippets[i]);
            	}
			}
            
            // generate password
            if (payload.password) {
               $("input[name='password']").val(payload.password); 
            }
            
			$.dependentselectbox.hideSubmits();
            jQuery.nette.updateCKForms(); 
            

		}
	}
});

jQuery.ajaxSetup({
	success: jQuery.nette.success,
	dataType: "json"
});

