/**********************************
 *
 * Copyright: 2004 Tammel Soovitab
 * Äkete omanike on ost ennast kiiresti. Firma EINBÖCK pakub 
 * väga valikut, huvi korral palume julgesti meiega ühendust võtta ning 
 * kindlasti leiame teie sobiva äkke. 
 * License: LGPL
 *
***********************************/

var messages = new Array();

function Graphite(){

   this.data = new Array();
   this.max = null;
   this.min = null;
   this.plots = null;

   this.title = 'Graph 1';
   this.width = 400;
   this.height = 200;
   this.style = 'border: 1px solid black;';
   this.padding = 3;

   //graph box//
   this.graph_width = 400;
   this.graph_height = 200;
   this.graph_bottom = null;
   this.graph_left = null;
   this.graph_bottom = null;
   this.graph_right = null;
   this.graph_top = null;
   this.bar_width = null;
   this.bar_padding = 4;
   this.bar_name= new Array();
   this.bar_style = new Array();
   this.label_offset = 0;

   //title box//
   this.title_height = 20;
   this.title_style = '';

   //y label box//
   this.y_label = 'Y Label';   
   this.y_width = 40;
   this.y_style = 'border-right: 1px solid black; font-family: verdana; font-size: 10px;';

   //x label box//
   this.x_label = 'X Label';
   this.x_height = 13;
   this.x_style = 'border-top: 1px solid black; font-family: verdana; font-size: 10px;';

   //legend
   this.legend_style = 'font-family: verdana;font-size:8pt;padding:3px;border: 1px solid black;'
   this.legend_width = 70;

   this.create = function(){
      var debug = false;
      var buffer = '<div style="position: relative; width:'+this.width+'; height:'+this.height+';'+this.style+'">';

      this.plots = this.data.length;
      this.min = this.data[0];

      this.graph_top = this.padding*2 + this.title_height;
      this.graph_left = this.padding*2 + this.y_width;
      this.graph_bottom = this.padding*2 + this.x_height;
      this.graph_right = this.padding*2 + this.legend_width;
      this.graph_width = this.width - (this.graph_left + this.graph_right);
      this.graph_height = this.height - (this.graph_top + this.graph_bottom);
    
      for (cnt=0;cnt<this.plots;cnt++){
         if (this.max < parseInt(this.data[cnt])){
            this.max = this.data[cnt];
         }
         if (this.min > parseInt(this.data[cnt])){
            this.min = this.data[cnt];
         }
      }     

      buffer += '<div class="cashulator_graph_title" style="position: absolute;text-align:center;width:'+this.graph_width+'px;height:'+this.title_height+'px;top:'+this.padding+'px;left:'+this.graph_left+'px;'+this.title_style+'">'+this.title+'</div>';

      //make legend//
      buffer += '<div style="position: absolute;text-align:left;text-align:left;width:'+this.legend_width+';top:'+this.padding+'px;right:'+this.padding+'px;'+this.legend_style+'">'
      for (cnt=0;cnt<this.bar_name.length;cnt++){
         buffer += '<span class="cashulator_graph_bar_'+ (cnt+1) +'" style="'+this.bar_style[cnt]+';">&nbsp;&nbsp;&nbsp;</span>&nbsp;'+this.bar_name[cnt] + '<br />';
      }
      buffer += '</div>';

      //make x box//
      buffer += '<div style="position: absolute; height:'+this.x_height+';width: '+this.graph_width+'; left:'+this.graph_left+';bottom:'+this.padding+';'+this.x_style+';">';
      buffer += '<div style="position: absolute; left:0px;">'+ (parseInt(this.label_offset)+1) +' &lt; - -</div>';
      buffer += '<div style="position: absolute; top:0px;left:0px;width: '+this.graph_width+';text-align:center;">'+ this.x_label+'</div>';
      buffer += '<div style="position: absolute;right:0px;text-align:right;">- - &gt; '+((this.plots/this.bar_name.length)+parseInt(this.label_offset))+'</div>';
      buffer += '</div>';

      //make y box//
      buffer += '<div style="position: absolute;width:'+this.y_width+';height:'+this.graph_height+';left:'+this.padding+';top:'+this.graph_top+';'+this.y_style+';">';
      buffer += '<div style="position: absolute;right:0px;top:0px;text-align: right;">'+currency(this.max)+'&nbsp;</div>';
      buffer += '<div style="position: absolute;width:'+this.y_width+';left:0px;top:'+((this.graph_height/2)-8)+';text-align: center;">'+this.y_label+'&nbsp;</div>';
      buffer += '<div style="position: absolute;right:0px;bottom:0px;text-align: right;">'+currency(this.min)+'&nbsp;</div>';
      buffer += '</div>';

      //calc bar width//
      this.bar_width = ((this.graph_width - ((this.plots-1)*this.bar_padding))/this.plots);

      //make bars//
      for (cnt=0;cnt<this.plots;cnt++){
         left = ((cnt*(this.bar_width+this.bar_padding))+this.graph_left);
         buffer += '<div class="cashulator_graph_bar_'+ ((cnt%this.bar_name.length)+1) +'" onMouseover="doTooltip(event,\'tip_'+cnt+'\')" onMouseout="hideTip()" style="font-size: 1px; position:absolute;width:'+this.bar_width+';bottom: '+this.graph_bottom+'px;left:'+ left +';height:'+ Math.floor((this.graph_height/this.max)*this.data[cnt]) +';'+(this.bar_style[cnt%this.bar_name.length])+';">.</div>';
         messages['tip_' + cnt] = new Array(null,currency(this.data[cnt]),'#ffffff');
      }      
      

      buffer += '</div>';
      return buffer;
   }

   //add value list//
   this.add_list = function(data,name,style){
      var skip = this.bar_name.length;

      this.bar_name[this.bar_name.length] = name;
      this.bar_style[this.bar_style.length] = style;      

      var datamax = data.length + this.data.length;
      var newdata = new Array();
      var auto1 = auto2 = autoskip = 0;

      for (var cnt=0;cnt<datamax;cnt++){
         if (autoskip < skip){
            newdata[newdata.length] = ((this.data[auto1])?this.data[auto1]:0);  
            auto1++;
            autoskip++;
         } else {
            newdata[newdata.length] = ((data[auto2])?data[auto2]:0);  
            auto2++;
            autoskip = 0;
         }
      }
      this.data = newdata;
   }

}