﻿jQuery.AjaxSubmitAction = {
    
    lock: function(e){
        jQuery(e.AjaxActionCfg.targets).each(
            function(index,t){
                var target = jQuery('#' + t);
                if(!target || target.length == 0) return;
	            jQuery.AjaxLockRegion.lock(target);
            }
        );
    },
    
    unlock: function(e){
        jQuery(e.AjaxActionCfg.targets).each(
            function(index,t){
                var target = jQuery('#' + t);
                if(!target || target.length == 0) return;
	            jQuery.AjaxLockRegion.unlock(target);
            }
        );
    },
	build: function(conf) {
	    var updateRegionCall = null; // This will be the function call made by the action or the last element in this build process
	    var parent = this;
	    
	    
	    
	    this.each(
	        function(index,a) {
	            var anchor = jQuery(a);
	            
	                    anchor.AjaxActionCfg = {
	                        form : conf.form,
	                        url : conf.url ? conf.url : anchor.attr('href'),
	                        targets : conf.targets,
	                        name: conf.name,
	                        selectionCriteria: conf.selectionCriteria,
	                        trigger: conf.trigger,
	                        region: conf.region,
	                        allowAction: conf.allowAction,
	                        callback: conf.callback,
	                        callwith: conf.callwith,
	                        madeAjaxCall: false,
	                        doLock: conf.doLock,
	                        ajax_in_progress : false,
	                        update_function_call : null
	                    }
	                    
	                   
        	            
	                    if(!anchor.AjaxActionCfg.form && !anchor.AjaxActionCfg.url){
	                        alert("Error: You must either specify a form or a url");
	                        return;
	                    }
	            
	            
	                    anchor.AjaxActionCfg.update_function_call = function(){
	                        if(!anchor.AjaxActionCfg.ajax_in_progress){
	                            if(anchor.AjaxActionCfg.callwith){anchor.AjaxActionCfg.callwith.call();}
	                            anchor.AjaxActionCfg.ajax_in_progress = true;
        	                    
                                if(anchor.AjaxActionCfg.doLock){
                                    jQuery.AjaxSubmitAction.lock(anchor);
                                }
                                mycallback = function(obj){
        	                        
                                anchor.AjaxActionCfg.ajax_in_progress = false;
                                if(anchor.AjaxActionCfg.callback){anchor.AjaxActionCfg.callback.call();}
                                if(anchor.AjaxActionCfg.doLock){
                                    jQuery.AjaxSubmitAction.unlock(anchor);
                                }
            	                        
                                if(anchor.AjaxActionCfg.selectionCriteria){
                                    $(anchor.AjaxActionCfg.selectionCriteria).AjaxSubmitAction({
                                            region: anchor.AjaxActionCfg.region,
                                            trigger: anchor.AjaxActionCfg.trigger,
                                            selectionCriteria: anchor.AjaxActionCfg.selectionCriteria,
                                            url: anchor.AjaxActionCfg.url,
                                            form: anchor.AjaxActionCfg.form,
                                            doLock: anchor.AjaxActionCfg.doLock,
                                            allowAction: anchor.AjaxActionCfg.allowAction,
                                            targets: anchor.AjaxActionCfg.targets
                                     });
                                 }
                            return obj;
                        }

	                    if(anchor.AjaxActionCfg.form){
                	        jQuery('#' + anchor.AjaxActionCfg.form).mvcAjaxForm('#'+anchor.AjaxActionCfg.region,mycallback,anchor.AjaxActionCfg.targets,anchor.AjaxActionCfg.url);
	                    }else{
	                        jQuery.mvc.request(anchor.AjaxActionCfg.url, '#'+anchor.AjaxActionCfg.region, "POST", false, null, mycallback,anchor.AjaxActionCfg.targets);	                        
	                    }
            	    }
	            }
	            
	            jQuery(a).unbind();
	            jQuery.event.add(a,anchor.AjaxActionCfg.trigger,function(e) {
	                if(!anchor.AjaxActionCfg.allowAction){e.preventDefault();}
	                /*var mycallback = null;*/
	                anchor.AjaxActionCfg.update_function_call.call();
                });
                
	        }
	    );
	    return parent;
	}
}





jQuery.AjaxLockRegion = {
    lock: function(lockableRegion){
    
        
        lockableRegion = jQuery(lockableRegion)[0];
        var position = jQuery.iUtil.getPosition(lockableRegion);
        var size = jQuery.iUtil.getSize(lockableRegion);
        var win_width = size.wb;
        var scrollToLeft=position.x;
        var win_height = size.hb;
        var scrollToBottom=position.y;
        
        if(win_width == 0 || win_height == 0){return;}
        
        var lockableRegionId = jQuery.AjaxLockRegion.getId(lockableRegion);  
        $('body').append("<div id='"+lockableRegionId+"' class='ajax_lock_region' style='padding-top:0px;border:1px solid black;background-color:#FFF;position: absolute;z-index:1000;display:block;' ></div>");
        $('#' + lockableRegionId).css({width:win_width,height:win_height,left:scrollToLeft+'px',top:scrollToBottom+'px'});
    },
    unlock: function(lockableRegion) 
    {
        var lockableRegionId = jQuery.AjaxLockRegion.getId(lockableRegion);
        $('#' + lockableRegionId).remove();
        
    },
    getId: function(lockableRegion){
        var lockableRegionId = jQuery(lockableRegion).attr('id');
        if(!lockableRegionId){
            alert("lockable region must have an Id the region with content" + lockableRegion.html() + " has no id");
        }
        return  lockableRegionId + '_ak_modal_div';
    }
}




//Extends jQuery to add the plugin.
jQuery.fn.extend(
	{
		AjaxSubmitAction : jQuery.AjaxSubmitAction.build,
		AjaxLockRegion : jQuery.AjaxLockRegion.build
	}
);
