/*
 function getAbsoluteXY(elem){
 var x=0,y=0;
  do{
   y+=elem.offsetTop || 0;
   x+=elem.offsetLeft || 0;
   var elem=elem.offsetParent;
  }while(elem);
 return [x,y];
 }
 
 
 function getWindowDimensions(){
 var x=0,y=0;
  if (typeof(window.innerWidth)=='number') {
  var x=window.innerWidth;
  var y=window.innerHeight;
  } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
  var x=document.documentElement.clientWidth;
  var y=document.documentElement.clientHeight;
  } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
  var x=document.body.clientWidth;
  var y=document.body.clientHeight;
  }
 return [x,y];
 }
 
 function getWindowScroll(){
 var x=0,y=0;
 if (typeof(window.pageYOffset)=='number') {
  var x=window.pageXOffset;
  var y=window.pageYOffset;
  } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
  var x=document.body.scrollLeft;
  var y=document.body.scrollTop;
  } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
  var x=document.documentElement.scrollLeft;
  var y=document.documentElement.scrollTop;
  }
 return [x,y];
 }
 
*/


// alert("sss");
(function popupDiv(){
var timer, on_timer; // var not need here ?
var popupDiv = window.popupDiv = function(params) {
    // The object is actually just the init constructor 'enhanced'
    return new popupDiv.fn.init(params);
};

var params;


popupDiv.fn = popupDiv.prototype = {
    pd_prefix: 'pd_',
    dv_suffix: 'dv_',
    hp_suffix: 'hp_',
    hp_tagType: 'div',
    dv_tagType: 'div',
    hp_class_on: '',
    hp_class_off: '',
    shift_x: 20,
    shift_y: 0,
    shift_x_rel: '',
    shift_y_rel: '', // otions are: clientHeight
    off_time: 750,
    on_time: 0,
    state: 'off',   // our state: when "on" - the div is visible, when "off" - not visible
    isHideSelects: 1, // do we hide "select" elements in buggy IE ?
    
    init: function (params){
     if(params){
//      alert(params);
      this.params = params;
      if(params.pd_prefix) this.pd_prefix = params.pd_prefix;
      if(params.dv_suffix) this.dv_suffix = params.dv_suffix;
      if(params.hp_suffix) this.hp_suffix = params.hp_suffix;
      if(params.fn_turnOn) this.fn_turnOn = params.fn_turnOn;
      if(params.fn_turnOff) this.fn_turnOff = params.fn_turnOff;
      if(params.shift_x) this.shift_x = params.shift_x;
      if(params.shift_y) this.shift_y = params.shift_y;
      if(params.shift_x_rel) this.shift_x_rel = params.shift_x_rel;
      if(params.shift_y_rel) this.shift_y_rel = params.shift_y_rel;
      if(params.hp_class_on) this.hp_class_on = params.hp_class_on;
      if(params.hp_class_off) this.hp_class_off = params.hp_class_off;
      if(params.off_time) this.off_time = params.off_time;
      if(params.on_time) this.on_time = params.on_time;
      if(params.isHideSelects) this.isHideSelects = params.isHideSelects;
      if(params.hp_tagType) this.hp_tagType = params.hp_tagType;
      if(params.dv_tagType) this.dv_tagType = params.dv_tagType;
     }
     return this;
    },
    
    fn_addHotPoint: function (divHot, params){
     var divName, div_hotpoint;
     if((typeof divHot == "string" || typeof divHot == "object")){
      if(typeof divHot == "string"){
       divName = divHot;
       div_hotpoint = document.getElementById(divName);
      }else{
       divName = divHot.id;
       div_hotpoint = divHot;
      }
      
       if(params)
        this.init(params);

      var c = this.pd_prefix.length + this.hp_suffix.length;
      var ctPart = this.pd_prefix + this.dv_suffix + divName.substr(c);
      c = ctPart.length;
      
      elems = document.getElementsByTagName(this.dv_tagType);
      var popupDiv_divs = new Array();

       for(var i=0;i<elems.length;i++){
        if(elems[i].id && (elems[i].id.substr(0,c)) == ctPart){
         popupDiv_divs.push(elems[i]);
         }
       }
       
      div_hotpoint.popupDiv = this;
       if(this.on_time){
        div_hotpoint.onmouseover = function(){
         this.popupDiv.fn_try_show();
        };
       }else{
        div_hotpoint.onmouseover = function(){
         this.popupDiv.fn_show();
        };
       }
      div_hotpoint.onmouseout = function(){
       this.popupDiv.fn_mouseout();
      };
       
       for(var i=0;i<popupDiv_divs.length;i++){
        popupDiv_divs[i].popupDiv = this;
        popupDiv_divs[i].onmouseover = function(){
         this.popupDiv.fn_cancelclose();
        };
        popupDiv_divs[i].onmouseout = function(){
         this.popupDiv.fn_mouseout();
        };
       }

      this.div_hotpoint = div_hotpoint;
      this.popupDiv_divs = popupDiv_divs;
     }
    },
    
 fn_start: function (){
     var nmPart = this.pd_prefix + this.hp_suffix;
     var c = nmPart.length;
     var el = document.getElementsByTagName(this.hp_tagType);
       for(var i=0;i<el.length;i++){
        if(el[i] && (el[i].id.substr(0,c)) == nmPart){
          if(!window.popupDivs){
           window.popupDivs = new Array();
          }
          window.popupDivs.push(el[i]);
          (new popupDiv()).fn_addHotPoint(el[i].id, this.params);
         }
       }
     },
 
 fn_mouseout: function (){
  // prevent opening when briefly rolled over the hot point so that mouse leaves it before
  // the div manages to open. If removed, the effect is that div opens up after you
  // sweep across the menu hot points, for example, and leave the mouse out,
  // the menu div will open anyway, and it will be the div associated with the last hot point
  // that you entered the mouse with
  if(this.state == 'off' && (typeof window.pd_on_timer!="undefined"))
    clearTimeout(window.pd_on_timer);
    
 this.fn_off_delay();
 },
  

 fn_off_delay: function (){
 var div = this.div_hotpoint;
  this.timer=setTimeout(
   function(){ 
    div.popupDiv.fn_mouseOutClose(); 
   },
   this.off_time);
 },
 
 fn_on_delay: function (){
 var div = this.div_hotpoint;
  window.pd_on_timer=setTimeout(
   function(){ 
    div.popupDiv.fn_show(); 
   },
   this.on_time);
 },
 
 
 fn_try_show: function (){
  if(typeof window.pd_on_timer!="undefined")
    clearTimeout(window.pd_on_timer);
  this.fn_on_delay();
 },
 
 
 fn_cancelclose: function (){
 if(typeof this.timer!="undefined")
   clearTimeout(this.timer);
 if(typeof window.pd_on_timer!="undefined")
   clearTimeout(window.pd_on_timer);
 },
 
 fn_mouseOutClose: function (){
  if(this.state == 'on'){
   this.fn_close();
   this.fn_switchSelects('visible');
  }
 },
 
 fn_close: function (){
// var this.popupDiv_div=document.getElementById('this.popupDiv_div');
  if(this.state == 'on'){
   for(var i=0;i<this.popupDiv_divs.length;i++){
    if(this.fn_turnOff){
     this.fn_turnOff(this.popupDiv_divs[i]);
    }else{
     this.popupDiv_divs[i].style.visibility='hidden';
//     this.popupDiv_divs[i].style.display='none';;
    }
   }
   if(this.hp_class_off)
    this.div_hotpoint.className = this.hp_class_off;
  this.state = 'off';
  }
  return false;
 },
 
 fn_openAll: function (){
   for(var i=0;i<this.popupDiv_divs.length;i++){
     if(this.popupDiv_divs[i].style.position != 'relative')
      this.fn_moveToHotPoint(this.popupDiv_divs[i]);
     if(this.fn_turnOn){
      this.fn_turnOn(this.popupDiv_divs[i]);
     }else{
      this.popupDiv_divs[i].style.visibility='visible';
//      this.popupDiv_divs[i].style.display='block';
     }
   }
   if(this.hp_class_on)
    this.div_hotpoint.className = this.hp_class_on;
   this.state = 'on';
 },
 
 fn_moveToHotPoint: function (popupDiv_div){
// var this.popupDiv_div=document.getElementById('this.popupDiv_div');
// var this.div_hotpoint=document.getElementById('bookmarkicon');
 var iCoords=getAbsoluteXY(this.div_hotpoint);
 var ix=iCoords[0]; var iy=iCoords[1];
 var rx=ix+this.shift_x;
 var ry=iy+this.shift_y;
  if(this.shift_y_rel)
   eval('ry=ry+this.div_hotpoint.' + this.shift_y_rel);
 var w_dim=getWindowDimensions();
 var w_scr=getWindowScroll();
 var xdif=w_dim[0]-(rx-w_scr[0]+popupDiv_div.clientWidth+20);
 var ydif=w_dim[1]-(ry-w_scr[1]+popupDiv_div.clientHeight+this.div_hotpoint.clientHeight+20);
  if(xdif<0) rx=rx+xdif;
  if(ydif<0) ry=ry+ydif;
 popupDiv_div.style.left=rx+'px';
 popupDiv_div.style.top=ry+'px';
 return false;
 },
 
 fn_show: function (){
   if(window.popupDivs)
    for(var i=0; i<window.popupDivs.length; i++){
     if(window.popupDivs[i].popupDiv != this)
      window.popupDivs[i].popupDiv.fn_close();
    }
  this.fn_cancelclose();
   if(this.isHideSelects > 0)
    this.fn_switchSelects('hidden');
  this.fn_openAll();
  return false;
 },
 
 fn_switchSelects: function (v){
// alert(ie);
 if(ie)
  for(var f=0; f<document.forms.length; f++)
   for(var e=0; e<document.forms[f].length; e++)
    if (document.forms[f].elements[e].options)
     document.forms[f].elements[e].style.visibility = v; 
 }

};

popupDiv.fn.init.prototype = popupDiv.fn;
})();

//document.getelementByID("pd_1").id

//alert("ss");


