window.onload = init;
var toolbarList;
var optImg;
var autoReloadInterval = 120000;
var hideOpts;
var tWidth = 437;
var tHeight = 667;
var popupSizesArr = new Object();
var isAutoReload = false;
popupSizesArr["stichwort"]     = new Array( 380, 431 );
popupSizesArr["portrait"]      = new Array( 432, 615 );
popupSizesArr["kurzmeldungen"] = new Array( 685, 485 );
popupSizesArr["buli_vorschau"] = new Array( 685, 485 );
popupSizesArr["mediabox"]      = new Array( 818, 605 );
popupSizesArr["mediabox_av"]   = new Array( 818, 605 );
popupSizesArr["mediabox_b"]    = new Array( 818, 605 );
popupSizesArr["mediabox_vb"]   = new Array( 818, 605 );
popupSizesArr["eventbox"]      = new Array( 770, 540 );
popupSizesArr["ticker"]        = new Array( 415, 665 );
popupSizesArr["statistik"]     = new Array( 425, 550 );
popupSizesArr["ergticker"]     = new Array( 100, 250 );

var isLt4   = (parseInt(navigator.appVersion)<4)?1:0;
var isNS4   = (document.layers)?1:0;

var isDOM   = (document.getElementById)?1:0;
var isIE   = 0;
var isIE40 = 0;
var isIE50 = 0;
var isIE55 = 0;
var isIE60 = 0;
var isIE70 = 0;

/*@cc_on
     @if (@_jscript_version >= 5.7) isIE70 = true;
     @elif (@_jscript_version >= 5.6)  isIE60 = true;
     @elif (@_jscript_version >= 5.2)  isIE55 = true;
     @elif (@_jscript_version >= 5.0)  isIE50 = true;
     @elif (@_jscript_version >= 4.0)  isIE40 = true;
     @end
     isIE = true;
@*/


if(isIE){
  tWidth = 441;
}


