﻿// DInfo 2 class //

var dinfo2 = null;
var kill_timer = 0;
var dinfo_div2 = null;
var dinfo2_margin = 10;
//

function DInfo2(point,html) {
  this.point_ = point;
  
  this.html_ = html;
  this.current_ = null;

}
DInfo2.prototype = new GOverlay();

// Creates the DIV representing this rectangle.
DInfo2.prototype.initialize = function(map) {
  // Create the DIV representing our rectangle
  var div = document.createElement("div");
  div.style.position = "absolute";
div.style.top = "0";
 div.style.left = "0";
  this.map_ = map;
  if(dinfo_div2 == null)
  {
    map.getPane(G_MAP_FLOAT_PANE).appendChild(div);
    this.div_ = div;
    dinfo_div2 = div;
    // let's create 4 info windows //
    var ts = ['tl','tr','bl','br'];
    for(var i in ts)
    {
        var v = ts[i];
        bl = document.getElementById("balloon_"+v).innerHTML;
        var new_bl = document.createElement("div");
        new_bl.innerHTML = bl;
        new_bl.id = "dinfo2_"+v;
        new_bl.style.position = "absolute";
        new_bl.style.top = "10px";
        new_bl.style.left = "10px";
        new_bl.style.zIndex = "2";
        new_bl.style.visibility = "hidden";
        
        this.div_.appendChild( new_bl ); 
    }
    var sizeDiv = document.createElement("div");
    sizeDiv.style.visibility = "hidden";
    sizeDiv.id = "sizeDiv";
    sizeDiv.className="balloon_text";
    sizeDiv.style.width = "auto";
    this.sizeDiv_ = sizeDiv;
    this.div_.appendChild(sizeDiv);
    //
      } else 
      {
        this.div_ = dinfo_div2;
        this.sizeDiv_ = document.getElementById("sizeDiv");
      }
}

// Remove the main DIV from the map pane
DInfo2.prototype.remove = function() {
  if(this.current_!=null)
  {
    this.current_.style.visibility = "hidden";
   
  }
  this.point_ = 0;
}

DInfo2.prototype.Hide = function() {
  this.map_.removeOverlay(this);
}

DInfo2.prototype.Show = function() {
  this.map_.addOverlay(this);
}


// Copy our data to a new DInfo
DInfo2.prototype.copy = function() {
  return new DInfo2(this.point_, this.html_);
}


// Redraw the rectangle based on the current projection and zoom level
DInfo2.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 rectangle
  var c1 = this.map_.fromLatLngToDivPixel(this.point_);
  // Container size //
  var map_cnt = this.map_.getContainer();
  var map_cnt_width = map_cnt.offsetWidth;
  var map_cnt_height = map_cnt.offsetHeight;
  if(map_cnt_width == 0)
  {
    map_cnt_width=810;
    map_cnt_height=365;
  }
  // Visible bounds
  var bnds = this.map_.getBounds(); // LatLanBounds
  var p1 = this.map_.fromLatLngToDivPixel(bnds.getNorthEast());// Point
  var p2 = this.map_.fromLatLngToDivPixel(bnds.getSouthWest());// Point
  // Let's decide sides
  var cnt_x = map_cnt_width +( c1.x- p1.x);
  var cnt_y = c1.y - p1.y;
  
  var leftright =( cnt_x > map_cnt_width/2 )? 'r':'l';
  var topbottom =( cnt_y > map_cnt_height/2 )? 'b':'t';
  
  var sampleDiv = document.getElementById("dinfo2_"+topbottom+leftright);
  var tmp = sampleDiv.getElementsByTagName("span");
  tmp[0].innerHTML = this.html_;
  sampleDiv.style.left = c1.x + "px";
  sampleDiv.style.top = c1.y + "px";
  // sizing //
  this.sizeDiv_.style.width = "auto";
  this.sizeDiv_.innerHTML = this.html_;
  var desired_w = this.sizeDiv_.offsetWidth ? this.sizeDiv_.offsetWidth : 200;
  this.sizeDiv_.style.width = desired_w+"px";
  
  var desired_h = this.sizeDiv_.offsetHeight ?  this.sizeDiv_.offsetHeight : 100;
  this.sizeDiv_.style.width = "auto";
  var child = sampleDiv.firstChild;
  while( child.className != 'rrb' ) child = child.nextSibling; 
  child.style.width = desired_w + 30 + "px";
  var IsIE = navigator.appName.indexOf("Microsoft") != -1;
  var add_margin = 0;
  if(IsIE) add_margin = 10;
  child.style.height = desired_h + dinfo2_margin + add_margin +  "px";
  
  
  //
  
  sampleDiv.style.visibility = "visible";
  
  GEvent.addDomListener(sampleDiv, "mouseover", function() {
       
     SuspendTimer2();
      });
      
  GEvent.addDomListener(sampleDiv, "mouseout", function() {
       
    ResumeTimer2();
      });    
  this.current_ = sampleDiv;

  
  
   
}

function SuspendTimer2()
{
      
      if( kill_timer !=0 )  
        {
            clearTimeout(kill_timer);
            kill_timer = 0;
        }
}

function ResumeTimer2()
{
    if(dinfo2.point_ != 0 )  hideInfo2(dinfo2.point_ );
}
///////


function showInfo2(point,text)
{
  if( kill_timer !=0 )  
  {
        clearTimeout(kill_timer);
        kill_timer = 0;
  }
  if( dinfo2 != null)
    {
        if(dinfo2.point_!=point)
        { 
              map.removeOverlay( dinfo2 );
              dinfo2 = new DInfo2(point,text);
              map.addOverlay( dinfo2 );
        } 
    }else
    {
              dinfo2 = new DInfo2(point,text);
              map.addOverlay( dinfo2 );
    }

}


function hideInfo2(point)
{
    if( dinfo2.point_ == point) // Otherwise there nothing to hide
    {
        if(kill_timer == 0)
       kill_timer = setTimeout("dinfo2.Hide();",KILL_TIME);
    }
}

function killInfo2()
{
    
    if((dinfo2!= null)&&(kill_timer!=0))
    {
        if( dinfo2.point_ != 0 )
        {
            dinfo2.Hide();
        }
    }
}


