

// Umfragelayer der Medienforschung einblenden //

/* $(document).ready(function() {
	// Einbindung Umfragelayer Medienforschung. "pages" enthält die Seiten, auf denen angezeigt werden soll. "rnd" ermöglicht die Zufallssteuerung bei der Ausspielung
	var pages = new Array("/visite/visite1050.html", "/visite/index.html");
	var link = "http://www.onsitebefragung.de/uc/ndr/visite2010/";
	var title = "Umfrage zu Visite";
	var text = 'Wir laden Sie ein, an einer Umfrage zum Online-Angebot von Visite teilzunehmen.<br />Ihre Meinung zählt!';
	
	var pages2 = new Array("/markt/start148.html", "/markt/index.html");
	var link2 = "http://www.onsitebefragung.de/uc/ndr/markt2010/";
	var title2 = "Umfrage zu Markt";
	var text2 = 'Wir laden Sie ein, an einer Umfrage zum Online-Angebot von Markt teilzunehmen.<br />Ihre Meinung zählt!';
	
	for(i=0; i < pages.length; i++) {
		if (location.href.match(pages[i]) ) {
			var rnd = Math.random();
			if(rnd < 1 ) {
				mf_layer.write(link,title,text);
			}
		}
	 }
	 
	 for(i=0; i < pages2.length; i++) {
		if (location.href.match(pages2[i]) ) {
			var rnd = Math.random();
			if(rnd < 1 ) {
				mf_layer.write(link2,title2,text2);
			}
		}
	 }
})
*/
function loadscript(e) {
			var s = document.createElement('script');
			s.setAttribute('type','text/javascript');
			s.setAttribute('src','/resources.alt/'+e);		
			document.getElementsByTagName('head')[0].appendChild(s);
}


