var current_tracking_number = "";
var current_progress = {};
var current_points = [];
var current_markers = {};
var current_data = {};

function resize_map(flag) {
  var windowHeight = 0;
  var windowWidth = 0;
  
  if (typeof(window.innerHeight) == 'number') {
    windowHeight = window.innerHeight;
    
  } else {
    if (document.documentElement && document.documentElement.clientHeight) {
       windowHeight = document.documentElement.clientHeight;
       
    } else {
      if (document.body && document.body.clientHeight)
        windowHeight = document.body.clientHeight;
        
    }
    
  }

  if (typeof(window.innerWidth) == 'number') {
    windowWidth = window.innerWidth;
    
  } else {
    if (document.documentElement && document.documentElement.clientWidth) {
       windowWidth = document.documentElement.clientWidth;
       
    } else {
      if (document.body && document.body.clientWidth)
        windowWidth = document.body.clientWidth;
        
    }
    
  }

  if (flag) {
    map.resizeTo(new YSize(windowWidth - 7, windowHeight - 157));
  } else {
    document.getElementById('default_map').style.height = (windowHeight - 157) + 'px';
    document.getElementById('default_map').style.width = (windowWidth - 7) + 'px';
  } 
  
}

function begin_plot(flag) {
  if (!flag) {
    document.getElementById('default_tracking_submit').style.display = 'none';
    document.getElementById('default_tracking_loading').style.display = 'block';
    
    current_tracking_number = document.getElementById('tracking_number').value;
    get_progress(current_tracking_number);
    
    return;
    
  } else {
    if (current_progress["error"]) {
      document.getElementById('default_tracking_loading').style.display = 'none';
      document.getElementById('default_tracking_submit').style.display = 'block';
      
      alert(current_progress.error);
      
      document.getElementById('tracking_number').focus();
      return;
    }
    
  }

  document.getElementById('default_wrap').style.display = 'none';
  document.getElementById('default_map').style.display = 'block';

  resize_map(1);
    
  plot_package();
    
}

function format_activity(activity) {
  var data = "<table class='activity'>";
  
  for (i in activity.date) {
    var activity_class = i % 2 == 0 ? "activity_row" : "activity_row_alt";
    
    data += "<tr class='" + activity_class + "'>";
    data += "<td class='activity_date'>" + activity.date[i] + " " + activity.time[i] + "</td>";
    data += "<td class='activity_message'>" + activity.activity[i] + "</td>";
    data += "</tr>";
    
  }
  
  data += "</table>";
  
  return data;
  
}

function plot_package() {
  var cc = 0;
  var current = {"city":"", "state":"", "activity":[], "time":[], "date":[], "lat":"", "lon":""};
  current_markers = {};
  
  var colors = {1:"FF", 2:"EE", 3:"DD", 4:"CC", 5:"BB", 6:"AA", 7:"99", 8:"88", 9:"77", 10:"66", 11:"55", 12:"44", 13:"33", 14:"22", 15:"11", 16:"00"};
  
  for (i=0; i < current_progress["progress"].length; i++) {
    var b = parseInt(i).length == 1 ? i * 10 : i + 10;
    
  }
  
  for (i in current_progress["progress"]) {
    var location = current_progress["progress"][i];

    if (current.city == location.city && current.state == location.state) {
      current.time[current.time.length] = location.time;
      current.date[current.date.length] = location.date;
      current.activity[current.activity.length] = location.activity;

      if (current_progress.progress[(parseInt(i) + 1)] && current_progress.progress[(parseInt(i) + 1)].city == current.city && current_progress.progress[(parseInt(i) + 1)].state == current.state)
        continue;
      
    } else {
      current = {"city":"", "state":"", "activity":[], "time":[], "date":[], "lat":"", "lon":""};
      
      current.city = location.city;
      current.state = location.state;
      
      current.lat = location.lat;
      current.lon = location.lon;
      
      current.time = [location.time];
      current.date = [location.date];
      current.activity = [location.activity];
    
      if (i != (current_progress.progress.length-1)) continue;

    }

    cc++;

    current_markers[cc] = new YMarker(new YGeoPoint(current.lat, current.lon));
    current_markers[cc].addAutoExpand("<span class='activity_marker'>" + cc + ") " + current.city + ", " + current.state + "</span>");
    
    current_data[cc] = format_activity(current);
    
    YEvent.Capture(current_markers[cc], EventsList.MouseClick, 
      function(m){
        current_markers[m.thisObj._expContent.substr(30, 1)].openSmartWindow(current_data[m.thisObj._expContent.substr(30, 1)]);
      });
        
    map.addOverlay(current_markers[cc]);
    
    current_points.push(new YGeoPoint(current.lat, current.lon)); 
    
    var color = colors[10 - cc] ? colors[10 - cc] : "00";
    
    map.addOverlay(new YPolyline(current_points, "#5263" + color, 4, 0.7));
  }
  
  map.drawZoomAndCenter(current_markers[1].YGeoPoint, 9); 
  
}

function privacy_policy(flag) {
  if (flag) {
    document.getElementById('privacy_shade').style.display = 'block';
    document.getElementById('privacy_policy').style.display = 'block';
  } else {
    document.getElementById('privacy_shade').style.display = 'none';
    document.getElementById('privacy_policy').style.display = 'none';
  }  
}

window.onload = function() {
  document.getElementById('email').innerHTML = "<a href=\"mailto:emiller" + "@" + "ccorpsoft" + "." + "com" + "?Subject=parcelplotr\">Emanuel Miller</a>";
  
  map.addTypeControl();
  map.addZoomLong();
  map.addPanControl();
  
  map.setMapType(YAHOO_MAP_REG);
  
  map.drawZoomAndCenter("San Francisco", 12);

  document.getElementById('tracking_submit').disabled = false;
  document.getElementById('tracking_number').focus();  
  
}

window.onresize = function() {
  resize_map();
  
}

