/**
 * frontend.js
 *
 * @category  gymnasium köniz lerbermatt
 * @package   frontend
 * @author    nothing <info@nothing.ch>
 * @copyright 2008 nothing from outer space (http://www.nothing.ch)
 * @license   http://www.nothing.ch/licence nothing gpl licence
 * @version   SVN: $Id$
 */

/**
 * registers additional plugins for the frontend.
 *
 * @category  gymnasium köniz lerbermatt
 * @package   frontend
 * @author    pi <pi@nothing.ch>
 */
ray.onDOMReady( function() {
    ray.info( "frontend.js :: onDOMReady" );

    if(ray.checkFlash(8,0) && typeof(ray.plugins.flashtitle) != "undefined") {
        ray.plugins.flashtitle.init();
    }

    var base = "/frontend/js/plugins/";
    ray.runtime.addWidget({ name: "subpage", selector: ".widget.subpage", base: base, weight: 25 });
    var base = "/frontend/js/widgets/";
    ray.runtime.addWidget({ name: "contactform", selector: ".widget.contactform", base: base, weight: 25, dependencies: ["ray-http"] });


    // set autocomplete=off for all fields which require it.
    $( "input.noautocomplete" ).forEach( function( element ) {
        element.node.setAttribute( "autocomplete", "off" );
    });

    // search bar
    $( "#searchform" ).on( "submit", function( element, event ) {
        if( $( "#search" ).node.value.length < 2 ) {
            $( "#searchwarning" ).addClass( "active" );
            var text = ($( "#search" ).node.value == "") ? "Suchbegriff wird ben&ouml;tigt" : "Min. 2 Zeichen";
            $( "#searchwarning span" ).setContent( text );

            // hide warning on next keypress, and if the focus
            // leaves the input field
            $( "#search" ).on( "keyup", function( element ) {
                $( "#searchwarning" ).removeClass( "active" );
            });
            $( "#search" ).on( "blur", function() {
                $( "#searchwarning" ).removeClass( "active" );
            });

            event.stopEvent();
        } else {
            // show loading
            $( "#searchloading" ).show();
            $( "#searchbox" ).addClass( "disabled" );
        }
    });
});
