// JavaScript Document

// Script Source: CodeLifter.com
// Copyright 2003
// Do not remove this header

isIE=document.all;
isNN=!document.all&&document.getElementById;
isN4=document.layers;
isHot=false;

var nowY;
var nowX;
var offsetx;
var offsety;
var mydrag;

function ddInit(e){
  topDog=isIE ? "BODY" : "HTML";
  whichDog=isIE ? document.all.popup : document.getElementById("popup");  
  hotDog=isIE ? event.srcElement : e.target;  
  while (hotDog.id!="titleBar"&&hotDog.tagName!=topDog){
    hotDog=isIE ? hotDog.parentElement : hotDog.parentNode;
  }  
  if (hotDog.id=="titleBar"){
    offsetx=isIE ? event.clientX : e.clientX;
    offsety=isIE ? event.clientY : e.clientY;
    nowX=parseInt(whichDog.style.left);
    nowY=parseInt(whichDog.style.top);
    ddEnabled=true;
    document.onmousemove=dd;
  }
}

function dd(e){
  if (!ddEnabled) return;
  whichDog.style.left=isIE ? nowX+event.clientX-offsetx : nowX+e.clientX-offsetx; 
  whichDog.style.top=isIE ? nowY+event.clientY-offsety : nowY+e.clientY-offsety;
  return false;  
}

function ddN4(whatDog){
  if (!isN4) return;
  N4=eval(whatDog);
  N4.captureEvents(Event.MOUSEDOWN|Event.MOUSEUP);
  N4.onmousedown=function(e){
    N4.captureEvents(Event.MOUSEMOVE);
    N4x=e.x;
    N4y=e.y;
  }
  N4.onmousemove=function(e){
    if (isHot){
      N4.moveBy(e.x-N4x,e.y-N4y);
      return false;
    }
  }
  N4.onmouseup=function(){
    N4.releaseEvents(Event.MOUSEMOVE);
  }
}

function hideMe(){
  if (isIE||isNN) whichDog.style.visibility="hidden";
  else if (isN4) document.popup.visibility="hide";
}

function showMe(){
  if (isIE||isNN) whichDog.style.visibility="visible";
  else if (isN4) document.popup.visibility="show";
}

//document.onmousedown=ddInit;
//document.onmouseup=Function("ddEnabled=false");


function openPopup(url, height, width, scroll, ueberschrift)
{
  try
  {
    // Ueberschrift setzen
    document.getElementById('popuphead').innerHTML = ueberschrift;
    
    // Hintergrund abdunkeln
    var arrayPageSize = getPageSize();
    document.getElementById('overlay').style.height = arrayPageSize[1] + 'px';
    document.getElementById('overlay').style.visibility='visible';
    document.getElementById('overlay').onclick = function() { closePopup(); }
    var test = new Effect.Appear('overlay', { duration: 0.3, from: 0.0, to: 0.2 });
    
    // iframe laden
    document.getElementById('ipopup').src = url;
    document.getElementById('ipopup').scrolling = scroll;
  
    
    var x = isIE ?  parseInt(document.body.clientWidth) : parseInt(window.innerWidth); 
    var y = isIE ?  parseInt(document.body.clientHeight) : parseInt(window.innerHeight);
    
    x = (x - width) / 2 ;
    y = (y - height) / 2 ;
    
    y = isIE ? parseInt(document.body.scrollTop) + y : parseInt(window.pageYOffset) + y;

    if (x < 0) x = 0;
    if (y < 0) y = 0;
  
    document.getElementById('popup').style.left = x;
    document.getElementById('popup').style.top = y;
    document.getElementById('popup').style.width = width;
    document.getElementById('popup').style.height = height;
    
    // Korrektur für Iframe im IE
    if (isIE) height = height - 2;
  
    document.getElementById('ipopup').style.height = height-25;
    
    // Popup einblenden
    document.getElementById('popup').style.visibility='visible';
    var test1 = new Effect.Appear('popup', { duration: 1.0, from: 0.0, to: 1.0 });
    
    document.getElementById('overlay').style.height = arrayPageSize[1] + 'px';
    
    nowX=parseInt(document.getElementById('popup').style.left);
    nowY=parseInt(document.getElementById('popup').style.top);
    
    mydrag = new Draggable('popup');
    return false;
  }
  catch (e)
  {
    return true;
  }
  
}

function closePopup()
{
//CAN - Inhalt des IFrames leeren
  document.getElementById('ipopup').src="/output/empty.html"
//Ende CAN
  mydrag.destroy();
  document.onmousemove = function() {};
  document.getElementById('popup').style.display = 'none';
  document.getElementById('popup').style.visibility='hidden';
  var E2 = new Effect.Appear('overlay', { duration: 0.3, from: 0.2, to: 0.0 });
  
  document.getElementById('overlay').onclick= function() {};
  window.setTimeout("hiddenPopup()", 500);  
}

function hiddenPopup()
{
  document.getElementById('overlay').style.display = 'none';
  document.getElementById('overlay').style.visibility='hidden';
  
}

function movePopup(e)
{
  var x = isIE ? event.clientX-offsetx : e.clientX-offsetx;
  var y = isIE ? event.clientY-offsety : e.clientY-offsety;
  
  // Position darf nicht negativ sein
  if (x < 0) x = 0;
  if (y < 0) y = 0;
  
  // Position darf über MAX nicht sein
  if (x > 1024) x = 1024;
  if (y > 768) y = 768;
  
  document.getElementById('popup').style.left=x; 
  document.getElementById('popup').style.top=y;
}

function startDrag()
{
  
  //document.onmousemove = movePopup;
  //offsetx=parseInt(document.getElementById('popup').style.width) / 2;
  //offsety=7;
}

function stopDrag()
{
  document.onmousemove = function() {};
}


function replacePopupLinks()
{
    if (!document.getElementsByTagName){ return; }

		var anchors = document.getElementsByTagName('a');

		// loop through all anchor tags

		for (var i=0; i<anchors.length; i++){

			var anchor = anchors[i];

			var relAttribute = String(anchor.getAttribute('rel'));

			if (anchor.getAttribute('href') && (relAttribute.toLowerCase().match('popup'))){

        anchor.onclick = function ()
          {
            var ueberschrift = this.getAttribute('title');
            if (ueberschrift == '') ueberschrift = document.title;
            var url = this.getAttribute('purl');
            if (url == '') url = this.href;
            var scroll1 = this.getAttribute('pscroll');
            if (!scroll1) scroll1 = 'auto'; 
            return(openPopup(url, this.getAttribute('pheight'), this.getAttribute('pwidth'), scroll1, ueberschrift));
          }

			}

		}

}
