// Browserchecks

safari = (navigator.appVersion.indexOf("Safari")!=-1) ? true : false;
moz = (navigator.appName.indexOf("Netscape")!=-1) ? true : false;
isMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;
opera = (navigator.userAgent.indexOf("Opera")!=-1) ? true : false;
ie55 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 5.5")!=-1)) ? true : false;
ie6 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 6")!=-1)) ? true : false;
isIE = ((document.all)&&(navigator.appVersion.indexOf("MSIE")!=-1)) ? true : false;
ie7plus = ((document.all)&&(navigator.appVersion.indexOf("MSIE")!=-1)&&(navigator.appVersion.charAt(navigator.appVersion.indexOf('MSIE')+5)>=7)) ? true : false;
ie6minus = (isIE && !ie7plus)? true : false;
IEmac = ((document.all)&&(isMac)) ? true : false;
NS4 = (document.layers) ? true : false;


// --- Stylesheet Korrekturen ---

if (ie6minus)
{
	 document.write('<link rel="stylesheet" href="'+template_dir+'styles/ie6_styles.php" type="text/css" />');  
	 var m_ml = Math.round((getViewportWidth()-890) / 2);
	 document.write('<style> #main { margin-left:' + m_ml + 'px !important; } </style>');
	 //window.onload = adoptToIE6();
}
if (ie7plus)
{
	 document.write('<link rel="stylesheet" href="'+template_dir+'/styles/ie7_styles.css" type="text/css" />'); 
}
if (isMac || safari) document.write('<link rel="stylesheet" href="'+template_dir+'/styles/mac_fonts.css" type="text/css" />');  
/*
function adoptToIE6()
{
	var m_ml = Math.round((getViewportWidth()-890) / 2);
	var m = $('main');
	if (m) m.style.marginLeft = m_ml + "px";
}
*/
// Erweitert alle ids (HTML-Elemente) um die Klasse ie6_add_style, wenn IE6 angesagt
function ie6_extend( ids, ie6_add_style)
{
	if (ie6minus)
	{
		for (var i = 0; i < ids.length; i++)
		{
			var elem = $(ids[i]);
			if (elem) elem.className += " "+ie6_add_style;
		}
	}
}
 
// Liefert Fensterbreite (aber nicht im IE6 ?!)
function getWinWidth()
{
	var w = document.viewport.getDimensions().width;	// Prototype vorrausgesetzt!
//	if (ie6minus) return w-16;		//im IE6 ist der Scroller immer sichtbar
//	else 
	return w;
}

// Liefert Fensterbreite (auch im IE6)
function getViewportWidth() {
	if (document.body) return document.body.offsetWidth;
	if (document.documentElement) return document.documentElement.offsetWidth;
	var w = self.innerWidth || (document.documentElement.clientWidth || document.body.clientWidth);
	if (ie6minus) return w-16;	//im IE ist der Scroller immer sichtbar
	else return w;
}

// Liefert Fensterhoehe

function getWinHeight()
{
	return document.viewport.getDimensions().height;
}

// str_replace fuer js

function str_replace(search, replace, subject)
{
    return subject.split(search).join(replace);
}

// Elemente ein/ausblenden

function showElem( id , elemType )
{
	if (elemType==null) elemType = 'block';
	$(id).style.display = elemType;
}

function hideElem( id )
{
	$(id).style.display = 'none';
}

function doPrint()
{
	//$(body).addClassName('print'); 
	print();
	//$(body).removeClassName('print'); 
}


// --- Blog ---

// Post ein/ausblenden

var recentlyOpen = null;
var autoClosePosts = true;	// alte Posts autom. schliessen und auch zu neuem Post springen, wenn unterhalb altem Post in der Seite
function openClosePost( idNr )
{
	if (posts && posts.length > 0)
	{
		for (var i = 0; i < posts.length; i++)
		{
			if (posts[i] == idNr)
			{
				openOrClosePost( posts[i] );
				if (autoClosePosts && recentlyOpen < idNr)
				{
				 	$("switcher"+idNr).scrollTo(); 
				}
				recentlyOpen = idNr;
			}
			else
			{
				if (autoClosePosts)
					openOrClosePost( posts[i], false );
			}
		}
	}
	else
	{
		openOrClosePost( idNr );
	}
}

