$(document).ready(function() {
    var date = new Date();
    var d = date.getDate();
    var m = date.getMonth();
    var y = date.getFullYear();

    var calendar = $('#calendar').fullCalendar({
        editable: true,
        header: {
            left: 'prev,next today',
            center: 'title',
            right: 'month'
        },
   
        events: "http://localhost/eStratopedo/pages/2oGrafeio/fullcalendar/events.php",
       
    
        eventDataTransform: function(eventdata){
            eventdata.title = eventdata.name;            
            eventdata.url = null;
            return eventdata;
        },
        
        dayClick: function(date, allDay, jsEvent, view) {
            //alert('Coordinates: ' + jsEvent.pageX + ',' + jsEvent.pageY);
            //alert('Current view: ' + view.name);
            // change the day's background color just for fun
            //$(this).css('background-color', 'red');
            
            $(function() {
                $( "#dialog-insert" ).dialog({
                    resizable: false,
                    modal: true,
                    buttons: {
                        "Εισαγωγή": function() {
                            var stid = $("#stelexos").val();
                            var ypid = $("#yphresia").val();
                            var title = $("#stelexos").children("option:selected").text() + ' ' + $("#yphresia").children("option:selected").text();
                        
                             var start = $.fullCalendar.formatDate(date, "yyyy-MM-dd");
                            
                            $.ajax({
                                url: 'http://localhost/eStratopedo/pages/2oGrafeio/fullcalendar/add_events.php',
                                //αλλαγη
                                data: 'stid='+ stid+ '&ypid='+ ypid + '&start='+ start,
                                type: "POST",
                                success: function(json) {
                                    alert('Added Successfully');
                                 //   alert (title);
                                }
                            });
                            calendar.fullCalendar('renderEvent',
                            {
                                title: title,
                                ypid: ypid,
                                stid: stid,
                                start: start,
                                allDay: allDay
                            },
                            true // make the event "stick"
                            );
                            $( this ).dialog( "close" );
                        },
                        "Ακύρωση": function() {
                            $( this ).dialog( "close" );
                        }
                    }
                });
            }); 

        },
        
        eventClick: function(event, jsEvent, view) {
             if (event.allDay === 'true') {
                event.allDay = true;
            } else {
                event.allDay = false;
            }
        var start = $.fullCalendar.formatDate(event.start, "yyyy-MM-dd HH:mm:ss");
           $(function() {
                $( "#dialog-delete" ).dialog({
                    resizable: false,
                    modal: true,
                    buttons: {
                        "Διαγραφή": function() {
                                                    
                        
                         
                            
                            $.ajax({
                                url: 'http://localhost/eStratopedo/pages/2oGrafeio/fullcalendar/delete_events.php',
                                //αλλαγη
                                data: 'id='+ event.id ,
                                type: "POST",
                                success: function(json) {
                                   // alert('Deleted Successfully');
                               
                                }
                            });
                            $( this ).dialog( "close" );
                                    alert ("Success");
                              $('#calendar').fullCalendar('removeEvents', event.id);
                        },
                        "Ακύρωση": function() {
                            $( this ).dialog( "close" );
                        }
                    }
                });
            }); 
        },
  

        // Convert the allDay from string to boolean
        eventRender: function(event, element, view) {
            if (event.allDay === 'true') {
                event.allDay = true;
            } else {
                event.allDay = false;
            }
        },
        selectable: true,
        selectHelper: true,
        //        select: function(start,end, allDay) {
        //            var title = prompt('Event Title:');
        //            var ypid = prompt('Event Ypid:');
        //            var url = prompt('Type Event url, if exits:');
        //            if (title) {
        //                var start = $.fullCalendar.formatDate(start, "yyyy-MM-dd HH:mm:ss");
        //                var end = $.fullCalendar.formatDate(end, "yyyy-MM-dd HH:mm:ss");
        //                $.ajax({
        //                    url: 'http://localhost/fullcalendar/add_events.php',
        //                    data: 'title='+ title+ '&ypid='+ ypid + '&start='+ start +'&end='+ end +'&url='+ url ,
        //                    type: "POST",
        //                    success: function(json) {
        //                        alert('Added Successfully');
        //                    }
        //                });
        //                calendar.fullCalendar('renderEvent',
        //                {
        //                    title: title,
        //                    ypid: ypid,
        //                    start: start,
        //                    end: end,
        //                    allDay: allDay
        //                },
        //                true // make the event "stick"
        //                );
        //            }
        //            calendar.fullCalendar('unselect');
        //        },
   
        eventDrop: function(event,delta) {
            var start = $.fullCalendar.formatDate(event.start, "yyyy-MM-dd HH:mm:ss");
            var end = $.fullCalendar.formatDate(event.end, "yyyy-MM-dd HH:mm:ss");
            $.ajax({
                url: 'http://localhost/eStratopedo/pages/2oGrafeio/fullcalendar/update_events.php',

                //allakse to!!!
                data: 'start='+ start +'&id='+ event.id ,
                type: "POST",
                success: function(json) {
                    alert("Updated Successfully");
                }
            });
             calendar.fullCalendar('renderEvent',
                            {
                                title: evevt.title,
                                ypid: ypid,
                                stid: stid,
                                start: delta,
                                allDay: allDay
                            },
                            true // make the event "stick"
                            );
        }
    });
  
});

