//Flackern von CSS-Backgrounds im IE verhindern
try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}


$(document).ready( function() {
	
	$('HTML').addClass('js');
	
		//input type file in hübsch
	$("input[type=file]").filestyle({ 
		//image: "choose-file.gif",
		//imageheight : 22,
		//imagewidth : 500,
		//width : 500
	});
	
	//felder mit Error-Class werden nach erfolgtem OnChange wieder "demarkiert"
	$(".required:not(.cssfMultiple) > input:not(.button), .required:not(.cssfMultiple) label input:first-child, .required:not(.cssfMultiple) > select, .required:not(.cssfMultiple) > textarea", $(".cssForm")).blur(
		function( objEvent ){
			if(this.tagName == 'SELECT'){
				//console.debug($(this).val());			
				if($(this).val() != '') {
					$(this).parent().removeClass( "cssfError" );
					//$(this).parent().addClass( "cssfValid" );
				} else {
					//$(this).parent().removeClass( "cssfValid" );
					$(this).parent().addClass( "cssfError" );
				}
			} else if(this.type == 'checkbox' || this.type == 'radio'){
				$(this).parent().parent().removeClass( "cssfError" );
			} else if(this.id.indexOf('email') != -1){
				if(this.value != '' && isEmail(this.value)) {
					$(this).parent().removeClass( "cssfError" );
					$(this).parent().addClass( "cssfValid" );
				} else {
					$(this).parent().removeClass( "cssfValid" );
					$(this).parent().addClass( "cssfError" );
				}
			} else if(this.value != '') {
				$(this).parent().removeClass( "cssfError" );
				$(this).parent().addClass( "cssfValid" );
			} else {
				$(this).parent().removeClass( "cssfValid" );
				$(this).parent().addClass( "cssfError" );
			}
		}
	);
	//aktivierung des aktuellen Elements über label.active
	$("input:not([type=checkbox], [type=radio]), select, textarea", $(".cssForm")).focus(
		function( objEvent ){
			//console.debug(this);
			$(this).parent().addClass('active');
		}
	);
	//deAktivierung des aktuellen Elements---------------------------------------------------------------------
	$("input:not([type=checkbox], [type=radio]), select, textarea", $(".cssForm")).blur(
		function( objEvent ){
			$(this).parent().removeClass('active');
		}
	);
	//checkbox im selben label aktivieren
	/*$("label:has(input[type=radio]>input[type=text]) input[type=text]", $(".selectorGroup")).focus(
		function( objEvent ){
			$(this).siblings("input[type=text]").focus();
		}
	);*/
	//bubbling abstellen bei click auf input
	$("input:not([type=checkbox], [type=radio],  [type=file], [type=button], [type=submit], [type=reset]), select, textarea", $(".cssForm")).click(
		function( objEvent ){
			return false;
		}
	);

	//vergrößern einer aktiven Textarea---------------------------------------------------------------------
	$("textarea", $(".cssForm")).focus(
		function( objEvent ){
			$(this).animate({
				height: '+=80'
			}, 500);
		}
	);
	//verkleinern einer aktiven Textarea---------------------------------------------------------------------
	$("textarea", $(".cssForm")).blur(
		function( objEvent ){
			$(this).animate({
				height: '-=80'
			}, 500);
		}
	);
	//ein-/ausblenden bestimmter Formelemente---------------------------------------------------------------------
	$(".cssfAdditionalFields", $(".cssForm")).each(
		function( ){
			//$(this).children("label").hide();
			$(this).append('<a href="#" target="_self">Zus&auml;tzliche Felder anzeigen</a>');
			//return false;
		}
	);
	$(".cssfAdditionalFields a", $(".cssForm")).click(
		function( objEvent ){
			anchorText = ($(this).siblings("label").css('display') == 'none') ? 'Zus&auml;tzliche Felder ausblenden' : 'Zus&auml;tzliche Felder anzeigen';
			$(this).html(anchorText).toggleClass('cssfHide');
			//console.debug($(this).siblings("label").css('display'));
			$(this).siblings("label").animate({
				height: 'toggle'
			}, 500);
			$(this).siblings("div").find("label").animate({
				height: 'toggle'
			}, 500);
			return false;
		}
	);
	$("label:has(span.cssfFlyOutInfo)", $(".cssForm")).children("input, textarea, select").focus(
		function( objEvent ){
			$(this).parent().css('position', 'relative');
			$(this).siblings("span.cssfFlyOutInfo").delay(500).animate({
				opacity: 'toggle',
				left: $(this).width() + 15
			}, 500, function(){
				$(this).parent().css('position', 'relative'); //fallback bei klick auf das  bereits aktive label
			});
		}
	);
	$("label:has(span.cssfFlyOutInfo)", $(".cssForm")).children("input, textarea, select").blur(
		function( objEvent ){
			//console.debug(this);
			$(this).siblings("span.cssfFlyOutInfo").animate({
				opacity: 'toggle',
				left: '-=20'
			}, 500, function(){
				$(this).parent().css('position', 'static');
			});
			
		}
	);
	//Faltbare Fieldsets ----------------------------------------------------------------

	$('fieldset.cssfFoldable', $(".cssForm")).each(
		function(){
			$(this).wrapInner('<div class="cssfInnerFoldable" />');
			$(this).prepend($(this).children('.cssfInnerFoldable').children('legend'));
		}
	);

	var foldTextShow = '<a href="#" target="_self">Inhalte anzeigen</a>';
	var foldTextHide = '<a href="#" target="_self">Inhalte ausblenden</a>';
	$('fieldset.cssfFoldable', $(".cssForm")).each(
		function(){
			if($(this).hasClass('cssfOpen')){
				$(this).append('<div class="cssfFoldNote">' + foldTextHide + '</div>');
			} else {
				$(this).children('.cssfInnerFoldable').hide();
				$(this).append('<div class="cssfFoldNote">' + foldTextShow + '</div>');
			}
		}
	);
	
	$("fieldset.cssfFoldable .cssfFoldNote", $(".cssForm")).click(
		function( objEvent ){

			nodeText = ($(this).siblings(".cssfInnerFoldable").css('display') == 'none') ?  foldTextHide : foldTextShow;
			$(this).html(nodeText).parent().toggleClass('cssfOpen');
		
			$(this).siblings(".cssfInnerFoldable").animate({
				height: 'toggle'
			}, 500);
			return false;
		}
	);

	
	
	//Datepicker aktivieren für class =datePicker --------------------------------------------------------------------------------------
	Date.firstDayOfWeek = 1;
	Date.format = 'dd.mm.yyyy';
	if($('.datePicker').length > 0){
		$(function(){
			$('.datePicker').datePicker({clickInput:true, createButton:false})
		});
	}
	
	//Multiple: Mehrere felder in einem Label werden auf Breite verteilt
	$(".cssfMultiple", $(".cssForm")).each(
		function(){
			$(this).children("input, .exInput, select, .exSelect", $(".cssForm")).each(
	 		function(i){
				elsInWrapper = $(this).parent().children("input, .exInput, select, .exSelect").length;
				elWidth = Math.floor(($(this).parent().width() - ((elsInWrapper-1)*5))/elsInWrapper);
				//console.debug($(this).parent().width() + " " + i);
				$(this).css({"width":elWidth, "margin-right":"3px"});
				if(i+1 == elsInWrapper){
					$(this).css("margin-right", "0");
				}
				}
			);
		}
	);
			
}); 


