/**

 *  Verschiedenen Javascript Funktionen 

 *  @ Intabo GmbH 2008 - 2010

 *  

 */

/*****************************************************************/

$(function(){

	

	/**

	 *	Funktionalität der Kommentarfunktion

	 */	 	

	/** WAI-ARIA für das Kommentarformular **/

    $('#comments form input.required').attr('aria-required', 'true');

    $('#comments form textarea.required').attr('aria-required', 'true');

    $('#comments form div.error input.required').attr('aria-invalide', 'true');

    $('#comments form div.error').attr('aria-live', 'assertive').attr('role', 'alert');

    

    /** Formularvalidierung mit WAI-ARIA Unterstützung **/

    $('#comments form').submit(function(){

        var error = false;

        $('.required', $(this)).each(function() {

            var $this = $(this);

            var email = false;

            $this.parent('div').removeClass('error').removeAttr('role').removeAttr('aria-live');

            $this.removeAttr('aria-invalide');

            $this.siblings('label[class*="error"]').remove();

            if($this.attr('class').split(' ').slice(-1) == "email") {

                if(/^[\w-+\.]+@([\w-]+\.)+[\w-]{2,}$/i.test($this.val())) {

                    email = false;

                }

                else {

                    email = true;

                }

            } 

            if($this.val().length == 0 || email ) {

                error = true;

                $this.parent('div').addClass('error').attr('role', 'alert').attr('aria-live', 'assertive');

                if($this.siblings('label[class*="error"]').length == 0) {

                    var $label = $this.siblings('label'); 

                    var text = $label.text().split('*');

                    var forAttr = $this.attr('id');

                    if(email) {

                        $label.before('<label class="error" for="' + forAttr + '">Bitte füllen sie das Feld «' + text[0] + '» mit einer gültigen E-Mail Adresse aus.</label>');

                    }

                    else {

                        $label.before('<label class="error" for="' + forAttr + '">Bitte füllen sie das Feld «' + text[0] + '» aus.</label>');

                    }

                    $this.attr('aria-invalide', 'true');

                }

            }

        });

        if(error) {

            return false;

        }

        return true;

    });

    /***

     *	 Kommentar-Formular auf- und zuklappen mir WAI-ARIA Unterstützung

     *	 Entstanden mit Hilfe der Ideen/Beschreibungen von 

     *	 Marco Zehe (http://www.marcozehe.de/2010/02/10/easy-aria-tip-5-aria-expanded-and-aria-controls/)

     *	 und 

     *	 Jens Grochtdreis (http://grochtdreis.de/weblog/2010/03/11/anreicherung-meiner-webseite-mit-wai-aria/)	     

     *	 	 	  

     */ 

    $('#comments h3').each(function() {

		var theform = $(this);

		var siblingid = theform.next("form").attr('id');

		theform.attr({

	                'aria-expanded': 'false',

	                'aria-controls': siblingid,

	                'tabindex': 0

	            })

		theform.bind("click keypress", function(e) {

			/**

			 *   Tastatursteuerung ermöglichen

			 */

			var type = e.type ? e.type : "";

			var tastenCode = e.keyCode ? e.keyCode : "";

		              

		    // Function bei Klick oder "Enter" ausführen 

		    if(type == "click" || tastenCode == 13) {

				$(this).siblings("form").slideToggle('fast', function(){

					if(!(theform.attr('aria-expanded') == 'true')) {

						theform.attr('aria-expanded', 'true');

					} else {

						theform.attr('aria-expanded', 'false');

					}

				});

			}

		});

	});



	

	/**

	 *	Sonstige Funktionen

	 */	 	

   /*** Label in Inputfeldern auf Jugendseite anzeigen ***/  
   if(!$.browser.msie)
   	$('label').labelOver('over-apply');   

   

   /*** Info Button Jugendseite ***/

   if($.browser.msie && ($.browser.version.substr(0,3)=="6.0")) {        

    return false;

   }

   else {

    $('#shoutbox a').click(function() {

        $('#info').load('/seiten/jugend/info.cfm #col1_content .inhalt');

        $('#info').ajaxComplete(function(){

            $('#close').remove();

            $(this).prepend('<a id="close" style="float:right; margin-right: 5px;" href="##" title="Diese Box schliessen"><img src="/grafiken/jugend/close.gif" alt="schliessen" /></a>').show(300);

            $('#close').click(function(){

                $(this).parent().empty().hide(500);

            });

        });    

        return false;

    });

   }   

});



 


