﻿var options = { path: '/', expires: 3650 };
function loadRouteConfigurations() {
    var routes = $.cookie('TravelTimeRoutes')
    if (routes == null) {
        $.cookie("TravelTimeRoutes", "", options);
    }
    $.ajax({
    method: "get", url: "/common/xml/routes.xml", 
    dataType: "xml",
    beforeSend: function() { /*$("#loading").show("fast"); */}, //show loading just when link is clicked
    complete: function() { /*$("#loading").hide("fast");*/ }, //stop showing loading when the process is complete
    success: responseAvailableRoutes
    });
}

function responseAvailableRoutes(obj) {
    //$(".content").show("slow"); //animation
    //   $(".content").html(html);

    if (obj == null || typeof (obj) != "object" || obj.firstChild == null) {
        $("#divRoutes").html = "<i>Invalid data.</i>";
        return;
    }

    var domdata = obj; // legacy code

    // Get a list of the <Route> element nodes in the file
    itemList = domdata.getElementsByTagName("Route");

    var routes = $.cookie('TravelTimeRoutes').split(",");
    var arrayLocation = new Array(routes.length - 1);
    var html = "";
    // Loop through all <Route> nodes
    for (var i = 0; i < itemList.length; i++) {
        // For each <Route> node, get child nodes.
        var nodeList = itemList.item(i).childNodes;

        // Loop through child nodes. Extract data from the text nodes that are
        // the children of the associated name, price, and calories element nodes.
        var id = "";
        var name = "";
        for (var j = 0; j < nodeList.length; j++) {
            var node = nodeList.item(j);

            if (node.nodeName == "ID") {
                id = node.firstChild.nodeValue;
            }
            if (node.nodeName == "DisplayName") {
                name = node.firstChild.nodeValue;
            }
        }

        var selectedIndex = -1;
        for (var q = 0; q < routes.length; q++) {
            if (routes[q] == id) {
                selectedIndex = q;
                q = routes.length;
            }
        }
        if (selectedIndex > -1) {

            if (selectedIndex < 1 && routes.length > 1) {
                arrayLocation[selectedIndex] = "\n<li><a href=\"javascript:;\" title=\"move up\" class=\"buttonSpace\">&nbsp;</a><a href=\"javascript:MoveDown(" + id + ");\" title=\"move down\" class=\"buttonDownArrow\">&nbsp;</a><span class=\"displayActive\"><input class=\"check\" type=\"checkbox\" id=\"ck" + id + "\" name=\"availroutes\" value=\"" + id + "\" checked=\"checked\" onchange=\"CheckBoxChange(this);\" /><label for=\"ck" + id + "\" onclick=\"$('#ck" + id + "').checked=false;\">" + name + "</label></span><div class=\"cb\"></div></li>";
            }
            else if (selectedIndex >= routes.length - 1 && routes.length > 1) {
            arrayLocation[selectedIndex] = "\n<li><a href=\"javascript:MoveUp(" + id + ");\" title=\"move up\" class=\"buttonUpArrow\">&nbsp;</a><a href=\"javascript:;\" title=\"move down\" class=\"buttonSpace\">&nbsp;</a><span class=\"displayActive\"><input class=\"check\" type=\"checkbox\" id=\"ck" + id + "\" name=\"availroutes\" value=\"" + id + "\" checked=\"checked\" onchange=\"CheckBoxChange(this);\" /><label for=\"ck" + id + "\" onclick=\"$('#ck" + id + "').checked=false;\">" + name + "</label></span><div class=\"cb\"></div></li>";
            }
            else if (routes.length > 1) {
            arrayLocation[selectedIndex] = "\n<li><a href=\"javascript:MoveUp(" + id + ");\" title=\"move up\" class=\"buttonUpArrow\">&nbsp;</a><a href=\"javascript:MoveDown(" + id + ");\" title=\"move down\" class=\"buttonDownArrow\">&nbsp;</a><span class=\"displayActive\"><input class=\"check\" type=\"checkbox\" id=\"ck" + id + "\" name=\"availroutes\" value=\"" + id + "\" checked=\"checked\" onchange=\"CheckBoxChange(this);\" /><label for=\"ck" + id + "\" onclick=\"$('#ck" + id + "').checked=false;\">" + name + "</label></span><div class=\"cb\"></div></li>";
            }
            else {
                arrayLocation[selectedIndex] = "\n<li><a href=\"javascript:;\" title=\"move up\" class=\"buttonSpace\">&nbsp;</a><a href=\"javascript:;\" title=\"move down\" class=\"buttonSpace\">&nbsp;</a><span class=\"displayActive\"><input class=\"check\" type=\"checkbox\" id=\"ck" + id + "\" name=\"availroutes\" value=\"" + id + "\" checked=\"checked\" onchange=\"CheckBoxChange(this);\" /><label for=\"ck" + id + "\" onclick=\"$('#ck" + id + "').checked=false;\">" + name + "</label></span><div class=\"cb\"></div></li>";
            }
        }
        else {
            html += "\n<li><a href=\"javascript:;\" title=\"move up\" class=\"buttonSpace\">&nbsp;</a><a href=\"javascript:;\" title=\"move down\" class=\"buttonSpace\">&nbsp;</a><span class=\"displayInactive\"><input class=\"check\" type=\"checkbox\" id=\"ck" + id + "\" name=\"availroutes\" value=\"" + id + "\" onchange=\"CheckBoxChange(this);\" /><label for=\"ck" + id + "\" onclick=\"$('#ck" + id + "').checked=true;\">" + name + "</label></span><div class=\"cb\"></div></li>";
        }
    }
    html = "<ul>\n" + arrayLocation.join("") + html + "\n</ul>";
    $("#divRoutes").html(html);
}

function CheckBoxChange(checkbox) {
    var routes = $.cookie('TravelTimeRoutes');
    var rteArray = new Array();
    if (routes != "") {
        rteArray = routes.split(",");
    }
    if (checkbox.checked) {
        rteArray.push(checkbox.value);
    }
    else {
        // remove it
        var tArray = rteArray;
        rteArray = new Array();
        for (var i = 0; i < tArray.length; i++) {
            if (checkbox.value != tArray[i] && tArray[i] != "") {
                rteArray.push(tArray[i]);
            }
        }
    }
    routes = rteArray.join();
    $.cookie('TravelTimeRoutes', routes, options);
    loadRouteConfigurations();
}

function MoveUp(id){
  var tempArray = new Array();
  var routes = $.cookie('TravelTimeRoutes');
  var rteArray = routes.split(",");
  rteArray.reverse();
  for (var i = 0; i < rteArray.length ; i++)
  {
    if(id != rteArray[i]){
      tempArray.unshift(rteArray[i]);
    }
    else
    {
      if(i+1 < rteArray.length)
      {
        tempArray.unshift(rteArray[i+1]);
      }
      tempArray.unshift(rteArray[i]);
      i++;
    }
  }
  routes = tempArray.join();
  $.cookie('TravelTimeRoutes', routes, options);
  loadRouteConfigurations();
}

function MoveDown(id){
  var tempArray = new Array();
  var routes = $.cookie('TravelTimeRoutes');
  var rteArray = routes.split(",");
  for (var i = 0; i < rteArray.length ; i++)
  {
    if(id != rteArray[i]){
      tempArray.push(rteArray[i]);
    }
    else
    {
      if(i+1 < rteArray.length)
      {
        tempArray.push(rteArray[i+1]);
      }
      tempArray.push(rteArray[i]);
      i++;
    }
  }
  routes = tempArray.join();
  $.cookie('TravelTimeRoutes', routes, options);
  loadRouteConfigurations();
}