function ClearRequest(){
	$( "#searchanimate" ).hide();
}

function ReplaceInSearchValue( string ){
	var replacer = '';
	for( i = 0; i < string.length; i++ ){
		var s = string.charAt( i );
		var c = string.charCodeAt( i );
		switch( c ){
		  case 196: replacer += 'Ae'; break; // Ä
		  case 228: replacer += 'ae'; break; // ä
		  case 214: replacer += 'Oe'; break; // Ö
		  case 246: replacer += 'oe'; break; // ö
		  case 220: replacer += 'Ue'; break; // Ü
		  case 252: replacer += 'ue'; break; // ü
		  case 223: replacer += 'ss'; break; // ß
		  default:  replacer += s; break;
		}
	}
	return replacer;
}

$( document ).ready(
	function(){

		var server = "/wetter/";
		if( window.location.host.indexOf( "10.208.100.34" ) != -1 ) server = "http://10.208.100.34:9090/wetter/";
		if( window.location.host.indexOf( "n-tv" ) != -1 || window.location.host.indexOf( "unitb" ) != -1 ) server = "/ext.jsp?s=wetter&f=";

		$( ".bookmark" ).css( "cursor", "pointer" );
		$( ".bookmark" ).click(
			function(){
				if( $.browser.msie || $.browser.mozilla ){
					if( actual.name == "deutschland" ){
						var bmurl = "http://www.n-tv.de/wetter/5-tage-wetter/?plz=" + $( ".jq_weather_plz" ).html() + "&ort=" + $( ".bookmark" ).attr( "title" );
						var bmtitle = "n-tv.de Wetter in " + $( ".jq_weather_city" ).html() + " ( PLZ " + $( ".jq_weather_plz" ).html() + " Bundesland " + $( ".jq_weather_bland" ).html() + " )";
					}
					if( actual.name == "welt" ){
						var bmurl = "http://www.n-tv.de/wetter/5-tage-reisewetter/?ortkey=" + welt.ortkey + "&ortname=" + $( ".bookmark" ).attr( "title" );
						var bmtitle = "n-tv.de Wetter in " + $( ".jq_weather_city" ).html() + " ( " + $( ".jq_weather_bland" ).html() + " )";
					}
					if( $.browser.msie ) window.external.AddFavorite( bmurl, bmtitle );
					if( $.browser.mozilla )window.sidebar.addPanel( bmtitle, bmurl, "" );
				}
				else $( ".bookmark" ).hide();
			}
		);

		// vhwelt, vhdtl
		if( $( "#searchanimate" ).length > 0 ){

			ClearRequest();
			var orttimer;
			var lastsearchvalue = "";

			var welt = new Object();
			welt.name = "welt";
			welt.searchurl = server + "ntvsearchwelt/search_{0}.xml";
			welt.key = "ortkey";
			welt.bland = "land";
			welt.bland_long = "land";
			welt.forecast = server + "ntvvorhersageweltweit/";
			welt.ortkey = "";


			var deutschland = new Object();
			deutschland.name = "deutschland";
			deutschland.searchurl = server + "ntvsearchdeutschland/search_{0}.xml";
			deutschland.key = "plz";
			deutschland.bland = "bl";
			deutschland.bland_long = "bl_long";
			deutschland.forecast = server + "ntvvorhersagedeutschland/";


			var bio = new Object();
			bio.name = "bio";
			bio.searchurl = server + "ntvsearchdeutschland/search_{0}.xml";
			bio.key = "plz";
			bio.bland = "bl";
			bio.forecast = server + "ntvpollendeutschland/";


			var actual = deutschland;
			if( $( ".category" ).html().indexOf( "Weltwetter" ) != - 1) actual = welt;
			if( $( ".category" ).html().indexOf( "Biowetter" ) != - 1 ) actual = bio;

			$("#searchanimate").ajaxStart(
				function() {
					$( this ).show();
				}
			);

			$("#searchanimate").ajaxStop(
				function() {
					window.setTimeout( "ClearRequest()", 1000 );
				}
			);

			$( "#weather_searchlocal" ).submit(
				function(){
					return false;
				}
			);

			$( "#weather_searchlocal_txt" ).keyup(
				function( e ){
					if ( $( "#weather_searchlocal_txt" ).val().length >= 3 ){
						window.clearTimeout( orttimer );
						orttimer = window.setTimeout(
							function(){
								if( $( "#weather_searchlocal_txt" ).val().length >= 3 && lastsearchvalue != $( "#weather_searchlocal_txt" ).val() ){
									lastsearchvalue = $( "#weather_searchlocal_txt" ).val();
									lastsearchvalue = ReplaceInSearchValue( lastsearchvalue );
									var url = actual.searchurl.replace( "{0}", lastsearchvalue );
									$.get( url,
										function( xml ){
											var options = "";
											var counter = 0;
											$( "item", xml ).each(
												function( item ){
													counter++;
													options += '<option value="' + $( this ).attr( actual.key ) + '" label="' + $( this ).attr( "city" ) + '">' + $( this ).attr( "city" ) + ' (' + $( this ).attr( actual.bland ) + ')</option>';
												}
											);
											if( counter > 0 ){
												$( "#weather_searchlocal_city" ).html( options );
												$( "#weather_searchlocal_city" ).change();
											}
										}
									);
								}
							}
						, 750 );
					}
				}
			);

			if( actual.name != "bio" ){
				$( "#weather_searchlocal_city" ).change(
					function(){
						var searchcity = ReplaceInSearchValue( $( "#weather_searchlocal_city :selected" ).attr( "label" ) );
						$.get( actual.forecast + $( "#weather_searchlocal_city" ).val() + "/" + searchcity + ".xml",
							function( xml ){
								welt.ortkey = $( xml ).find( 'prognose' ).attr( actual.key );
								$( ".jq_weather_plz" ).html( $( xml ).find( 'prognose' ).attr( actual.key ) );
								$( ".jq_weather_bland" ).html( $( xml ).find( 'prognose' ).attr( actual.bland_long ) );
								$( ".jq_weather_city" ).html( $( xml ).find( 'prognose' ).attr( 'city' ) );
								$( ".bookmark" ).attr( "title", $( xml ).find( 'prognose' ).attr( 'linkname' ) );
								//$( "#weather_searchlocal_txt" ).val( $( xml ).find( 'prognose' ).attr( 'city' ) );
								$( "day", xml ).each(
									function( i, day ){
										$( ".jq_weather_date_" + ( i + 1 ) ).html( $( day ).attr( "date" ) );
										$( ".jq_weather_weekday_" + ( i + 1 ) ).html( $( day ).attr( "weekday" ) );
										$( ".jq_weather_vsymbol_" + ( i + 1 ) ).attr( "src", $( day ).attr( "vsymbol" ) );
										$( ".jq_weather_vsymbol_" + ( i + 1 ) ).attr( "alt", $( day ).attr( "vsymboltext" ) );
										$( ".jq_weather_vsymbol_" + ( i + 1 ) ).attr( "title", $( day ).attr( "vsymboltext" ) );
										$( ".jq_weather_nsymbol_" + ( i + 1 ) ).attr( "src", $( day ).attr( "nsymbol" ) );
										$( ".jq_weather_nsymbol_" + ( i + 1 ) ).attr( "alt", $( day ).attr( "nsymboltext" ) );
										$( ".jq_weather_nsymbol_" + ( i + 1 ) ).attr( "title", $( day ).attr( "nsymboltext" ) );
										$( ".jq_weather_asymbol_" + ( i + 1 ) ).attr( "src", $( day ).attr( "asymbol" ) );
										$( ".jq_weather_asymbol_" + ( i + 1 ) ).attr( "alt", $( day ).attr( "asymboltext" ) );
										$( ".jq_weather_asymbol_" + ( i + 1 ) ).attr( "title", $( day ).attr( "asymboltext" ) );
										$( ".jq_weather_nnsymbol_" + ( i + 1 ) ).attr( "src", $( day ).attr( "nnsymbol" ) );
										$( ".jq_weather_nnsymbol_" + ( i + 1 ) ).attr( "alt", $( day ).attr( "nnsymboltext" ) );
										$( ".jq_weather_nnsymbol_" + ( i + 1 ) ).attr( "title", $( day ).attr( "nnsymboltext" ) );
										$( ".jq_weather_tmin_" + ( i + 1 ) ).html( $( day ).attr( "tmin" ) );
										$( ".jq_weather_tmax_" + ( i + 1 ) ).html( $( day ).attr( "tmax" ) );
										$( ".jq_weather_rain_" + ( i + 1 ) ).html( $( day ).attr( "rain" ) );
										$( ".jq_weather_windrsymbol_" + ( i + 1 ) ).attr( "src", $( day ).attr( "windsymbol" ) );
										$( ".jq_weather_winds_" + ( i + 1 ) ).html( $( day ).attr( "winds" ) );
										$( ".jq_weather_windr_" + ( i + 1 ) ).html( $( day ).attr( "windr" ) );
										$( ".jq_weather_sunhours_" + ( i + 1 ) ).html( $( day ).attr( "sunhours" ) );
										$( ".jq_weather_sunrise_" + ( i + 1 ) ).html( $( day ).attr( "sunrise" ) );
										$( ".jq_weather_sunset_" + ( i + 1 ) ).html( $( day ).attr( "sunset" ) );
										$( ".jq_weather_moonup_" + ( i + 1 ) ).html( $( day ).attr( "moonup" ) );
										$( ".jq_weather_moondown_" + ( i + 1 ) ).html( $( day ).attr( "moondown" ) );
									}
								);
							}
						);
					}
				);
			}
			else{
				$( "#weather_searchlocal_city" ).change(
					function(){
						var searchcity = ReplaceInSearchValue( $( "#weather_searchlocal_city :selected" ).attr( "label" ) );
						$.get( actual.forecast + $( "#weather_searchlocal_city" ).val() + "/" + searchcity + ".xml",
							function( xml ){
								$( ".jq_weather_plz" ).html( $( xml ).find( 'pollen' ).attr( "plz" ) );
								$( ".jq_weather_bland" ).html( $( xml ).find( 'pollen' ).attr( "bl_long" ) );
								$( ".jq_weather_city" ).html( $( xml ).find( 'pollen' ).attr( 'city' ) );
								//$( "#weather_searchlocal_txt" ).val( $( xml ).find( 'pollen' ).attr( 'city' ) );
								$( "polle", xml ).each(
									function( i, polle ){
										$( ".jq_pollen_name" + ( i + 1 ) ).html( $( polle ).attr( "name" ) );
										$( ".jq_pollen_item" + ( i + 1 ) ).attr( "src", $( polle ).attr( "symbolurl" ) );
										$( "day", polle ).each(
											function( j, day ){
												if( j == 0 ){
													$( ".jq_pollen_date_" + ( i + 1 ) ).html( $( day ).attr( "date" ) );
													$( ".jq_pollen_weekday_" + ( i + 1 ) ).html( $( day ).attr( "weekday" ) );
												}
												$( ".jq_pollen_item" + ( i + 1 ) + "_symbol" + ( j + 1 ) ).css( "backgroundPosition", "18px " + ( ( $( day ).attr( "grad" ) * -38 ) -32 ) + "px" );
											}
										);
									}
								);
							}
						, "xml" );
					}
				);
			}
		}


		// bio, index, vhdtl, vhwelt, vhpolle
		if( $( "#weather_searchlocal_txt" ).length > 0 ){
			$( "#weather_searchlocal_txt" ).focus(
				function(){
					$( "#weather_searchlocal_txt" ).val("");
					$( "#weather_searchlocal_txt" ).unbind( "focus" );
				}
			);
		}

		// deutschland, index, welt
		if( $( ".jq_weatherimagelink" ).length > 0 ){
			var MapCache = new Object();
			MapCache = function(){
				this.maps = new Object();
			}
			MapCache.prototype = {
				getmap : function( url, imaplink ){
					var name = url.substring( url.lastIndexOf( "/" ) + 1, url.lastIndexOf( "_" ) );
					if( this.maps[ name ] == null ){
						$.get( server + "map/" + name + ".xml",
							function( xml ){
								var basepath = $( xml ).find( 'map' ).attr( 'basepath' );
								var html = '<map name="' + name + '" id="' + name + '">';
								$( "item", xml ).each(
									function( item ){
										if( $(this).attr( "type" ) == 'local' ){
											var imgname = $( "#jq_weatherimage" ).attr( "src" );
											var href1 = imgname.substring( 0, imgname.lastIndexOf( "/" ) + 1 ) + $(this).attr( "href" ) + imgname.substring( imgname.lastIndexOf( "_" ), imgname.length  );
											html += '<area coords="' + $(this).attr( "coords" ) + '" title="Wetter f&uuml;r ' + $(this).attr( "title" ) + '" shape="' + $(this).attr( "shape" ) + '" href="' + href1 + '"/>\n';
										}
										else{
											if( $(this).attr( "plz" ) ) html += '<area coords="' + $(this).attr( "coords" ) + '" title="Detailwetter f&uuml;r ' + $(this).attr( "title" ) + '" shape="' + $(this).attr( "shape" ) + '" href="' + basepath + '?plz=' + $(this).attr( "plz" ) + '&ort=' + $(this).attr( "ort" ) + '"/>\n';
											else html += '<area coords="' + $(this).attr( "coords" ) + '" title="Detailwetter f&uuml;r ' + $(this).attr( "title" ) + '" shape="' + $(this).attr( "shape" ) + '" href="' + basepath + '?ortkey=' + $(this).attr( "ortkey" ) + '&ortname=' + $(this).attr( "ortname" ) + '"/>\n';
										}
									}
								);
								html += '</map>';
								mapcache.maps[ name ] = html;
								mapcache.setmap( name, imaplink, url );
							}
						);
					}
					else mapcache.setmap( name, imaplink, url );
				},
				rewrite_map_links : function( url ){
					var day = url.substring( url.lastIndexOf( "_" ) + 1, url.lastIndexOf( ".png" ) );
					$.each( this.maps,
						function( key, value ){
							value = value.replace( /_[1-5]/g, "_" + day );
							mapcache.maps[ key ] = value;
						}
					);
				},
				rewrite_links : function( url ){
					var map = url.substring( url.lastIndexOf( "/" ) + 1, url.lastIndexOf( "_" ) );
					$( ".jq_weatherimagelink" ).each(
						function( i ){
							var ourl = $( this ).attr( "href" );
							ourl = ourl.substring( 0, ourl.lastIndexOf( "/" ) + 1 ) + map + ourl.substring( ourl.lastIndexOf( "_" ), ourl.length );
							$( this ).attr( "href", ourl );
						}
					);
				},
				setmap : function( name, imaplink, url ){
					if( imaplink == false ) mapcache.rewrite_map_links( url );
					else mapcache.rewrite_links( url );
					$( "#jq_imagemap" ).html( mapcache.maps[ name ] );
					var html = document.getElementById( "jq_weatherimage" ).outerHTML; // IE
					if ( html ){
						html = html.replace(/useMap=[#a-zA-Z_]+/," ").replace(">"," useMap=" + "#"  + name +">");
						document.getElementById( "jq_weatherimage" ).outerHTML = html;
					}
					else{
						$( "#jq_weatherimage" ).removeAttr( "useMap" );
						$( "#jq_weatherimage" ).attr( "useMap", "#" + name );
					}
					var img = new Image();
					$( img ).load(
						function(){
							$( "#jq_weatherimage" ).attr( "src", url );
					    }
					).attr( "src", url );
					$( "#jq_imagemap map area[href^='http']" ).unbind();
					$( "#jq_imagemap map area[href^='http']" ).click(
						function(){
							mapcache.getmap( $( this ).attr( "href" ), true );
							return false;
						}
					);
				}
			}
			var mapcache = new MapCache();

			$( ".jq_weatherimagelink" ).each(
				function( i ){
					if( i == 0 ) mapcache.getmap( $( this ).attr( "href" ), false );
				}
			);

			$( ".jq_weatherimagelink" ).click(
				function(){
					$( ".jq_weatherimagelink" ).removeClass( "active" );
					$( this ).addClass( "active" );
					mapcache.getmap( $( this ).attr( "href" ), false );
					return false;
				}
			);
		}



	}

);