$jq = jQuery.noConflict();

$jq(document).ready(function() {
    //On Hover Over
    function megaHoverOver(){

        //$jq("#"+$jq(this).attr("menuid")).css('left', left);
        //$jq("#"+$jq(this).attr("menuid")).stop().fadeTo('fast', 1).show();
        var left = $jq("#menu_box").position().left;
        var item_left = $jq(this).position().left;

        $jq(this).find(".sub").css('left', left-item_left);
        $jq(this).find(".sub").stop().fadeTo('fast', 0.9).show(); //Find sub and fade it in
        //
        //$jq(this).find(".sub").stop().slideDown("fast"); //Find sub and fade it in
        /*(function($jq) {
            //Function to calculate total width of all ul's
            $jq.fn.calcSubWidth = function() {
                rowWidth = 0;
                //Calculate row
                $jq(this).find("ul").each(function() { //for each ul...
                    rowWidth += $jq(this).width(); //Add each ul's width together
                });
            };
        })($jq);

        if ( $jq(this).find(".row").length > 0 ) { //If row exists...

            var biggestRow = 0;

            $jq(this).find(".row").each(function() {	//for each row...
                $jq(this).calcSubWidth(); //Call function to calculate width of all ul's
                //Find biggest row
                if(rowWidth > biggestRow) {
                    biggestRow = rowWidth;
                }
            });

            $jq(this).find(".sub").css({'width' :biggestRow}); //Set width
            $jq(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin

        } else { //If row does not exist...

            $jq(this).calcSubWidth();  //Call function to calculate width of all ul's
            $jq(this).find(".sub").css({'width' : rowWidth}); //Set Width

        }*/

        //$jq(this).find(".sub").css({'left' :'100px'}); //Set width
    }
    //On Hover Out
    function megaHoverOut(){
        /*$jq("#"+$jq(this).attr("menuid")).stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
              $jq(this).hide();  //after fading, hide it
          });*/
        //$jq(this).find(".sub").stop().slideUp("fast");
          $jq(this).find(".sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
              $jq(this).hide();  //after fading, hide it
          });
    }

    //Set custom configurations

    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: 500, // number = milliseconds delay before onMouseOut
         out: megaHoverOut // function = onMouseOut callback (REQUIRED)
    };

    
    $jq("ul#topnav li .sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
    $jq("ul#topnav li").hoverIntent(config); //Trigger Hover intent with custom configuration
    $jq('#topnav').lavaLamp({speed: 300});
    //$jq('#test').lavaLamp();
    $jq("ul#topnav .sub ul li").hover(function(){
        $jq("ul#topnav .sub ul li").removeClass('liselected');
        $jq(this).addClass('liselected');
    }, function() {
        $jq("ul#topnav .sub ul li").removeClass('liselected');
    });
    $jq("ul#topnav .sub ul li").click(function() {
        window.location=$jq(this).find("a").attr("href"); return false;
    });
});