function openOrClosePost( idNr, doOpen, jumpTo )
{
	if (arguments.length <= 1) doOpen = ($("complete"+idNr).style.display == "none");
	if (arguments.length <= 2) jumpTo = false;
	
	var teaserPics = $("intro"+idNr).select("img.teaser");
	
	if (doOpen)
	{
		if ($("complete"+idNr))
			$("complete"+idNr).style.display = "block";
		if ($("switcher"+idNr))
			$("switcher"+idNr).className = "closer";
		if (teaserPics.length > 0)
			teaserPics.each( function( e ) { e.style.display = 'none'; } );
		if (jumpTo) setTimeout(function(){ $("switcher"+idNr).scrollTo(); }, 120);
 	}
	else
	{
		if ($("complete"+idNr))
			$("complete"+idNr).style.display = "none";
		if ($("switcher"+idNr))
			$("switcher"+idNr).className = "opener";
		if (teaserPics.length > 0)
			teaserPics.each( function( e ) { e.style.display = 'block'; } );
	}
}


// --- Mediagallery ---

// Mediamenu

var activeThumb = 0;
function showMediaItem( idNr )
{
	// Flashfilm
 	// var flashMovie = getFlashObject("flashMovie_main");
	var flashMovie = swfobject.getObjectById("flashMovie_main");
	if (flashMovie) flashMovie.SetVariable("/:jumpTo", idNr );
  
  // Ggf. Alt-Img.
	for (var i=0; i<=9; i++)
	{
		if ( $("image"+i) )
		{
			if (i == idNr) 
				$("image"+i).style.display="block";
			else 
				$("image"+i).style.display="none";
		}
		if ( $("mediainfo"+i) )
		{
			if (i == idNr) 
				$("mediainfo"+i).style.display="block";
			else 
				$("mediainfo"+i).style.display="none";
		}
	}
	
	// thumbnail
	$("thumb"+activeThumb).className = "thumb";
	$("thumb"+idNr).className = "thumb active";
	activeThumb = idNr;
	
	updateMediaInfo();
}

var showMm_step = 0;
function showMediamenu( doStart )
{
	if (arguments.length<1) doStart = false;
	var mm = $('mediamenu');
	if (!mm) return;
	
	if (doStart) 
	{
		mm.style.height = 0;
		mm.style.top = 0;
		
		progr = 0;
	}
	else
	{
		progr = easeInOutSine(1, ++showMm_step, 20, 0);
  	
		mm.style.height = Math.round( progr * 46 ) + 'px';
		mm.style.top = -Math.round( progr * 46 ) + 'px';
	}
	
	if (progr<1)
	{
		setTimeout( "showMediamenu()", 40);
	}
}


// Infotext aus/einblenden

var mi_width_max = 200;
var mi_width = 0;
var mi_new_width = 0;

function showHideMediaInfo( doShow )
{
	var mi = $("mediainfo");
	if (!mi) return;
	if (arguments.length<1) doShow = (mi.style.display=='none');
	else if (doShow && isOpenMediaInfo() || !doShow && !isOpenMediaInfo()) return;
		
	if (doShow)
	{
		mi_width = -20;
		mi_new_width = mi_width_max;
		setMediainfoGfx( true );
	}
	else
	{
		mi_width = mi_width_max;
		mi_new_width = -20;
		setMediainfoGfx( true );
	}
}

function isOpenMediaInfo()
{
	var mi = $("mediainfo");
	if (!mi) return false;
	return (mi.style.display!='none');
}

var step = 0;
var steps_max = 7;

function setMediainfoGfx( doStart )
{
	
	if (arguments.length<1) doStart = false;
	if (doStart) step = 0;
	
	//var w = (mi.style.width.indexOf('px')>-1)? mi.style.width.substr(0, mi.style.width.indexOf('px')) : mi.style.width;
	progr = easeInOutSine(1, ++step, steps_max, 0);
	recent_width = Math.round( progr * (mi_new_width - mi_width) + mi_width );

	var mi = $("mediainfo");
	mi.style.width = ((recent_width<0)? 0 : recent_width) +'px';
	mi.style.display = (recent_width<=0)? 'none' : 'block';
	$("mediainfo_bg_left").style.width = recent_width+20 + 'px';
	$("mediainfo_bg_right").style.marginLeft = recent_width+20 + 'px';
	$("mediainfo_bg_right").className = (recent_width<=120)? 'closed' : 'open';
	
	if (progr<1)
	{
		setTimeout( "setMediainfoGfx()", 40);
	}
}

// Infotext updaten

