var ail = new AnemaIconList();
var movieData = {};

function preloadIcons(){
	var icons = ["images/linkpfeil_h.gif", "images/sn_top_h.png", "images/sn_back_h.png", "images/sn_back_c.png", "images/sn_next_h", "images/sn_next_c", "images/print_h.png", 'images/home_cold.png', 'images/home_hot.png'];
	for( var i=0; i<icons.length; i++ ){
		new Image().src = icons[i];
	}
}
Acu.registerEvent( window, "load", preloadIcons );

function initOnload(){
	ail.load();
	ail.activateSwapping();
	ail.preload();

	for( var p in movieData ){
		var obj = document.getElementById( p );
		if( obj ){
			var html = movieData[p];
			html = html.replace( /\-!\-/g, "--" );	// Notwendig, damit W3C-Validator nicht über -- meckert.
			html = Acu.phpUrlDecode( html );
			obj.innerHTML = html;
		}
	}

	// Das ÖGS-Sprachsymbol ist ein Sonderfall.
	// onmouseover Symbol muss der eingeschlossene Link heiß gesetzt werden.
	// onmouseout wieder kalt, jedoch nur, wenn ÖGS nicht aktiv ist.
	var sym = document.getElementById( "oegs" );
	var ln = document.getElementById( "oegs_link" );
	var h = function(){
		ln.className = "link_spr_h";
	};
	Acu.registerEvent( sym, "mouseover", h );
	Acu.registerEvent( ln, "focus", function(e){
		sym.swapper.heatUp();
		ln.className = "link_spr_h";
	});

	h = function(){
		// Ob wir heiß sind merken wir daran, dass das heiße Bild
		// des Symbols gleich dem kalten ist.
		if( sym.swapper.imgCold == sym.swapper.imgHot ){
			// Setze wieder heiß - just in case.
			ln.className = "link_spr_h";
		}else{
			ln.className = "link_spr_c";
		}
	};
	Acu.registerEvent( sym, "mouseout", h );
	Acu.registerEvent( ln, "blur", function(e){
		sym.swapper.coolDown();
		// Ob wir heiß sind merken wir daran, dass das heiße Bild
		// des Symbols gleich dem kalten ist.
		if( sym.swapper.imgCold == sym.swapper.imgHot ){
			// Setze wieder heiß - just in case.
			ln.className = "link_spr_h";
		}else{
			ln.className = "link_spr_c";
		}
	});

	fontSizeCtrl.stylesheet = new Stylesheet( 1 );	// 1: 0 sind die eigentlichen Styles, 1 die eingebetteten Anpassungen.
	fontSizeCtrl.plusBtn = document.getElementById( "sg_btn_p" );
	fontSizeCtrl.minusBtn = document.getElementById( "sg_btn_m" );
	if( fontSizeCtrl.minusBtn ){
		h = function(){
			if( fontSizeCtrl.current > fontSizeCtrl.min ){
				if( fontSizeCtrl.current >= fontSizeCtrl.max ){
					fontSizeCtrl.plusBtn.swapper.activate();	// Der sollte deaktiviert gewsen sein.
				}
				adjustFontSize( -1 );
			}else{
				fontSizeCtrl.minusBtn.swapper.deactivate();	// Kein Swapping mehr.
			}
		};
		Acu.registerEvent( fontSizeCtrl.minusBtn, "click", h );

		$( fontSizeCtrl.minusBtn.parentNode ).keypress( function(e){
			if( e.keyCode == 13 ){	// enter
				if( fontSizeCtrl.current > fontSizeCtrl.min ){
					if( fontSizeCtrl.current >= fontSizeCtrl.max ){
						fontSizeCtrl.plusBtn.swapper.activate();	// Der sollte deaktiviert gewsen sein.
					}
					adjustFontSize( -1 );
				}else{
					fontSizeCtrl.minusBtn.swapper.deactivate();	// Kein Swapping mehr.
				}
			}
		});

		if( fontSizeCtrl.current <= fontSizeCtrl.min ){
			fontSizeCtrl.minusBtn.swapper.deactivate();
		}
	}

	if( fontSizeCtrl.plusBtn ){
		h = function( e ){
			if( fontSizeCtrl.current < fontSizeCtrl.max ){
				if( fontSizeCtrl.current <= fontSizeCtrl.min ){
					fontSizeCtrl.minusBtn.swapper.activate();	// Der sollte deaktiviert gewsen sein.
				}
				adjustFontSize( 1 );
			}else{
				fontSizeCtrl.plusBtn.swapper.deactivate();	// Kein Swapping mehr.
			}
		};
		Acu.registerEvent( fontSizeCtrl.plusBtn, "click", h );

		$( fontSizeCtrl.plusBtn.parentNode ).keypress( function(e){
			if( e.keyCode == 13 ){	// enter
				if( fontSizeCtrl.current < fontSizeCtrl.max ){
					if( fontSizeCtrl.current <= fontSizeCtrl.min ){
						fontSizeCtrl.minusBtn.swapper.activate();	// Der sollte deaktiviert gewsen sein.
					}
					adjustFontSize( 1 );
				}else{
					fontSizeCtrl.plusBtn.swapper.deactivate();	// Kein Swapping mehr.
				}
			}
		});

		if( fontSizeCtrl.current >= fontSizeCtrl.max ){
			fontSizeCtrl.plusBtn.swapper.deactivate();
		}
	}

	var fontLabel = document.getElementById( "sg_txt" );
	var fontCtrlContainer = document.getElementById( "font_ctrl_container" );
	if( fontLabel ){
		h = function( e ){
			if (!e) var e = window.event;
			if( e.preventDefault ) e.preventDefault();
			if( e.stopPropagation ) e.stopPropagation();
			if( e.returnValue ) e.returnValue = false;
			if( e.cancelBubble ) e.cancelBubble = true;
			//alert( "trying to prevent default" );
			return false;
		};
		Acu.registerEvent( fontLabel, "click", h );
		Acu.registerEvent( fontLabel, "dblclick", h );
		Acu.registerEvent( fontCtrlContainer, "click", h );
		Acu.registerEvent( fontCtrlContainer, "dblclick", h );

		/*h = function( e ){
			if (!e) var e = window.event;
			var target = (e.target) ? e.target : e.srcElement;

			if( target == fontLabel ){
				if( e.preventDefault ) e.preventDefault();
				if( e.stopPropagation ) e.stopPropagation();
				if( e.returnValue ) e.returnValue = false;
				if( e.cancelBubble ) e.cancelBubble = true;
				//alert( "trying to prevent default" );
				return false;
			}
		};
		Acu.registerEvent( window, "click", h );
		Acu.registerEvent( window, "dblclick", h );*/
	}
}

