DEBUG_MODE = false;


 


//##################### Funcões  ########################
function isset () {

    var a = arguments, l = a.length, i = 0, undef;
    
    if (l === 0) {
        throw new Error('Empty isset'); 
    }
    
    while (i !== l) {
        if (a[i] === undef || a[i] === null) {
            return false; 
        }
        i++; 
    }
    return true;
}        








//##################### Jquery  ########################


    function ajax_logic(url,attr,ajax_loading,form_id,status){
 
            ret_att = ""; 
             

            if(attr!=null){
                ret_att = attr;   
            }
            if(form_id!=null){
                if(attr!=null){
                    ret_att = ret_att+'&'+$('#'+form_id).serialize();
                }
                else {
                    ret_att =  $('#'+form_id).serialize();
                }
            } 
            if(ajax_loading==null){
                ajax_loading = true;
            }
            if(status!=null){
                switch(status){
                    case 0:
                        DEBUG_MODE = true;
                        break;
                }
                  
            }            
            
            if(DEBUG_MODE){
                data_type = "html";
            }
            else {
                data_type = "json";
            }            
   
            $.ajax({
                url: url,
                type: "POST",
                data: ret_att,
                dataType: data_type,
                error: function(){alert('Houveram erros ja requisição ajax');

                    if(DEBUG_MODE){
                            alert("Erro ao requisitar o AJAX.");
                        };
                    }
                    ,
                success: function(ret){
         
                    if(DEBUG_MODE){
                        alert('Conteudo: '+ret);
                        alert('Attr: '+ret_att);
                    }

                    
                    if(isset(ret['load_page'])){
                        if(isset(ret['load_position'])){
                            if(isset(ret['load_action'])){
                                switch(ret['load_action']){
                                    case 'append':
                                         $(ret['load_position']).append(ret['load_page']);
                                        break;
                                    case 'prepend':
                                         $(ret['load_position']).prepend(ret['load_page']);
                                        break;
                                    case 'html':
                                         $(ret['load_position']).html(ret['load_page']);
                                        break;
                                }
                            }
                           
                        }
                        else {
                            $('#cv_ajax_box').html(ret['load_page']);
                        }
                        
                    }
                    if(isset(ret['reload_page'])){
 
                        page_check_and_load();
                    }                        
                    
                    if(isset(ret['clean_form'])){ 
                        limpar_campos(); 
                    }
                    if(isset(ret['redirect'])){
                        if(isset(ret['redirect_timeout'])){
                            
                            setTimeout(redirect_url(ret),ret['redirect_timeout']);
                        }
                        else {
                            redirect_url(ret);
                        }
                      
                    }
                    
                    
                    if(isset(ret['message'])){
 
                        if(isset(ret['status'])){
                            if(ret['status']=="adv"){
                               $('#message').html(ret['message']);
                               $('#message').fadeIn(600,function(){
                                     if(isset(ret['message_timeout'])){
                                  
                          
                                            setTimeout('hide_message()',ret['message_timeout']);
                          
                                       }                                   
                               }); 
                    
                            }
                            else {
                                alert(ret['message']);
                            }
                        }                        
                    }                    
                    if(isset(ret['special_command'])){
                        eval(ret['special_command']);
                    }
                    
                    if(isset(ret['return'])){
                        switch(ret['return']){
                            case 0:
                                return_var = false;
                                break;
                            case 1:
                                return_var = true;
                        }
                    }
                }
            }) 
            
            // Executar imagem de loading
            if(ajax_loading){
                 $("#loading").stop(true, true).bind("ajaxStart", function() {
                    $('#cv-content-loading').hide(0,function(){
                        $('#loading').stop(true, true).fadeIn(300);
                    });            
                    
                })
                $("#loading").stop(true, true).bind("ajaxStop", function() {
                    $('#loading').hide(0,function(){
                        $('#cv-content-loading').stop(true, true).fadeIn(400);
                    });
                    $('#loading').removeClass("show_loading");            
        
                })                           
            }
            else {
                $("#loading").unbind('ajaxStart');  
            }
               
    } 
    
    
    
    
    function page_check_and_load(url){
 
      if(url!=null){
        url = url;
      }
      else {
        url = window.location.href;
      }                  
 
      
        if(url.indexOf('#!')>-1){
  
        // Getting methods & action     
        hash = url.split('#!');   
 

        hash = hash[1].split('/');
        action = hash[0];
        method = hash[1];  
        plus_attrs = hash[2];                    
        attr = "action="+action+"&method="+method+'&'+plus_attrs;                
 
        // Getting the URL    
        url = url.split('#!');                                    
        url = url[0].split('/');
                                
        url = url[(url.length-1)];
        url = url.split('#!');
        if(url.indexOf('?')){
            url = url[0].split('?');
            url = url[0];
        }
        
        folder = url;
        url = url[0];
        
        url = SITE_BASE_URL+"includes/ajax/admin/"+folder+"/"+action+".php";
      
 
         ajax_logic(url,attr); 
        
    }         
    }
    
    
 function redirect_url(ret){
 
    window.location.href = ret['redirect'];
 }     
 function hide_message(){ 
     $('#message').fadeOut(600);
 } 
 function limpar_campos(){

             $("input:not([type=hidden]),textarea").each(function(){
                 if($(this).val()!==''){
                     $(this).val('');
                             
                 }
             })        
             $("select").each(function(){
                $(this).children('option:first').attr('selected','true');
             })   
    
}         
