
Button = function(x,y,opt_opts){

    this.opt_opts_      = opt_opts || {};
    this.text_           = opt_opts.text || "";
    this.title_         = opt_opts.title || "";
    this.isActive_       = opt_opts.isActive || false;
    this.isAutoPushOut_  = opt_opts.isAutoPushOut || false;
    this.divbutton_      = null;
    this.background_      = opt_opts.bgColor ||  "white";
    this.color_          = opt_opts.fontColor || "black";
    this.padding_        = opt_opts.padding  ||  "0px";
    this.fontWeight_     = opt_opts.fontWeight || "normal";
    this.fontFamily_     = opt_opts.fontFamily || "Arial";
    this.fontSize_       = opt_opts.fontSize   || "13px";
    
    this.ltborder_       = this.opt_opts_.border1 || 'black';
    this.rbborder_       = this.opt_opts_.border2 || 'white';
    this.bgImage_        = this.opt_opts_.bgImage || ''; 
    this.borderWidth_    = this.opt_opts_.borderWidth || '3px'; 
    
    this.turnOnEvent_     = opt_opts.turnOnEvent || function(){}
    this.turnOffEvent_    = opt_opts.turnOffEvent || function(){}
    this.turnedOn_ =       opt_opts.turnenOn || function(){}   
    this.turnedOff_ =       opt_opts.turnedOff || function(){}   
  
  
    this.beforeOff_ =       opt_opts.beforeOn || function(){}   
    this.beforeOn_ =        opt_opts.beforeOff|| function(){}   
  
    this.afterOff_ =       opt_opts.afterOn || function(){}   
    this.afterOn_ =        opt_opts.afterOff|| function(){}   
  
  
     
   
   
    this.width_           = opt_opts.width || 20;
    this.height_          = opt_opts.height || 20;
    
    this.xPos_ = x;
    this.yPos_ = y;
    
};

Button.prototype = new GControl();



Button.prototype.disactive = function()
{
     this.setButtonInStyle_();
     this.isActive_ = false;
}
Button.prototype.active = function()
{
     this.setButtonOutStyle_();
     this.isActive_ = true;
}




Button.prototype.pushIn = function()
{
        if(this.isActive_){
            this.setButtonInStyle_();
            this.isActive_ = false;
            this.turnOnEvent_();
        }
}



Button.prototype.pushOut = function()
{
     if(!this.isActive_){
         this.setButtonOutStyle_();
         this.isActive_ = true;
         this.turnOffEvent_();
    }

}
Button.prototype.isPushed = function()
{
    return !this.isActive_;
}


Button.prototype.OnClickEvent_ = function(){
        if(this.isActive_)
        {
            //alert('on');
            
            this.setButtonInStyle_();
            this.beforeOff_();
            this.turnOnEvent_();
            this.afterOff_();
        
            this.isActive_ = false;
            
            if(this.isAutoPushOut_){
                var me = this;
                setTimeout(function(){me.OnClickEvent_()},500);
            }
                
            
        }
        else
        {
            this.setButtonOutStyle_();
            this.beforeOn_();
            this.turnOffEvent_();
            this.isActive_ = true;
            this.afterOn_();
        
        }

};

//Button.prototype.turnOnEvent = function(){}
//Button.prototype.turnOffEvent = function(){}



Button.prototype.initialize = function(map)
{
  var container = document.createElement("div");
  this.divbutton_ = document.createElement("div");
  
  container.appendChild(this.divbutton_);
  map.getContainer().appendChild(container);

  
  var me = this;

  this.divbutton_.appendChild(document.createTextNode(this.text_));
  this.divbutton_.title = this.title_;
  
  this.setButtonStyle_();
  if(this.isActive_)
     this.setButtonOutStyle_();
  else
     this.setButtonInStyle_();
  
  
  this.divbutton_.style.width=this.width_+'px';
  this.divbutton_.style.height=this.height_+'px';
  
  
  GEvent.addDomListener(this.divbutton_, "click",   function (e){
  me.OnClickEvent_(e)});
  
  
  if(this.bgImage_ != ''){
      this.divbutton_.style.backgroundImage='url(\''  +this.bgImage_+ '\')';
  }
  return container;
}


