﻿

// balloons for Classifieds class //
function BalloonClsf(point,title,html) {
  this.point_ = point;
  this.title_ = title;
  this.html_ = html;
  this.current_ = null;

}

var balloon = null;
var ballon_div = null;

BalloonClsf.prototype = new GOverlay();

// Creates the DIV representing this rectangle.
BalloonClsf.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.width="1px";
div.style.height="1px";
div.style.left = "0";
this.map_ = map;
if(ballon_div == null)
  {
    map.getPane(G_MAP_FLOAT_PANE).appendChild(div);
    this.div_ = div;
    ballon_div = div;
     var sample = document.getElementById("balloon_clsf");
    this.div_.visibility = "hidden";
    this.div_.innerHTML = sample.innerHTML;
    GEvent.addDomListener(this.div_, "mouseover", function() {
       
     SuspendTimerBalloon();
      });
      
    GEvent.addDomListener(this.div_, "mouseout", function() {
     ResumeTimerBalloon();
      });    
  }else 
      {
        this.div_ = ballon_div;
      }

}


// Remove the main DIV from the map pane
BalloonClsf.prototype.remove = function() {
  if(this.current_!=null)
  {
    this.current_.style.visibility = "hidden";
   
  }
  this.point_ = 0;
}

BalloonClsf.prototype.Hide = function() {
  this.map_.removeOverlay(this);
}

BalloonClsf.prototype.Show = function() {
  this.map_.addOverlay(this);
}


// Copy our data to a new DInfo
BalloonClsf.prototype.copy = function() {
  return new BalloonClsf(this.point_,this.title_, this.html_);
}

// Redraw the rectangle based on the current projection and zoom level
BalloonClsf.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 tmp = this.div_.getElementsByTagName("span");
  tmp[0].innerHTML = this.html_;
  var tmp = this.div_.getElementsByTagName("h1");
  tmp[0].innerHTML = this.title_;
  this.div_.style.visibility = "visible";
  var dd = this.div_.firstChild;
  while( String(dd.className).lastIndexOf("balloon_clsf_cnt") == -1) 
  {
     dd = dd.nextSibling;
  } 
  dd.className = "balloon_clsf_cnt_"+topbottom+leftright;
  
  this.div_.style.left = c1.x + "px";
  this.div_.style.top = c1.y + "px";
  
  
  
 
  this.current_ = this.div_;

  
  
   
}

function SuspendTimerBalloon()
{
      
      if( kill_timer !=0 )  
        {
            clearTimeout(kill_timer);
            kill_timer = 0;
        }
}

function ResumeTimerBalloon()
{
    if(balloon.point_ != 0 )  hideInfoBalloon(balloon.point_ );
}
///////


function showInfoBalloon(point,title,text)
{
  if( kill_timer !=0 )  
  {
        clearTimeout(kill_timer);
        kill_timer = 0;
  }
  if( balloon != null)
    {
        if(balloon.point_!=point)
        { 
              map.removeOverlay( balloon );
              balloon = new BalloonClsf(point,title,text);
              map.addOverlay( balloon );
        } 
    }else
    {
              balloon = new BalloonClsf(point,title,text);
              map.addOverlay( balloon );
    }

}


function hideInfoBalloon(point)
{
    if( balloon.point_ == point) // Otherwise there nothing to hide
    {
        if(kill_timer == 0)
       kill_timer = setTimeout("balloon.Hide();",KILL_TIME);
    }
}

function killInfoBalloon()
{
    
    if((balloon!= null)&&(kill_timer!=0))
    {
        if( balloon.point_ != 0 )
        {
           balloon.Hide();
        }
    }
}



