$(document).ready(function() {
    /**
     * Supersized - Background image full screen
     */
    $.supersized({
        slides :    [   //Background image
                        {image : 'http://www.stumpf-metall.de/upload/hintergrundbilder/bg_body_2.jpg'}
                    ]
    });
    // *******************

    /**
     * jQuery Accordion
     */
    $("#accordion_produkte" ).accordion();
    $("#accordion_aktuelles" ).accordion();
    // *******************

    /**
     * Nav Mega-DropDown
     */
    function megaHoverOver(){
        $(this).expose({closeSpeed: 'fast', color:"black", loadSpeed: 'normal', opacity: 0.5}).load();
        $(this).find(".sub").stop().fadeTo('fast', 1).show();

        //Calculate width of all ul's
        (function($) { 
            jQuery.fn.calcSubWidth = function() {
                rowWidth = 0;
                //Calculate row
                $(this).find("ul").each(function() {                    
                    rowWidth += $(this).width(); 
                });
            };
        })(jQuery); 

        if ( $(this).find(".row").length > 0 ) { //If row exists...
            var biggestRow = 0; 
            //Calculate each row
            $(this).find(".row").each(function() {                             
                rowWidth = $(this).width(); 
                //Find biggest row
                if(rowWidth > biggestRow) {
                    biggestRow = rowWidth;
                }
            });
            //Set width
            $(this).find(".sub").css({'width' :biggestRow});
            $(this).find(".row:last").css({'margin':'0'});

        } else { //If row does not exist...

            $(this).calcSubWidth();
            //Set Width
            $(this).find(".sub").css({'width' : rowWidth});

        }
        
        if($(window).width() <= 1010){
            $('li.partner .sub').css({'left' : '-236px'});
        }
    }

    function megaHoverOut(){
        $(this).find(".sub").stop().fadeTo('fast', 0, function() {
            $(this).hide();
            $.mask.close();
        });
    }


    var config = {    
        sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
        interval: 100, // number = milliseconds for onMouseOver polling interval    
        over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
        timeout: 300, // number = milliseconds delay before onMouseOut    
        out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
    };

    $("ul#main_nav li .sub").css({'opacity':'0'});
    $("ul#main_nav li").hoverIntent(config);
    // *******************

    /**
     * Button Werksverkauf change image
     */
    $("#button-werksverkauf img").hover(
        function () {
            var hoverPath = 'images/bu_werksverkauf_over.png';
            $(this).attr({src: hoverPath});
        }, 
        function () {
            var outPath = 'images/bu_werksverkauf.png';
            $(this).attr({src: outPath});
        }
    );
    // *******************

    /**
     * News panel on top
     */
    
    //Expand Panel
    $("#open").click(function(){
        $(this).expose({closeSpeed: 'fast', color:"black", loadSpeed: 'normal', opacity: 0.5}).load();
        $("div#panel").slideDown("slow");
        $('span.navlinks').css('z-index', '9998');

    });	

    // Collapse Panel
    $("#close").click(function(){
        $.mask.close();
        $("div#panel").slideUp("slow");
        $('.navlinks').css('z-index', '10000');
    });		

    // Switch buttons from "Log In | Register" to "Close Panel" on click
    $("#toggle a").click(function () {
        $("#toggle a").toggle();
    });
    // *******************
    
    /**
     * Autoclear input fields
     */
    $('input:text').autoclear();
    // *******************
    
    /**
     * Produktfinder expose
     */
    $('#produktfinder_wraper').click(function() {
        // perform exposing for the clicked element
        $(this).expose({closeSpeed: 'fast', color:"black", loadSpeed: 'normal', opacity: 0.5});
    });
    // *******************
    
    /**
     * Produktsuche mit autocomplete
     */
    $.widget("custom.catcomplete", $.ui.autocomplete, {
        _renderMenu: function( ul, items ) {
            var self = this,
                currentCategory = "";
            $.each( items, function( index, item ) {
                if ( item.category != currentCategory ) {
                    ul.append( "<li class='ui-autocomplete-category'>" + item.category + "</li>" );
                    currentCategory = item.category;
                }
                self._renderItem( ul, item );
            });
        }
    });

    $("#produktsuche").catcomplete({
        minLength: 3,
        delay: 0,
        source: "getSearchItems.php",
        select: function (event, ui) {
            $("#produkt-suche").append('<'+'input type="hidden" name="type" value="' + ui.item.category + '" />');
            $("#produkt-suche").submit()
        }
    });
		
});