Acu.registerEvent( window, "load", initOnload );


function incrementFontSize(){
alert("incrementFontSize");
	if( fontSizeCtrl.current < fontSizeCtrl.max ){
		if( fontSizeCtrl.current <= fontSizeCtrl.min ){
			fontSizeCtrl.minusBtn.swapper.activate();	// Der sollte deaktiviert gewsen sein.
		}
		adjustFontSize( 1 );
	}else{
		fontSizeCtrl.plusBtn.swapper.deactivate();	// Kein Swapping mehr.
	}
}
function decrementFontSize(){
alert("decrementFontSize");
	if( fontSizeCtrl.current > fontSizeCtrl.min ){
		if( fontSizeCtrl.current >= fontSizeCtrl.max ){
			fontSizeCtrl.plusBtn.swapper.activate();	// Der sollte deaktiviert gewsen sein.
		}
		adjustFontSize( -1 );
	}else{
		fontSizeCtrl.minusBtn.swapper.deactivate();	// Kein Swapping mehr.
	}
}


var fontSizeCtrl = {
	min : 0,
	max : 0,
	current : 0,
	plusBtn : null,
	minusBtn : null,
	stylesheet : null,
	adjustableRules : []	// Array von Objekten mit den Properties selector und base.
};
function adjustFontSize( delta ){
	var newValue = fontSizeCtrl.current + delta;
	if( newValue >= fontSizeCtrl.min && newValue <= fontSizeCtrl.max ){
		// Passe Style-Klassen an.
		for( var i=0; i<fontSizeCtrl.adjustableRules.length; i++ ){
			var rule = fontSizeCtrl.adjustableRules[i];
			if( rule && rule.selector && rule.base ){
				var styleObj = fontSizeCtrl.stylesheet.getStyles( rule.selector );
				if( styleObj ){
					styleObj.fontSize = (rule.base + newValue ) + "px";
				}
			}
		}

		// Schicke neuen Wert an den Server.
		var request = Acu.getHttpRequest();
		request.open( "POST", "adjust_font_size.php", false );
		request.setRequestHeader( "Content-type","application/x-www-form-urlencoded" );
		request.setRequestHeader( "Connection","close" );
		var params = 'size=' + ((delta<0)?"down":"up");
		request.send( params );

		// Deaktiviere bzw. aktiviere Swapping.
		if( newValue <= fontSizeCtrl.min ){
			fontSizeCtrl.minusBtn.swapper.deactivate();
			newValue = fontSizeCtrl.min;
		}else{
			fontSizeCtrl.minusBtn.swapper.activate();
		}
		if( newValue >= fontSizeCtrl.max ){
			fontSizeCtrl.plusBtn.swapper.deactivate();
			newValue = fontSizeCtrl.max;
		}else{
			fontSizeCtrl.plusBtn.swapper.activate();
		}

		fontSizeCtrl.current = newValue;
	}
}

