/*
 * WDR Wissen Javascript
 * 
 * jQuery Menü
 * jQuery Bilder resize
 * jQuery Video- Audioplayer anzeigen
 * jQuery Programmtipps merken
 *
 * Copyright (c) 2008 pro in space GmbH
 */
$(document).ready(function() {

	// resize selcet for ie
	if ($.browser.msie && parseInt($.browser.version) < 9) {
		selectObj = $('.selectnavi');
		if (selectObj.length > 0) {
			selectObj.focus(function(){
				$(this).parents('.teaserright').css('overflow','visible');
				$(this).css('width','auto');
			});
			selectObj.blur(function(){
				$(this).css('width','13.182em');
			});
			selectObj.change(function(){
				$(this).css('width','13.182em');
			});
		}
	}

	// Counter fuer die Anzeige der Programmtipp Anzahl
	// wird in r_merkliste aus der session gesetzt
	if (typeof(progtippcounter) == "undefined") {
		progtippcounter = 0;
	}
	
	// Merkfunktion Formular Verarbeitung
	tippsClick = function (clicked, id) {		
		// did des tipps
		var hiddenval = $(clicked).attr('value');
		
		// Prueft, ob das Formularfeld ausgewaehlt war
		var chk_class = $(clicked).is(':checked');
	
		if(chk_class == true) {
			// Markiert das Div mit dem Formular bei Click
			$(clicked).parent().parent().parent().addClass("highlight");
			progtippcounter ++;
			// neue id in session schreiben
			$.post("/wissen/common/merken.php5?Progtipp0="+hiddenval);
		} else {
			// Entfernt die Markierung des Div mit dem Formular bei Click
			$(clicked).parent().parent().parent().removeClass("highlight");
			if(progtippcounter > 0) progtippcounter --;
			// id aus session löschen
			$.post("/wissen/common/merken.php5?delete="+hiddenval);
		}
		
		// Hier wird der Counter Wert eingetragen
		if(document.getElementById('merkenanzahl')) {
			document.getElementById('merkenanzahl').innerHTML = progtippcounter;
			if (progtippcounter==1) {
				document.getElementById('merkenendung').innerHTML = "";
			} else {
				document.getElementById('merkenendung').innerHTML = "en";
			}
		}
	};

	// flash hide players
	$('.audioplayer').hide();
	$('.videoplayer').hide();
	// hide and show player
	newposition = -1000;
	offset = 44;
	$('.playeropen > a').click(function() {
		playerblock = $(this).parent().next();
		if(playerblock.hasClass("audioplayer")) {	
			playerblock.slideToggle('normal');
			return false;
		} else {
			if(playerblock.hasClass("withpadding")) {
				offset = 64;
			}
			if(!playerblock.hasClass("opened")) {
				playerblock.addClass("opened");
				playerblock.slideDown('normal',function(){
						blockheight = playerblock.height();
						oldposition = playerblock.children(".videocontainer").position().left;
						if (newposition==-1000) {
							newposition = oldposition-offset;
						}
						playerblock.css({'height':blockheight});
						playerblock.children('.videocontainer').css({'position':'absolute','z-index':'100000','left':newposition});
						var videoobject = playerblock.children().children('object');
      				});
				return false;
			} else {
				playerblock.removeClass("opened");
				playerblock.slideUp('normal',function(){
						playerblock.css({'height':'auto'});
						playerblock.children(".videocontainer").css({'position':'relative'});
      				});
				var objArr = document.getElementsByTagName('object');
				for(var i = 0; i < objArr.length; i++) {
					if(typeof objArr[i].mediaPlayPause == 'function') {
						objArr[i].mediaPlayPause();
					}
				}
				return false;
			}
		}
	});
	$('.audioplayerX').click(function() {
		$(this).parent().slideUp('normal');
		var objArr = document.getElementsByTagName('object');
		for(var i = 0; i < objArr.length; i++) {
			if(typeof objArr[i].mediaPlayPause == 'function') {
				objArr[i].mediaPlayPause();
			}
		}
		return false;
	});
	$('.videoplayerX').click(function() {
		$(this).parent().parent().parent().slideUp('normal');
		$(this).parent().parent().parent().removeClass('opened');
		var objArr = document.getElementsByTagName('object');
		for(var i = 0; i < objArr.length; i++) {
			if(typeof objArr[i].mediaPlayPause == 'function') {
				objArr[i].mediaPlayPause();
			}
		}
		return false;
	});

	
	// dynamisches menü für video-, audio- und linktipp
	// letzte link-gruppe activ setzen - andere ausblenden
	$('ul#listtop:eq(0)> li').eq(($('ul#listtop:eq(0)> li').length)-2).children().addClass('activ');
	for(i=0; i<($('ul#listtop:eq(0)> .listcontent').length)-1; i++) {
		$('ul#listtop:eq(0)> li.listcontent').eq(i).hide();
	}
	// mouseover funktion
	$('ul#listtop:eq(0)> li > a').click(function() {
		// set link activ setzen
		$(this).parent().parent().find('li > a').removeClass('activ');
		$(this).addClass('activ');
		// boxen verstecken und anzeigen
		$(this).parent().next().slideDown('normal').siblings('li.listcontent:visible').slideUp('normal');
	});
	
	// multiteaser boxen länge angleichen
	maxheight = 0;	
	for(i=0; i<($('div.multicont_zeile > div').length)-1; i++) {
		if (maxheight == 0 || maxheight < $('div.multicont_zeile > div').eq(i).height()) {
			maxheight = $('div.multicont_zeile > div').eq(i).height();
		}
	}
	maxheight = maxheight*(1/12);
	maxheight = maxheight+'em';
	var cssObj = {height: maxheight};
	$('div.multicont_zeile > div').css(cssObj);


	// alle sendunglegende details beim laden der seite ausblenden und show links erstellen und einblenden
	var sendunglegende = ($('div.sendunglegende .info').length);
	var sendunglegende = ($('div.sendunglegende .info').length);
	for(i=0; i<sendunglegende; i++) {		
		// details ausblenden
		if (!$('div.sendunglegende .info .detail').eq(i).hasClass('open')) {
			$('div.sendunglegende .info .detail').eq(i).hide();
		}		
		// show link erstellen
		$('div.sendunglegende .info .button').html('<a title="Informationen einblenden">Informationen einblenden</a>');
		// show link einblenden
		$('div.sendunglegende .info .button').eq(i).show();
		$('div.sendunglegende .info .button').eq(i).hover(function() {
			$(this).addClass('hoverstuff');
		}, function() {
			$(this).removeClass('hoverstuff');
		});
	}
	
	// click funktion detail und link ein- ausblenden
	$('div.sendunglegende .info .box').bind("click", function(){		
		// id des elements das behandelt werden soll
		var theID = $(this).parent().attr('id');
		// fall: details ein- oder ausgeblendet
		var theCase = $('div.sendunglegende .info#'+theID+' .detail').css("display");		
		// details einblenden
		if(theCase == 'none') {			
			$('div.sendunglegende #'+theID+' .button').html('<a title="Informationen ausblenden">Informationen ausblenden</a>');	
			$('div.sendunglegende #'+theID+' .detail').slideDown();
		} else {
		// details ausblenden
			$('div.sendunglegende #'+theID+' .button').html('<a title="Informationen einblenden">Informationen einblenden</a>');			
			$('div.sendunglegende #'+theID+' .detail').slideUp();
		}
	});
});

