/*
 * Copyright (c) Perforce Software, Inc. 1997-2016
 * Licensed Materials - All Rights Reserved.
 */

function startStopTimer()
{
try {
    if(startAnimation === true) {
      startAnimation = false;
      mapID.performAction("stop");
      mapID.setUpdateInterval(0);
      startButton.setImage("images/start.gif");
      startButton.setRolloverImage("images/starth.gif");
    } else {
       startAnimation = true;
       mapID.performAction("start");
       mapID.setUpdateInterval(5);
      startButton.setImage("images/stop.gif");
      startButton.setRolloverImage("images/stoph.gif");
   }   
  } catch (e) {
    alert("An error occurred during property setting :"+e);
  }
}
function setCreateNodeInteractor()
{
  var properties = {
      name: "New Node",
      size: "15",
      type: "4"
 };
 mapID.setCreateNodeInteractor("node", false, properties);
}


function setInteractor(interactor)
{
    mapID.setInteractor(interactor);
    displaySelection();
}

function deleteIt()
{
  mapID.getSelectionManager().deleteSelection();
  setInteractor(selectInteractor);
  selectButton.setSelected(true);
}

// Selection management : allow editing properties.
function editSelection(){
  var selection=mapID.getSelectionManager().getSelection();
  if (selection != null && selection.length === 1) {
    var names = selection[0].getObjectPropertyNames().sort();
    // restore the value and allow text edition
    for(var i=0; i<names.length; i++){
         var pName = names[i];
         var pVal=selection[0].getObjectProperty(pName);
         var obj=document.getElementById("f"+pName);
         obj.value=pVal;
         obj.style.backgroundColor="#FFFFFF";
         obj.style.border="thin inset";
         obj.readOnly=false;
    }
    // show commit buttons
    var span = document.getElementById("commitId");
    span.style.display = "block";      
   }
 }
// Selection management : validate the change in properties.
function changeProperties(valid){
  try {
    var selection = mapID.getSelectionManager().getSelection()[0];
    var change=false;
    for(var pName in selection.properties[0]){
      var obj=document.getElementById("f"+pName);
      if(obj!=null) {
        if(valid) {
          var pVal=selection.getObjectProperty(pName);
          if(pVal != obj.value) {
              selection.setObjectProperty(pName, obj.value);
              change=true;
          }
        }
      }
    }   
    if(change) {    
      mapID.getSelectionManager().commitSelectionProperties(true);  
    }
  } catch (e) {
    alert("An error occurred during property setting :"+e)
  }
  // rebuild the selection panel
  displaySelection();
}
// Selection management : display a message when mouse moves over selection
// table.
function mouseOver(){
  window.status = selection_i18n_label;
  if (messages) {
    if (IlvBrowserInfo.instance.ie5up && document.readyState != 'complete')
      return;
    messages.displayMessage(selection_i18n_label);
  }
}
// Selection management : display the current properties.
function displaySelection(){
  if(typeof(mapID)=="undefined"){
    return;
  }
  var selection=mapID.getSelectionManager().getSelection();
  // disable edit menu.
  mapID.getPopupMenu().getMenu().getChild(3).setEnabled(selection.length > 0);
  // disable delete selection button
  deleteButton.setEnabled(selection.length > 0);
  try {
    var p = "<table class='layerToolStyle' onMouseOver='mouseOver();' title='"+selection_i18n_label+"'>";
    // header
    p += "<thead class='layerToolStyle'><tr><td colspan=2>"+selection_i18n_label+"</td></tr></thead>";
    // body
    p += "<tbody>";
    if (selection != null && selection.length == 1) {
        var names = selection[0].getObjectPropertyNames().sort();
        for(var i=0; i<names.length; i++){
        p += "<tr><td class='layerToolStyle'>" + names[i] + "</td><td><input class='layerToolStyle' style='border:thin none;margin-top:1;margin-left:1'+ id='f"+names[i]+"' readOnly='true'/></td></tr>";
       }                        
    } else {
        p += "<tr><td colspan=2 class='layerToolStyle'>"+none_i18n_label+"</td></tr>";
    }
    p += "</tbody>";
    // footer
    p += "<tfoot><tr><td colspan=2>";
    p += "<span id='commitId' class='layerToolStyle' style='display:none;'>";
    p += "<input type='button' id='okId' value='"+commit_i18n_label+"' onclick='changeProperties(true)'/>";
    p += "<input type='button' id='koId' value='"+cancel_i18n_label+"' onclick='changeProperties(false)'/>";
    p += "</span>";
    p += "</td></tr></tfoot>";
    p += "</table>";
    var span = document.getElementById('tableId');
    span.innerHTML = p;
    if(names!=null) {
      for(var i=0; i<names.length; i++){
           var pName = names[i];
           var pVal=selection[0].getObjectProperty(pName);
           var obj=document.getElementById("f"+pName);
           obj.value=pVal;
       }
    }
  } catch (e) {
    alert("An error occurred during property sheet refresh "+e)
  }
}

// enable or disable menu items according to view zoom
function updateZoomMenuItems (view) {
  var zoomLevel = view.getCurrentZoomLevel();
  var maxZoomLevel = view.getMaxZoomLevel()/view.getZoomFactor();
  //enable the "Zoom In" menu item if the max zoom level is not reached
  mapID.getPopupMenu().getMenu().getChild(1).setEnabled(zoomLevel < maxZoomLevel);
  zoomInButton.setEnabled(zoomLevel < maxZoomLevel);
  //enable the "Zoom Rect" menu item if the max zoom level is not reached
  if(zoomLevel > maxZoomLevel && zoomRectButton.isSelected()){
    selectButton.setSelected(true);
    mapID.setInteractor(selectInteractor);
  }
  zoomRectButton.setEnabled(zoomLevel < maxZoomLevel);
  //enable the "Zoom Out" menu item if the zoom level is not equal to 1
  mapID.getPopupMenu().getMenu().getChild(2).setEnabled(zoomLevel > 1);
  zoomOutButton.setEnabled(zoomLevel > 1);
  showAllButton.setEnabled(zoomLevel > 1);
  //enable the "pan" menu item if the the zoom level is not equal to 1
  mapID.getPopupMenu().getMenu().getChild(0).setEnabled(zoomLevel > 1);     
  //panButton.setEnabled(zoomLevel > 1);
  panTool.setEnabled(zoomLevel > 1);  
}

var startAnimation = false;
if(typeof overview != "undefined"){
  var ov =overview.getJViewsDHTMLObject(); 
  ov.setColor("D00000");
  ov.setOpacity(0.1);
  ov.setFillOn(true);
  ov.setLineWidth(1);
  ov.setAnimatedPan(true);
}
displaySelection();

document.title=title_i18n_label;

function tooltipCallback(view, index) {
  var message = view.getHitInfos().tooltip[index]; // retrieve the tooltip
                                                    // message of current index
  var msg = '<table class=\'tooltip\'>';
  for (key in message) {
    msg += '<tr class=\'tooltipTitle\'><td>';
    msg += unescape(key);
    msg += '</td><td>' + unescape(message[key]) + '</td></tr>';
  }
  msg += '</table>';
  return msg;
}