// usage: log('inside coolFunc', this, arguments);
// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function(){
log.history = log.history || [];   // store logs to an array for reference
log.history.push(arguments);
arguments.callee = arguments.callee.caller;
if(this.console) console.log( Array.prototype.slice.call(arguments) );
};
// make it safe to use console.log always
(function(b){function c(){}for(var d="assert,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEnd,info, log,markTimeline,profile,profileEnd,time,timeEnd,trace,warn".split(","),a;a=d.pop();)b[a]=b[a]||c})(window.console=window.console||{});



//settings and global functions
var ntv = new Object();
ntv = {
	random:	Math.round( Math.random() * 999998 + 1 ),
	basepath: window.location.protocol+"//"+window.location.host,
	pageUrl: "http://www.teleboerse.de",
	articleID: "",
	videotag: "video",
	log: function( message ) {
		if( "console" in window && console.debug ) {
			console.debug( message );
		}
	}
};

function printview(url) {
  	var mw = window.open(url, "ntv_Meldung_Druckansicht", "width=835,height=600,scrollbars=yes,toolbar=no,status=no,menubar=no,location=no");
  	mw.focus();
	return false;
}

//IVW-Load
function loadIVW(options) {

	var settings = $.extend({
		"ivw_tag": ivw_code,
		"pageType": ntv_pageType
	}, options);

	var ivw_image = new Image();
	if(typeof (ntv_section)!="undefined"){
	ivw_image.src="http://ntv.ivwbox.de/cgi-bin/ivw/CP/"+settings.ivw_tag+";/"+ntv_sitename+"/"+ntv_section+"/"+settings.pageType+"?r="+escape(document.referrer)+"&d="+(Math.random()*100000);
	}
}

//IVW-Load durch Videoplayer
function reloadIVW() {
	if( ntv_location != 'media' ){
		loadIVW({
			"ivw_tag": ntv.videotag,
			"pageType": "video"
		});
	}
}



function playerEvent(event, data){
	switch(event){
		case "RTL_ADBLOCK_START":
			break;

		case "RTL_ADBLOCK_END":
			break;

		case "RTL_CHAPTER_CHANGE":
			$(".video_box #infoBox").ntv_playerChangeInfoBox(event, data);
			break;
		case "RTL_CONTENT_END":
			$(".video_box #infoBox").ntv_playerChangeInfoBox(event);
			break;
	}
	return true;
}


// jquery functions

