// jquery functions

(function($) {


	$.fn.ntv_navigation = function( settings ) {

		// clearing submit

		$("#wrapper_header #submit").attr({ value: ""});


		// 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_search_head = function() {

		var url="";

		$( "input, div", this ).each( function( ) {
			if( $(this).attr("id")=="news" || $(this).attr("id")=="shares" ){
				$( this ).change( function() {
					enableAutosuggest(document.getElementById("search"));
				});
			}
			if( $(this).attr("id")=="search" ){
				$( this ).keyup( function(e) {
					if (e.keyCode==13){
						var id = $( "input:checked", "#headsearch" ).attr( "id" );
						switch( id ) {
							case "news":
								url = "http://www.teleboerse.de/suche/?a=search&at=all&q=" + $( "input#search", "#headsearch" ).val();
								break;
							default:
								url = "http://kurse.teleboerse.de/DE/showpage.aspx?pageID=65&keyword=" + $( "input#search", "#headsearch" ).val();
								break;
						}

						window.location.href = url;
						return false;
					}
				});
			}
			if( $(this).attr("id")=="submit" ){
				$( this ).click( function() {

					var id = $( "input:checked", "#headsearch" ).attr( "id" );
					switch( id ) {
						case "news":
							url = "http://www.teleboerse.de/suche/?a=search&at=all&q=" + $( "input#search", "#headsearch" ).val();
							break;
						default:
							url = "http://kurse.teleboerse.de/DE/showpage.aspx?pageID=65&keyword=" + $( "input#search", "#headsearch" ).val();
							break;
					}

					window.location.href = url;
					return false;
				});
			}

		});
		return false;

	};


	$.fn.ntv_medrect = function() {
		if( $(this).height() >  30 && $("#medrect_ref").length>0 ){
			$("#medrect_ref").before('<DIV style="BORDER-TOP: #b0b0b0 2px dotted" class="tools_head f11 vm">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()});
		}
	}

})(jQuery);