var glossPopCfg = {
	width : 400,
	height : 300,
	url : ""
};

function displayGlossaryEntry( id ){
	var pop = window.open( glossPopCfg.url + '?glo=' + id, 'pop', 'width=' + glossPopCfg.width + ', height=' + glossPopCfg.height + ',scrollbars=yes' );
}


function clearInput( obj ){
	if( !obj.cleared ){
		obj.value = "";
		obj.cleared = true;
	}
}




var courseData = [];
function updateCourseDetails(){
	var select = document.getElementById( 'crs' );
	var index = select.selectedIndex;
	if( index > 0 ){
		var data = courseData[index-1];
		document.getElementById( 'lbl_crs_number' ).innerHTML = data.number;
		document.getElementById( 'lbl_crs_fee' ).innerHTML = data.fee;
		document.getElementById( 'lbl_crs_duration' ).innerHTML = data.duration;
		document.getElementById( 'course_details' ).style.display = "";
	}else{
		document.getElementById( 'course_details' ).style.display = "none";
	}
}


function subscribeToNewsletter(){
	var name = document.getElementById( "nlrc_name" ).value;
	var email = document.getElementById( "nlrc_email" ).value;
	var params = "?sp=subscribe";
	params += "&name=" + Acu.phpUrlEncode( name );
	params += "&email=" + Acu.phpUrlEncode( email );
	location.href = "index.php" + params;
	return false;
}

function startSearch(){
	document.getElementById( "sp" ).value = "search";	// sp: SPecial site param
//	alert("sp: " + document.getElementById( "sp" ).value);
	document.form.submit();
}

function flashwarn_close() {
	document.getElementById('flashwarn').style.display = "none";
}



