/*
* Phybo javascript API
* Hack it at your own risk!
*
* v0.1 
* 05/06/2009
*/

var phybo_server = 'http://app.phybo.com';
var phybo_refresh_period = 3*60; //in seconds

//Main class object
PhyboChartAPI = {
	init: function() {
	  var scripts = PhyboChartAPI.getPhyboScripts();
    
	  for (i = 0; i<scripts.length; i++) {
      var script = scripts[i];
      var displayElement = script.parentNode;
      if (displayElement!=null) {
        new PhyboChartLoader(script);
      }
    }
	},

  getPhyboScripts: function() {
    var resultScripts = [];

    var myScripts = document.getElementsByTagName('script');

    if (myScripts!=null && myScripts.length > 0) {
      for (var i = 0; i < myScripts.length; i++) {
        var script = myScripts[i];
        if (script.innerHTML && script.innerHTML.length > 0) {
          if (script.innerHTML.indexOf('phybo_object_key') > 0){
            resultScripts.push(script);
          }
        }
      }
    }
    return resultScripts;
  }
  
}


//PhyboChartLoader
//For loading charts

//PhyboChartLoader Constructor
function PhyboChartLoader(script) {
  this.phy_interval_counter = null;
  this.script = script;
  this.refreshDate = null;

  this.initialize();

}


//PhyboChartLoader Methods

//initialize
PhyboChartLoader.prototype.initialize =  function() {

  phybo_object_key = null;
  phybo_property_name = null;
  phybo_graph_width = null;
  phybo_graph_height = null;
  phybo_graph_days = null;

  try {
    eval(this.script.innerHTML);

    if (phybo_object_key != null && phybo_property_name != null) {

      this.object_key = phybo_object_key;

      this.phybo_property_name = phybo_property_name;

      if (phybo_graph_width == null) {
        phybo_graph_width = 200;
      }
      this.width = phybo_graph_width;

      if (phybo_graph_height == null) {
        phybo_graph_height = 100;
      }
      this.height = phybo_graph_height;

      if (phybo_graph_days == null) {
        phybo_graph_days = 7;
      }
      this.days = phybo_graph_days;

      var scriptNode = document.createElement('script');
      scriptNode.src = this.getScriptUrl();
      this.script.parentNode.appendChild(scriptNode);

      var mySelf = this;
      this.phy_interval_counter = setInterval(function(){mySelf.updateChart();},100);

      phybo_sensor_key = null;

    }else {
      phybo_sensor_key = null;
    }

  }catch (e) {
    phybo_sensor_key = null;
  }

};

PhyboChartLoader.prototype.getScriptUrl = function (){
  if (window.location.hostname == 'localhost') {
    phybo_server = 'http://localhost:8080';
  }
  return phybo_server + '/api/js/phyboChartProperty/'+ this.object_key + '/' + this.phybo_property_name +
                            '?width=' + this.width + '&height=' + this.height + 
                            '&days=' + this.days;
};


//checkReload
PhyboChartLoader.prototype.checkReload = function (){
  var compareDate = new Date();
  compareDate.setSeconds(compareDate.getSeconds() - phybo_refresh_period);
  return (compareDate > this.refreshDate);
}

//loadChart
PhyboChartLoader.prototype.updateChart = function (){
  try {
    if (PhyChart) {
      if (!this.refreshDate) {
        this.refreshDate = new Date();

      }else {
        if (this.checkReload()) {
          this.refreshDate = new Date();

          var scriptNode = document.createElement('script');
          scriptNode.src = this.getScriptUrl();

          this.script.parentNode.appendChild(scriptNode);

        }
      }
      
    }
  }catch (e) {
    //Ignore exception
  }
};


//PhyChart
//For visualizing charts

//PhyChart Constructor
function PhyChart(objectKey, propertyName, graphWidth, graphHeight, graphDays, displayUrl) {
  this.objectKey = objectKey;
  this.propertyName = propertyName;
  this.graphWidth = graphWidth;
  this.graphHeight = graphHeight;
  this.graphDays = graphDays;
  this.displayUrl = displayUrl;

  this.display();

}

//findDisplayElements
PhyChart.prototype.findDisplayElements = function() {
  var candidateScripts = PhyboChartAPI.getPhyboScripts();

  var displayElements = [];

  if (candidateScripts != null && candidateScripts.length > 0) {
    for (var i = 0; i < candidateScripts.length; i++) {
      phybo_object_key = null;
      phybo_property_name = null;
      phybo_graph_width = null;
      phybo_graph_height = null;
      phybo_graph_days = null;
      
      try {
        eval(candidateScripts[i].innerHTML);
        
        if (phybo_graph_width == null) {
          phybo_graph_width = 200;
        }
        if (phybo_graph_height == null) {
          phybo_graph_height = 100;
        }
        if (phybo_graph_days == null) {
          phybo_graph_days = 7;
        }

        if (phybo_object_key == this.objectKey && phybo_property_name == this.propertyName) {
          if (phybo_graph_width == this.graphWidth) {
            if (phybo_graph_height == this.graphHeight) {
              if (phybo_graph_days == this.graphDays) {
                if (candidateScripts[i].parentNode != null) {
                  displayElements.push(candidateScripts[i].parentNode);
                }
              }
            }
          }
        }

      }catch (e) {
        phybo_sensor_key = null;
      }
    }
            
  
  }
  return displayElements;
}

//deletePreviousImg
PhyChart.prototype.deletePreviousImg = function (displayElement){
  var myGraphs = displayElement.getElementsByTagName('img');

  if (myGraphs!=null && myGraphs.length > 0) {
    for (var i = 0; i < myGraphs.length; i++) {
      displayElement.removeChild(myGraphs[i]);
    }
  }
  
  var myEmtpyMessages = displayElement.getElementsByTagName('div');

  if (myEmtpyMessages!=null && myEmtpyMessages.length > 0) {
    for (var i = 0; i < myEmtpyMessages.length; i++) {
      displayElement.removeChild(myEmtpyMessages[i]);
    }
  }
}

//display
PhyChart.prototype.display = function() {
  var displayElements = this.findDisplayElements();

  if (displayElements != null && displayElements.length > 0) {
    for (var i = 0; i < displayElements.length; i++) {
      if (displayElements[i] != null) {

        this.deletePreviousImg(displayElements[i]);

        if (this.displayUrl == null || this.displayUrl.length <=0) {
          var noDataText = document.createElement('div');
          noDataText.className = 'noGraphData';
          noDataText.style.width = phybo_graph_width + 'px';
          noDataText.style.height = phybo_graph_height + 'px';
          noDataText.style.lineHeight = phybo_graph_height + 'px';
          noDataText.innerHTML = "No data";
          displayElements[i].appendChild(noDataText);
          return;
        } else {
            var img = document.createElement('img');
            img.border = "0";
            img.src = this.displayUrl;
            img.alt = 'phybo object, ' + phybo_property_name;
            displayElements[i].appendChild(img);
        }
      }
    }
  }

};



//Attach onload event
try {
  if (eventAttached) {
  }else {
    eventAttached = false;
  }
}catch(e){
  eventAttached = false;
}

if (!eventAttached) {
  try {
    window.addEventListener('load',function(){PhyboChartAPI.init();},false);
    eventAttached = true;
    
  } catch (e) {
    window.attachEvent('onload',function(){PhyboChartAPI.init();});
    
  }
}

