﻿jQuery.noConflict();
jQuery(document).ready(function($){
    
    //---- Random Header images ----
    var dorandom = 1;
    var images = new Array();
    images[images.length] = 'http://www.bingk.de/images/layout/head01.jpg';
    images[images.length] = 'http://www.bingk.de/images/layout/head02.jpg';
    if(dorandom==1) random = Math.floor(Math.random()*images.length);
    else random = 0;
    $('#Header').css('background', 'url(' + images[random] + ') no-repeat scroll 0pt 0pt transparent');


    //---- Navigation: ----
    // IE6/IE7 hack for z-index
    var zIndexNumber = 9990;
    $('div').each(function() {
        $(this).css('zIndex', zIndexNumber);
        zIndexNumber -= 10;
    });
    zIndexSubNavig = $("div.SubNavig").css('zIndex');
    //$("#overlay").css('zIndex', zIndexSubNavig-20);

    //On Hover Over     
    function megaHoverOverTopnavi(){ 
        var a = $(this).children("a");
        var className = a.attr('class');
        var classNavActive = className+'_NavActive';
        var position = $(this).position();        
        var marginLeft = position.left;        
        //$(this).find(".subLayer").css('margin-left', marginLeft+'px');                
        a.attr('class', classNavActive);
        $(this).find(".subLayer").stop().fadeTo('fast', 1.0).show(); //Find sub and fade it in
        
        (function($) {
            //Function to calculate total width of all ul's
            jQuery.fn.calcSubWidth = function() {
                rowWidth = 0;
            };
        })(jQuery);
        if ( $(this).find(".row").length > 0 ) { //If row exists
            var biggestRow = 0;
            if(jQuery.browser.msie && jQuery.browser.version == "6.0"){
                // $(this).find("ul.column:last").css('margin-right', 0);
            }
            $(this).find(".row").each(function() {    //for each row
                $(this).calcSubWidth(); //Call function to calculate width of all ul's
                //Find biggest row
                if(rowWidth > biggestRow) {
                    biggestRow = rowWidth;
                }
            });
            ulMarginRight = parseInt($(this).find("ul.column").css('margin-right'));
            ulCount = $(this).find("ul.column").size();
            ulOffset = (ulCount-1)*ulMarginRight;                        
        } else { //If row does not exist...
            $(this).calcSubWidth();  //Call function to calculate width of all ul's
            $(this).find(".sub").css({'width' : rowWidth}); //Set Width
        }
    }
    //On Hover Out
    function megaHoverOutTopnavi(){
        var a = $(this).children("a");
        var className = a.attr('class');        
        var classNavInactive = className.replace('_NavActive', '');        
        a.attr('class', classNavInactive);
        $(this).find(".subLayer").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
            $(this).hide();  //after fading, hide it            
            
        });
    }
    
    
    
    //Set custom configurations
    var configTopnavi = {
        sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
        interval: 80, // number = milliseconds for onMouseOver polling interval
        over: megaHoverOverTopnavi, // function = onMouseOver callback (REQUIRED)
        timeout: 200, // number = milliseconds delay before onMouseOut
        out: megaHoverOutTopnavi // function = onMouseOut callback (REQUIRED)
    };
    
    
    $("#topnav li .subLayer").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
    $("#topnav li").hoverIntent(configTopnavi); //Trigger Hover intent with custom configurations
    
});
