//--------------------------------------------------------------------------
// divPopup.js.
//--------------------------------------------------------------------------

// Constructor til divPopup. Instantieres automatisk een gang,
// længere nede i denne fil
//--------------------------------------------------------------------------
function divPopup(id) {
  divPopup[id]=this;
  this.id=id;
  this.active=0;
  this.hidden=new Array();
  if (id=='instance') {
    document.write('<style type="text/css">#'+id+' { position:absolute; left:0px; top:0px; visibility:hidden;z-index:200 }');
		document.write('.popiframe { position:absolute; left:0px; top:0px; visibility:hidden;z-index:199;filter:progid:DXImageTransform.Microsoft.Shadow(color=#777777, Direction=135, Strength=5)}');
		document.write('</style>\n');
    document.write('<div id="'+id+'">divPopup</div>\n');
		document.write('<iframe class=popiframe id="iframe'+id+'" FRAMEBORDER=no border=1></iframe>\n');
	}
}

// Initiliaseringsfunktion. Aktiveres ved onmouseover-callbacks fra
// 'trigger-elementet'. Initialiserer divPopup instansen, hvis den
// ikke allerede er initialiseret.
//--------------------------------------------------------------------------
divPopup.prototype.init=function() {
  if (!this.css) {
    if (document.getElementById) {
			this.element=document.getElementById(this.id);
			this.element_iframe=document.getElementById('iframe'+this.id);
    }
    if (!this.element) return false;
    this.css=this.element.style;
		if (this.element_iframe) {
			this.css_iframe=this.element_iframe.style;
		}
    if (typeof(this.element.innerHTML)=='undefined') this.write=divPopup_NNwrite;
    else this.write=divPopup_IEwrite;
  }
  return true;
}

// To metoder, der skriver indholdet af en div. Initialiseringsfunktionen
// udv‘lger den rigtige funktion ud fra browsertypen.
//--------------------------------------------------------------------------
function divPopup_NNwrite(html) {
  this.element.document.write(html);
  this.element.document.close();
}
function divPopup_IEwrite(html) {
  this.element.innerHTML=html;
}

// Denne metode skjuler div-instansen (hvis den ikke er aktiv) 
//--------------------------------------------------------------------------
divPopup.prototype.close=function() {
  if (divPopup.timer && this.id==divPopup.timer.id) {
    clearTimeout(divPopup.timer.id);
    divPopup.timer=null;
    return;
  }
  this.css.visibility="hidden";
	//this.css.display='none';
	if (this.css_iframe){
		//this.css_iframe.display='none';
		this.css_iframe.visibility="hidden";
	}
  this.trigger=null;
  if (this.autoClose) {
    if (divPopup.isIE) { // IE
      this.children=null;
      document.onmousemove=this.documentOnMouseMove;
    }
  }
  divPopup.activeDivs--;
  if (divPopup.activeDivs==0) {
    for(var e=0;e<this.hidden.length;e++) {
      var elm=this.hidden[e];
      if (elm.style) elm.style.visibility='visible';
      else elm.visibility='visible';
    }
  }
  this.active=0;
}

function opensoon(trigger,event,
                  html,
                  isAbsolute,x,y,
                  autoClose,delay) {
  var left,top;

  if (typeof(x)=='undefined') x=0;
  if (typeof(y)=='undefined') y=0;
//alert(event.x+' '+event.offsetX+' '+x);
  if (!isAbsolute) {
    if (typeof(trigger.x) != 'undefined') { // nn
      //left=trigger.x+x; // Du'r ikke med popup i div'er, der er flyttet
      //top=trigger.y+y;  
      left=event.pageX+x;
      top=event.pageY+y;
    } else {
      if (event.layerX!=null && typeof(event.layerX) != 'undefined') { // moz
				left=event.layerX+x;
				top=event.layerY+y;
      } else { // ie
        left=(event.x-event.offsetX)+x;
        top=(event.y-event.offsetY)+y;
      }
    }
  }
  else {
    left=x;
    top=y;
  }

  trigger.onmouseout=new Function("if (divPopup.timer) { clearTimeout(divPopup.timer.id); divPopup.timer=null; }");

  if (divPopup.timer) clearTimeout(divPopup.timer.id);
  divPopup.timer=new Object();
  divPopup.timer.trigger=trigger;
  divPopup.timer.html=html;
  divPopup.timer.x=left;
  divPopup.timer.y=top;
  divPopup.timer.autoClose=autoClose;
  divPopup.timer.id=setTimeout('divPopup.instance.opener(divPopup.timer.trigger,'+
                                                        'divPopup.timer.html,'+
                                                        'divPopup.timer.x,'+
                                                        'divPopup.timer.y,'+
                                                        'divPopup.timer.autoClose);'+
                               'divPopup.timer=null;',
                               ((delay)?(delay):(400)));
}