var autoHideMediaInfo = true;
var autoOpenMediaInfo = false;
var restarted = false;
function updateMediaInfo()
{
	var mi = $("mediainfo");
	if (!mi) return;
	var h = mi.getHeight();
	if (safari && !restarted)	// fuer Safari, nach xxx ms nochmal schauen, da Hoehe gern als 0 ermittelt
	{
		setTimeout( updateMediaInfo, 240 );
		restarted = true;
		return;
	}

	$("mediainfo_bg_left").style.height  = h + 'px';
	$("mediainfo_bg_right").style.height = ( (h-7)>=0? h-7 : 0 ) + 'px';
	var top = (420 - h);
	if (ie6minus) top -= 70;
	$("mediainfo").style.top = top + 'px';
	$("mediainfo_bg_left").style.top = top + 'px';
	$("mediainfo_bg_right").style.top = top + 'px';

	if (isOpenMediaInfo())
	{
		if (autoHideMediaInfo && !autoOpenMediaInfo) showHideMediaInfo( false );
	}
	else
	{
		if (autoOpenMediaInfo) showHideMediaInfo( true );
	}
}

// Aufrufe aus Flash

function openMediaInfo()
{ 
	showHideMediaInfo( true );
}
function closeMediaInfo()
{ 
	showHideMediaInfo( false );
}

// Tooltip

var tooltip_visible = 0;
var tooltip_ov;
var tooltip_bg;

function initTooltip() {
	tooltip_ov = $('tooltip');
	tooltip_bg = $('tooltip_bg');
}

function showTooltip( idNr ) {
	if (!tooltip_ov) return;
	tooltip_ov.display = 'block';
  tooltip_bg.style.display = 'block';
  tooltip_visible = 1;
  
  for (var i=0; i<=20; i++)
	{
		if ( $("preview"+i) )
		{
			if (i == idNr) 
  			$("preview"+i).style.display = 'block';
  		else
  			$("preview"+i).style.display = 'none';
  	}
  }

  tooltip_bg.style.width = tooltip_ov.getWidth()+6+'px';
  tooltip_bg.style.height = tooltip_ov.getHeight()+2+'px';
  
  tooltip_ov.style.display = 'block';
	tooltip_bg.style.display = 'block';
  document.onmousemove = toolTipMousePos;
}

function hideTooltip() {
	tooltip_ov.style.display = 'none';
	tooltip_bg.style.display = 'none';
	tooltip_visible = 0;
 	//document.onmousemove = null; // fuer den IE muss der Tooltip staendig mit der Maus verschoben werden - sonst Bug, wenn Maus ueber letzter Tooltip-Posi
}

function toolTipMousePos(e) {
    if (!e) e = window.event;
    var body = (window.document.compatMode && window.document.compatMode == "CSS1Compat") ?
    	window.document.documentElement : window.document.body || null;
    mousey = e.pageY ? e.pageY : e.clientY + body.scrollTop;
    mousex = e.pageX ? e.pageX : e.clientX + body.scrollLeft;
  	if (!tooltip_ov) return;
  	tooltip_ov.style.top = mousey-25+'px';
  	tooltip_ov.style.left = mousex+10+'px'; 
  	if (!tooltip_bg) return;
  	tooltip_bg.style.top = tooltip_ov.style.top;
  	tooltip_bg.style.left = tooltip_ov.style.left;
}


// --- Eventsearch ---

function showHideSearch( imageIDNr, searchBoxID )
{
	if ($(searchBoxID).style.display == "none")
	{
		$(searchBoxID).style.display = "block";
		$('selImg'+imageIDNr).className = "arrowUp";
	}
	else
	{
		$(searchBoxID).style.display = "none";
		$('selImg'+imageIDNr).className = "arrowDown";
	}
}

function showStartSearch()
{
	$('startsearch').style.display = "block";
}

var selectIsExpanded = false;
function expandIEselect()
{
	if (!isIE) return;
	if (selectIsExpanded) return;
	
	var s = $('placesearchSelect'); // selectbox
	var d = s.ancestors()[1];	// div class="searcharea" ...
  
 	d.style.overflow = "visible";
  s.style.position = "absolute";
  s.style.zIndex = "20";
	s.style.width = "300px";
	
	s.size = 20;
	s.multiple = true;
	
	selectIsExpanded = true;
}

function contractIEselect()
{
	if (!isIE) return;
	var s = $('placesearchSelect'); // selectbox
	if (s.size == 1) return;
	
	s.size = 1; 
	s.multiple = false;
	
	s.style.width = "";
	s.className = "#searchdetails select";
	
	setTimeout(function() {selectIsExpanded = false;}, 250);
}

