var scheds = {};

function example() {
    
    var taskStatus = {'Stand':'Stand',
        'Table ronde':'Table-ronde',
        'Atelier':'Atelier', 
        'Concert':'Concert', 
        'Conference':'Conference', 
        'Repas':'Repas'}

    d3.json("le-prog-json", function(error, JsonContent) {
    if (error)
        return console.warn(error);
        $.each( JsonContent, 
            function( EventType, ListEvType ) {
                $.each( ListEvType, 
                    function( Day, ListEvent ) {
                        var placeNames = [];                        
                        for ( var i = 0; i < ListEvent.length; i++) {
                            placeNames.push(ListEvent[i]["placeName"]);
                            // convert json text to js Date
                            ListEvent[i]["startDate"] = new Date(ListEvent[i]["startDate"]);
                            ListEvent[i]["endDate"] = new Date(ListEvent[i]["endDate"]);
                            }
                        //placeNames.push("unk")
                        scheds[EventType+"_"+Day] = new d3.sched("SVG_"+Day)
                                .taskTypes(placeNames)
                                .taskStatus(taskStatus)
                                .tickFormat("%H:%M");
                        //scheds[EventType+"_"+Day].timeDomainMode("fit");
                        //scheds[EventType+"_"+Day].margin(margin);
                        scheds[EventType+"_"+Day](ListEvent);
                        //scheds[EventType+"_"+Day].zoomed();
                        scheds[EventType+"_"+Day].redraw("SVG_"+Day, ListEvent)
                });           
        });
    });

};

// Query Programme
example();

function changeTimeDomain(timeDomainString) {
    this.timeDomainString = timeDomainString;
    switch (timeDomainString) {
    case "1hr":
    format = "%H:%M:%S";
    sched.timeDomain([ d3.time.hour.offset(getEndDate(), -1), getEndDate() ]);
    break;
    case "3hr":
    format = "%H:%M";
    sched.timeDomain([ d3.time.hour.offset(getEndDate(), -3), getEndDate() ]);
    break;

    case "6hr":
    format = "%H:%M";
    sched.timeDomain([ d3.time.hour.offset(getEndDate(), -6), getEndDate() ]);
    break;

    case "1day":
    format = "%H:%M";
    sched.timeDomain([ d3.time.day.offset(getEndDate(), -1), getEndDate() ]);
    break;

    case "1week":
    format = "%a %H:%M";
    sched.timeDomain([ d3.time.day.offset(getEndDate(), -7), getEndDate() ]);
    break;
    default:
    format = "%H:%M"

    }
    sched.tickFormat(format);
    sched.redraw(tasks);
}

function getEndDate() {
    var lastEndDate = Date.now();
    if (tasks.length > 0) {
    lastEndDate = tasks[tasks.length - 1].endDate;
    }

    return lastEndDate;
}

function addTask() {

    var lastEndDate = getEndDate();
    var taskStatusKeys = Object.keys(taskStatus);
    var taskStatusName = taskStatusKeys[Math.floor(Math.random() * taskStatusKeys.length)];
    var taskName = taskNames[Math.floor(Math.random() * taskNames.length)];

    tasks.push({
    "startDate" : d3.time.hour.offset(lastEndDate, Math.ceil(1 * Math.random())),
    "endDate" : d3.time.hour.offset(lastEndDate, (Math.ceil(Math.random() * 3)) + 1),
    "taskName" : taskName,
    "status" : taskStatusName
    });

    changeTimeDomain(timeDomainString);
    
    sched.redraw(tasks);
};

function removeTask() {
    tasks.pop();
    changeTimeDomain(timeDomainString);
    sched.redraw(tasks);
};