// Denne metode placerer div-instansen rigtigt og viser den.
// Metoden benyttes af de statiske metoder popup, popupAt, open og openAt
//--------------------------------------------------------------------------
divPopup.prototype.opener=function(trigger,
                                   html,
                                   x,y,
                                   autoClose) {
  if (typeof(html)=='undefined') return; // Returner, hvis html-sourcen er udefineret

  if (!html.match(/^\s*<|^$/)) { // Vi har fået en div-id i stedet for html
    if (!divPopup[html]) { // Hvs instansen ikke allerede findes
      new divPopup(html); // s† lav en ny instans..
    }
    // †bn den den div-instans, det handler om...
    divPopup[html].opener(trigger,'',x,y,autoClose);
    return;
  }
  if (!this.init()) return;

  //this.active++;
  if (trigger==this.trigger) return; // Returner hvis allerede †ben
  if (this.trigger!=null) this.close();

  this.trigger=trigger;
  this.autoClose=autoClose;
  this.css.left=x+'px';
  this.css.top=y+'px';

  divPopup.activeDivs++;

  if (html!='') this.write(html);

  this.css.visibility='visible';
	if (this.css_iframe) {
		this.css_iframe.visibility='visible';
	}
  if (this.css_iframe) {
    this.css_iframe.left=x;
    this.css_iframe.top=y;
    this.css_iframe.width=this.element.offsetWidth;
		this.css_iframe.height=this.element.offsetHeight;
  }

  this.active=1;

  if (autoClose) {

    if (divPopup.isIE) { // IE
      this.children=new Array();
      this.sweepChildren(trigger);
      this.sweepChildren(this.element);
      this.documentOnMouseMove=document.onmousemove;
      document.onmousemove=new Function('divPopup.'+this.id+'.mousemove(event);');
    }
    else { // NN
      this.trigger.onmouseout=new Function('divPopup.'+this.id+'.mouseout();');
      this.element.onmouseout=new Function('divPopup.'+this.id+'.mouseout();');
      this.element.onmouseover=new Function('divPopup.'+this.id+'.mouseover();');
    }
  }
}

// Find alle child-elementer rekursivt og tilf›j dem til this.children 
//-------------------------------------------------------------------------
divPopup.prototype.sweepChildren=function(el) {
  this.children[this.children.length]=el;
  if (typeof(el.children)=='object') {
    var child;
    for(child=0;child<el.children.length;child++) {
     	this.sweepChildren(el.children[child]);
    }
  }
}

// Mouseout callback. NN only ---------------------------------------
divPopup.prototype.mouseout=function() {
  this.active=0;
  window.setTimeout('if (divPopup.'+this.id+'.active==0) divPopup.'+this.id+'.close();',100);
}
// Mouseover callback. NN only --------------------------------------
divPopup.prototype.mouseover=function() { // NN only
  this.active=1;
}

// Mousemove callback. IE only --------------------------------------
divPopup.prototype.mousemove=function(event) { // IE only
  var src=event.srcElement;
  for(var i=0;i<this.children.length;i++) {
    if (this.children[i]==src) {
      return true;
    }
  }
  this.close();
  return true;
}

// Statiske variable
//-------------------------------------------------------------------------
divPopup.isIE=(document.all)?(true):(false);
divPopup.activeDivs=0;

// Statiske onmouseover-callbacks, der kan assignes til 'trigger-elementet'
// '...At' tager absolutte positioner
// 'popup..' viser div-instansen og lukker den, n†r musen forlader den
// 'open..' viser div-instansen uden at lukke den automatisk
//-------------------------------------------------------------------------
divPopup.popup=function(trigger,event,html,x,y,delay)   { opensoon(trigger,event,html,false,x,y,true,delay); }
divPopup.popupAt=function(trigger,event,html,x,y,delay) { opensoon(trigger,event,html,true,x,y,true,delay); }
divPopup.open=function(trigger,event,html,x,y,delay)    { opensoon(trigger,event,html,false,x,y,delay); }
divPopup.openAt=function(trigger,event,html,x,y,delay)  { opensoon(trigger,event,html,true,x,y,delay); }
divPopup.popupNow=function(trigger,event,html,x,y)   { opensoon(trigger,event,html,false,x,y,true,1); }
divPopup.popupNowAt=function(trigger,event,html,x,y) { opensoon(trigger,event,html,true,x,y,true,1); }
divPopup.openNow=function(trigger,event,html,x,y)    { opensoon(trigger,event,html,false,x,y,1); }
divPopup.openNowAt=function(trigger,event,html,x,y)  { opensoon(trigger,event,html,true,x,y,1); }

divPopup.close=function() { divPopup.instance.close(); }


// Her instatieres en div og gemmes i en statisk variabel 
//-------------------------------------------------------------------------
// Her er der tilføjet en if. /Rasmus 16.12.03
if (!divPopup['instance']) {
	new divPopup('instance');
}

// popup frame
//function divPopup.popupurlNow(id,event,url,iframe,x,y) {
divPopup.popupurlNow=function(id,event,url,iframe,x,y) {
  if (window.frames[iframe].location.href == 'about:blank') {
    window.frames[iframe].location.href=url;
  }
  divPopup.popupNow(id,event,iframe,x,y);
}