resizeContentPic = function(which, srcBig, srcSmall, format) {
	duration = 300;
	which = which.parent();
	//alert(which.html());
	if (format == 'hoch') {
		if ($(which).css('width') == '36.5em') {
			$(which).animate({width: '10em'}, duration);
			$(which).children('.artikelbild').children('img').animate({float: 'left', width: '10em', height: '13.3333em'}, duration);
			$(which).children('div.pictext_lupe').animate({width: '10em'}, duration);
			$(which).children('div.pictext_lupe').children('p').animate({width: '7em'}, duration);
			$(which).children('div.pictext_lupe').children('a').children('img').attr('src', '/wissen/common/template-elemente/img/icons/lupe_plus.gif');
			$(which).children('div.pictext_lupe').children('a').children('img').attr('alt', 'Bild vergrößern');
			$(which).children('div.pictext_lupe').children('a').attr('title', 'Bild vergrößern');
			$(which).children('.artikelbild').children('img').attr('src', srcSmall);
		} else {
			$(which).children('.artikelbild').children('img').attr('src', srcBig);
			$(which).animate({width: '36.5em'}, duration);
			$(which).children('.artikelbild').children('img').animate({float: 'left',	width: '36.5em', height: '48.7em'}, duration);
			$(which).children('div.pictext_lupe').animate({width: '36.5em'}, duration);
			$(which).children('div.pictext_lupe').children('p').animate({width: '33.1666em'}, duration);
			$(which).children('div.pictext_lupe').children('a').children('img').attr('src', '/wissen/common/template-elemente/img/icons/lupe_minus.gif');
			$(which).children('div.pictext_lupe').children('a').children('img').attr('alt', 'Bild verkleinern');
			$(which).children('div.pictext_lupe').children('a').attr('title', 'Bild verkleinern');
		}
	} else {
		if ($(which).css('width') == '36.5em') {
			$(which).animate({width: '13.3333em'}, duration);
			$(which).children('.artikelbild').children('img').animate({float: 'left', width: '13.3333em', height: '10em'}, duration);
			$(which).children('div.pictext_lupe').animate({width: '13.3333em'}, duration);
			$(which).children('div.pictext_lupe').children('p').animate({width: '11em'}, duration);
			$(which).children('div.pictext_lupe').children('a').children('img').attr('src', '/wissen/common/template-elemente/img/icons/lupe_plus.gif');
			$(which).children('div.pictext_lupe').children('a').children('img').attr('alt', 'Bild vergrößern');
			$(which).children('div.pictext_lupe').children('a').attr('title', 'Bild vergrößern');
			$(which).children('.artikelbild').children('img').attr('src', srcSmall);
		} else {
			$(which).children('.artikelbild').children('img').attr('src', srcBig);
			$(which).animate({width: '36.5em'}, duration);
			$(which).children('.artikelbild').children('img').animate({float: 'left',	width: '36.5em', height: '27.375em'}, duration);
			$(which).children('div.pictext_lupe').animate({width: '36.5em'}, duration);
			$(which).children('div.pictext_lupe').children('p').animate({width: '33.1666em'}, duration);
			$(which).children('div.pictext_lupe').children('a').children('img').attr('src', '/wissen/common/template-elemente/img/icons/lupe_minus.gif');
			$(which).children('div.pictext_lupe').children('a').children('img').attr('alt', 'Bild verkleinern');
			$(which).children('div.pictext_lupe').children('a').attr('title', 'Bild verkleinern');
		}
	}
}