$( function(){
	$( "#funky" ).mouseover( function(e){
		this.src = "images/lupe_h.png";
	});
	$( "#funky" ).mouseout( function(e){
		this.src = "images/lupe_c.png";
	});
	$( "#funky" ).focus( function(e){
		this.src = "images/lupe_h.png";
	});
	$( "#funky" ).blur( function(e){
		this.src = "images/lupe_c.png";
	});

	$( "#nl_subscribe,#subscribe_to_course" ).mouseover(function(e){
		this.style.color = "#6b975a";
	});
	$( "#nl_subscribe,#subscribe_to_course" ).mouseout(function(e){
		this.style.color = "#8c8c8c";
	});
	$( "#nl_subscribe,#subscribe_to_course" ).focus(function(e){
		this.style.color = "#6b975a";
	});
	$( "#nl_subscribe,#subscribe_to_course" ).blur(function(e){
		this.style.color = "#8c8c8c";
	});
	$( "#nl_subscribe,#subscribe_to_course" ).click(function(e){
		document.form.submit();
	});
	//8c8c8c
/*	$( "#nl_subscribe" ).click(function(e){
		var name = document.getElementById( "nlrc_name" ).value;
		var email = document.getElementById( "nlrc_email" ).value;
		var params = "?sp=subscribe";
		params += "&name=" + Acu.phpUrlEncode( name );
		params += "&email=" + Acu.phpUrlEncode( email );
		location.href = "index.php" + params;
		return false;
	});*/
	$( "#nlrc_email,#nlrc_name" ).keyup(function(e){
		if( $( "#nlrc_email" ).val() == "" && $( "#nlrc_name" ).val() == "" ){
			$( "#sp" ).val( "" );
		}else{
			$( "#sp" ).val( "subscribe" );
		}
	});
	$( "#search_term" ).keyup(function(e){
		if( this.value == "" ){
			$( "#sp" ).val( "" );
		}else{
			$( "#sp" ).val( "search" );
		}
	});

	$( ".link_size" ).focus( function(e){
		this.firstChild.swapper.heatUp();
	});
	$( ".link_size" ).blur( function(e){
		this.firstChild.swapper.coolDown();
	});


	var o = null;
	for( var i=0; i<ail.list.length; i++ ){
		icon = ail.list[i];
		o = document.getElementById( icon.id );
		if( o ){
			o.cold = icon.imgCold;
			o.hot = icon.imgHot;
			$( o ).focus( function(e){
				//alert( "ID: " + self.parentNode.id );
				this.style.backgroundImage = "url(" + this.hot + ")";
			});
			$( o ).blur( function(e){
				this.style.backgroundImage = "url(" + this.cold + ")";
			});
		}
	}


	$( ".link_spr_c" ).focus( function(e){
		$( this ).css( {
			color : "#6b975a"
		});
	});
	$( ".link_spr_c" ).blur( function(e){
		$( this ).css( {
			color : "#a7a6a6"
		});
	});

	$( ".skip_link" ).focus( function(e){
		$( this ).css( {
			display: "block",
			width: "915px",
			margin: "5px auto 10px auto",
			position: "static"
		});
	});
	$( ".skip_link" ).blur( function(e){
		$( this ).css( {
			display: "inline",
			width: "auto",
			margin: "0px",
			position: "absolute"
		});
	});

	$( ".link_brot" ).focus( function(e){
		$( this ).css( {
			color : "#6b975a"
		});
	});
	$( ".link_brot" ).blur( function(e){
		$( this ).css( {
			color : "#717070"
		});
	});

	$( ".link_nl" ).focus( function(e){
		$( this ).css( {
			color : "#6b975a"
		});
	});
	$( ".link_nl" ).blur( function(e){
		$( this ).css( {
			color : "#8c8c8c"
		});
	});

	$( ".link_start" ).focus( function(e){
		$( this ).css( {
			color : "#6b975a",
			backgroundImage: "url(./images/linkpfeil_h.gif)"
		});
	});
	$( ".link_start" ).blur( function(e){
		$( this ).css( {
			color : "#70716f",
			backgroundImage: "url(./images/linkpfeil_c.gif)"
		});
	});

	$( ".link_top" ).focus( function(e){
		$( this ).css( {
			color : "#6b975a",
			backgroundImage: "url(./images/sn_top_h.png)"
		});
	});
	$( ".link_top" ).blur( function(e){
		$( this ).css( {
			color : "#8c8c8c",
			backgroundImage: "url(./images/sn_top_c.png)"
		});
	});

	$( ".link_back" ).focus( function(e){
		$( this ).css( {
			color : "#6b975a",
			backgroundImage: "url(./images/sn_back_h.png)"
		});
	});
	$( ".link_back" ).blur( function(e){
		$( this ).css( {
			color : "#8c8c8c",
			backgroundImage: "url(./images/sn_back_c.png)"
		});
	});

	$( ".link_next" ).focus( function(e){
		$( this ).css( {
			color : "#6b975a",
			backgroundImage: "url(./images/sn_next_h.png)"
		});
	});
	$( ".link_next" ).blur( function(e){
		$( this ).css( {
			color : "#8c8c8c",
			backgroundImage: "url(./images/sn_next_c.png)"
		});
	});

	$( ".link_print" ).focus( function(e){
		// Nach einem Click sollten wir nicht wieder heiß werden.
		if( !this.clicked ){
			$( this ).css( {
				color : "#6b975a",
				backgroundImage: "url(./images/print_h.png)"
			});
		}
	});
	$( ".link_print" ).blur( function(e){
		$( this ).css( {
			color : "#8c8c8c",
			backgroundImage: "url(./images/print.png)"
		});
	});
	$( ".link_print" ).click( function(e){
		$( this ).css( {
			color : "#8c8c8c",
			backgroundImage: "url(./images/print.png)"
		});
		this.clicked = true;
	});
	$( ".link_print" ).mouseout( function(e){
		$( this ).css( {
			color : "#8c8c8c",
			backgroundImage: "url(./images/print.png)"
		});
	});
	$( ".link_print" ).mouseover( function(e){
		$( this ).css( {
			color : "#6b975a",
			backgroundImage: "url(./images/print_h.png)"
		});
		this.clicked = false;
	});

	$( ".link_titel_o,.link_titel_m,.link_test" ).focus( function(e){
		$( this ).css( {
			color : "#6b975a"
		});
	});
	$( ".link_titel_o" ).blur( function(e){
		$( this ).css( {
			color : "#818080"
		});
	});

	/*
	$( ".link_titel_m" ).focus( function(e){
		$( this ).css( {
			color : "#6b975a"
		});
	});
	$( ".link_titel_m" ).blur( function(e){
		$( this ).css( {
			color : "#818080"
		});
	});
	*/


	/*//Acu.detectEnterKeyPressed
	$( "#search_term" ).keypress( function(e){
		if( Acu.detectEnterKeyPressed( e ) ){ startSearch(); }
	});*/
	$( "#funky" ).click( function(e){ startSearch(); } );


	if( document.form ){
		document.form.onsubmit = function(e){
			$( "#subscribe_to_course" ).hide();
			$( "#subscribe_to_course_wait" ).show();
		};
	}

});


function printPage( param, value ){
	var pop = window.open( "index.php?mode=print&" + param + "=" + value, "printView", "height=549,width=751,resizable=yes,scrollbars=yes,menubar=yes" );
}
