/* An SignBox is like an info window with flexible styling.
 * @param {GLatLng} latlng Point to place bar at
 * @param {Object} opts Passes configuration options - content, 
 *   offsetVertical, offsetHorizontal, className, height, width
 */
function SignBox(latlng, opts) {
  this.latlng_ = latlng;
  this.content_ = opts.content || "ACIS Live";
  this.offsetVertical_ = opts.offsetVertical || -5;
  this.offsetHorizontal_ = opts.offsetHorizontal || -5;

  this.className_ = opts.className || "";  
  this.height_ = opts.height || 200;
  this.width_ = opts.width || 310;
}

/* SignBox extends GOverlay class from the Google Maps API
 */
SignBox.prototype = new GOverlay();

var preload = new Image(); 
preload.src="/images/infowindow.png"; 
var psmUpdating = false;  
var psmSet = false;
var currentPSM = "";

/* Creates the DIV representing this SignBox
 * @param {GMap2} map The map to add signbox to
 */
SignBox.prototype.initialize = function(map) {
  // Create the DIV representing our Bar
  var div = document.createElement("div");

  if (this.className_ != "") {
    div.className = this.className_;
  } else {
    div.style.border = "1px solid #000000";
    div.style.position = "absolute";
    div.style.background = "url('/images/infowindow.png')";
    //div.style.backgroundColor = "#FFFFFF";
    div.style.padding = "2px";
    div.style.width = this.width_ + "px";
    div.style.height = this.height_ + "px";
  } 
 
  var contentDiv = document.createElement("div");
  contentDiv.innerHTML = this.content_;

//  var topDiv = document.createElement("div");
//  topDiv.style.textAlign = "right";
//  topDiv.style.paddingRight = "10px";
//  var closeImg = document.createElement("img");
//  closeImg.src = "http://www.google.com/intl/en_us/mapfiles/close.gif";
//  topDiv.appendChild(closeImg);

  var closeImg = document.createElement("img");
  closeImg.src = "http://www.google.com/intl/en_us/mapfiles/close.gif";
  var outerTable = document.createElement("table");
  outerTable.width = "100%";
  outerTable.style.width = "100%";
  var topRow = outerTable.insertRow(0);
  var topCell = topRow.insertCell(0);
  var topTable = document.createElement("table");
  topTable.width = "100%";
  topCell.appendChild(topTable);
  var topBannerRow = topTable.insertRow(0);
  var naptanCell = topBannerRow.insertCell(0);
  var closeCell = topBannerRow.insertCell(1);
  var naptanDiv = document.createElement("div");
  naptanDiv.id = "naptan";
  naptanCell.appendChild(naptanDiv);
  closeCell.id = "close";
  closeCell.appendChild(closeImg);

  var contentRow = outerTable.insertRow(1);
  var contentCell = contentRow.insertCell(0);

  var contentTable = document.createElement("table");
  contentCell.id = "content";
  contentTable.width = "95%";
  contentTable.insertRow(0).insertCell(0).innerHTML = "loading data";
  contentCell.appendChild(contentTable);
  contentCell.vAlign = "top";
  var vPaddingCell = contentRow.insertCell(1);
  vPaddingCell.id = "rightPad";
  vPaddingCell.innerHTML = "<img src='images/blank.gif' width='1' height='118' />";
  var linkRow = outerTable.insertRow(2);
  var linkCell = linkRow.insertCell(0);
  var link = document.createElement("div");
  linkCell.id = "link";
  linkCell.colSpan = 2;
  linkCell.appendChild(link);
  link.href = "#";
  link.innerText = detachWindowText;
  link.textContent = detachWindowText;
  this.link_ = link;
  var psmList;

  function removeSignBox(ib, m) {
    return function() { 
      GEvent.trigger(ib, "closeclick");
      m.removeOverlay(ib);
    };
  }

  function removeSignBoxEx(ib, m) {
    return function() { 
      GEvent.trigger(ib, "closeclick");
      m.removeOverlay(ib);
      window.open(ib.link_.href,'','width=320,height=195');
    };
  }  

//  function popupSign(ib, map) {
//    removeSignBox(ib, map);
//    window.open(ib.link_.href,'signPopup','width=240,height=180');
//  }
    
  GEvent.addDomListener(closeImg, 'click', removeSignBox(this, map));
//  GEvent.addDomListener(link, 'click', removeSignBox(this, map));
  GEvent.addDomListener(link, 'click', removeSignBoxEx(this, map));
 
  div.appendChild(outerTable);
  div.style.display = 'none';

  map.getPane(G_MAP_MARKER_PANE).appendChild(div);

  this.map_ = map;
  this.div_ = div;
  this.naptan_ = naptanDiv;
  this.contentTable_ = contentTable;
  psmUpdating = true;
}

/* Remove the main DIV from the map pane
 */
SignBox.prototype.remove = function() {
  this.div_.parentNode.removeChild(this.div_);
  psmUpdating = false;
  currentPSM = "";
  psmSet = false;
}

//MS New code to switch between visible PSM

function psmUpdate(i) {
    if (i >= psmList.length)
    {
        i = 0;
    }
    
    if (psmUpdating == true && document.getElementById("psmRow"))
    {
        document.getElementById("psmRow").textContent = psmList[i];
        document.getElementById("psmRow").innerText = psmList[i];
        //So that full refreshes can show correct message
        currentPSM = psmList[i];
        psmSet = true;

        setTimeout("psmUpdate("+(i+1)+")",3000);
    }
    
}
  
