
var days = ['Domingo', 'Lunes', 'Martes', 'Miercoles', 'Jueves', 'Viernes', 'Sabado'];

var hexDigits = new Array("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");

function hex(x) {
    return isNaN(x) ? "00" : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];
}

function rgb2hex(rgb) {
    rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
    return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
}

var c1 = rgb2hex($(".calendar-color-ordinario").css("background-color"));

var c2 = rgb2hex($(".calendar-color-reprogramacion").css("background-color"));

var c3 = rgb2hex($(".calendar-color-recuperacion").css("background-color"));


$('#calendar_profesor').fullCalendar({
    defaultView: 'agendaWeek',
    aspectRatio: 1.5,
    allDaySlot:false,
    minTime: "08:00:00",
    maxTime: "24:00:00",
    scrollTime:"23:00:00",
    slotEventOverlap: false,
    slotLabelInterval:'01:00:00',
    contentHeight: 'auto',
    lang: 'es',
    axisFormat: 'HH:mm',
    eventSources: [
        {
            events: function(start, end, timezone, callback) {
                $.ajax({
                    url: urlHP,
                    type: 'GET',
                    success: function(doc) {
                    var events = [];
                    $(doc['Datos']).each(function() {
                        //  var back = ["#ff0000","blue","gray"];
                        // var rand = back[Math.floor(Math.random() * back.length)];
                        events.push({
                            title:$(this).attr('seccion')+' | '+$(this).attr('aula')+' | '+$(this).attr('curso'),
                            start: doc['Semanas'][$(this).attr('dia')]+'T'+$(this).attr('ini'),
                            end: doc['Semanas'][$(this).attr('dia')]+'T'+$(this).attr('fin'),
                            color: c1
                        });
                    });
                    $(doc['Reprog']).each(function() {
                        var f = $(this).attr('fecha_asignada');

                        if($(this).attr('fecha_asignada') != null)
                        {
                            var d = new Date(f);
                            var dayName = days[(d.getDay() + 1)];
                            var vali = false;

                            $(doc['Semanas']).each(function() {
                                var dateAr = f.split('-');
                                var newDate = dateAr[2] + '-' + dateAr[1] + '-' + dateAr[0];
                                var datEr = doc['Semanas'][dayName];
                                datEr = datEr.split('-');
                                datEr = datEr[2] + '-' + datEr[1] + '-' + datEr[0];
                                if(datEr == newDate)
                                    vali = true;
                            });

                            if(vali)
                            {
                                events.push({
                                    title: $(this).attr('seccion')+' | '+$(this).attr('aula')+' | '+$(this).attr('curso'),
                                    start: doc['Semanas'][dayName]+'T'+$(this).attr('ini'),
                                    end: doc['Semanas'][dayName]+'T'+$(this).attr('fin'),
                                    color: c2
                                });
                            }

                            vali = false;
                        }



                    });
                    $(doc['Recup']).each(function() {
                        var f = $(this).attr('fecha_asignada');


                        if($(this).attr('fecha_asignada') != null)
                        {
                            var d = new Date(f);
                            var dayName = days[(d.getDay() + 1)];
                            var vali = false;

                            $(doc['Semanas']).each(function() {
                                var dateAr = f.split('-');
                                var newDate = dateAr[2] + '-' + dateAr[1] + '-' + dateAr[0];
                                var datEr = doc['Semanas'][dayName];
                                datEr = datEr.split('-');
                                datEr = datEr[2] + '-' + datEr[1] + '-' + datEr[0];
                                if(datEr == newDate)
                                    vali = true;
                            });

                        }

                        if(vali)
                        {
                            events.push({
                                title: $(this).attr('seccion')+' | '+$(this).attr('aula')+' | '+$(this).attr('curso'),
                                start: doc['Semanas'][dayName]+'T'+$(this).attr('ini'),
                                end: doc['Semanas'][dayName]+'T'+$(this).attr('fin'),
                                color: c3
                            });
                        }

                        vali = false;
                    });
                    callback(events);
                }
            });
            }
        }
    ]
});


$('#calendar_alumno').fullCalendar({
    defaultView: 'agendaWeek',
    aspectRatio: 1.5,
    allDaySlot:false,
    minTime: "06:00:00",
    maxTime: "24:00:00",
    scrollTime:"23:00:00",
    slotEventOverlap: false,
    slotLabelInterval:'01:00:00',
    contentHeight: 'auto',
    lang: 'es',
    axisFormat: 'HH:mm',
    eventSources: [
        {
            events: function(start, end, timezone, callback) {
                $.ajax({
                    url: urlHA,
                    type: 'GET',
                    success: function(doc) {

                    var events = [];
                        $(doc['Datos']).each(function() {
                            //  var back = ["#ff0000","blue","gray"];
                            // var rand = back[Math.floor(Math.random() * back.length)];
                            events.push({
                                title:$(this).attr('seccion')+' | '+$(this).attr('aula')+' | '+$(this).attr('curso'),
                                start: doc['Semanas'][$(this).attr('dia')]+'T'+$(this).attr('ini'),
                                end: doc['Semanas'][$(this).attr('dia')]+'T'+$(this).attr('fin'),
                                color: c1
                            });
                        });
                        $(doc['Reprog']).each(function() {
                            var f = $(this).attr('fecha_asignada');
                            var d = new Date(f);
                            var dayName = days[(d.getDay() + 1)];
                            var vali = false;

                            $(doc['Semanas']).each(function() {
                                var dateAr = f.split('-');
                                var newDate = dateAr[2] + '-' + dateAr[1] + '-' + dateAr[0];
                                var datEr = doc['Semanas'][dayName];
                                datEr = datEr.split('-');
                                datEr = datEr[2] + '-' + datEr[1] + '-' + datEr[0];
                                if(datEr == newDate)
                                    vali = true;
                            });

                            if(vali)
                            {
                                events.push({
                                    title: $(this).attr('seccion')+' | '+$(this).attr('aula')+' | '+$(this).attr('curso'),
                                    start: doc['Semanas'][dayName]+'T'+$(this).attr('ini'),
                                    end: doc['Semanas'][dayName]+'T'+$(this).attr('fin'),
                                    color: c2
                                });
                            }

                            vali = false;

                        });
                        $(doc['Recup']).each(function() {
                            var f = $(this).attr('fecha_asignada');
                            var d = new Date(f);
                            var dayName = days[(d.getDay() + 1)];
                            var vali = false;

                            $(doc['Semanas']).each(function() {
                                var dateAr = f.split('-');
                                var newDate = dateAr[2] + '-' + dateAr[1] + '-' + dateAr[0];
                                var datEr = doc['Semanas'][dayName];
                                datEr = datEr.split('-');
                                datEr = datEr[2] + '-' + datEr[1] + '-' + datEr[0];
                                if(datEr == newDate)
                                    vali = true;
                            });

                            if(vali)
                            {
                                events.push({
                                    title: $(this).attr('seccion')+' | '+$(this).attr('aula')+' | '+$(this).attr('curso'),
                                    start: doc['Semanas'][dayName]+'T'+$(this).attr('ini'),
                                    end: doc['Semanas'][dayName]+'T'+$(this).attr('fin'),
                                    color: c3
                                });
                            }

                            vali = false;
                        });
                    callback(events);
                }
            });
            }
        }
    ]
});