/**
* Plataforma Linker
* ---
* Funciones y extensiones para jQuery
* @uses Libreria jQuery 
* ---
*/
jQuery.expr[':'].hiddenByParent = function(a) { 
   return jQuery(a).is(':hidden') && jQuery(a).css('display') != 'none'; 
};
/**
* Habilita los links para poder cargar html dentro de un contenedor
* por medio de ajax.
*/
jQuery.fn.ajaxLink=function(){
    $(this).each(function(){
        if($(this).data("parsed")==true)
            return;
        if($(this).attr("rel")!="ajax")
            return;
        // LLamada ajax
        $(this).click(function(){
            var target=$(this).attr("tar");
            if(target=="" || target==undefined || target==null){
                target="#tab_cont_ajax";
            }
            if(target=="#tab_cont_ajax"){
                $("#tab_main_cont").hide(); // Ocultamos tabs normales
            }
            if($(target).length>0){
                showLoader();
                var self=this;
                var url=$(this).attr("href");
                $(target).html("");
                $(target).load(url,function(){
                    $(target).data("url",url);
                    if($(self).attr("callback")!="" && $(self).attr("callback")!=undefined){
                        eval($(self).attr("callback"));
                    }
                    if(target=="#tab_cont_ajax"){   
                        $("#tab_cont_ajax").css("display","block");
                    }
                    if($(this).attr("callback")!=undefined && $(this).attr("callback")!=""){
                        eval($(this).attr("callback"));
                    }
                    if($(this).attr("update")!="false"){
                        docReady();
                    }
                    hideLoader();
                });
            }
            return false;
        });
        // Si ya parseamos el anchor, lo apuntamos
        if($(this).data("parsed",true));
    });
}
/**
* Recarga el contenedor cuyo contenido fue cargado mediante un link de la función
* ajaxLink()
*/
jQuery.fn.reload=function(){
    var callback=arguments[0];
    var target=$(this[0]);
    if(target.data("url")==undefined || target.data("url")=="")
        return false;
    
    showLoader();
    target.load(target.data("url"),function(){
        hideLoader();
        if(typeof callback=="function")
            callback();
        docReady();
    });
    return;    
}
/**
* Funcion para capturar los formularios por GET
* y procesar las variables para enviarlas en formato
* correcto de url phplus.
* También verifica que el formato de cada input sea el correcto en caso 
* de que tenga una regla de validación desde el HTML, y tambien hablilita
* el envio de formularios por medio de ajax
*/
jQuery.fn.richForms=function(){
    $(this).each(function(){
        if($(this).data("rich")==true)
                return;
        $(this).data("rich",true);
        $(this).submit(function(){
            var obj=$(this);
            var uri="";
            var ret=true;
             $(this).find("input[type=text],input[type=password],textarea,input[type=hidden],select").each(function(i){
                if($(this).attr("type")=="hidden"){
                    uri+=this.name+','+this.value+"/";
                    return true;
                }
                // Si tiene alt y el valor es alt, esta vacio
                if($(this).val()==$(this).attr("alt")){
                    $(this).val("");
                }
                if($(this).is(':hidden') || $(this).is(':hiddenByParent'))
                    return;
                if(($(this).attr('validate')=="no-empty" || $(this).attr('validate')=="") && this.value==""){
                    if($(this).attr('error')!="" && $(this).attr('error')!=null){
                        displayError($(this).attr('error'));
                    }else{
                        displayError("Favor de llenar el campo: "+$(this).attr('name'));
                    }
                    this.focus();
                    ret=false;
                    return false;
                }else if($(this).attr('validate')=="email"){
                    // Validamos que sea email
                    var emailre=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
                    if(!emailre.test($(this).attr('value'))){
                        if($(this).attr('error')!='' && $(this).attr('error')!=null){
                            displayError($(this).attr('error'));
                        }else{
                            displayError("Favor de introducir una dirección de correo válida");
                        }
                        this.focus();
                        ret=false;
                        return false;
                    }
                }else if($(this).attr('validate')=="number"){
                    // Validamos campo de tipo numerico
                    if(isNaN($(this).attr('value')) || $(this).attr('value')==""){
                        if($(this).attr('error')!='' && $(this).attr('error')!=null){
                            displayError($(this).attr('error'));
                        }else{
                            displayError("Favor de introducir un valor numérico");
                        }
                        $(this).attr('value',"");
                        this.focus();
                        ret=false;
                        return false;
                    }
                }else if($(this).attr('validate')=="custom" || $(this).attr('validate')=="regex"){
                    // Validamos la RegEx
                    if($(this).attr('regex')!="" && $(this).attr('regex')!=null){
                        var filter=eval("/"+$(this).attr('regex')+"/");
                        if(!filter.test($(this).attr('value'))){
                            if($(this).attr('error')!='' && $(this).attr('error')!=null){
                                displayError($(this).attr('error'));
                            }else{
                                displayError("Favor de introducir un valor numérico");
                            }
                            this.focus();
                            ret=false;
                            return false;
                        }
                    }
                }
                uri+=this.name+','+this.value+"/";
            });
            if(ret==false)
                return false;
            if(ret==true){
                // Envio mediante AJAX   
                if($(this).attr("rel")=="ajax"){
                    var action=$(this).attr("action");
                    if(action=="" || action==undefined)
                        action=document.location.href;
                    if($(this).attr("enctype")=="multipart/form-data"){
                        // Tenemos archivos adjuntos
                        // Creamos iframe
                        showLoader();
                        var callback=$(this).attr("callback");
                        if(callback==undefined || callback=="")
                            callback="popMsg('Registro guardado correctamente','Guardar')";
                        var fname="tmp_ajax_form";
                        if($("#"+fname).length==0){
                            $("<iframe></iframe>")
                                .hide()
                                .attr("src","about:blank")
                                .attr("id","tmp_ajax_file_uploader")
                                .attr("name",fname)
                                .attr("id",fname)
                                .attr("onload",callback+";hideLoader(); docReady(); parent.document.close();")
                                .appendTo("body");
                        }
                        $(this).attr("target",fname);
                        return true;
                    }else{
                        var self=this;
                        showLoader();
                        $.post(action,$(this).serialize(),function(){
                            if($(self).attr("callback")!=undefined && $(self).attr("callback")!="")
                                eval($(self).attr("callback"));
                            else
                                popMsg("Registro guardado","Información");    
                            hideLoader();
                        });
                        return false;
                    }
                }
            
                if($(this).attr("method").toLowerCase()=="post")
                    return true;
                if(ret)
                    window.location.href=uri;
                return false;
            }
        });
    });
}
/**
* Convierte el input en checkbox
*/
jQuery.fn.toCheckbox=function(){
    $(this).each(function(i){
        var hiddentmp = $(document.createElement("input"));
            $(hiddentmp).attr("value","0");
            $(hiddentmp).attr("type","hidden");
            $(hiddentmp).attr("name",$(this).attr("name"));  
        var checktmp = $(document.createElement("input"));
            $(checktmp).attr("value",$(this).attr("default"));
            $(checktmp).attr("type","checkbox");
            $(checktmp).attr("name",$(this).attr("name"));
        if($(this).attr("value") == $(this).attr("default"))
            $(checktmp).attr("checked","checked");
            
        var contenedor = $(this).parent();
            $(contenedor).append($(hiddentmp));
            $(contenedor).append($(checktmp));
            $(this).remove();
    });
}
/**
* Establece el formato de entrada para un campo de texto
* en formato de regex.
* Se puede enviar el regex en el atributo "mask" del objeto
* o bien se puede enviar como parametro al momento de instanciar la funcion
* puede ser un objeto RegEx o un string.
*/
jQuery.fn.mask=function(){
    var args=arguments[0] || null;
    $(this).each(function(){
        if($(this).data("masked")==true)
            return;
            
        if($(this).attr("mask")!=undefined && $(this).attr("mask")!="")
            var regex=new RegExp("^"+$(this).attr("mask")+"$","g");
        else
            var regex=null;
        if(regex==null && args!=null){
            if(typeof args=="object"){
                regex=args;
            }else{
                regex=new RegExp(args,"g");
            }
        }
        if(regex==null)
            return false;
        $(this).data("masked",true);
        $(this).keypress(function(event){
            if(event.charCode==0)
                return true;
            if(this.selectionStart!=this.selectionEnd)
                $(this).val("");
            var value=$(this).val()+String.fromCharCode(event.charCode);
            if(event.charCode==0)
                return true;
            var sel=selText();

            if(sel.length>0){
                $(this).val("");
            }
            if(!regex.test(value))
                return false; 
        })
    });
}
/**
* Agrega imagen de icon dependiendo la extension
*/
jQuery.fn.fileIcon=function(){
    $(this).each(function(){
        if(this.hasIcon!=true){
            var file=$(this).text();
            file=file.split('.');
            var ext=file[file.length-1];
            var icon='';
            switch(ext.toLowerCase()){
                case 'gif':
                case 'jpg':
                case 'png':
                case 'jpeg':
                    icon='image';
                    break;
                case 'doc':
                case 'docx':
                    icon='word';
                    break;
                case 'xls':
                case 'xlsx':
                    icon='excel';
                    break;
                case 'ppt':
                case 'pptx':
                    icon='power_point';
                    break;
                case 'mpg':
                case 'avi':
                    icon='video';
                    break;   
                case 'pdf':
                    icon='pdf';
                    break;
                case 'zip':
                case 'rar':
                    icon='zip';
                    break;
                default:
                    if($(this).hasClass("folder")){
                        icon="folder-closed";
                    }else
                        icon='unknown';
                    break;
            }
            icon=PATH_IMG+'icon_'+icon+'.png';
            icon_tag=$('<img />');
            $(icon_tag).attr("src",icon);
            $(icon_tag).css("margin-right","3px");
            //$(this).prepend(icon_tag);
            $(this).css({
                'padding-left':'23px',
                'background':'transparent url('+icon+') no-repeat 0px center'
            });
            this.hasIcon=true;
        }
    })    
}
/**
* Misma que load() pero el envio se hace por post en vez de por get
*/
jQuery.fn.loadPost=function(){
    var url=arguments[0] || null;
    var params=arguments[1] || {};
    var callback=arguments[2] || false;
    if(callback!=false)
        if(typeof callback!="function")
            callback=false;
            
    if(url==null)
        return false;
    if(this.length==0)
        return false;
    $(this).each(function(){
        var obj=this;
        $.post(url,params,function(data){
            $(obj).html(data); 
            if(callback!=false)
                callback(data);
        });
    })
}
/**
* Funciones de eventos para Mobile Safari
*/
jQuery.fn.touchstart=function(){
    var callback=arguments[0];
    $(this).each(function(){
        this.addEventListener("touchstart",function(event){
            if(typeof callback=="function")
                callback(event);  
        },false);  
    })
    return;    
}
jQuery.fn.touchmove=function(){
    var callback=arguments[0];
    $(this).each(function(){
        this.addEventListener("touchmove",function(event){
            if(typeof callback=="function")
                callback(event);  
        },false);  
    })
    return;    
}
jQuery.fn.touchend=function(){
    var callback=arguments[0];
    $(this).each(function(){
        this.addEventListener("touchend",function(event){
            if(typeof callback=="function")
                callback(event);  
        },false);  
    })
    return;     
}