SignBox.prototype.SetContent = function(stopRef,stopName,pac,psm,time,newContent) {
 //this.naptan_.textContent = stopRef;
 if(pac.length > 0)
 {
   this.naptan_.innerHTML = stopRefText + "<b>" + pac + "</b>";
   this.link_.href = "/pip/stop_simulator.asp?naptan="+pac+"&pscode=&dest=";
 }
 else
 {
   this.naptan_.innerHTML = stopRefText + "<b>" + stopRef + "</b>";
   this.link_.href = "/pip/stop_simulator.asp?naptan="+stopRef+"&pscode=&dest=";
 }
 var newTable = document.createElement("table");
 newTable.width = "100%";
 var row = newTable.insertRow(newTable.rows.length - 1);
 var cell = row.insertCell(0);
 cell.className = "stopNameRow";
 cell.textContent = stopName + " - " + time;
 cell.innerText = stopName + " - " + time;
 cell.colSpan = 3;
 if((newContent.length < 1) || (newContent[0] == null))
 {
   row = newTable.insertRow(newTable.rows.length);
   cell = row.insertCell(0);
   cell.className = "noDeparturesRow";
   cell.innerHTML = noDeparturesText;
   cell.colSpan = 3;
 }
 if(newContent[0] == "SYSTEMDOWN")
 {
   row = newTable.insertRow(newTable.rows.length);
   cell = row.insertCell(0);
   cell.className = "noDeparturesRow";
   cell.innerHTML = notAvailableText;
   cell.colSpan = 3;
   newContent[0] = null;
 }
 
 if((psm.length > 1) && (newContent.length > 4))
 {
  newContent.pop();
 }
 for(contentIndex in newContent)
 {
  if(newContent[contentIndex] == null)
  {
   break;
  }
  row = newTable.insertRow(newTable.rows.length);
  cell = row.insertCell(0);
  
  var departureFields = newContent[contentIndex].split("#");
  
  if (departureFields[0] == "SYSTEMMESSAGE")
  {
    cell.innerHTML = departureFields[1];
    cell.colSpan = 3;
    row.className = "systemMessageRow";
  }
  else
  {
    row.className = "departureRow";
  
    cell.innerHTML = departureFields[0];
    cell.width = "30";
    cell = row.insertCell(1);
    cell.innerHTML = departureFields[1];
    cell.width = "150";
    cell = row.insertCell(2);
    cell.innerHTML = departureFields[2];
    cell.align = "right";  
  }
 }
 var psmRow = newTable.insertRow(newTable.rows.length);
 var psmCell = psmRow.insertCell(0);
 psmList = psm.split(",");
 psmCell.className = "psmRow";
 psmCell.id = "psmRow";
 psmCell.textContent = currentPSM;
 psmCell.innerText = currentPSM;
 psmCell.colSpan = 3;

 var parent = this.contentTable_.parentNode;
 parent.replaceChild(newTable, this.contentTable_);
 this.contentTable_ = newTable;
 
 // Only want to kick off psm refresh first time it opens
 if (psmList.length > 0 && psmUpdating == true && psmSet == false)
 {
    psmUpdate(0);
 }
}

/* Copy our data to a new SignBox
 * @return {SignBox} Copy of signbox
 */
SignBox.prototype.copy = function() {
  psmUpdating = false;
  var opts = {};
  opts.latlng = this.latlng_;
  opts.content = this.content_;
  opts.offsetVertical = this.offsetVertical_;
  opts.offsetHorizontal = this.offsetHorizontal_;
  

  opts.className = this.className_ || "";  
  opts.height = this.height_;
  opts.width = this.width_;
  return new SignBox(this.latlng, opts);
}

/* Redraw the Bar based on the current projection and zoom level
 * @param {boolean} force Helps decide whether to redraw overlay
 */
SignBox.prototype.redraw = function(force) {
  // We only need to redraw if the coordinate system has changed
  if (!force) return;

  // Calculate the DIV coordinates of two opposite corners of our bounds to
  // get the size and position of our Bar
  var pixPosition = this.map_.fromLatLngToDivPixel(this.latlng_);

  // Now position our DIV based on the DIV coordinates of our bounds
  this.div_.style.width = this.width_ + "px";
  this.div_.style.left = (pixPosition.x + this.offsetHorizontal_) + "px";
  this.div_.style.height = this.height_ + "px";
  this.div_.style.top = (pixPosition.y + this.offsetVertical_) + "px";
  this.div_.style.display = 'block';

  // if we go beyond map, pan map
  var mapWidth = this.map_.getSize().width;
  var mapHeight = this.map_.getSize().height;
  var bounds = this.map_.getBounds();
  var boundsSpan = bounds.toSpan();
  var longSpan = boundsSpan.lng();
  var latSpan = boundsSpan.lat();
  var degWidth = (this.width_/mapWidth) * longSpan;
  var degHeight = (this.height_/mapHeight) * latSpan;

  if (this.latlng_.lng() + degWidth > bounds.getNorthEast().lng()) {
    this.map_.panTo(this.latlng_);
  }   

  var bottompt = new GLatLng( (this.latlng_.lat() + degHeight), this.latlng_.lng());
  if (!bounds.contains(bottompt)) {
    this.map_.panTo(this.latlng_);
  }
  
}