// ---- Homepage ----

var resizeTimeoutID = null;

function resizeBGImg()
{
	var vw = document.viewport.getWidth();
	var vh = document.viewport.getHeight();
	var iw = $('bg_img').getWidth();
	var ih = $('bg_img').getHeight();
	
	$('bg_img_holder').style.width = vw + "px";
	$('bg_img_holder').style.height = vh + "px";
	if (ie6minus) return;
	
	if (vw/vh > iw/ih) {  // Fenster hat Querformat
		$('bg_img').style.width = "100%";
		$('bg_img').style.height = "auto";
		ih = $('bg_img').getHeight();
		if (ih > vh) $('bg_img').style.marginTop = (vh - ih) + "px";
		$('bg_img').style.marginLeft = 0;
	}
	else		// Fenster hat Hochformat
	{
		$('bg_img').style.width = "auto";
		$('bg_img').style.height = "100%";
		iw = $('bg_img').getWidth();
		if (iw > vw) $('bg_img').style.marginLeft = Math.round((vw - iw)/2) + "px";
		$('bg_img').style.marginTop = 0;
	}
	
	clearTimeout(resizeTimeoutID);
	resizeTimeoutID = setTimeout(resizeBGImg, 120);
}

// ---- Newsletter ----

function showHideNLInfo( checker )
{
	if ($('NL_pressInfo'))
		$('NL_pressInfo').style.display = (checker.checked)? "block" : "none";
}

function acceptAddInfo()
{
	if ($('addInfo').value != "")
		$('attribute1').value += ' -- ' + $('addInfo').value;

	var v = $('attribute1').value;
	v = str_replace("ä", "ae", v);
	v = str_replace("Ä", "Ae", v);
	v = str_replace("ö", "oe", v);
	v = str_replace("Ö", "Oe", v);
	v = str_replace("ü", "ue", v);
	v = str_replace("Ü", "Ue", v);
	v = str_replace("ß", "ss", v);
	$('attribute1').value = v;
}

// --- Helper ---

// Findet Position von Document-Objekten
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

// Linear -> Sinus Funktion
function easeInOutSine(mult, counter, counter_max, offset) {
	return -mult/2 *(Math.cos(Math.PI*counter/counter_max) - 1) + offset;
}

// Linear -> Quadratische Funktion
function easeInOutCubic(t, b, c, d) {
    if((t/=d/2) < 1) return c/2*t*t*t + b;
    return c/2*((t-=2)*t*t + 2) + b;
}

// Linear -> Exponentielle Funktion
function easeInOutExpo(t, b, c, d) {
    if(t==0) return b;
    if(t==d) return b+c;
    if((t/=d/2) < 1) return c/2 * Math.pow(2, 10 *(t - 1)) + b - c * 0.0005;
    return c/2 * 1.0005 *(-Math.pow(2, -10 * --t) + 2) + b;
}

// Liefert das Flash-Objekt zurueck
function getFlashObject( movieName ) {
	if ( top.window.document[movieName]) return top.window.document[movieName];
	if ( navigator.userAgent.indexOf("Windows")!=-1 && navigator.appVersion.indexOf("MSIE")==-1) {
		if ( top.document.embeds && top.document.embeds[movieName]) return top.document.embeds[movieName]; 
		else return false;
	}else{
		return testElementbyId(movieName);
	}
	return false;
}

function str_replace(search, replace, subject) {
    return subject.split(search).join(replace);
}

// UTF-8 Encoder (von selfhtml.org)
function encode_utf8(rohtext) {
	// dient der Normalisierung des Zeilenumbruchs
	rohtext = rohtext.replace(/\r\n/g,"\n");
	var utftext = "";
	for(var n=0; n<rohtext.length; n++)
	    {
	    // ermitteln des Unicodes des  aktuellen Zeichens
	    var c=rohtext.charCodeAt(n);
	    // alle Zeichen von 0-127 => 1byte
	    if (c<128)
	        utftext += String.fromCharCode(c);
	    // alle Zeichen von 127 bis 2047 => 2byte
	    else if((c>127) && (c<2048)) {
	        utftext += String.fromCharCode((c>>6)|192);
	        utftext += String.fromCharCode((c&63)|128);}
	    // alle Zeichen von 2048 bis 66536 => 3byte
	    else {
	        utftext += String.fromCharCode((c>>12)|224);
	        utftext += String.fromCharCode(((c>>6)&63)|128);
	        utftext += String.fromCharCode((c&63)|128);}
	    }
	return utftext;
}