Button.prototype.initOnContainer = function(divCont)
{
  var container = document.createElement("div");
  
  this.divbutton_ = document.createElement("div");
  container.appendChild(this.divbutton_);
  divCont.appendChild(container);
  var me = this;

  this.divbutton_.appendChild(document.createTextNode(this.text_));
  this.divbutton_.title = this.title_;
  this.setButtonStyle_();
  
  if(this.isActive_)
     this.setButtonOutStyle_();
  else
     this.setButtonInStyle_();
  
  
  this.divbutton_.style.width=this.width_+'px';
  this.divbutton_.style.height=this.height_+'px';
 
  GEvent.addDomListener(this.divbutton_, "click",   function (e){
 
  me.OnClickEvent_(e)});
  
  if(this.bgImage_ != '')
      this.divbutton_.style.backgroundImage='url(\''  +this.bgImage_+ '\')';
  
  
   container.setAttribute('style', 'position:absolute');
   container.style.left = this.xPos_ + 'px';
   container.style.top = this.yPos_ + 'px';
   return container;
}

Button.prototype.removeFromCont = function()
{
    var parent =  this.divbutton_.parentNode;
    parent.removeChild(this.divbutton_);
    //this.divbutton_.outerHTML = "";//ie   

}



Button.prototype.getDefaultPosition = function() {
    var me = this;  
    return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(this.xPos_, this.yPos_));
}

Button.prototype.getDiv = function()
{
    return this.divbutton_;
}

Button.prototype.setBackgroundImage = function(path)
{
    this.bgImage_ = path;
    this.divbutton_.style.backgroundImage='url(\''  +path+ '\')';      
}
Button.prototype.setBackgroundColor = function(color)
{
    this.divbutton_.style.backgroundColor = "white";
}
Button.prototype.setLtBorderColor = function(color)
{
    this.ltborder_ = color;
}
Button.prototype.setRbBorderColor = function(color)
{
    this.rbborder_=color;
}

/*
Button.prototype.setWidthHeight = function(w,h)
{
    this.divbutton_.style.width=w+'px';
    this.divbutton_.style.height=h+'px';
}
*/
Button.prototype.setButtonStyle_ = function() {
  this.divbutton_.style.textDecoration = "none";
  this.divbutton_.style.color = "#0000cc";
  this.divbutton_.style.font = "small Arial";
  this.divbutton_.style.padding = "0px";
  this.divbutton_.style.marginBottom = "3px";
  this.divbutton_.style.textAlign = "center";
  this.divbutton_.style.width = "6em";
  this.divbutton_.style.cursor = "pointer";
  //control.style.width = "20px";
  this.divbutton_.style.height = "20px";
  this.divbutton_.style.border = "2px solid black";
  this.divbutton_.style.fontFamily = this.fontFamily_;
  this.divbutton_.style.color = this.color_;
  this.divbutton_.style.padding = this.padding_;
  this.divbutton_.style.fontWeight = this.fontWeight_;
  this.divbutton_.style.fontSize = this.fontSize_;
   
}


Button.prototype.setButtonInStyle_ = function()
{
  this.divbutton_.style.borderBottom = this.borderWidth_ +  " solid " +this.ltborder_;
  this.divbutton_.style.borderRight = this.borderWidth_ + " solid " +this.ltborder_;
  this.divbutton_.style.borderTop = this.borderWidth_ + " solid " + this.rbborder_;
  this.divbutton_.style.borderLeft = this.borderWidth_ + " solid " + this.rbborder_;
}
Button.prototype.setButtonOutStyle_ = function()
{
  this.divbutton_.style.borderBottom = this.borderWidth_ + " solid " +this.rbborder_;
  this.divbutton_.style.borderRight = this.borderWidth_ + " solid " +this.rbborder_;
  this.divbutton_.style.borderTop = this.borderWidth_ + " solid " + this.ltborder_;
  this.divbutton_.style.borderLeft = this.borderWidth_ + " solid " + this.ltborder_;
  
  
}

Button.prototype.toString = function(){
    return '[Object Button]'
}