function init() {
    if ( !document.getElementById ) {
        return false;
    }

		var toolbar = document.getElementById('toolbarLeft');
		toolbarList = document.getElementById( 'toolbarLeftList' );
		if ( !toolbar ) {
			toolbar = document.createElement( 'div' );
			toolbar.id = 'toolbarLeft';
			toolbar.className = 'tbleft';
			document.getElementById('toolbar').appendChild( toolbar );
		}
		if ( !toolbar ) {
			return false;
		}
		if ( !toolbarList ) {
			toolbarList = document.createElement( 'ul' );
			toolbarList.id = 'toolbarLeftList';
			toolbarList.className = 'inlineList';
			toolbar.appendChild( toolbarList );
		}
    if ( toolbar && toolbarList ) {
    	optImg = document.createElement( 'img' );
    	optImg.src = 'img/optionen.gif';
    	optImg.alt = '';
    	var optLink = document.createElement( 'a' );
    	optLink.appendChild( optImg );
    	optLink.href = '#';
    	var optLinkDiv = document.createElement( 'div' );
    	optLinkDiv.appendChild( optLink );
    	var optLinkSpan = document.createElement( 'span' );
    	optLinkSpan.appendChild( document.createTextNode( 'Optionen' ) );
    	optLink.appendChild( document.createTextNode( 'Optionen' ) );
    	optLinkDiv.className = 'optlink';
    	optLinkDiv.onmouseover = showOptions;
    	toolbarList.onmouseover = showOptions;
    	optLinkDiv.onmouseout = hideOptions;
    	toolbarList.onmouseout = hideOptions;
    	toolbarList.className = 'optionList inv';
    	toolbar.insertBefore( optLinkDiv, toolbarList );
    	var arli = document.createElement( 'li' );
    	var arlia = document.createElement( 'a' );
   		var myUrl = window.location.href;
   		if ( myUrl.indexOf( 'autoReload=1' ) >= 0 ) {
   			myUrl = myUrl.replace(/autoReload=1/g, "autoReload=0");
   			isAutoReload = true;
   		} else if ( myUrl.indexOf( 'autoReload=0' ) >= 0 ) {
   			myUrl = myUrl.replace(/autoReload=0/g, "autoReload=1");
   		} else if ( myUrl.indexOf( '?' ) >= 0 ) {
   			myUrl += '&autoReload=1';
   		} else {
   			myUrl += '?autoReload=1';
   		}
   		if ( myUrl.indexOf( 'manualReload=1' ) >= 0 ) {
   			;
   		} else if ( myUrl.indexOf( 'manualReload=0' ) >= 0 ) {
   			myUrl = myUrl.replace(/manualReload=0/g, "manualReload=1");
   		} else if ( myUrl.indexOf( '?' ) >= 0 ) {
   			myUrl += '&manualReload=1';
   		} else {
   			myUrl += '?manualReload=1';
   		}
   		arlia.href = myUrl;
   		arlia.appendChild( document.createTextNode( ( isAutoReload ? 'AutoReload aus' : 'AutoReload an' ) ) );
   		arlia.title = ( isAutoReload ? 'automatisches Aktualisieren ausschalten' : 'Seite automatisch alle ' + ( autoReloadInterval > 60000 ? (autoReloadInterval/60000) + ' Minuten' : (autoReloadInterval/1000) + ' Sekunden' ) + ' aktualisieren' );
    	arli.appendChild( arlia );
    	toolbarList.insertBefore( arli, toolbarList.firstChild );
    }

		if ( window.location.href.indexOf( 'positioned=1' ) < 0 ) {
			
		  var scrW = screen.availWidth;   //verfuegbare Bildschirmbreite
		  var scrH = screen.availHeight;  //verfuegbare Bildschirmhoehe
		  var resW = 400;                 //Breite Testfenster
		  var resH = 400;                 //Hoehe Testfenster
		  var winW = 0;                   //innere Breite Testfenster/berechnete Fensterbreite
		  var winH = 0;                   //innere Hoehe Testfenster/berechnete Fensterhoehe
		  var difW = 0;                   //berechnete Rahmenbreite
		  var difH = 0;                   //brerchnete Rahmenhoehe
		  var brdW = 0;                   //browserabhaengige zusaetzliche Breite
		  var brdH = 0;                   //browserabhaengige zusaetzliche Hoehe
		  var posX = 0;                   //horizontale Startposition des Fensters
		  var posY = 0;                   //vertikale Startposition des Fensters

		  if ( window.opera ) {
		    //Opera
		    brdW = 2;
		  } else if ( window.navigator.vendor ) {
		    if ( document.clientWidth ) {
		      //Konqueror
		      brdW = 2;
		    } else {
		      //Safari
		      brdW = 2;
		    }
		  } else if ( window.atob ) {
		    //Firefox, braucht Korrektur nur unter *nix
		    var os = navigator.platform.toLowerCase();
		    brdW = ( ( os.indexOf('linux') > -1 || os.indexOf('unix') > -1 ) ? 36 : 0 );
		  } else if ( document.defaultCharset  ) {
		    //Internet Explorer
		    brdH = -15;
		  }

		  posX = ( self.screenX || self.screenLeft );
		  posY = ( self.screenY || self.screenTop  );

		  //Fenster auf Testgroesse setzen
		  //self.moveTo( 0, 0 );
		  self.resizeTo( resW, resH );

		  //innere Groesse des Testfensters bestimmen
		  if (window.innerWidth) {
		    winW = window.innerWidth;
		    winH = window.innerHeight;
		  } else if (window.document.documentElement && window.document.documentElement.clientWidth ) {
		    winW = window.document.documentElement.clientWidth;
		    winH = window.document.documentElement.clientHeight;
		  } else {
		    winW = window.document.body.offsetWidth;
		    winH = window.document.body.offsetHeight;
		  }

		  //Rahmenmasse sind Aussenmasse des Testfensters minus innere Masse
		  difW = resW - winW;
		  difH = resH - winH;

		  //benoetigte Fesntergroesse ermitteln
		  var content = document.getElementById( 'tickerSmall' ) || document.getElementById( 'tickerAll' );
		  if ( content && content.offsetWidth ) {
		    winW = content.offsetWidth + 12 + brdW + difW;
		    winH = content.offsetHeight + 12 + brdH + difH;
		  } else {
		    winW = 400;
		    winH = 600;
		  }

		  //Fenstergroesse maximal Bildschirmgroesse
		  if ( scrW < winW && scrH < winH ) {
		    winW = scrW;
		    scrH < winH
		  } else if ( scrH < winH ) {
		    winW += 20;
		    winH = scrH;
		    if ( scrW < winW ) {
		      winW = scrW;
		    }
		  } else if ( scrW < winW ) {
		    winW = scrW;
		    winH += 20;
		    if ( scrH < winH ) {
		      winH = scrH;
		    }
		  }

		  if ( posX + winW > scrW ) {
		    posX = scrW - winW;
		  }

		  if ( posY + winH > scrH ) {
		    posY = scrH - winH;
		  }

		  //Fenstergroesse auf Inhalt anpassen
		  self.resizeTo( winW, winH );
		}

    if ( isAutoReload ) {
    	window.setTimeout("autoReload()", autoReloadInterval);
    }

    initPopupLinks();
}

function autoReload() {
	var actUrl = location.href;
	if ( actUrl.indexOf( 'manualReload=1' ) > -1 ) {
		location.href = actUrl.replace(/manualReload=1/gi, "manualReload=0");
	} else {
		location.reload();
	}
}

function showOptions() {
	if ( hideOpts ) {
		window.clearTimeout( hideOpts );
	}
 	toolbarList.className = 'optionList';
}

function reallyHideOptions() {
 	toolbarList.className = 'optionList inv';
}

function hideOptions() {
	if ( hideOpts ) {
		window.clearTimeout( hideOpts );
	}
	hideOpts = window.setTimeout( "reallyHideOptions()", 300 );
}