printPage = function() {
	var cssObjPrint = {height:"auto"};
	$('div.multicont_zeile > div').css(cssObjPrint);
	window.print();
}

// ie only
window.onbeforeprint = myFunction;
function myFunction(){
	var cssObjPrint = {height:"auto"};
	$('div.multicont_zeile > div').css(cssObjPrint);
}

// Zähler-Funktionen für das Gästebuch
// bei erreichen von maxValue wird ALERT ausgelöst
var alerted = false;
var running = false;
function checkMax(maxValue) {
	if(document.getElementById && !running) {
		running = true;
		var anzahl = document.getElementById("gbtext").value.length;
		var rest = parseInt(maxValue) - parseInt(anzahl);
		if(rest <= 0) {
			if(!alerted) showMsg(rest);
			alerted = true;
			document.getElementById("gbzeichenzahl").firstChild.nodeValue = "0";
			var textfeld = document.getElementById("gbtext");
			textfeld.value = textfeld.value.substring(0, 1000);
		} else {
			document.getElementById("gbzeichenzahl").firstChild.nodeValue = rest;
		}
	}
	running = false;
}
function showMsg(rest) {
	var absRest = Math.abs(parseInt(rest));
	if(!alerted) {
		if(rest == 0) {
			alert("Achtung: Die Zeichenbegrenzung wurde erreicht!");
		} else {
			alert('Achtung: Die Zeichenbegrenzung wurde erreicht! Die letzten '+absRest+' Zeichen werden abgeschnitten!');
		}
	}
}

/* Videoplayer Funkionen */
/* initialer Test des VideoPlayers, ob JavaScript aktiviert ist */
function getScriptState() {
	return true;
};
function getReferer() {
	return escape(location.href); /* Seiten URL wird als Referer an das Zählpixel übergeben */
};
/* onunload */
window.onunload = function() {
	var objArr = document.getElementsByTagName('object');
	for(var i = 0; i < objArr.length; i++) {
		if(typeof objArr[i].stopAllMedia == 'function') { objArr[i].stopAllMedia(); }
	}
};

//
// Popupfenster
//
function popup(url, breite, hoehe, arg, name) {

  var winname = "win";
  var winarg = "";
  var x = 0;
  var y = 0;

  if(name != '') winname = name;

  if(winname == 'popup') { winname = winname + (new Date()).getTime(); }

  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;
    if(arg != '') {
      winarg += ','+arg;
    } else {
      winarg += ',resizable,menubar,scrollbars';
    }
  }

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

  returnonclick = false;
  eval("if(!" + winname + " && !" + winname + ".open) {returnonclick = true\;}");
  return returnonclick;
  //eval("if (!" + winname + ".closed && " + winname + ".location) {return false} else {return false}");
}

function popup_quiz(url) {
  return popup(url, 622, 500, '', 'wissen_popup_quiz');
}