var gbl_ndr = {
	bookmarks: function() {
		$('.footerleftlist li:last').append('|');
	    $('.footerleftlist').append('<li><a href="javascript:gbl_ndr.openbookmarks();" title="">Lesezeichen setzen</a></li>');
	    $('#bookmarklist li').hover( function() { $(this).css('background-color','#cecece')}, function() {$(this).css('background-color','#ffffff')});
	    $('#bookmarklist a').each(function() {
	    	var ref = $(this).attr("href");
	    	ref = ref.replace(/ADRESSE/, encodeURIComponent(document.location.href));
	    	ref = ref.replace(/UEBERSCHRIFT/, encodeURIComponent(document.title));
	    	$(this).attr('href', ref);
	    });

	},
	openbookmarks: function() {
	    $('#bookmarklist').slideToggle();
	},
	formcheck: function() {
		// if (!document.referrer.match(/ndr\.de/)) { return false }
		var fields = gbl_ndr.getparams()
		$(".centerform :input, .centerform :checkbox").each( function() {
			if(fields[$(this).attr("name")]) {
				if(fields[$(this).attr("name")] == "fielderror") {
					$(this).parent(".formfield").css({"border": "2px solid #c00", "padding": "4px", "margin": "4px 0"});
					if($(this).is(":checkbox")) {
						$(this).parent(".littleElements").css({"border": "2px solid #c00", "padding": "4px 4px 4px 25px", "margin": "4px 0"});
					}
				} else {
					if(($(this).attr("type") != "hidden") && ($(this).attr("type") != "radio")) { // exclude hidden fields for TIC-Reasons ;-)
						var vv = fields[$(this).attr("name")].replace(/\+/g, "%20");			
						$(this).val(decodeURIComponent(vv));
					} 
				}
			}
		} )
	},
	splitparams: function(q) {
		if (q == "") return;
		var werte = unescape(q);
		werte = werte.slice(1);
		var wp = werte.split("&");
		for (var i=0; i < wp.length; i++) {
			var name = wp[i].substring(0,wp[i].indexOf("="));
			var wert = wp[i].substring(wp[i].indexOf("=")+1, wp[i].length);
			this[name] = wert;
		}
	},
	getparams: function() {
		p = new gbl_ndr.splitparams(window.location.search);
		return p;
	},
	createoverlay: function() {
		if (typeof document.body.style.maxHeight === "undefined") {
			$("body","html").css({height: "100%", width: "100%"});
			$("html").css("overflow","hidden");
			$("select").hide();
			if (document.getElementById("hideselect") === null) {//iframe to hide select elements in ie6
				$("body").append("<iframe id='hideselect'></iframe><div id='overlay'></div>");
				$("#overlay").click(gbl_ndr.removeoverlay);
			}
		} else {//all others
			if(document.getElementById("overlay") === null){
				$("body").append("<div id='overlay'></div>");
				$("#overlay").click(gbl_ndr.removeoverlay).css("top", window.pageYOffset);
			}
		}
		
		if(tb_detectMacXFF()){
			$("#overlay").addClass("overlayMacFF");//use png overlay so hide flash
		}else{
			$("#overlay").addClass("overlayBG");//use background and opacity
		}
	},
	removeoverlay: function() {

		$("#overlay").fadeOut("fast",function(){$('#overlay, #hideselect').trigger("unload").unbind().remove();});
		
		if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
			$("body","html").css({height: "auto", width: "auto"});
			$("html").css("overflow","");
			$("select").show();
		}
		document.onkeydown = "";
		document.onkeyup = "";
		return false;
	},
	updatemedialinks: function() {
		$(".olplayer").each(function() {		
			var f = $(this).attr("href").replace(/.*\//, "/media/");
			var loc = "http://"+location.hostname;
			if (location.port != ""){
  			loc = loc + ":" + location.port;
			}					
			//var loc = location.href.substr(0,location.href.lastIndexOf("/"));
			var i = loc + f.replace(/\.html/, "-ondemandplayer.html");
			
			
			$(this).click(function() {
				gbl_ndr.player(i);
				return false;
			}).attr("title", i)
		})
		
		// Bugfix: Abfrage, ob mehrere Player auf der Seite existieren, oder zusätzlich zum embedded ein Overlay-Link
		// Wenn ja, Panels für Formatauswahl deaktivieren - to be fixed!
		
		if (($("div[id='stage']").length > 1) || ($(".olplayer").length > 0)) {
			$("#cont div[id='panel']").css("display","none");
		}
	},
	player: function(e) {
		if ($("#mediathekplayer").is(".mplay") != true) {
			$("body").append('<div id="mediathekplayer" class="mplay"></div>');
			$(".mplay:last").css("display","none");
		}

		gbl_ndr.createoverlay();
				
		if ($(".mplay").is(":hidden")) {
		var vscroll = window.pageYOffset || document.documentElement.scrollTop;
		$(".mplay").css({"display": "block","top": eval(vscroll+50)+"px"});
		$("#overlay").unbind().click(gbl_ndr.closeplayer);
		}

		try {
			$("#mediathekplayer").load(e, function() {
				gbl_ndr.activateplayer();
			})
		} catch(ee) {
			alert(ee);
		}
		
		document.onkeydown = function(e) {
			if(e == null) {
				keycode = event.keyCode;
			} else {
				keycode = e.which;
			}
			if(keycode == 27) {
				gbl_ndr.closeplayer();
			}
		}
	},
	activateplayer: function() {
		// adds dynamic life to Player
		$("#mediathekplayer .boxhead").append('<a href="javascript:gbl_ndr.closeplayer();" id="mplayerclose" title="Player schließen">X</a>');
		$("#mediathekplayer").addClass("has_js");

		$("#paneltabs li:not(#handle)").click( function() {

				if($("#panel").css('height') == "18px") {
					$("#panel").animate({height: 166}, "fast");
					$("#panel li").css({'background-image': 'url(/images.alt/arrow_up.gif)'});
				} else if ($(this).is(".activepanel")) {
					$("#panel").animate({height: 18}, "fast");
					$(".panelelement").css('display','none');
					$("#panel li").css({'background-image': 'url(/images.alt/arrow_down.gif)'});
				}
				$("#paneltabs li").removeClass('activepanel');
				$(this).addClass('activepanel');
				var myid = $(this).attr('id');
				var mypan = myid.replace(/tab$/, "panel");
				$(".panelelement").css('display','none');
				$("#"+mypan).css('display', 'block');		
		});
		$("#formatselect>div").hover(function() { 
			$(this).css("background-position","0 -68px");
		}, function() {
			$(this).css("background-position","0 0");
		});
		
		$("#flashselect div, #windowsselect div").hover(function() {
			$(this).css({
				"background-image": "url(/images.alt/formatselect_hilite_triple.png)",
				"background-position": "65px bottom",
				"background-repeat": "repeat-y"
				});
			}, function() {
			$(this).css({"background-image": "none"});
			});
		
		$("body>#mediathekplayer #optionspanel a").click(function() {
			
			if(location.href.match(/\/berlin2009\//)) {
				var f = $(this).attr("href").replace(/.*\//, "/");					
				var loc = location.href.substr(0,location.href.lastIndexOf("/"));
				var i = loc + f.replace(/\_format/, "-ondemandplayer_format");
			} else {
				var f = $(this).attr("href").replace(/.*\//, "/media/");
				var loc = "http://"+location.hostname;
				if (location.port != ""){
	  			loc = loc + ":" + location.port;
				}					
				var i = loc + f.replace(/_format/, "-ondemandplayer_format");
			}
			
			gbl_ndr.player(i);
			return false;
		})
			
		$("#link").focus(function() { $("#link").select()  });
		gbl_ndr.attachhelp();
	},
	closeplayer: function() {
	
		if(jQuery.browser.msie && window["flashplayer"]) {
			try {
			window["flashplayer"].showStopper();
			} catch(e) {
				// alert(e);
			}
		} 
	
		$("#mediathekplayer #paneltabs li, #mediathekplayer #mplayerclose, #mediathekplayer #optionspanel div").unbind();
		$("#mediathekplayer").empty();
		$(".mplay").css("display","none");
		gbl_ndr.removeoverlay();
			
	},
	attachhelp: function() {

		$("#helpme").hover(function() {
			$("#helpme").attr('src', '/images.alt/playericon_hilfe_h.gif');
			$("#helptext").css("display", "block");
			$("#mplayer").css("top", "-2000px");
		}, function() {
			$("#helpme").attr('src', '/images.alt/playericon_hilfe_l.gif');
			$("#helptext").css("display", "none");
			$("#mplayer").css("top", "7px");
			$(".widescreen #mplayer").css("top", "0px");
		});
		
	},
	mtplayerpop: function(e) {
		if(e == null) {
				var mtplayerurl = "http://www.ndr.de/flash/mediathek/index.html";
		} else {
				var mtplayerurl = e;
		}		
		window.name = "main";
		var mtplayer = window.open(mtplayerurl,"mediathek","width=1024, height=576, resizable=1, status=0, menubar=0, location=0");
		mtplayer.focus();	
		return false;
	},	
	zappplayerpop: function(e) {
		var zappplayer = window.open(e,"zapp","width=960, height=661, resizable=1, status=0, menubar=0, location=0");
		zappplayer.focus();	
		return false;
	},
	s45minplayerpop: function(e) {
		var zappplayer = window.open(e,"main","width=768, height=600, resizable=0, status=0, menubar=0, location=0");
		zappplayer.focus();	
		window.name = "main";
		return false;
	}		
};

function tb_detectMacXFF() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1) {
    return true;
  }
}

var mf_layer = {
	write: function(link,headline,text) {
		var l_link = (link) ? link : '#';
		var l_headline = (headline) ? headline : 'Umfrage';
		var l_text = (text) ? text : 'Ihr Meinung zählt!';
		var mychecker = mf_layer.get_cookie();
		var mylayer = '<div id="dynwrite"><div id="layerLabel">Umfrage</div><div id="closer"><a href="javascript:mf_layer.close()" class="text">schließen</a><a href="javascript:mf_layer.close()" class="cross">x</a></div><h2>' + l_headline + '</h2><p>' + l_text + '</p><a href="#" class="copytext" onmouseover="window.status=\'Zur Umfrage\'; return true;" onmouseout="window.status=\'\'; return true;" onClick="javascript:mf_layer.popup(\'' + l_link + '\');return false;"><img src="/images.alt/popup_startbtn.gif" alt="" /></a></div>';
	
			
		if(mychecker != "true") {
			mf_layer.put_cookie("true");
			$("body").append(mylayer);
			}
		},
	close: function() {
			$("#dynwrite").hide();
		},
	put_cookie: function(mywert) {
			document.cookie = "warda="+mywert+";expires=1";
		},
	get_cookie: function() {
			var Wert = "";
			if(document.cookie) {
				  var results = document.cookie.match ( '(^|;) ?warda=([^;]*)(;|$)' );
				  if ( results )
				    return ( unescape ( results[2] ) );
				  else
				    return null;
			}
		},
	popup: function(t) {
		popup_win = window.open(t,'popwin','width=775,height=750,left='+((screen.width-750)/2)+',top='+((screen.height-750)/2)+',toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=yes,scrollbars=yes,scrolling=auto');
		popup_win.focus();
	}
};

jQuery.fn.dyntable = function(set) {

	settings = jQuery.extend({
		minwidth: "96px",
		maxwidth: "192px",
		tablewidth: "548px",
		openimage: "/images.alt/dyntable_open.gif",
		closeimage: "/images.alt/dyntable_close.gif",
		tooltip: "Klicken Sie, um die Breite der Spalten anzupassen",
		noimage: "false",
		dohover: "false",
		doclick: "true",
		fadeinactive: "true"			
	}, set);

	var $this = jQuery(this);
	
	var count = 1;
	$this.find("th").each( function() {
		$(this).attr("id","c"+count);
		var cont = $(this).html();
		$(this).html("<div class='outer'><div class='inner'>"+cont+"</div></div>").css("width", settings.minwidth);
		count++;
	});
	$this.find("tr").each( function() {
		var count = 1;
		$(this).find("td").each( function() {
			$(this).attr("headers", "c"+count);
			var cont = $(this).html();
			$(this).html("<div class='outer'><div class='inner'>"+cont+"</div></div>").css("width", settings.minwidth);
				
			count++;
		});		
	});
	$this.addClass("dyntable");
	
	if(settings.noimage == "true") {
	$this.find("th").css("background-image", "none");
	}
	
	$this.find(".inner").css("width", settings.maxwidth);
	$this.find(".outer").css({"width": settings.minwidth, "overflow": "hidden"});
	
	if(settings.doclick == "true") {
		$this.find("th, td").click( function() {
		if($(this).attr("id")) {
			var e = $(this).attr("id").substr(1,$(this).attr("id").length);
			} else {
			var e = $(this).attr("headers").substr(1,$(this).attr("headers").length);
		}
			
			if($(this).find(".outer").css("width") == settings.maxwidth) {
				
				// dtfunc.setcol(e,settings.minwidth);
			} else {
				dtfunc.resetcols();
				dtfunc.setcol(e,settings.maxwidth);
				$("th#c"+e).addClass("opened");
			}
		}).css("cursor","pointer");
	}
	if(settings.dohover == "true") {
	}	
	$this.find("th:first").trigger("click");
}

var dtfunc = {

setcol: function(e,w) {
	
	$("th#c"+e+" .outer, td[@headers='c"+e+"'] .outer").css({"width": w});
		
	if(settings.fadeinactive == "true") {
	 	$("td[@headers='c"+e+"'] .outer").css({"color":"#333"});
	}
	},
resetcols: function() {
		$(".dyntable").find(".outer").css({"width": settings.minwidth});
		
		if(settings.fadeinactive == "true") {
		$(".dyntable td").find(".outer").css({"color": "#aaa"});
		}
		
		$(".dyntable th").removeClass("opened");
	}
}


// Defaults für jqueryUI Datepicker






// ------------ Historische Restfunktionen -------------------

var externalLinkText = "Internet-Link";
var textForNewWindow = "(in neuem Fenster)";

function changeTitleAndTarget() {
	if(!document.getElementsByTagName) return;
	var links = document.getElementsByTagName("a");
	for(i=0; i < links.length; i++) {
		var singleLink = links[i];
		titleOld = singleLink.getAttribute("title");
		if(titleOld) {
			if(titleOld.indexOf(externalLinkText) != -1) {
				titleAddition = titleOld.substring((titleOld.indexOf(externalLinkText)) + externalLinkText.length, titleOld.length);
				singleLink.setAttribute("title", externalLinkText + " " + textForNewWindow + titleAddition);
				singleLink.target = "_blank";
			}
		}
	}
}

/* needs two functions, if a ...... user enters the default Value... */
function onFocusCheckEntry(which, cont) {
	if(which.value == cont) {
		which.value = "";
	}
}

function onBlurCheckEntry(which, cont) {
	if(which.value == '') {
		which.value = cont;
	}
}

function topnavigation(which) {
	if(which != 'clear') {
		document.getElementById("topnav").src = "/images.alt/topnav_" + which + ".gif";
	} else {
		document.getElementById("topnav").src = "/images.alt/topnav_hilite.gif";
	}
}

function mainnavigation(which, destState) {
	var source = which.firstChild.src
	var replaceState = source.substr(source.length - 5, source.length);
	which.firstChild.src = which.firstChild.src.replace(replaceState, destState + '.gif');	
}