function initPopupLinks() {
	var links = document.getElementsByTagName( 'a' );
	for ( var i=0; i<links.length; i++ ) {
		var relTxt = links[i].rel;
		if ( relTxt ) {
			switch ( relTxt ) {
				case 'ergticker':
          try {
            links[i].attachEvent( 'onclick', relPopupIE );
          } catch (err) {
            links[i].setAttribute( 'onclick', 'return relPopup(this);' );
          }
					break;
				/*
				case 'smallticker':
					links[i].onclick = smallticker;
					break;
				case 'bigticker':
					links[i].onclick = bigticker;
					break;
				*/
			}
		}
	}
}

function relPopupIE(evt) {
  var e = evt || window.evt;
  var orig = e.target || e.srcElement;
  return relPopup(orig);
}

function relPopup(orig){
  while ( orig.nodeName.toLowerCase() != 'a' && orig.nodeName.toLowerCase() != 'body' ) {
    orig = orig.parentNode;
  }
  var relTxt = orig.getAttribute( 'rel' );
  var url = orig.getAttribute( 'href' );
  if ( !relTxt || !url ) {
    return true;
  }
  if ( url.indexOf( "?" ) >= 0 ) {
      url += '&popup';
  } else {
      url += '?popup';
  }
  return popup( url, popupSizesArr[relTxt][0], ( isIE ? popupSizesArr[relTxt][1] - 20 : popupSizesArr[relTxt][1] ) );
}

/**
 * popup-Fenster öffnen
 * Achtung! BA PopUps müssen scrollbar, resizable sein und ein Menubar haben
 *
 */
function popup(url, breite, hoehe){

  var winname = "popup" + (new Date()).getTime(); 
  var winarg = "";
  var x = 0;
  var y = 0;
  windowisopen = false;
  eval("if(window." + winname + " && window." + winname + ".closed==false) { windowisopen = true\; }");

  if(!windowisopen) {
    if(screen.width > 640) { 
      x = (screen.availWidth  - breite) / 2;
      y = (screen.availHeight - hoehe) / 2; 
    }
    winarg  = 'width=' + breite + ',height=' + hoehe + ',top=' + y + ',left='+ x;
    winarg += ',resizable,menubar,scrollbars';
  }

  eval("" + winname + " = window.open( url, winname, winarg)\;");
  eval("" + winname + ".focus()\;");
  eval("if (" + winname + ".opener == null) " + winname + ".opener = self\;");

  return false;
} // popup






/*
var tickerHeight = 0;
var bodyPadding = 8;

window.onload = init;
window.onresize = init;
var firstInit = true;

function init() {
    if ( !document.getElementById ) {
        return false;
    }
    document.getElementById('tickerMeldungen').style.display = 'none';
    if ( window.innerHeight ) {
        tickerHeight = window.innerHeight;
    } else if ( document.documentElement.clientHeight ) {
        tickerHeight = document.documentElement.clientHeight;
    }
    document.getElementById('tickerMeldungen').style.display = 'block';
    var meldungenTopOffset = document.getElementById('tickerMeldungen').offsetTop;
    var osParent = document.getElementById('tickerMeldungen').offsetParent;
    while ( osParent ) {
        meldungenTopOffset += osParent.offsetTop;
        osParent = osParent.offsetParent;
    }
    var newHeight = tickerHeight - meldungenTopOffset - bodyPadding;
    if ( newHeight < 100 ) {
        newHeight = 100;
    }
    document.getElementById('tickerMeldungen').style.height = newHeight + 'px';

		var toolbar = document.getElementById('toolbar');
    if ( toolbar && firstInit ) {
   		var divActualize = document.createElement( 'div' );
   		divActualize.className = 'tbleft';
   		var imgActualize = document.createElement( 'img' );
   		imgActualize.src = "img/icon_reload.gif";
   		imgActualize.alt="Icon zum Aktualisieren des Tickers";
   		var spanActualize = document.createElement( 'span' );
   		var aActualize = document.createElement( 'a' );
   		spanActualize.appendChild( aActualize );
   		aActualize.className = 'weisscol';
   		var myUrl = window.location.href;
   		var index = myUrl.indexOf( 'autoReload=1' );
   		var isAutoReload = false;
   		if ( myUrl.indexOf( 'autoReload=1' ) >= 0 ) {
   			myUrl = myUrl.replace(/autoReload=1/g, "autoReload=0");
   			isAutoReload = true;
   		} else if ( myUrl.indexOf( 'autoReload=0' ) >= 0 ) {
   			myUrl = myUrl.replace(/autoReload=0/g, "autoReload=1");
   		} else if ( myUrl.indexOf( '?' ) >= 0 ) {
   			myUrl += '&autoReload=1';
   		} else {
   			myUrl += '?autoReload=1';
   		}
   		aActualize.href = myUrl;
   		aActualize.appendChild( document.createTextNode( ( isAutoReload ? 'AutoReload aus' : 'AutoReload an' ) ) );
   		aActualize.title = ( isAutoReload ? 'automatisches Aktualisieren ausschalten' : 'Seite automatisch alle 2 Minuten aktualisieren' );
   		divActualize.appendChild( imgActualize );
   		divActualize.appendChild( spanActualize );
   		toolbar.appendChild( divActualize );
    }

    if ( firstInit && autoReload == 1 ) {
    	window.setTimeout("window.location.reload()", 120000);
    }

    firstInit = false;
}
*/
