$(document).ready(function() {

	// Tooltip in NAVI
	$('ul#navi li').hover(
		function() {
			$(this).find('a:first').addClass('hover');
			//$(this).find('.tooltip').show();
		},
		function() {
			//$(this).find('.tooltip').hide();
			$(this).find('a:first').removeClass('hover');
		}
	);

	// FORMULAR-HANDLING
	$("form.jqtransform").jqTransform();

	$("form").live('submit',function() {
		/*
		if($(this).attr('id') == "enquiry-form") {
			return true;
		}		
		*/

		var error = false;
		$(this).find(".validate").each(function(){
			$(this).validate.init(this);
			if($(this).hasClass("error")) {
				error = true;
			}
		});
		if(error) {
			alert("Bitte prüfen und ggf. vervollständigen Sie die notwendigen Angaben.");
			return false;
		} else {
			return true;
		}
	});

	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) {
					doError(o);
				} else {
					doSuccess(o);
				}
			},
			radio: function(o) {
				var wrapper = $(o).parents('li');
				if($(wrapper).find('input[type="radio"]:checked').length == 0) {
					doError(o);
					doError(wrapper);
				} else {
					doSuccess(o);
					doSuccess(wrapper);
				}

			}			
		};
		function doSuccess(o) {
			$(o).removeClass("error");
			if($(o).parents('.jqTransformSelectWrapper').length == 1) {
				$(o).parents('.jqTransformSelectWrapper').removeClass("error");
			}
		}
		function doError(o) {
			$(o).addClass("error");
			if($(o).parents('.jqTransformSelectWrapper').length == 1) {
				$(o).parents('.jqTransformSelectWrapper').addClass("error");
			}
		}
	};
	
	// (Text entfernen bei foucs...)
	$('.replace').live('focus',function(){
		if($(this).val() == $(this).attr("title")) {
			$(this).val("");
		}
	});

	$('.replace').live('blur',function(){
		if($(this).val() == '') {
			$(this).val($(this).attr("title"));
		}
	});

	// numerische Felder
	$('.numeric').numeric(',');


	/* Toggles */
	if($('.toggles').length > 0) {

		// Prevent clicks on link within the toggle "button"
		$('.toggle .title a').live('click',function(event) {
			event.preventDefault();
			$(this).parent().click();
			return false;
		});


		$('.toggle .title').live('click',function() {
			$(this).stop();

			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
		$('.toggles').each(function(i,e) {
			if($(e).find('.open-toggle').length > 0) {

				$(e).find('.toggle:not(.open-toggle) .text').hide();
				//var toggle_index = $(e).find('.open-toggle').index();
				//$(e).find('.open-toggle').removeClass('open-toggle');
				//$(e).find('.toggle').eq(toggle_index).find('.title').click();
				footer_fix_ie6();
			} else {
				if($(e).hasClass('toggles-closed') === false) { // alle geschlossen bleiben?
					//$(e).find('.toggle').eq(0).find('.title').click();
					$(e).find('.toggle').eq(0).addClass('open-toggle');
					$(e).find('.toggle:gt(0) .text').hide();
					footer_fix_ie6();
				} else {
					$('.toggles .text').hide(0,function(){
						footer_fix_ie6();
					});
				}
			}  
		});

	}
	





	// Kreditanfrage
	if($('#enquiry-form').length > 0) {
		var form_id = $('#enquiry-form');

		/*
		
		// Felder sperren
		if($('#children').val() == 0) {
			$('#children-age').attr('disabled',true);
			$('#child-benefit').attr('disabled',true);
		}

		if($('#martial-status').val() != 2) {
			$('#job-partner').attr('disabled',true);
			$('#salary-partner').attr('disabled',true);
		}

		// Kinder
		$('#children').live('change', function() {
			var dependent_fields = $(form_id).find('#children-age, #child-benefit');
			
			if($(this).val() == 0) {
				$(dependent_fields).each(function(i,e) {
					$(e).attr('disabled',true).val('');
					if($(e).attr('id') == "children-age") {
						$(e).val($(e).attr('title'));	
					}
				});
			} else {
				$(dependent_fields).each(function(i,e){
					$(e).removeAttr('disabled');
				});
			}
		});

		// Familienstand
		$('#martial-status').live('change', function() {
			var dependent_fields = $(form_id).find('#job-partner, #salary-partner');

			if($(this).val() != 2) {
				$(dependent_fields).each(function(i,e) {
					$(e).attr('disabled',true).val('');
				});
			} else {
				$(dependent_fields).each(function(i,e) {
					$(e).removeAttr('disabled');
				});
			}
		});

		*/
	}

	// Formular in Teaser	
	if($('.small-credit-form').length > 0) {

		$('.small-credit-form').each(function(i,e) {
			var context = $(e);
			$(context).find('input[name="period"]').hide();
			var iniPeriod = 12;

			var period_slider = $(context).find('.period-slider').slider({
				range: "min",
				value: iniPeriod,
				min: 12,
				max: 20,
				step: 1,
				slide: function(event, ui) {
					$(context).find('input[name="period"]').val(ui.value);
					$(this).find('.ui-slider-handle').attr('title',ui.value);
				}
			});
		});
	}

	// Anfrage-Link Overlay
	$('.overlay-handle').live('mouseover mouseout',function(event) {
		if(event.type == "mouseover") {
			$(this).stop();			
			$(this).find('.overlay').show();
		} else {
			$(this).find('.overlay').hide();
		}
	});

});

function footer_fix_ie6() {
	if($.browser.msie) {
		$('#content-wrapper').css({height:'auto'});
		$('#footer').css({bottom:0,position:'absolute',display:'inline-block'});
	}
}
