/**
 * RealEstateInvestor tooltips
 * 
 */

ShowTooltip = function (owner){
  Tooltip._show(owner,arguments);
}

function ShowHint(owner,value,title,hovered_class){
	if (typeof(hovered_class)!='undefined'){
		var old_class = owner.className;

		Tooltip.Show(owner,
			'text',value,'Title',title,'width','auto','position','mouse',
			'onmouseover',function(){owner.className=hovered_class},
			'onmouseout',function(){owner.className=old_class});
	}
  	else {
		Tooltip.Show(owner,'text',value,'Title',title,'width','auto','position','mouse');
  	}
  	owner.alt = "";	
  	owner.title = "";
}

/** 
 * Constructor of tooltip object
 */
 
function Tooltip(owner){
  this.owner = owner;
  this.h_s = new Array();//hidden_selects
}

Tooltip.Show = function (owner){
  Tooltip._show(owner,arguments);
}

Tooltip._show = function(owner,args){
  if(typeof(document.readyState)!='undefined'){ //IE
  	if (document.readyState!='complete'){
		return;
	}
  }
  var tooltip = owner.tooltip;
  if (typeof(tooltip)=='undefined'){
    var tooltip = owner.tooltip = new Tooltip(owner);	
    if (((args.length-1) % 2) == 0){
      for (var i=0;i<((args.length-1)/2);i++){
	    property_name = args[i*2+1];
	    if (typeof(tooltip[property_name])!='undefined'){
		  tooltip[property_name] = args[i*2+2];
		}
		else {
		  alert('wrong property'+property_name);
		}
	  }
    }
	else {
	  alert('count of arguments should be odd!');
	}
	tooltip.init();		
  }
}

Tooltip.ShowUserHint = function(owner,value,title,hovered_class){
	if (typeof(hovered_class)!='undefined'){
		var old_class = owner.className;

		Tooltip.Show(owner,
			'text',value,'Title',title,'width','auto','position','user',
			'onmouseover',function(){owner.className=hovered_class},
			'onmouseout',function(){owner.className=old_class});
	}
  	else {
		Tooltip.Show(owner,'text',value,'Title',title,'width','auto','position','user');
  	}
  	owner.alt = "";	
  	owner.title = "";
}

