$(document).ready(function() {
	var no_submenu = false; // Flash-Einstiegs-Seite?
	if($('body').attr('id') == "flash-intro" || $('body').attr('id') == "trainer-forum") {
		no_submenu = true;
	}
	
	// N A V I -------
	
	$('a').attr('title', '');
	
	$('ul#navi li.level1').hoverIntent(
		function() {
			var context = this;
			$(context).stop();
			$(context).find('a.level1').toggleClass('hover');			
			if(no_submenu === false) {
				$(context).find('ul.sub').show();
			}
		},
		function() {
			var context = this;
			$(context).stop();
			$(context).find('a.level1').toggleClass('hover');
			if(!$(context).hasClass('current')) {
				if(no_submenu === false) {
					$(context).find('ul.sub').hide();
				}
			}
			$(context).find('ul.menu').hide(); // Seiten nicht in Navi-Fix 
		}
	);

	$('ul#navi li.level2').hoverIntent(
		function() {
			var context = this;
			$(context).stop();
			$(context).find('a.level2').toggleClass('hover');
			$(context).find('ul.menu').slideToggle('fast',function(){
				$(context).stop();
			});
		},
		function() {
			var context = this;
			$(context).stop();
			if(!$(context).hasClass('no-menu')) {
				$(context).find('ul.menu').slideToggle(100,function(){
					$(context).find('a.level2').toggleClass('hover');
				});
			} else {
				$(context).find('a.level2').toggleClass('hover');
			}

		}
	);
	// Ende: NAVI ------

	// Trainer-Forum Left-Menu
	if($(document).find('#left-menu').length > 0) {
		$('#left-menu .level-3 a').hover(
			function() {
				$(this).parents('li.level-2').find('a.bg-element').addClass('hover');
			},
			function() {
				$(this).parents('li.level-2').find('a.bg-element').removeClass('hover');
			}
		);
	}
	// Ende: Trainer-Forum Left-Menu

	/* FORMULARE */
	// hellere Schriftfarbe setzen
	//$('input[type=text]').add('[type=password]').each(function(i,e) {
	$('input[type=text],input[type=password],textarea.replace').each(function(i,e) {
		if($(e).val() == $(e).attr('title')) {
			$(e).addClass('given');
		}
	});

	// (Text entfernen bei focus...)
	$('.replace').live('focus',function(){
		if($(this).val() == $(this).attr("title")) {
			$(this).removeClass('given');
			if($(this).hasClass("password") && !isReplaced) {
				var elem_id = '#' + $(this).attr('id');
				$(elem_id).replaceWith('<input type="password"' + getElemAttributes($(elem_id),false) + '/>');
				var isReplaced = true;
				setTimeout(function(){
					$(elem_id).focus();
				}, 10);
			} else {
				$(this).val("");
			}
		}
	});

	$(".replace").live('blur', function(){
		if($(this).val() == '') {
			$(this).val($(this).attr("title"));
			if($(this).hasClass("password")) {
				var elem_id = '#' + $(this).attr('id');
				$(elem_id).replaceWith('<input type="text"' + getElemAttributes($(this),true) + '/>');
				$(elem_id).addClass('given');
			} else {
				$(this).addClass('given');
			}
			var isReplaced = false;
		}
	});

	$('input.password').each(function(i,e) {
		//$(this).replaceWith('<input class="text replace validate password bg-element" type="text" name="UserPsw" value="Passwort*" title="Passwort*" rel="required" />');
		if($(e).val() == $(e).attr('title')) {
			$(e).replaceWith('<input type="text"' + getElemAttributes(e,true) + '/>');
		}
	});

	function getElemAttributes(e,keep_value) {
		var e_id = 		(typeof $(e).attr("id") != "undefined") ? ' id="' + $(e).attr('id') + '"' : "";
		var e_class = 	(typeof $(e).attr("class") != "undefined") ? ' class="' + $(e).attr('class') + '"' : "";
		var e_rel = 	(typeof $(e).attr("rel") != "undefined") ? ' rel="' + $(e).attr('rel') + '"' : "";
		var e_name = 	(typeof $(e).attr("name") != "undefined") ? ' name="' + $(e).attr('name') + '"' : "";
		if (keep_value) {
			var e_value = (typeof $(e).attr("value") != "undefined") ? ' value="' + $(e).attr('value') + '"' : "";
		} else {
			var e_value = "";//
		}

		var e_title = 	(typeof $(e).attr("title") != "undefined") ? ' title="' + $(e).attr('title') + '"' : "";
		var given_attributes = e_id + e_class + e_rel + e_name + e_value + e_title;
		return given_attributes;
	}

	new function() {
		$.fn.validate = {
			init: function(o) {
				if($(o).attr("rel") == 'email') { this.email(o) }
				if($(o).attr("rel") == 'required') { this.required(o) }
				if($(o).attr("rel") == 'radio') { this.radio(o)}
			},
			email: function(o) {
				var email  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
				if (o.value.match(email)) {
					doSuccess(o);
				} else {
					doError(o);
				}
			},
			required: function(o) {
				var check_for;
				// LABEL VOR INPUT? -> auf LEER prüfen, andernfalls [title]
				if ($(o).parents('form').hasClass('form-with-labels')) {
					check_for = "";
				} else {
					check_for = $(o).attr('title');
				}
				
				if (o.value == check_for) {
					if($(o).parents('form').hasClass('form-with-labels')) {
						doError($(o).parents('form').find('label[for="'+$(o).attr('id')+'"]'));
					}
					if($(o).parents('.jqTransformSelectWrapper').length > 0) {
						doError($(o).parents('.jqTransformSelectWrapper'));
					}
					doError(o);
				} else {
					if($(o).parents('form').hasClass('form-with-labels')) {
						doSuccess($(o).parents('form').find('label[for="'+$(o).attr('id')+'"]'));
					}				
					if($(o).parents('.jqTransformSelectWrapper').length > 0) {
						doSuccess($(o).parents('.jqTransformSelectWrapper'));
					}
					doSuccess(o);
				}
			},
			radio: function(o) {
				var wrapper = $(o).parents('li');
				if($(wrapper).find('input[type="radio"]:checked').length == 0) {
					$(wrapper).addClass('error');
					doError(o);
					//doError(wrapper);
				} else {
					doSuccess(wrapper);
					//doSuccess(wrapper);
					doSuccess(o);
				}
			}
		};
		function doSuccess(o) {
			$(o).removeClass("error");
		}
		function doError(o) {
			$(o).addClass("error");
		}
	};

	$("form").live('submit',function() {
		var error = false;
		$(this).find(".validate").each(function(){
			$(this).validate.init(this);
			if($(this).hasClass("error")) {
				error = true;
			}
		});
		if(error) {
			alert(_js_alert);
			return false;
		} else {
		
			if($(this).attr('id') == "rating-form") {
				if(typeof $(this).attr('action') != "undefined" && $(this).attr('action') != "") {
					var url = $(this).attr('action');
					var form_data = $(this).serializeArray();
					addRating(url, form_data);
				}
				return false;
			}
			
		
			return true;
		}
	});

	// Formulare
	$('.hide-on-js').hide();

	$("form.jqtransform").jqTransform();


	/* Toggles */
	$('.toggle .title').live('click',function() {

		var toggle_link = $(this);
		var toggle_box = $(toggle_link).parents('.toggle');
		var toggle_content = $(toggle_box).find('.text');
		var toggle_container = $(toggle_box).parents('.toggles');
		
		if(!$(toggle_box).hasClass('open-toggle')) {
			$(toggle_container).find('.toggle').removeClass('open-toggle').find('.text').slideUp(300);
			$(toggle_content).slideToggle(300,function() {
				$(toggle_box).toggleClass('open-toggle');
				footer_fix_ie6();
			});
		} else {
			$(toggle_content).slideToggle(300,function() {
				$(toggle_box).toggleClass('open-toggle');
				footer_fix_ie6();
			});
		}

	});
	$('.toggles .text').hide(0,function(){
		footer_fix_ie6();
	});
	// Andere Elemente ausblenden, 1. oeffnen
	if($('.toggles').length > 0) {

		$('.toggles').each(function(i,e) {
			if($(e).hasClass('toggles-closed') === false) { // alle geschlossen bleiben?
				$(e).find('.toggle').eq(0).find('.title').click();
				footer_fix_ie6();
			}
		});

		/* Erstes Element öffnen
		$('.toggles .toggle').eq(0).find('.title').click();
		footer_fix_ie6();
		*/
	}
	
	/* BUNDESWEITE TRAININGS */
	if($('div#map-wrapper').length > 0) {
		$('#map-wrapper .hotspot').hide().attr('title','');
		window.setTimeout('',1000); // Warten bis Hintergründe geladen sind

		
		var intTime = 0;
		$('#map-wrapper .hotspot').each(function(i){
			var elm = this;
			window.setTimeout(function(){$(elm).fadeIn('100');}, intTime);
			intTime += 800;
		});

		$('#map-wrapper .hotspot').hoverIntent(
			function() {
				var context = this;
				var link_id = $(context).attr('id').split('map-hotspot-');
				var map_wrapper = $(context).parents('div#map-wrapper');
				$(context).addClass('open-hotspot');
				$(context).find('div#map-overlay-'+link_id[1]).css({marginLeft:$(context).width()+'px'}).show();
			},
			function () {
				var context = this;
				var link_id = $(context).attr('id').split('map-hotspot-');
				var map_wrapper = $(context).parents('div#map-wrapper');
				$(context).find('.overlay').hide().css({marginLeft:0});
				$(context).removeClass('open-hotspot');
			}
		)
	}

	function fadeHotspot(id) {
		$(id).animate({opacity:1},800,function () {
			return true;
		});
	}

	/* Ende: BUNDESWEITE TRAININGS */
	
	
	/* Bewertung abgeben */
	function addRating(url, form_data) {
		var container = $('#rating-overlay-content');
		
		// Bewertung abschicken
		$(container).load(url + '/1', form_data, function(response, status, xhr){
			
		});
		$(container).focus();
	}

	

});

// PHP-Equivalent in_array...
function in_array(value, arr) {
	var contains = false;
	for (var i = 0;i < arr.length;i++) {
		if (arr[i] == value) {
			contains = true;
			break;
		}
	}
	return contains;
}

// Aktivsetzen eines Links
function setActive(item) {
	$(item).parents().addClass('active');
}

// Jquery .load() mit angegebenem Callback --------------
function loadTemplate(container, url, callback_fn) {
	if(!url || !container) {
		return false;
	} else {
		url = _path + url;
		if(!callback_fn) {
			$(container).load(url);
		} else {
			$(container).load(url,callback_fn);
		}
	}
}

function scrollTo_container(scroll_box,offset) {
	if(!scroll_box) scroll_box = document;
	$(scroll_box).scrollTop(offset);
}

function footer_fix_ie6() {
	$('#footer').css({bottom:'0'});
}