function isEmail(adresse){
	Suche = /.+@..+\...+/;
	Ergebnis = Suche.test(adresse);
	if (Ergebnis == true){
		return true;
	}else{
		return false;
	}
}

/*
 * Style File - jQuery plugin for styling file input elements  Copyright (c) 2007-2009 Mika Tuupola
 * Licensed under the MIT license:   http://www.opensource.org/licenses/mit-license.php
 * Based on work by Shaun Inman, altered by thomic   http://www.shauninman.com/archive/2007/09/10/styling_file_inputs_with_css_and_the_dom
  */
(function($) {
    
    $.fn.filestyle = function(options) {
                
        /* TODO: This should not override CSS. */
        var settings = {
            width : this.parent().width()*0.68,
			imagewidth : this.parent().width()*0.3,
			imageheight : 22
        };
                
        if(options) {
            $.extend(settings, options);
        };
                        
        return this.each(function() {
            
            var self = this;
            var wrapper = $("<div>")
                            .css({
                                "width": settings.imagewidth + 20 + "px",
                                "height": settings.imageheight + "px",
                                //"background": "url(" + settings.image + ") 0 0 no-repeat",
                                "background-position": "right",
                                "display": "inline",
                                "position": "absolute",
                                "overflow": "hidden"
                            });
                            
            var filename = $('<input class="file" disabled="disabled" value="- keine Datei gew&auml;hlt -">')
                             .addClass($(self).attr("class"))
                             .css({
                                 "display": "inline",
                                 "width": settings.width + "px"
                             });

			 var btn = $('<input class="button secondary" type="button" value="Durchsuchen...">')
                             .css({
                                 "width": settings.imagewidth + "px",
								 "margin-right" : 0,
								 //"margin-top" : "-2px",
								 "=margin-top" : 0,
								 "height": "1.7em",
								 "float" : "right"
                             });
						

            $(self).before('<br />', btn, filename);
            $(self).wrap(wrapper);

            $(self).css({
                        "position": "relative",
                        "height": settings.imageheight + "px",
                        "width": settings.width + "px",
                        "display": "inline",
                        "cursor": "pointer",
                        "opacity": "0.0"
                    });

            if ($.browser.mozilla) {
                if (/Win/.test(navigator.platform)) {
                    $(self).css("margin-left", "-120px");                    
                } else {
                    $(self).css("margin-left", "-168px");                    
                };
            } else {
                $(self).css("margin-left", settings.imagewidth - settings.width + "px");                
            };

            $(self).bind("change", function() {
                filename.val($(self).val());
            });
      
        });
        

    };
    
})(jQuery);