(function($) {

	$.fn.ntv_bookmarking = function() {
		var html = "";
		html += "<p>";
		html += "Lesezeichen im Browser setzen<br />";
		html += "<button class=\"bookmark\" type=\"button\">Lesezeichen</button>";
		html += "</p>";

		var event = null;
		if( navigator.userAgent.toLowerCase().indexOf('ie') != -1 ) {
			event = function() {
				try {
					window.external.AddFavorite( window.location, document.title );
				} catch ( e ) {	ntv.log(e);}
			};
		} else if( navigator.userAgent.toLowerCase().indexOf('moz') != -1 ) {
			event = function() {
				try {
					window.sidebar.addPanel( document.title, window.location, '' );
				} catch ( e ) { ntv.log(e);	}
			};
		}

		return this.each( function() {
			if ( event != null ) {
				$( this ).html( html ).find( "button.bookmark" ).click( event );
			} else {
				$( this );
			}
		});
	};

	// click to big pictures
	$.fn.ntv_click_to_big = function() {
		return this.each( function() {
			var image = new Image();

			image.onload = function() {

				var $container = $( "<div class='big_pic' />" ).append( image );
				var img = $container.find( "img" ).get( 0 );

				$container.css( "height", $( "body" ).height()+"px" );
				$( "body" ).prepend( $container );

				var posX = $( self ).scrollTop() + ( $( self ).height()/2 - img.height/2 );
				var posY = $( self ).width() / 2 - img.width / 2;

				$( img ).css( "top", posX+"px" );
				$( img ).css( "left", posY+"px" );

				$container.fadeIn( "slow" );
				$container.bind( "click", function() {
					$container.fadeOut( "slow", function() {
						try {
							$( $container ).remove();
						} catch (err) {}
					});
				});

				loadIVW();

			};

			image.src = this.href;




		});
	};

	$.ntv_show_sendForm = function(url) {


		var $layer = $( '<div class="defaultLayer" />' );
		var $formContainer = $('<div id="email_form_layer" class="clearfix"/>');
		var articleUrl = url?url:ntv.pageUrl;


		var posX = $( self ).scrollTop() + ( $( self ).height()/2 - 170 );
		var posY = $( self ).width() / 2 - 344;

		$layer.prepend($formContainer);

		$layer.css( "height", $( "body" ).height()+"px" );
		$formContainer.css( {"top": posX+"px", "left": posY+"px"} );


		$( "body" ).prepend( $layer );
		$layer.fadeIn( "slow" );



		$formContainer.load(articleUrl + '?service=ajax&action=remail_wide&cache=false', function(response, status){
			if(status=="error"){
				$formContainer.html('<div class="close" onclick="$(\'.defaultLayer\').fadeOut(\'slow\');"></div><span class="error">Das Formular konnte nicht geladen werden</span>');
				window.setTimeout('$(".defaultLayer").fadeOut("slow")', 2000);
			}
		});

	};

	// navigation for the fb ticker
	$.fn.ntv_fb_ticker_navigation = function() {
		$container = $( this );
		$( this ).find( "ul.navi li a" ).each( function( index, item ) {
			$( item ).click( function() {
				var activeLi = $container.find( "ul.navi li.active" ).get( 0 );
				var activateLi = $container.find( "ul.navi li" ).get( index );

				var activeTb = $container.children( "div.active" ).get( 0 );
				var activateTb = $container.children( "div" ).get( index );

				$( activeLi ).removeClass( "active" );
				$( activateLi ).addClass( "active" );

				$( activeTb ).removeClass( "active" );
				$( activateTb ).addClass( "active" );

				return false;
			});
		});
	};


	$.fn.ntv_hpnews_search = function() {
		return this.each( function() {
			var g_cleared = false;
			var g_interval = null;
			var inputField = $(this);

			if ($(this).attr("value").length >= 3) {
				window.clearInterval(g_interval);
				$('#hpnews_loading').show();
				g_interval = window.setInterval(function () {

					$("#hpnews_city").empty();

					$.get( "http://www.n-tv.de/ext.jsp?s=wetter&f=ntvsearchdeutschland/search_" + encodeURI($(inputField).val()) + ".xml",
					function( xml ){
						$( "item", xml ).each(
								function( i, item ){
									$("#hpnews_city").append("<option value=\"" + $( item ).attr( "wmo" ) + ";" + $( item ).attr( "city" ) + "\">" + $( item ).attr( "city" ) + "(" + $( item ).attr( "bl" ) + ")</option>");
								}
						);

						$('#hpnews_loading').hide();
					});

				/*
					//$.getJSON("http://www.n-tv.de/tools/wetter2007/dispatcher?tpl=content&decode=1&plz_ort_fragment="+encodeURI($(inputField).val())+"&jsoncallback=?", function (data) {
					$.getJSON("hpnews_json.html", function (data) {

						if (data) {
							$("#hpnews_city").empty();
							$.each(data.data, function (index, d) {
								$("#hpnews_city").append("<option value=\"" + d.key + "\">" + d.value + "</option>");
							});
						}
						$('#hpnews_loading').hide();
					});
				*/


					window.clearInterval(g_interval);
				}, 500);
			}
		});
	};

	$.fn.ntv_hpnews_generate = function() {
		return this.each( function() {


				$('#hpnews_code').hide();
				$("#hpnews_code textarea").empty();



				var html = "<iframe src=\"http://www.n-tv.de/myntv";
				html += "?";
				var elements = $("#hpnews_form input:checked");
				if (elements.length > 1) {
					$("#hpnews_error").hide();

					$.each(elements, function (index, item) {
						html += item.name + "=" + item.value;
						if (index < elements.length - 1) {
							html += "&";
						}
					});
					if ($("#hpnews_form input[@name=\"size\"]:checked").val() == "maxi") {
						if ($("#hpnews_form #hpnews_city").val()) {
							var params = $("#hpnews_form #hpnews_city").val().split(";");
							html += "&city=" + encodeURI(params[1]) + "&wmo=" + params[0];
						} else {
							html += "&city=Berlin&wmo=10384";
						}
					}
					html += "\" name=\"n-tv.de\" scrolling=\"no\" frameborder=\"0\" ";
					if ($("#hpnews_form input[@name=\"size\"]:checked").val() == "mini") {
						html += "width=\"200px\" height=\"250px\">";
					} else {
						if ($("#hpnews_form input[@name=\"size\"]:checked").val() == "midi") {
							html += "width=\"300px\" height=\"300px\">";
						} else {
							if ($("#hpnews_form input[@name=\"size\"]:checked").val() == "maxi") {
								html += "width=\"400px\" height=\"350px\">";
							}
						}
					}
					html += "</iframe>";

					$("#hpnews_form #hpnews_code textarea").val(html);
					$('#hpnews_code').toggle(500);

				} else {
					$("#hpnews_error").show();
				}
		});

	};

	$.fn.ntv_hpnews_show = function() {
		return this.each( function() {
			if($(this).attr("id")=="hpnews_showmini"){
				$('#iframe_mini').toggle(500);
				$('#hpnews_mini').attr('checked','checked');
				//$('#hpnews_weather').hide();
			}
			if($(this).attr("id")=="hpnews_showmidi"){
				$('#iframe_midi').toggle(500);
				$('#hpnews_midi').attr('checked','checked');
				//$('#hpnews_weather').hide();
			}
			if($(this).attr("id")=="hpnews_showmaxi"){
				$('#iframe_maxi').toggle(500);
				$('#hpnews_maxi').attr('checked','checked');
				//$('#hpnews_weather').show();
			}
		});
	};


	// make text clickable
	$.fn.ntv_js_link = function() {
		return this.each( function() {
			var link = $( this ).find( "a" ).get( 0 );

			if( link != undefined ) {
				var href = $( link ).attr( "href" );
				var target = $( link ).attr( "target" ) || "_self";

				$( this ).css( "cursor", "pointer" );
				$( this ).click( function() {
					window.open( href, target );
				});
			}
		});
	};


	$.fn.ntv_media_teaser = function( options ) {
		var settings = $.extend({
			interval: 5000,
			speed: 350,
			wrapper: ".wrapper_nav"
		}, options);

		return this.each( function() {
			var teaser = this;

			// navigation functionality
			/*
			var $navigation = $( this ).find( "ul#mediathek_teaser_header" );
			var $links = $( this ).find( "a.videos, a.programm" );

			$links.each(function( index, link ){
				$( this ).click(function(){
					$navigation.find( "li.active" ).removeClass( "active" );
					$( this ).parent( "li" ).addClass( "active" );
					var container = $( this ).attr( "href" );

					$( teaser ).find( "#mediathek_teaser_body div.active" ).removeClass( "active" );
					$( container ).addClass( "active" );

					return false;
				});
			});
			*/

			// fader functioniality
			var $items = $( this ).find( "ul.fade li" );
			var $wrapper = $( this ).find( settings.wrapper );

			var index = 0;
			var count = $items.size();
			var interval = null;
			var intervalStart = null;
			var timeRemaining = null;

			// pause and resume fading
			$items.hover(
				function() {
					timeRemaining = settings.interval - ( new Date().getTime() - intervalStart );
					window.clearTimeout( interval );
				},
				function(){
					setFadeTimeout( timeRemaining );
				}
			);

			function slide( active, next, clear ) {


				$wrapper.html( $( ".wrapper_text", next ).html() );
				$( active ).removeClass( "active" );
				$( next ).addClass( "active" );

				/*

				// text change
				$wrapper.fadeOut( settings.speed, function() {
					$wrapper.html( $( ".wrapper_text", next ).html() );
					$wrapper.fadeIn( settings.speed );
				});

				// image change
				$( active ).fadeOut( settings.speed, function() {
					$( active ).removeClass( "active" );
					$( next ).fadeIn( settings.speed, function(){
						$( next ).addClass( "active" );
					});

				});

				*/

				clear ? window.clearTimeout( interval ) : setFadeTimeout( settings.interval );
			}

			function setFadeTimeout( timeout ){
				var timout = timout || settings.interval;

				interval = window.setTimeout( function() {
					var active = $items[index];
					index < count-1 ? index++ : index = 0;
					var next = $items[index];
					slide( active, next, false );
				}, timeout );

				intervalStart = new Date().getTime();
			}

			if(count > 0) {
				setFadeTimeout( settings.interval );
			}

		});
	};

/*
	$.fn.ntv_medrect = function() {
		if( $(this).height() >  30 && $("#medrect_ref").length>0 ){
			$("#medrect_ref").before('<div class="medrect_header">Anzeige</div>');
			$(this).css({'top' : $("#medrect_ref").offset().top,  'left' : $("#medrect_ref").offset().left, 'display' : 'block'});
			$("#medrect_ref").css({'width' : $("#medrect").width(), 'height' : $("#medrect").height()});
			$("#medrect_ref").after('<div class="medrect_footer"></div>');
		}
	}
*/

	$.fn.ntv_medrect = function( rePos ) {
		if( $(this).height() >  30 && $("#medrect_ref").length>0 ){
			if ( !rePos ){
				$("#medrect_ref").before('<div class="medrect_header">Anzeige</div>');
			}
			$(this).css({'top' : $("#medrect_ref").position().top,  'left' : $("#medrect_ref").position().left, 'display' : 'block'});
			$("#medrect_ref").css({'width' : $("#medrect").width(), 'height' : $("#medrect").height()});
			if ( !rePos ){
				$("#medrect_ref").after('<div class="medrect_footer"></div>');
			}
		}
	};

	$.fn.ntv_navigation = function( settings ) {
		// settings
		var s = $.extend({
			activeClass: "hover",
			last_link: null,
			pageWidth: 996,
			time: 250,
			timeout: null,
			speed: 'fast'
		}, settings);

		// events
		return this.each( function() {

			// layer events + positioning
			var $layer = $( this ).next();

			if( $layer.length !== 0 ) {
				// if the layer is to far to the right.
				if( $( this ).offset().left + $layer.width() > s.pageWidth ) {
					var overhang = ( $( this ).offset().left + $layer.width() ) - s.pageWidth;
					$layer.css( "left", $( this ).offset().left - overhang );
				}

				$layer.bind( "mouseover", function(e) {
					window.clearTimeout( s.timeout );
				});
				$layer.bind( "mouseout", function(e) {
					s.timeout = window.setTimeout( function() {
						navigator.userAgent.toLowerCase().indexOf('ie') != -1 ?
							$layer.hide() : $layer.fadeOut( s.speed );
					}, s.time );
				});

				// secondary links events
				$( this ).bind( "mouseover", function(e) {
					if( s.last_link != this ) {
						navigator.userAgent.toLowerCase().indexOf('ie') != -1 ?
							$( s.last_link ).next().hide() : $( s.last_link ).next().fadeOut( s.speed );
						s.last_link = this;
					} else {
						window.clearTimeout( s.timeout );
					}
					navigator.userAgent.toLowerCase().indexOf('ie') != -1 ?
						$layer.show() : $layer.fadeIn( s.speed );
				});
				// mouseout
				$( this ).bind( "mouseout", function(e) {
					s.timeout = window.setTimeout( function() {
						navigator.userAgent.toLowerCase().indexOf('ie') != -1 ?
							$layer.hide() : $layer.fadeOut( s.speed );
					}, s.time );
				});

				// layer links events
				var $links = $layer.find( "a" );
				// generate tree for coloring
				$links.each( function( index, anchor ) {
					$( anchor ).data( "tree", new Array() );
					(function( a ) {
						$( anchor ).data( "tree" ).push( a );
						var parent = $( a ).closest( "ul" ).prev( "a" );
						if( parent.length !== 0 ) {
							arguments.callee( parent );
						}
					})( anchor );
				});
				$links.hover(
					// mouseover
					function(e) {
						var tree =  $( e.target ).data( "tree" );
						$.each( tree, function() {
							$( this ).addClass( s.activeClass );
						});
					},
					// mouseout
					function(e) {
						var tree =  $( e.target ).data( "tree" );
						$.each( tree, function() {
							$( this ).removeClass( s.activeClass );
						});
					}
				);
			}
		});
	};


	$.fn.ntv_playerGoTo = function(container) {
		return this.each(function(){

			var player = $("> :first-child", container)[0];
			if(player){
				var part = ($(this).attr("class")).replace(/part_/g,"");
				player.startChapter(part);
			}

		});
	}


	$.fn.ntv_playerChangeInfoBox = function(event,part) {


		switch(event){
			case "RTL_CHAPTER_CHANGE":
				if ($(".part_" + part + " .info",this).length>0){
					$(this).find("li").each(function(){
						$(this).removeClass("active");
						$(".info",this).hide();
					});
					$(".part_" + part + " .info",this).toggle(500);
					$(".part_" + part,this).addClass("active");
				}
				break;
			case "RTL_CONTENT_END":
				$(this).find("li").each(function(){
					$(".info",this).hide();
					$(this).removeClass("active");
				});
				break;
		}
	}


	$.fn.ntv_player_new = function(settings) {

		var options = $.extend(
				{
					"w3c": true,
					"bgcolor": "#09224E",
					"advertising": true,
					"modus": "vod",
					"theme": "video",
					"teaser_url": null
				}, settings
		);

		var container = $(this);

		ntv.videotag = options.theme;

		var src = "http://www.teleboerse.de/stat/videoplayer/new/ntv_player.swf?data=" + options.data;

		if($(".video_box #infoBox").length>0){

			$("#infoBox").find("li").each(function(){

				var li = $(this);

				$(".topic", this).click(function(){
					$(li).ntv_playerGoTo(container);
				});
			});

			src = src + "&js_event_function=playerEvent&js=1";
		}

		if(options.modus == "live"){
			src = src + "&stopafter=1800&geoipcheck=1";
		}

		src = src + "&as=0";

		if(options.advertising == true && window.location.href.indexOf("ad=of")<0){
			src = src + "&admeta=http://www.n-tv.de/stat/videoplayer/admeta.xml";
			src = src + "&adcall=http://ad.de.doubleclick.net/adx/www.teleboerse.de/" + ivw_code + ";pack=;theme="+options.theme+";playerseite=ja;"+n_pbt+"playersize=" + options.width +  "x" + options.height + ";dcmt=text/xml;";
			src = src + "&tile=10";
			src = src + "&ord=ord=" + ip_random;
			src = src + "&adclasses=0";
			if(options.modus == "live"){
				src = src + "&adconfig=1,0,0";
			}else{
				src = src + "&adconfig=1,1,1";
			}
		}
		src = src + "&bgcolor=" + options.bgcolor;
		if(options.teaser_url){
			src = src + "&teaser_url=" + escape(options.teaser_url);
		}
		src = src + "&angebot=ntv&ivw=/"+ntv_sitename+"/"+ntv_section;

		return this.each(function(){
			$(this).flashembed(
					{
						src: src,
						width: options.width,
						height: options.height,
						w3c: options.w3c,
						bgcolor: options.bgcolor
					});
		});
	};




	$.fn.ntv_player = function(settings) {
		var options = $.extend(
				{
					"w3c": true,
					"bgcolor": "#09224E",
					"advertising": true,
					"modus": "vod",
					"theme": "video",
					"stationid": "http://www.n-tv.de/stat/videoplayer/ntv_stationid.f4v",
					"teaser_url": null
				}, settings
		);

		var container = $(this);

		ntv.videotag = options.theme;

		if(options.modus == "live" || options.modus == "eventfeed"){
			options.video = "rtmp://fms.n-tv.de" + options.video;
		}else{
			options.video = "rtmp://fms.n-tv.de/ntv" + options.video;
		}

		//var src = "http://www.n-tv.de/stat/videoplayer/ntv_player.swf?flv="+options.video+"&type="+options.modus;
		var src = "http://www.n-tv.de/stat/videoplayer/ntv_player.swf?flv="+options.video+"&type="+options.modus+(options.stationid?"&stationid="+options.stationid:"");

		if(options.modus == "live"){
			src = src + "&stopafter=1800&geoipcheck=1";
		}
		if( options.image ){
			src = src + "&as=0&startimage=" + options.image;
		}else{
			src = src + "&as=1";
		}
		if(options.advertising == true && window.location.href.indexOf("ad=of")<0){
			src = src + "&admeta=http://www.n-tv.de/stat/videoplayer/admeta.xml";
			src = src + "&adcall=http://ad.de.doubleclick.net/adx/www.teleboerse.de/" + ivw_code + ";pack=;theme="+options.theme+";playerseite=ja;"+n_pbt+"playersize=" + options.width +  "x" + options.height + ";dcmt=text/xml;";
			src = src + "&tile=10";
			src = src + "&ord=ord=" + ip_random;
			src = src + "&adclasses=0";
			if(options.modus == "live"){
				src = src + "&adconfig=1,0,0";
			}else{
				src = src + "&adconfig=1,1,1";
			}
		}
		src = src + "&bgcolor=" + options.bgcolor;
		if(options.teaser_url){
			src = src + "&teaser_url=" + escape(options.teaser_url);
		}
		src = src + "&angebot=ntv&ivw=/"+ntv_sitename+"/"+ntv_section;

/*
		ntv.videotag = options.theme;

		var src = relat_prefix + "flash/NTV_Player_AS3.swf?f="+options.video+"&pm="+options.modus;
		if(options.advertising == true){
			src = src + "&preroll=http://ad.de.doubleclick.net/adx/www.teleboerse.de/" + ivw_code + ";pack=;theme="+options.theme+";"+ip_additional+"playersize=" + options.width +  "x" + options.height + ";sz=320x240;tile=10;pos=pre;dcmt=text/xml;ord="+ip_random;
			src = src + "&postroll=http://ad.de.doubleclick.net/adx/www.teleboerse.de/" + ivw_code + ";pack=;theme="+options.theme+";"+ip_additional+"playersize=" + options.width +  "x" + options.height + ";sz=320x240;tile=10;pos=postroll;dcmt=text/xml;ord="+ip_random;
			//src = src + "&cuttin=http://ad.de.doubleclick.net/adx/www.teleboerse.de/" + ivw_code + ";pack=;theme="+options.theme+";"+ip_additional+"playersize=" + options.width +  "x" + options.height + ";sz=320x240;tile=10;pos=int;dcmt=text/xml;ord="+ip_random;
		}
		if( options.image ){
			src = src + "&ppic=" + options.image;
		}
*/

		return this.each(function(){
			$(this).flashembed(
					{
						src: src,
						width: options.width,
						height: options.height,
						w3c: options.w3c,
						bgcolor: options.bgcolor
					});
		});
	};

	$.fn.ntv_poll = function( url ) {
		return this.each(function(){
			var container = this;

			$( this ).find( "form" ).submit(function() {
				var choice = $( this ).find( "input:checked" );
				$( container ).load( url, choice);
				return false;
			});
		});
	};

	/*
	$.fn.ntv_poll = function() {

		return this.each( function() {
			var poll = this;
			var poll_container = $(this).find( ".poll_container" );
			var result_link = $(this).find( "a.result" );

			$( this ).find( "form" ).submit(function() {
				var choice = $( this ).find( "input:checked" );
				var url = this.action;

				$.ajax({
					type: "POST",
					url: url,
					data: choice,
					dataType: "xml",
					success: function( xml ) {
						var result = $( "<table cellspacing='0' />" );
						result.append( "<caption>"+ $( "question", xml ).text() +"</caption>" );

						$( "answer", xml ).each(function(index, answer) {

							result.append( "<tr><th>" + $(answer).text() + "</th></tr>" );
							var position = $(answer).attr("percent") > 85 ? "poll_left" : "poll_right";
							result.append(
								"<tr><td><div>" +
								"<p style='display: none; width:" + $(answer).attr("percent") + "%;'>" +
								"<span class='"+ position +"'>" + $(answer).attr("percent") + "%</span>" +
								"</p></div></td></tr>"
							);
						});

						$(poll_container).html( result );
						$(poll).css( "backgroundImage", "none" );
						$(result).find( "p" ).fadeIn();
						$(result_link).fadeOut();
					},
					error: function (XMLHttpRequest, textStatus, errorThrown) {
						alert( textStatus );
					}
				 });

				return false;
			});
		});
	};
	*/

	$.fn.ntv_search = function() {

		return this.each(
				function() {
					var activeID;
					var a = $(this).find( ".select a" ).each(
						function() {

							if( $(this).attr( "class" )=='active' ) {
								activeID = $(this);
							}

							$(this).click(
									function(){
										$(activeID).removeClass( 'active' );
										$(this).addClass( 'active' );
										activeID = $(this);

										//$( "#search_form #searchabout" ).attr( "value",$(this).attr( "id" ) );

										$( "#search_form" ).attr( { "action": "", "target": "_self" } );

										if ( $(this).attr("id")=="ntv" ){
											$( "#search_form input[name='at']" ).attr( "value","all" );
										}
										if ( $(this).attr("id")=="news" ){
											$( "#search_form input[name='at']" ).attr( "value","m" );
										}
										if ( $(this).attr("id")=="gallery" ){
											$( "#search_form input[name='at']" ).attr( "value","b" );
										}
										if ( $(this).attr("id")=="video" ){
											$( "#search_form input[name='at']" ).attr( "value","v" );
										}

										if ( $(this).attr("id")=="quote" ){
											//$( "#search_form" ).attr("action","http://kurse.teleboerse.de/DE/showpage.aspx");
											//$( "#search_form" ).attr("target","_blank");
											window.open("http://kursdaten.teleboerse.de/teleboerse/kurse_einzelkurs_suche.htn?seite=suche&suchbegriff="+$( "#search_form input[name='q']" ).attr( "value"), "tb");
											return false;

										}

										var issearchstring = false;
										var inp = $( "#search_form" ).find( "input" ).each(
												function() {
													if( $(this).attr( "type" ) == "text" && $(this).attr( "value" )){
														issearchstring = true;
													}
												}
										);
										if (issearchstring==true){
											$( "#search_form" ).submit();
										}
										return false;
									}
							);

						}
					);

					var isdetailsearch = false;
					var detailinp = $( "#search_form #detailsearch" ).find( "input" ).each(
							function() {
								if( $(this).attr( "type" ) == "text" && $(this).attr( "value" )){
									isdetailsearch = true;
								}
							}
					);
					if (isdetailsearch==true){
						$('#detailsearch').show();
						var a = $(this).find( "a.more_light" ).each(
								function() {
									$(this).html("Detailsuche schlie&szlig;en");
								}
						);
					}
				}
		);
	};



	$.fn.ntv_search_detail = function() {
		return this.each( function() {
			if ( $('#detailsearch').css("display") == "none" ){
				$('#detailsearch').show();
				$(this).html("Detailsuche schlie&szlig;en");
			} else {
				$('#detailsearch').hide();
				$(this).html("Detailsuche");
			}
		});
	};


	$.fn.ntv_search_head = function() {

		var enableSuggestSearch = function(){
			$("#search").suche({
				limit: 10,
				ajaxBaseUrl: "http://kursdaten.teleboerse.de/teleboerse/ajax/",
				resultDiv: "dropDownResults",
				resultWidth: 250
			});
		};


		enableSuggestSearch();

		$( this ).find( "input" ).each( function( ) {
			if( $(this).attr("id")=="news" || $(this).attr("id")=="shares" ){
				$( this ).change( function() {

					if($(this).attr("id")=="news"){
						$("#search").unbind();
						$("#search").val("");
					}else{
						enableSuggestSearch();
					}
				});
			}
		});

		return this.each(function () {
			$( this ).submit(function(){
				var id = $( "input:checked", this ).attr( "id" );
				var url = "http://kurse.teleboerse.de/DE/showpage.aspx";
				switch( id ) {
					case "news":
						url = "http://www.teleboerse.de/suche/?a=search&at=all&q=" + $( "input#search", this ).val();
						break;
					default:
						url = "http://kursdaten.teleboerse.de/teleboerse/kurse_einzelkurs_suche.htn?seite=suche&suchbegriff=" + $( "input#search", this ).val();
						break;
				}
				window.location.href = url;
				return false;
			});
		});
	};


	$.fn.ntv_slideshow = function( images, settings ) {
		var s = $.extend({
			/* defaults */
			buttonNextClass: 'next',
			buttonPrevClass: 'previous',
			fadeTime: 200,
			imgContainerClass: 'imgContainer',
			loadingAnimClass: 'loading',
			onClick: function(){},
			textContainerClass: 'textContainer',
			preloadImages: true
		}, settings );

		if( s.preloadImages ) {
			(function(index) {
				var tempImg = new Image();
				var callee = arguments.callee;
				tempImg.src = images[index];
				tempImg.onload = function() {
					images[index + 1] && callee(index + 1);
				};
			})(0);
		}

		return this;
	};


	$.fn.ntv_social_bookmarking = function() {
		return this.each( function() {
			var list = $( this ).find( "ul.jq_social_bookmarks_list" );
			var target = $( this ).find( "span.bm_service" );

			// Rollover
			$( list ).find( "a" ).each( function( index, li ) {
				$( li ).hover(
					function() {
						var text = $( this ).text();
						$( target ).text( text );
					},
					function() {
						$( target ).text( "" );
					}
				);
			});

			// neues Fenster bei click
			$( list ).find( "a" ).each( function() {
				$( this ).click( function() {
					window.open( $(this).attr( "href" ) );
					return false;
				});
			});

		});

	};

	$.fn.ntv_video_play = function() {
		return this.each( function() {
			try {
				var a = $(this).find( "a" ).get( 0 );
				var href = a.href;
				$( a ).append( "<span class='play'></span>" ).css( "cursor", "pointer" );
				/*
				$( this ).bind( "click", function( e ) {
					ntv.log( "open: " + href );
					return false;
				});
				*/
			} catch ( e ) {}
		});
	};

	$.fn.ntv_buttonVideo = function() {
		return this.each( function() {

			var image = $("img",this);
			var imageWidth = image.width();
			var imageHeight = image.height();

			var buttonSrc = "http://www.n-tv.de/stat/images/buttons/butt_play_v_l.png";
			var buttonHoverSrc = "http://www.n-tv.de/stat/images/buttons/butt_play_v_l_roll.png";
			var posLeft = Math.round((imageWidth - 88)/2) + "px";
			var posTop = Math.round((imageHeight - 50)/2) + "px";

			if (imageWidth<440){
				buttonSrc = "http://www.n-tv.de/stat/images/buttons/butt_play_v_m.png";
				buttonHoverSrc = "http://www.n-tv.de/stat/images/buttons/butt_play_v_m_roll.png";
				posLeft = Math.round((imageWidth - 58)/2) + "px";
				posTop = Math.round((imageHeight - 34)/2) + "px";
			}
			if (imageWidth<220){
				buttonSrc = "http://www.n-tv.de/stat/images/buttons/butt_play_v_s.png";
				buttonHoverSrc = "http://www.n-tv.de/stat/images/buttons/butt_play_v_s_roll.png";
				posLeft = Math.round((imageWidth - 30)/2) + "px";
				posTop = Math.round((imageHeight - 18)/2) + "px";
			}

			var $button = $('<img>');
			$button.attr("src",buttonSrc);
			$button.css({"position": "absolute","left": posLeft, "top": posTop});

			$(this).append($button);

			$(this).mouseover(function(){
				$button.attr("src",buttonHoverSrc);
			});
			$(this).mouseout(function(){
				$button.attr("src",buttonSrc);
			});
		});
	};

	$.fn.ntv_buttonGallery = function() {
		return this.each( function() {

			var image;

			if($(".images",this).length>0){
				image = $(".images",this);
			}else{
				image = $("img",this);
			}

			var imageWidth = image.width();
			var imageHeight = image.height();

			var buttonSrc = "http://www.n-tv.de/stat/images/buttons/butt_play_bs_l.png";
			var buttonHoverSrc = "http://www.n-tv.de/stat/images/buttons/butt_play_bs_l_roll.png";
			var posLeft = Math.round((imageWidth - 88)/2) + "px";
			var posTop = imageHeight - 70 + "px";

			if (imageWidth<440 || imageHeight<300){
				buttonSrc = "http://www.n-tv.de/stat/images/buttons/butt_play_bs_m.png";
				buttonHoverSrc = "http://www.n-tv.de/stat/images/buttons/butt_play_bs_m_roll.png";
				posLeft = Math.round((imageWidth - 58)/2) + "px";
				posTop = imageHeight - 50 + "px";
			}
			if (imageWidth<220){
				buttonSrc = "http://www.n-tv.de/stat/images/buttons/butt_play_bs_s.png";
				buttonHoverSrc = "http://www.n-tv.de/stat/images/buttons/butt_play_bs_s_roll.png";
				posLeft = Math.round((imageWidth - 30)/2) + "px";
				posTop = imageHeight - 25 + "px";
			}

			var $button = $('<img>');
			$button.attr("src",buttonSrc);
			$button.css({"position": "absolute","left": posLeft, "top": posTop});

			$(this).append($button);

			$(this).mouseover(function(){
				$button.attr("src",buttonHoverSrc);
			});
			$(this).mouseout(function(){
				$button.attr("src",buttonSrc);
			});
		});
	};



	$.fn.ntv_topflop = function() {

		$("#topflop_header").find( "li" ).each(
			function() {
				$(this).removeClass( "active" );
			}
		);
		$(this).parent("li").addClass( "active" );
		$("#topflop_dax").hide();
		$("#topflop_tecdax").hide();
		$("#topflop_mdax").hide();
		$("#topflop_dow").hide();
		$("#topflop_nasdaq").hide();

		switch( $(this).attr("class")) {
			case "dax":
				$("#topflop_dax").show();
				return false;
				break;
			case "tecdax":
				$("#topflop_tecdax").show();
				return false;
				break;
			case "mdax":
				$("#topflop_mdax").show();
				return false;
				break;
			case "dow":
				$("#topflop_dow").show();
				return false;
				break;
			case "nasdaq":
				$("#topflop_nasdaq").show();
				return false;
				break;
			default:
				return false;
				break;
		}
		return false;
	};


	$.fn.ntv_switch_chart = function() {


		$( this ).parent("li").parent("ul").find( "a" ).each(
			function() {
				$(this).removeClass("active");
			}
		);
		$( this ).addClass("active");
		$("#head_chart_img").attr({ src: $(this).attr("href")});



		if ($(this).parent("li").parent("ul").hasClass("navi")){

			$("#head_chart .time .day a").css('visibility','visible');
			$("#head_chart .time .week a").css('visibility','visible');

			switch( $(this).parent("li").attr("class")) {
				case "dax":
					$("#head_chart .link_index").attr({ href: "http://kursdaten.teleboerse.de/teleboerse/kurse_einzelkurs_uebersicht.htn?i=159096"});
					$("#head_chart .more_light").html("Alle Unternehmen im DAX");
					break;
				case "tecdax":
					$("#head_chart .link_index").attr({ href: "http://kursdaten.teleboerse.de/teleboerse/kurse_einzelkurs_uebersicht.htn?i=158375"});
					$("#head_chart .more_light").html("Alle Unternehmen im TecDAX");
					break;
				case "esx":
					$("#head_chart .link_index").attr({ href: "http://kursdaten.teleboerse.de/teleboerse/kurse_einzelkurs_uebersicht.htn?i=159194"});
					$("#head_chart .more_light").html("Alle Unternehmen im DJ Euro Stoxx 50");
					break;
				case "dow":
					$("#head_chart .link_index").attr({ href: "http://kursdaten.teleboerse.de/teleboerse/kurse_einzelkurs_uebersicht.htn?i=2303555"});
					$("#head_chart .more_light").html("Alle Unternehmen im Dow Jones Industrial");
					break;
				case "nas":
					$("#head_chart .link_index").attr({ href: "http://kursdaten.teleboerse.de/teleboerse/kurse_einzelkurs_uebersicht.htn?i=149002"});
					$("#head_chart .more_light").html("Alle Unternehmen im NASDAQ 100");
					break;
				case "sp":
					$("#head_chart .link_index").attr({ href: "http://kursdaten.teleboerse.de/teleboerse/kurse_einzelkurs_uebersicht.htn?i=2303839"});
					$("#head_chart .more_light").html("Alle Unternehmen im S&P 500 ");
					break;
				case "n225":
					$("#head_chart .link_index").attr({ href: "http://kursdaten.teleboerse.de/teleboerse/kurse_einzelkurs_uebersicht.htn?i=148429"});
					$("#head_chart .more_light").html("Alle Unternehmen im Nikkei 225 ");
					$("#head_chart .time .day a").css('visibility','hidden');
					$("#head_chart .time .week a").css('visibility','hidden');
					if( $("#head_chart .time .day a").hasClass("active") || $("#head_chart .time .week a").hasClass("active") ){
						$("#head_chart .time .month a").click();
					}
					break;
				default:
					break;
			}

			var mainlink = $(this).attr("href");
			$( "#head_chart .time" ).find( "a" ).each(
				function() {
					switch( $(this).parent("li").attr("class")) {
						case "day":
							$(this).attr({ href: mainlink.replace(/time=.+/, "time=0")});
							break;
						case "week":
							$(this).attr({ href: mainlink.replace(/time=.+/, "time=8")});
							break;
						case "month":
							$(this).attr({ href: mainlink.replace(/time=.+/, "time=100")});
							break;
						case "month6":
							$(this).attr({ href: mainlink.replace(/time=.+/, "time=600")});
							break;
						case "year":
							$(this).attr({ href: mainlink.replace(/time=.+/, "time=10000")});
							break;
						case "max":
							$(this).attr({ href: mainlink.replace(/time=.+/, "time=50000")});
						default:
							break;
					}
				}
			);
		}





		if ($(this).parent("li").parent("ul").hasClass("time")){
			var timeclass = $(this).parent("li").attr("class");
			$( "#head_chart .navi" ).find( "a" ).each(
					function() {
						switch( timeclass ) {

						case "day":
							$(this).attr({ href: $(this).attr("href").replace(/time=.+/, "time=0")});
							break;
						case "week":
							$(this).attr({ href: $(this).attr("href").replace(/time=.+/, "time=8")});
							break;
						case "month":
							$(this).attr({ href: $(this).attr("href").replace(/time=.+/, "time=100")});
							break;
						case "month6":
							$(this).attr({ href: $(this).attr("href").replace(/time=.+/, "time=600")});
							break;
						case "year":
							$(this).attr({ href: $(this).attr("href").replace(/time=.+/, "time=10000")});
							break;
						case "max":
							$(this).attr({ href: $(this).attr("href").replace(/time=.+/, "time=50000")});
						default:
							break;
						}
					}
				);
		}


		return false;

	};



	$.fn.ntv_tools = function() {

		$("#tools_header").find( "li" ).each(
			function() {
				$(this).removeClass( "active" );
			}
		);
		$(this).parent("li").addClass( "active" );

		$("#tools_bestselect").hide();
		$("#tools_osrechner").hide();
		$("#tools_matrix").hide();


		switch( $(this).attr("class")) {
			case "bestselect":
				$("#tools_bestselect").show();
				return false;
				break;
			case "osrechner":
				$("#tools_osrechner").show();
				return false;
				break;
			case "matrix":
				$("#tools_matrix").show();
				return false;
				break;
			default:
				return false;
				break;
		}
		return false;
	};


	// Mediathek

	$.fn.ntv_media_detail = function( options ) {
		var settings = $.extend({
			backgroundColor: "#383838",
			linkColor: "#FFFFFF"
		}, options);

		return this.each( function() {
			var $container = $( this );
			var $link = $( this ).find( "a" );
			var $detail = $( this ).find( ".detail" );
			var backgroundColor = $( this ).css( "backgroundColor" );
			var timeout = null;

			$link.hover(
					// over
					function() {

						timeout = window.setTimeout(function(){
							//$container.css( "backgroundColor", settings.backgroundColor );
							$detail.css( "backgroundColor", settings.backgroundColor );
							$detail.show();
						}, 200);
					},
					// out
					function() {
						window.clearTimeout( timeout );
						$detail.hide();
						//$container.css( "backgroundColor", backgroundColor );
						$detail.css( "backgroundColor", backgroundColor );
					}
			);
		});

	};


	$.fn.ntv_slideTeaser = function() {

		var slider = $(this);
		var duration = 500;
		var left = 0;
		var teaserCount = slider.find("ul").children("li").length - slider.find("ul li ul").children("li").length;
		var slideWidth = slider.find( "ul li" ).width() + parseInt( slider.find( "ul li" ).css( "padding-right") );
		var slideEnd = teaserCount * slideWidth - slider.width() - parseInt( slider.find( "ul li" ).css( "padding-right") );

		var height = 0;
		$( slider ).find( "li" ).each( function() {
			if ( $(this).height() > height ){
				height = $(this).height();
			}
		});
		slider.find( ".slide" ).css( "height", height );

		if ( teaserCount * slideWidth > 690 ){
			slider.find( ".forward a" ).css( "display", "block" );
		}

		slider.find( ".forward a" ).click(
			function(){

				slider.find( "ul" ).animate( { left: left - slideWidth }, duration );
				slider.find( ".backward a" ).css( "display", "block" );
				left = left - slideWidth;
				if (left <= -slideEnd ){
					slider.find( ".forward a" ).css( "display", "none" );
				}
				return false;
			}
		);

		slider.find( ".backward a" ).click(
			function(){
				slider.find( "ul" ).animate( { left: left + slideWidth }, duration );
				slider.find( ".forward a" ).css( "display", "block" );
				left = left + slideWidth;
				if (left >= 0 ){
					slider.find( ".backward a" ).css( "display", "none" );
				}
				return false;
			}
		);

		slider.find(".teaser_mod" ).ntv_modDetail();
	};



	$.fn.ntv_prevBox = function() {

		var newest_link = $(this).find( "a.newest" );
		var most_viewed_link = $(this).find( "a.most_viewed" );
		var newest_box = $(this).find( "div.newest" );
		var most_viewed_box = $(this).find( "div.most_viewed" );

		$(newest_link).click(
			function(){
				$( most_viewed_link ).parent().removeClass( "active" );
				$( newest_link ).parent().addClass( "active" );

				$(newest_box).show();
				$(most_viewed_box).hide();
				return false;
			}
		);
		$(most_viewed_link).click(
			function(){
				$( newest_link ).parent().removeClass( "active" );
				$( most_viewed_link ).parent().addClass( "active" );

				$(most_viewed_box).show();
				$(newest_box).hide();
				return false;
			}
		);

		$(this).find( ".ajax_container" ).ntv_scrollAjax();
	};

	$.fn.ntv_scrollAjax = function() {

		var container = $(this);
		var html_cache = new Object();

		var init = function(){

			var backward = $(container).find( ".backward a" );
			var forward = $(container).find( ".forward a" );

			$(backward).css( "display", "none" );
			$(forward).css( "display", "none" );

			if($(backward).attr("href")){
				preload($(backward));
			}
			if($(forward).attr("href")){
				preload($(forward));
			}

		}

		var preload = function(button){
			var url = $(button).attr("href");

			if(!html_cache[url]){
				$.get( url, function(html) {
					html_cache[url] = html;
					setClick(button);
				});
			}
			else{
				setClick(button);
			}
		}

		var setClick = function(button){

			$(button).click(
				function(){
					$(container).html(html_cache[$(this).attr("href")]);
					init();
					return false;
				}
			);
			$(button).css( "display", "block" );
		}

		init();

	};


	$.fn.ntv_modDetail = function() {

		var ref_html;

		$( this ).find( "> li" ).each( function() {

			$( this ).find( ".teaser img" ).click( function() {

				ref_html = $( this ).closest("li").find(".detail").html();

				$('#mod_detail').toggle( 1000, function() {
					$("#mod_detail .wrapper").html( ref_html );
				});
				$("#mod_detail").toggle(1000);
			});
		});
	};



	$.fn.ntv_bilder_slideshow = function( options ) {
		var settings = $.extend({
			backward: ".switch a.backward",
			forward: ".switch a.forward",
			images: null,
			imageContainer: "#wrapper_img",
			loadImageSrc: relat_prefix + "bilderserie/img/loader.gif",
			speed: "slow",
			startIndex: 0,
			textContainer: "#wrapper_txt p"
		}, options);

		var $backward = $( settings.backward );
		var $forward = $( settings.forward );
		var $imageContainer = $( settings.imageContainer );
		var imageContainerHeight = $imageContainer.height();
		var $textContainer = $( settings.textContainer );
		var $creditContainer;

		var $loadingImg = $( "<img />" ).load( function () {
			$( this ).css( "marginTop", (imageContainerHeight - this.height)/2 );
			$imageContainer.append( this );
		}).attr({
			alt: "Bild wird geladen",
			src: settings.loadImageSrc
		});


		var imgCount = settings.images.length;
		var index = 0;
		index = settings.startIndex>0 && settings.startIndex<=imgCount ? settings.startIndex : index;

		var switchImage = function( i, preload, getIVW ) {

			var $nextImg = $( "<img />" ).load(

					function() {
						var marginTop = imageContainerHeight - this.height - 27;
						$( this ).
							css({
								//cursor: "pointer",
								display: "none",
								marginTop: marginTop
							})
							.addClass( "slide_img" );
						$loadingImg.hide();
						$imageContainer.find( "img.slide_img" ).replaceWith( this );
						$( this ).fadeIn( settings.speed, function(){
						});
						$textContainer.html( $("#wrapper_captions p:nth-child(" + (index+1) + ")").html() );
						$creditContainer.html( settings.images[i].credit != "" ? "(Foto: " + settings.images[i].credit + ")" : "" );
						$creditContainer.css("padding-right", 340 - this.width/2);

						if( getIVW ){
							loadIVW();
						}
					}
			).attr( {
				src: settings.images[i].src,
				alt: "",
				title: settings.images[i].credit != "" ? "(Foto: " + settings.images[i].credit + ")" : ""
				}
			);

			if( preload && i < imgCount-1 ) {
				var $preloadImage = $( "<img />" );
				$preloadImage.attr( "src", settings.images[i+1].src );
			}

		};

		// get the first image and show it
		$imageContainer.append('<span class="credit"></span>');
		$creditContainer = $imageContainer.find( "span.credit" );
		switchImage( index, true, false );

		// register click events
		return this.each( function() {
			var that = this;
			$(".counter", that).html((index + 1) + " / " + imgCount);

			$forward.click(function() {
				index == imgCount-1 ? index = 0 : index++;
				switchImage( index, true, true );
				$(".counter", that).html((index + 1) + " / " + imgCount);
				return false;
			});

			$backward.click(function() {
				index == 0 ? index = imgCount-1 : index--;
				switchImage( index, false, true );
				$(".counter", that).html((index + 1) + " / " + imgCount);
				return false;
			});
		});
	};

	$.fn.ntv_slideshowInline = function() {

		/* ##### Container ####### */

		var $container = $( "<div id='slideshow_inline' />" );
		$container.css( "height", $( "body" ).height()+"px" );
		$( "body" ).prepend( $container );
		var html = '';
		html += '<div class="slideshow">';
		html += '<div class="clearfix">';
		html += '<a href="" class="backward" title="zur&uuml;ck"><span>voriges Bild</span></a>';
		html += '<a href="" class="forward" title="vorw&auml;rts"><span>n&auml;chstes Bild</span></a>';
		html += '</div>';
		html += '<img class="slide_img"/>';
		html += '<span class="credit">&copy; OL</span>';
		html += '<span class="exit">X SCHLIESSEN</span>';
		html += '</div>';
		$container.append( html );

		$container.find(".slideshow").click( function(){
			return false;
		});

		$container.find(".exit").click( function(){
			$container.fadeOut( "slow", function() {
				try {
					$( $container ).remove();
				} catch (err) {}
			});
		});

		$container.fadeIn( "slow" );
		$container.click( function() {
			$container.fadeOut( "slow", function() {
				try {
					$( $container ).remove();
				} catch (err) {}
			});
		});


		/* ##### Slideshow ####### */

		var imageJSON = $(this).attr("href");
		var images;
		var imgCount;

		var $backward = $( "#slideshow_inline a.backward" );
		var $forward = $( "#slideshow_inline a.forward" );
		var $imageContainer = $( "#slideshow_inline .slideshow" );
		var loadImageSrc = relat_prefix + "bilderserie/img/loader.gif";
		var speed = "slow";
		var $loadingImg = $( "<img />" ).load( function () {
			$imageContainer.append( this );
		}).attr({
			alt: "Bild wird geladen",
			src: loadImageSrc
		});

		var index = 0;

		var ivw_code_slideshow = "pa_swe_i";

		$.getJSON( imageJSON, function(data) {
			if( data.length>0 ){
				images = data;
				imgCount = images.length;
				// get the first image and show it
				switchImage( index, true, true );

				// register click events
				$forward.click(function() {
					index == imgCount-1 ? index = 0 : index++;
					switchImage( index, true, true );
					return false;
				});

				$backward.click(function() {
					index == 0 ? index = imgCount-1 : index--;
					switchImage( index, false, true );
					return false;
				});
	       }
		});

		var switchImage = function( i, preload, getIVW ) {

			var $nextImg = $( "<img />" ).load(

					function() {

						$( this ).css( "display", "none");
						$( this ).addClass( "slide_img" );
						$loadingImg.hide();

						$imageContainer.find( "img.slide_img" ).replaceWith( this );
						$imageContainer.css( {"width": $(this).width() + "px", "top": ($( self ).scrollTop()+50) + "px"});


						$( this ).fadeIn( speed, function(){
						});

						if( getIVW ){
							loadIVW();
						}
					}
			).attr( {
				src: images[i].src,
				alt: images[i].credit != "" ? "(Foto: " + images[i].credit + ")" : "",
				title: images[i].credit != "" ? "(Foto: " + images[i].credit + ")" : ""
				}
			);

			if( preload && i < imgCount-1 ) {
				var $preloadImage = $( "<img />" );
				$preloadImage.attr( "src", images[i+1].src );
			}

		};

		return false;
	};


	$.fn.ntv_slideshowSimple = function(options) {

		return this.each(function(){
			var settings = $.extend({
				images: null
			}, options);
			var index = 0;
			var $image = $(this);
			var timeout = null;

			var imagePreload = [];

			if(settings.images.length>1){
				$image.load(function(){
					if( settings.images.length > index+1 && !imagePreload[index+1]){
						imagePreload[index+1] = new Image();
						imagePreload[index+1].src = settings.images[index+1];
					}
					$image.fadeIn(500);
					window.clearTimeout(timeout);
					timeout = window.setTimeout(switchImage, 5000);
				});
			}

			var switchImage = function(){
				if( settings.images.length > index+1){
					index += 1;
				}else{
					index = 0;
				}

				$image
					.fadeOut(500,
						function(){
							$image.attr("src", settings.images[index]);
					});
			};

			$image.attr("src", settings.images[0]);
			imagePreload[0] = new Image();
			imagePreload[0].src = settings.images[0];
		});
	};

	$.fn.ntv_media_tab_teaser = function(options) {

		return this.each(function(){
			var tabList = $(options.tabList, this);
			var content = $(options.content, this);

			$("a", tabList).click(function(){
				$("li", tabList).removeClass("active");
				$(this).closest("li").addClass("active");

				var index = $("a", tabList).index(this);
				$(content).removeClass("active");
				$(content).eq(index).addClass("active");

				return false;
			});


		});

	};

	$.fn.ntv_kompaktnews_teaser = function() {

		return this.each( function() {
			var teaser = this;

			var $items = $( this ).find( "div.tabItem" );
			var $navitems = $( this ).find( "ul li" );
			var $wrapper = $( this ).find( "#teaser_kompaktnews .mediathek_teaser_body" );

			var index = 0;
			var count = $items.size();
			var timer = null;
			var interval = 3000;


			function slide() {

				window.clearTimeout( timer );
				timer = window.setTimeout( function() {

					$items.each( function() {
						$( this ).removeClass( "active" );
					});
					$navitems.each( function() {
						$( this ).removeClass( "active" );
					});
					index < count-1 ? index++ : index = 0;
					$($items[index]).addClass( "active" );
					$($navitems[index]).addClass( "active" );

					slide();
				}, interval );
			}

			if(count > 0) {
				timer = window.setTimeout( slide, interval);
			}

		});
	};


	$.fn.ntv_zertFilter = function(url){

		var container = $(this);
		var theme = "";

		var loadAjax = function(url){
			$("ul.teaser",container).html("Daten werden geladen");
			$.get( url, function(html) {
				$("ul.teaser",container).html(html);
			});
		};

		loadAjax(url);

		$(".select >li", container).each(function(){

			var li_main = $(this);
			if ($(">a",li_main).attr("href")==url){
				$(li_main).addClass("active");
				$("strong.theme",container).html($(">a",li_main).text());
			}

			$(">a",this).click(function(){
				theme = "";
				$(".select >li", container).removeClass("active");
				$(li_main).addClass("active");
				if($(this).attr("href")!=""){
					theme = $(this).text();
					url = $(this).attr("href");

					$("li", li_main).each(function(){
						if ($("a",this).attr("href")==url){
							$(li_main).addClass("active");
							theme = $("a",this).text();
						}
					});

					$("li", li_main).each(function(){
						var li_sub = $(this);
						$(li_sub).removeClass("active");

						if ($("a",this).attr("href")==url){
							$(li_sub).addClass("active");
						}
					});


					loadAjax(url);
					$("strong.theme",container).html(theme);
				}
				return false;
			});


			$("li", li_main).each(function(){

				var li_sub = $(this);
				$(li_sub).removeClass("active");

				if ($("a",this).attr("href")==url){
					$(li_main).addClass("active");
					$(li_sub).addClass("active");
					$("strong.theme",container).html($("a",this).text());
				}

				$("a",this).click(function(){
					$("li",li_main).removeClass("active");
					if($(this).attr("href")!=""){
						$("strong.theme",container).html($(this).text());
						$(li_sub).addClass("active");
						loadAjax($(this).attr("href"));
					}
					return false;
				});
			});
		});
	}

	$.fn.ntv_settingLiHeight = function() {

		var cols = 2;
		var row = [];
		var maxHeight = 0;

		$(this).find("li").each(function(index){

			row.push($(this));
			if($(this).height()>maxHeight)maxHeight = $(this).height();

			if(index%cols!=0){
				for (var i = 0; i < row.length; ++i){
					row[i].css("height", maxHeight + "px");
				}
				row = [];
				maxHeight = 0;
			}

		});
	}

	$( document ).ready( function() {

		$(".article_wide .center .teaser_list ul").ntv_settingLiHeight();

		// Hide toggle elements if user has javascript enabled.
		$( "#comment" ).hide();
		$( "#email_form" ).hide();
		$( "#bookmarking" ).hide();
		$( "#detailsearch" ).hide();
		$( ".jq_close" ).show();

		// suche head
		$( "#header_top form" ).ntv_search_head();

		// Make p clickable on index pages.
		$( ".jq_link" ).ntv_js_link();

		// Add rollover, etc to the social bookmark links
		$( ".jq_socialbookmarks" ).ntv_social_bookmarking();

		// add bookmarking code for ie and mozilla
		$( ".jq_bookmarking" ).ntv_bookmarking();

		// video play button
		$( ".jq_videoteaser" ).ntv_video_play();
		$( ".jq_buttonVideo" ).ntv_buttonVideo();
		$( ".jq_buttonGallery" ).ntv_buttonGallery();

		// fb_ticker_naviagtion
		$( "#jq_fb_ticker" ).ntv_fb_ticker_navigation();

		// Add rollover, etc to the fastsearch navi
		$( ".jq_search" ).ntv_search();

		// video_teaser
		//$( ".mediathek_teaser" ).ntv_media_teaser();

		// umfragen
		$( ".poll" ).ntv_poll();

		// weather
		$("#weather_head_ort").focus( function() {
			$("#weather_head_ort").val("");
			$("#weather_head_plz").val("-1");
			$("#weather_head_ort").unbind("focus");
		});

		//placement rectangle
		$("#medrect").ntv_medrect();

		//Reset HP News
		$("#hpnews_search").val("z.B. Berlin o. 10119");

		//add links to head_chart
		$("#head_chart .dax a").ntv_switch_chart();

	});
})(jQuery);