Object.extend(Tooltip.prototype,{
  name:null,
  modal:false, 
  oncreate:null,
  onmouseout:null,
  onmouseover:null,
  
  position:'owner', //'mouse, user';
  xbase:0,
  ybase:0,
  
  Title:null,  
  text:null,
  div:null,
  autohide:true,
  left:-1,
  top:-1,
  autohide_ie_fix:true,
  x_offset:0,
  y_offset:0,
  

  hide_timeout:300,
  hide_timer:null,
  
  show_timeout:500,
  show_timer:null,
  show_close_button:false,
  width:null,
  
  old_onmousemove:null,  
  title_div:null, 
  hint_div:null,
  
  
  shadow_size:8,
  animation_timeout:50,
  animation_timer:null,
  glass:null,

  de:document.documentElement,
  d:document,
  
  init:function(){
  	var this1 = this;

  	if (this.modal) {
    	this.createGlass();
  	}  
  
  	var hint = this.hint_div =  this.createEl("div");
  	hint.className = "tooltip";
  	this.d.body.appendChild(hint);
  
  	if (this.width!=null){
    	hint.style.width = this.width;
  	}
  
  	var brdr = this.createEl("div");
  	brdr.className = "border";
  	hint.appendChild(brdr);

  	if ((this.Title!=null) && (this.Title!='')){
    	var t = this.createEl('div');
    	t.innerHTML = this.Title;
    	t.className = "title";
    	t.onmousedown = function(e) {this1.startDragging(e)};
    	t.onmouseup = function(e) {this1.stopDragging(e)};
		brdr.appendChild(t);
		this.title_div = t;
  	}
  
  	var content = this.createEl('div');
  	content.className = "content";
  	brdr.appendChild(content);
  
  	if (this.div!=null){
    	var d = this.d.getElementById(this.div);
    	d.parentNode.removeChild(d);
    	d.style.display = "block";
    	content.appendChild(d);
    	this.disableAutohideForInputs(d);
  	}
  	if (this.text!=null) {
    	content.innerHTML = this.text;
  	}
  
  	if (this.show_close_button){
    	this.createCB();
  	}
  
  	this.createShadow();
  
  	if (!this.modal){
  		this.initActions();
  	}  
  
  	if (this.oncreate!=null){
    	this.oncreate();
  	}
  	if (this1.onmouseover!=null){
		this1.onmouseover(); 
  	}
  
  	this.fixSInIE();
  	this.show();
  	
  },
  createGlass:function(){
    var g = this.glass = this.createEl('div',{opacity:0.3,filter:"alpha(opacity:30)",position:'absolute',left:'0px',top:'0px',width:this.getWinW()+'px',height:this.getWinH()+'px',background:'black',display:'none'});
    g.innerHTML = '&nbsp';
    this.d.body.appendChild(g);  
  },
  disableAutohideForInputs:function(d){
    var inputs = d.getElementsByTagName('INPUT');
    for(var i=0;i<inputs.length;i++){
      var input = inputs[i];
      if ((input.type=='text') || (input.type=='password')){
         var this1 = this;
         if(!input.onfocus){
           input.onfocus = function(){this1.autohide_saved = this1.autohide; this1.autohide = false};
         }
         if(!input.onblur){
           input.onblur = function(){this1.autohide = this1.autohide_saved;};
         }
      }
    }
  },
  createEl:function(name,style){
   var el = this.d.createElement(name);
   for(key in style){
     if (typeof(style[key])!='function'){
       el.style[key] = style[key]; 
     }
   }
   return el;
  },
  createSImg:function(src,style){
	style.position="absolute";
	var sh1 = this.createEl('img',style); 
	this.hint_div.appendChild(sh1);
	sh1.alt=""; 
	sh1.src=src;
	return sh1;
  },
  createShadow:function(){
  	with(this){
		createSImg(Tooltip.img_base+"sh_rt.png",{width:"8px",height:"8px",right:"0px",top:"0px"});
		createSImg(Tooltip.img_base+"sh_rb.png",{width:"8px",height:"8px",right:"0px",bottom:"0px"});
		createSImg(Tooltip.img_base+"sh_lb.png",{width:"8px",height:"8px",left:"0px",bottom:"0px"});
		createSImg(Tooltip.img_base+"sh_b.png",{width:(hint_div.offsetWidth-16) + "px",height:"8px",left:"8px",style:"0px"});
		createSImg(Tooltip.img_base+"sh_r.png",{width:"8px",height:(hint_div.offsetHeight-16) + "px",right:"0px",top:"8px"});
	}
  },
  startDragging:function(e){
	var this1 = this;
	with(this){
		this.old_onmousemove = d.onmousemove; 
		if (typeof(event)!='undefined') {
			var newx = (event.clientX+de.scrollLeft);
			var newy = (event.clientY+de.scrollTop);
		}
		else {
			var newx = (e.pageX);
			var newy = (e.pageY);	  
		}
		this.drag_left_offset = newx - getPOL(this.hint_div);
		this.drag_top_offset =  newy - getPOT(this.hint_div);
		d.onmousemove = function(e){this1.trackMM(e);return false;};
	}
  },
  stopDragging:function(e){
	this.d.onmousemove = this.old_onmousemove; 
	this.old_onmousemove = null;
  },
  trackMM:function(e){
	var newx;
	var newy;
	if (typeof(event)!='undefined') {
	  newx = (event.clientX+this.de.scrollLeft);
	  newy = (event.clientY+this.de.scrollTop);
	}
	else {
	  newx = (e.pageX);
	  newy = (e.pageY);	  
	}
	
	this.hint_div.style.left = newx - this.drag_left_offset + 'px';
	this.hint_div.style.top = newy - this.drag_top_offset + 'px';	

	this.hideSInIE();
  },
  /**
   * if IE opening dropdown menu for select element causing onmouseout event 
   * which brings to closing tooltip window
   * as workaround we register onclick event and set autohide property to false
   */
  fixSInIE:function(){
  if (Object.isIE){
    var inner_selects = this.hint_div.getElementsByTagName("select");
    for(j=0;j<inner_selects.length;j++){  //registed unclick to disable hidding
	  var this1 = this;
	  inner_selects[j].onclick = function(){
	    this1.autohide_ie_fix = false;
	  }
    }
  }
  },
  createCB:function(){
  if (this.title_div==null){
    var titles = this.hint_div.getElementsByTagName('h5');
	if (titles.length!=0){
	  this.title_div = titles[0];
	}
  }
  if (this.title_div==null){
    var t = this.createEl('div');
    t.className = "title";
    var t_img = this.createEl('IMG');
	t_img.src = '/images/spacer.gif';
	t_img.width = 21;
	t_img.height = 21;
	t.appendChild(t_text);
	this.hint_div.appendChild(t);
  }
  
  var c_img  = this.createEl('IMG',{height:'16px',width:'16px',cursor:'pointer'});
  c_img.src = '/images/delete.png';
  c_img.align = 'right';
  c_img.vspace = '1';
  c_img.hspace = '1';
  
  var this1 = this;
  c_img.onclick = function(){
    this1.hideDelayed();
  }
  this.title_div.insertBefore(c_img,this.title_div.firstChild);
  },
  initActions:function(){
  var this1 = this;
  this.owner.onmouseover = function(){
  	if (this1.visible){
      this1.cancelH();
	}
	else {
	  if (!this1.is_showing){
	    this1.show();
	  }
	}
	if (this1.onmouseover!=null){
		this1.onmouseover(); 
	}		
  };
  
  this.owner.onmouseout  = function() {
    if (this1.visible){
      this1.hideTooltip();
	}
	else {
	  this1.stopShowTimer();
	}
	if (this1.onmouseout!=null){
		this1.onmouseout();
	}
  };
  
  this.hint_div.onmouseover = function(){
    this1.autohide_ie_fix = true;
    this1.cancelH();
    if (this1.onmouseover!=null){
		this1.onmouseover();
	}
  }

  this.hint_div.onmouseout  = function(){
    if (this1.autohide && this1.autohide_ie_fix){
      this1.hideTooltip();
    }
	if (this1.onmouseout!=null){
		this1.onmouseout();
	}    
  }
  },
  startMouseTracking:function(){
	var this1 = this; 
	this.old_onmousemove = this.d.onmousemove;
  	this.d.onmousemove = function(e) {this1.trackMouseMove(e)};
  },

  stopMouseTracking:function(){
		this.d.onmousemove = this.old_onmousemove; 
		this.old_onmousemove = null;
  },

  trackMouseMove:function(e){
	if (typeof(event)!='undefined') {
	  	this.xbase = event.clientX+this.de.scrollLeft;
	  	this.ybase = event.clientY+this.de.scrollTop;
	}
	else {
		this.xbase = e.pageX;
		this.ybase = e.pageY;
	}
  },
  
  show:function(){
  if (this.modal){
    this.showPanel();
  }
  else {
    if (this.show_timer==null){
  	  if ((this.position=='mouse') || (this.position=='user')) {
  		this.startMouseTracking();
  	  }
  	
      var this1 = this;
	  var f = function(){
	    this1.showPanel();
	  }  
      this.show_timer = setTimeout(f,this.show_timeout);
    }
  }
  },

  showPanel:function(){
      var this1 = this;
	  var note_top,note_left,note_width,note_heigh,top,left;
	  if (this.modal==true){
			var top 	= (this.getWinH()-this.hint_div.offsetHeight)/2;
	  		var left 	= (this.getWinW()-this.hint_div.offsetWidth)/2;;
	  }
	  else if (this.position=='user'){
			var top 	= this.getPOT(this.owner)-this.hint_div.offsetHeight/3+this.owner.offsetHeight;
	  		var left 	= this.getPOL(this.owner)-(this.hint_div.offsetWidth-this.owner.offsetWidth)/2+4;
	  }
	  else {
		if (this.position=='mouse'){
			this.stopMouseTracking();
			note_top 	= this.ybase;
	  		note_left 	= this.xbase;
	  		note_width 	= 1;
	  		note_height = 1;
		}
		else {
	  		note_top 	= this.getPOT(this.owner);
	  		note_left 	= this.getPOL(this.owner);
	  		note_width 	= this.owner.offsetWidth;
	  		note_height = this.owner.offsetHeight;
		}
	  	
      	if (((note_left-this.getScrX())<this.getWinW()/2) 
	    &&((note_top-this.getScrY())<this.getWinH()/2)){
		 var top = note_top+note_height+18;
      	 var left = note_left+10; 
	  	}
	  	else if (((note_left-this1.getScrX()+note_width)>this.getWinW()/2)
	  	&&((note_top-this.getScrY())<this.getWinH()/2)){
		var top = note_top+note_height+18;
      	var left = note_left-this.hint_div.offsetWidth+note_width+this.shadow_size;
	  	}
	  	else if (((note_left-this.getScrX()+note_width)>this.getWinW()/2)
	  	&&((note_top-this.getScrY()+note_height)>this.getWinH()/2)){
		var top = note_top-this.hint_div.offsetHeight+this.shadow_size;
      	var left = note_left-this.hint_div.offsetWidth+note_width+this.shadow_size;
	  	}
	  	else {
		var top = note_top-this.hint_div.offsetHeight+this.shadow_size;
		var left = note_left; 
	  	}

	  	if (Object.isSafari){
	  		var top = top + this.d.body.scrollTop;	
	  		var left = left + this.d.body.scrollLeft;
	  	}
	  }
      this1.showTooltip(left,top);  	
	  this1.show_timer = null;
  },

  stopShowTimer:function(left,top){
  if (this.show_timer!=null){
  	
  	if ((this.position=='mouse') || (this.position=='user')){
  		this.stopMouseTracking();
  	}
  	
    clearTimeout(this.show_timer);
	this.show_timer = null;
  }
  },

  showTooltip:function(left,top){
  this.left = left     = this.checkWinX(left+this.x_offset);
  this.top = top      = this.checkWinY(top+this.y_offset);
  var newx = this.hint_div.style.left       = left + "px";
  var newy = this.hint_div.style.top        = top + "px"; 
  this.hideSInIE();
  this.startBorderAnimation();
  this.visible = true;
  },

  startBorderAnimation:function(){
  	var count_steps = this.count_steps = 3;
  	if ((this.position=='mouse') || (this.position=='user')){
  		this.b_left		= this.xbase;
  		this.b_top	 	= this.ybase;
  		this.b_height 	= 1;
  		this.b_width 	= 1;  	
  	}
  	else {
  		this.b_left	= this.getPOL(this.owner);
  		this.b_top	 	= this.getPOT(this.owner);
  		this.b_height 	= this.owner.offsetHeight;
  		this.b_width 	= this.owner.offsetWidth;
  	}
  
  	if (Object.isSafari){
  		this.b_left	= this.b_left + this.d.body.scrollLeft;
  		this.b_top	 	= this.b_top + this.d.body.scrollTop;
  	}
  	this.b_left_step	  = (this.left-this.b_left)/count_steps;
  	this.b_top_step	  = (this.top-this.b_top)/count_steps;
  	this.b_height_step = (this.hint_div.offsetHeight-this.b_height-this.shadow_size)/count_steps;
  	this.b_width_step  = (this.hint_div.offsetWidth-this.b_width-this.shadow_size)/count_steps;
  	var this1 = this;
  	if (typeof(this.brdr)=='undefined'){
    	this.brdr = this.createEl('div',{border:'1px solid gray',position:'absolute',overflow:'hidden'});
    	this.brdr.innerHTML = '  ';
		this.d.body.appendChild(this.brdr);
  	}
  	this.setBorderSizes();  
  	this.brdr.style.display = 'block';
  	var this1 = this;
  	this.animation_timer = window.setTimeout(function(){this1.borderIteration()},this.animation_timeout);
  },

  setBorderSizes:function(){
    with(this){
  		brdr.style.left 	= b_left+'px';
  		brdr.style.top		= b_top+'px';
  		brdr.style.width	= b_width+'px';
  		brdr.style.height	= b_height+'px';
    }
  },
 
  borderIteration:function(){
  	with(this){
  		if (count_steps>0){
  			b_left	  = b_left   + b_left_step;
			b_top	  = b_top	  + b_top_step;
  			b_height  = b_height + b_height_step;
  			b_width   = b_width  + b_width_step;  
			setBorderSizes();
  			count_steps--;
			var this1 = this;
  			this.animation_timer = window.setTimeout(function(){this1.borderIteration()},animation_timeout);
  		}
  		else {
    		brdr.style.display = 'none'; 
    		hint_div.style.visibility = "visible";
			animation_timer = null;
	
			if (modal==true){
	  			glass.style.display = 'block';
	  			glass.style.width = getWinW()+'px';
	  			glass.style.height = d.body.clientHeight+'px';
			}
  		}
  	}
  },

  hideTooltip:function(){
  	with(this){
  		if (hide_timer==null){
    		var this1 = this;
			var f = function(){
	  			this1.hideDelayed();
	  			this1.hide_timer = null;
			}
    		hide_timer = setTimeout(f,hide_timeout);
  		}
  	}
  },

  hideDelayed:function(){  
    with(this){
  		if (d.focus) d.focus(); //for IE
  		hint_div.style.visibility = "hidden";
  		showSInIE();
  		visible = false;
  		if (old_onmousemove!=null){ //if this hides when dradding
  			d.onmousemove = old_onmousemove; //alert(this.old_onmousemove);
			old_onmousemove = null;
  		}
  
  		if (modal==true){
	  		glass.style.display = 'none';
  		}
    }
  },

  cancelH:function(){
  	with(this){
  		if (hide_timer!=null){  
    		clearTimeout(hide_timer);
			hide_timer = null;
  		}
  	}
  },

  getWinW:function(){
  	return this.de.clientWidth || 0;
  },

  getWinH:function(){
  	return this.de.clientHeight || 0;
  },

  getScrX:function(){
  	return this.de.scrollLeft;// || window.scrollX || 0;
  },

  getScrY:function(){
    return this.de.scrollTop;// || window.scrollY || 0;
  },

/**
 * Takes a note and a proposed X position, and modifies the position so that
 * the note will fit within the border of the visible browser window.
 */
  checkWinX:function(newX) { 
  	with (this){
    	if (newX<getScrX()-5){
	  		return getScrX()+5;
		}
		if((newX+this.hint_div.offsetWidth+5)>(getScrX() + getWinW())){
	  		return getScrX() + getWinW() - this.hint_div.offsetWidth-5; 
		}
		return newX;
  	}
  },

  checkWinY:function(newY) { 
  with (this){
    if (newY<getScrY()-5){
	  return getScrY()+5;
	}  
    if((newY+this.hint_div.offsetHeight+5)>(getScrY() + getWinH())){
	  return getScrY() + getWinH() - this.hint_div.offsetHeight-5; 
	}
	return newY;
  }
  },

/**
 * in IE selects are always on top so we shold hide
 * selects inderneath option box
 */
  hideSInIE:function(){
  if (Object.isIE){	
	var selects = this.d.getElementsByTagName("select");
	var inner_selects = this.hint_div.getElementsByTagName("select");
	var rect = this.hint_div.getBoundingClientRect();
	var i;var j;var k;
	for(i=0;i<selects.length;i++){
		var sel = selects[i];
		var is_inner = false;
		for(j=0;j<inner_selects.length;j++){
			if (inner_selects[j]==sel){
			  is_inner = true;
			}
		}
	    if ((!is_inner) && (this.selectIntersects(sel,rect))){ //should to hide
			sel.style.visibility = 'hidden'; 
		    this.h_s.push(sel);
		}
		else {
		  var num;
		  if ((num = this.isHidden(sel))!=-1){
		  	sel.style.visibility = 'visible';
			this.h_s.splice(num,1); 
		  }
		}
	}
  }
  },
/**
 * finds 'sel' among hidden element returns it's key if found
 * else returns -1
 */
  isHidden:function(sel){
  var i;
  for(i=0;i<this.h_s.length;i++){
    if (this.h_s[i]==sel) return i;
  }
  return -1;
  },

  showSInIE:function(){
  if (Object.isIE){
	  while(this.h_s.length>0){
	    var sel = this.h_s.pop();
		sel.style.visibility = 'visible'; 
	  }
  }
  },

  selectIntersects:function(sel,r){
	  var r1 = sel.getBoundingClientRect();
	  if ((r1.left>r.left) 
	  && (r1.left<r.right) 
	  && (r1.top>r.top) 
	  &&(r1.top<r.bottom)) return true;
	  
	  if ((r1.right>r.left) 
	  && (r1.right<r.right) 
	  && (r1.top>r.top) 
	  &&(r1.top<r.bottom)) return true;
	  
	  if ((r1.right>r.left) 
	  && (r1.right<r.right) 
	  && (r1.bottom>r.top) 
	  &&(r1.bottom<r.bottom)) return true;
	  
	  if ((r1.left>r.left) 
	  && (r1.left<r.right) 
	  && (r1.bottom>r.top) 
	  &&(r1.bottom<r.bottom)) return true;
	  return false;
  },

  getPOL:function(obj){
	var x;
    x = obj.offsetLeft;
    if (obj.offsetParent != null)
    x += this.getPOL(obj.offsetParent);
    return x;		
  },

  getPOT:function(obj){
	var y;
    y = obj.offsetTop;
    if (obj.offsetParent != null)
    y += this.getPOT(obj.offsetParent);
    return y;
  }
});

Tooltip.images_preloaded = new Array();

Tooltip.preloadImage = function(url){
    var v = new Image();
    v.src = url;
    Tooltip.images_preloaded.push(v);
}

Tooltip.preloadImages = function(){    
	Tooltip.preloadImage(Tooltip.img_base+"sh_rt.png");
	Tooltip.preloadImage(Tooltip.img_base+"sh_rb.png");
	Tooltip.preloadImage(Tooltip.img_base+"sh_lb.png");
	Tooltip.preloadImage(Tooltip.img_base+"sh_b.png");
	Tooltip.preloadImage(Tooltip.img_base+"sh_r.png");
}
Tooltip.img_base = '/images/rcorners/shadow/';
Tooltip.preloadImages();