// <![CDATA[

/***********************************************************
 * Message class
 * 
 * Used to create a single informational window in
 * the middle of the map.
 ****************************/
function GMessageController(blocking,showloading) {
  this.blocking = blocking;
  this.messagetext_style = {fontWeight:'bold',color:'#333333'};
  this.message_style = {display:'none',position:'relative',backgroundColor:'#FFFFFF',opacity:0.7,filter:'alpha(opacity=70)'};
  this.messageloading_style = {display:'none',float:'left',position:'absolute',height:'15px',width:'15px'};
  this.showloading = showloading;
  this.msgs_ = {};
  this.order_ = [];
  this.iorder_ = {};
  GControl.call(this,this._printable,this._selectable);
}

GMessageController.prototype = new GControl();

GMessageController.prototype.setMessage = function(msg,id) { 
  this.msgs_[id] = msg || "Loading...";
  this.moveToTop(id); 
  this.textdiv_.innerHTML = this.msgs_[this.order_[this.order_.length-1]];
  this.redraw(1);
}


GMessageController.prototype.moveToTop = function(id) { 
  if ( typeof(this.iorder_[id]) !== "undefined" && this.iorder_[id] !== null ) {
     this.order_.splice(parseInt(this.iorder_[id]),1); 
  }
  this.order_.push(id); 
  for ( var i = 0; i < this.order_.length; i++ ) {
    this.iorder_[this.order_[i]] = i;
  }
}

GMessageController.prototype.isShown = function() { 
  return this.shown;
}

/****************************
 * Message.initialize
 * 
 * creates the actual message
 ****************************/
GMessageController.prototype.initialize = function(map) {
  this.container=map.getContainer();

  // Create the DIV representing our message
  var div = document.createElement("div");
  div.className="message";
  div.id="message";

  var textcontainer = document.createElement("div");
  textcontainer.className="messagetext";
  textcontainer.id="messagetext";

  this.loadimg = '/images/loading.gif';
  this.loadimgheight = 15;
  this.loadimgwidth = 15;

  var img = document.createElement("img");

  img.src = this.loadimg;
  img.id = "messageloading";

  this.div_ = div;
  this.textdiv_ = textcontainer;
  this.img_ = img;
  this.container.appendChild(this.div_);
  this.div_.appendChild(textcontainer);
  this.div_.appendChild(img);
  for(var k in this.messageloading_style) { 
    this.img_.style[k] = this.messageloading_style[k];
  }
  for(var k in this.messagetext_style) { 
    this.textdiv_.style[k] = this.messagetext_style[k];
  }
  for(var k in this.message_style) { 
    this.div_.style[k] = this.message_style[k];
  }
 
  if ( this.order_.length > 0 )
    this.textdiv_.innerHTML = this.msgs_[this.order_[this.order_.length-1]];
  GEvent.trigger(this,'initialize');
  return this.div_;
}

/****************************
 * Message.remove
 * 
 * Remove the main DIV from the map pane
 ****************************/
/*
Message.prototype.remove = function() {
  this.div_.parentNode.removeChild(this.div_);
}
*/

/***************************
 * Message.hide
 * 
 * Hide the message.
 ****************************/
GMessageController.prototype.hide = function(id) {
  if ( typeof(this.iorder_[id]) != "undefined" && this.iorder_[id] != null ) {
    if ( this.order_.length > 1 ) {
      this.moveToTop(id);
      this.deleteMessage(id);
      this.redraw();
    } else {
      this.div_.style.display="none";
      this.shown = false;
      this.deleteMessage(id);
    }
  } 
}

GMessageController.prototype.printable = function() {
   return false;
}


GMessageController.prototype.selectable = function() {
   return false;
}

GMessageController.prototype.setStyle = function(elname,style) {
   var s = elname + "_style";
   for ( var k in style ) {
     this[s][k] = style[k];
   }
   var div = document.getElementById(elname);
   for( var k in this[s][k] ) {
     div.style[k] = this[s][k];
   }
   if ( this.container ) return this.redraw();
   return 0;
}

GMessageController.prototype.getDefaultPosition = function() {
   new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(0,0));
}

GMessageController.prototype.deleteMessage = function(id) {
   delete this.msgs_[id];
   this.order_.splice(parseInt(this.iorder_[id]),1);
   delete this.iorder_[id];
}

/***************************
 * Message.show
 * 
 * Display the message.
 ****************************/
GMessageController.prototype.show = function(id) {
   this.moveToTop(id);
   this.div_.style.display="block";
   this.shown = true;
   this.redraw(1);
}


/***************************
 * Message.redraw
 * 
 * Redraw on all map actions
 ****************************/
GMessageController.prototype.redraw = function(force) {
  if(!force) { 
    return;
  }

  var cw = this.container.clientWidth;
  var ch = this.container.clientHeight;

  if ( this.order_.length > 0 ) {
    this.textdiv_.innerHTML = this.msgs_[this.order_[this.order_.length-1]];
  } else {
    this.textdiv_.innerHTML = "";
  }
  
  var w = this.div_.clientWidth;
  var h = this.div_.clientHeight;
  this.img_.src = this.loadimg;
  var imageloff = Math.round((cw - this.loadimgwidth)/2);
  var imagetoff = ch/2 - this.loadimgheight - 15;

  this.img_.style.left = imageloff + "px";
  this.img_.style.top = imagetoff + "px";
 
  if ( this.blocking ) {
      if ( this.showloading ) {
          this.img_.style.display = "block";
      } else {
          this.img_.style.display = "none";
      }
      this.textdiv_.style.lineHeight = ch + "px";
      this.div_.style.height = ch + "px";
  } else {
      this.textdiv_.style.left = ((cw-w)/2) + "px";
      this.textdiv_.style.top = ((ch-h)/2) + "px";
      this.textdiv_.style.padding = "20px";

  }

}

// ]]>
