﻿/*ApuntesUniversidad.com*/


function ApunteSubido() {
    alert('Gracias por ayudar a esta comunidad. Tu crédito a aumentado en 2!!');

}


function string_to_slug(str) {
    str = str.replace(/^\s+|\s+$/g, ''); // trim
    str = str.toLowerCase();

    // remove accents, swap ñ for n, etc
    var from = "àáäâèéëêìíïîòóöôùúüûñç·/_,:;";
    var to = "aaaaeeeeiiiioooouuuunc------";
    for (var i = 0, l = from.length; i < l; i++) {
        str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
    }

    str = str.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
    .replace(/\s+/g, '-') // collapse whitespace and replace by -
    .replace(/-+/g, '-'); // collapse dashes

    return str;
}

jQuery.fn.center = function (absolute) { return this.each(function () { var t = jQuery(this); t.css({ position: absolute ? 'absolute' : 'fixed', left: '50%', top: '50%', zIndex: '99' }).css({ marginLeft: '-' + (t.outerWidth() / 2) + 'px', marginTop: '-' + (t.outerHeight() / 2) + 'px' }); if (absolute) { t.css({ marginTop: parseInt(t.css('marginTop'), 10) + jQuery(window).scrollTop(), marginLeft: parseInt(t.css('marginLeft'), 10) + jQuery(window).scrollLeft() }) } }) };


$(function () {
    $('a[rel="external"]').attr('target', '_blank');
});


///*Privacidad*/
//$(document).ready(function () {
//    $('#Privacidad').each(function () {
//        var $link = $(this);
//        var $dialog = $('<div></div>')
//			.load($link.attr('href') + ' #content')
//			.dialog({
//			    autoOpen: false,
//			    title: $link.attr('title'),
//			    width: 500,
//			    height: 300
//			});

//        $link.click(function () {
//            $dialog.dialog('open');

//            return false;
//        });
//    });
//});

function EnviaApunteForm() {
    Autocompleta("Texbox_Universidades", "/json.asmx/universidades", "", false);
    AutocompletaCarreras("TextBox_Carrera", "/json.asmx/carreras", "", false);
    $('#Texbox_Universidades').Watermark('Introduzca y seleccione una universidad...');
    $('#TextBox_Carrera').Watermark('Introduzca y seleccione una carrera...');
    $('#TextBox_Carrera').attr("disabled", true);
    $('#Nombre_DocumentoTextBox').focus();
    
}

function MuestraDialogoModal(titulo, mensaje) 
{
    var $dialog = $('<div></div>').html(mensaje).dialog({ autoOpen: true, title: titulo, modal: true, resizable: false, width: 460 , height: 250});
}


function MuestraDialogoModalCapa(titulo, Capa) {
    $("#" + Capa).dialog({ autoOpen: true, title: titulo, modal: true, resizable: false, width: 650, height: 250 });
}


    function Change(event, ui) {
        //$("#" + event.target.id + "_Hidden").val(null);
    }

    function selectItem(event, ui) {
        $("#" + event.target.id + "_Hidden").val(ui.item.id);
        $('#TextBox_Carrera').attr("disabled", false);
        $('#TextBox_Carrera').focus();


    }

    function selectItemCarrera(event, ui) {

        $("#" + event.target.id + "_Hidden").val(ui.item.id);
        //$('#TextBox_Carrera').attr("disabled", true);
        $("#TextBox_Carrera").autocomplete("close");
        $("#TextBox_Carrera").blur();
        $("#doc_ObservacionesAutor").focus();

    }

    function Source(request, response, urlRequest, StoredProcedure) {
        $.ajax({
            url: urlRequest,
            data: "{ 'nombre': '" + request.term + "' }",
            dataType: "json",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            dataFilter: function (data) { return data; },
            success: function (data) {
                response($.map(data.d.busqueda, function (item) {
                    $('Texbox_Universidades_Hidden').val(item.Id);
                    return {
                        value: item.Nombre,
                        id: item.Id

                    }
                }))
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(textStatus);
            }
        });
    }


    function SourceExt(request, response, urlRequest, StoredProcedure) {

        id_Universidad = $("#Texbox_Universidades_Hidden").val();
        $.ajax({
            url: urlRequest,
            data: "{ 'nombre': '" + request.term + "', 'id_universidad': '" + id_Universidad + "' }",
            dataType: "json",
            type: "POST",
            contentType: "application/json; charset=utf-8",
            dataFilter: function (data) { return data; },
            success: function (data) {
                response($.map(data.d.busqueda, function (item) {
                    return {
                        value: item.Nombre,
                        id: item.Id
                    }
                }))
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert(textStatus);
            }
        });
    }

    function Autocompleta(Control, Url, Procedimiento, todo) {
        var params = new Object();

        $("#" + Control).autocomplete({
            source: function (request, response) {
                if (todo) {
                    request.term == 'all';
                }
                Source(request, response, Url, Procedimiento)
            },
            minLength: 2,
            delay: 200,
            select: selectItem,
            change: Change
        });
    }

    function AutocompletaCarreras(Control, Url, Procedimiento, todo) {
        var params = new Object();

        $("#" + Control).autocomplete({
            source: function (request, response) {
                if (todo) {
                    request.term == 'all';
                }
                SourceExt(request, response, Url, Procedimiento)
            },
            minLength: 1,
            delay: 200,
            select: selectItemCarrera,
            change: Change
        });
    }





