/*
* Dropdown Border
*
* Copyright (c) 2011
* Ivan Tatic (ivan.tatic@saturized.com)
* Dual licensed under the MIT and GPL licenses.
* Uses the same license as jQuery, see:
* http://jquery.org/license
*
* @version 1
*/
;(function($) {
    var options = {
    };

    $.fn.dropdownBorder = function(options) {
        var opts = $.extend({}, $.fn.dropdownBorder.defaults, options);

        return this.each(function(i) {
            var d = $(this);
            d.append('<span class="drpdwn bdt" /><span class="drpdwn bdl" /><span class="drpdwn bdr" /><span class="drpdwn bdb" /><span class="drpdwn cbl" /><span class="drpdwn cbr" />');
        });
    };

    // For debugging
    function debug($obj) {
        if (window.console && window.console.log) {
            window.console.log($obj);
        }
    }

    // Default options
    $.fn.dropdownBorder.defaults = {
    };
})(jQuery);

function megaHoverOver(){
    $(this).find(".dropdown").stop().show();
    var rowMargin = 30;

    //Calculate width of all ul's
    (function($) {
        jQuery.fn.calcSubWidth = function() {
            rowWidth = 0;
            rowMargin = 30;
            //Calculate row
            $(this).find("ul").each(function() {
                rowWidth += $(this).width() + rowMargin;
            });
        };
    })(jQuery);

    if ( $(this).find(".row").length > 0 ) { //If row exists...
        var biggestRow = 0;
        //Calculate each row
        $(this).find(".row").each(function() {
            $(this).calcSubWidth();
            //Find biggest row
            if(rowWidth > biggestRow) {
                biggestRow = rowWidth;
            }
        });
        //Set width
        $(this).find(".dropdown").css({'width' :biggestRow});
        $(this).find(".row:last").css({'margin':'0'});

    } else { //If row does not exist...

        $(this).calcSubWidth();
        //Set Width
        $(this).find(".dropdown").css({'width' : rowWidth-rowMargin });
    }
}

function megaHoverOut(){
  $(this).find(".dropdown").stop().hide();
}
$(document).ready(function(){
    $('#menu-main li div.dropdown ul.featured li').each( function() {
        featuredImgURL = $(this).find('img').attr('src');
        $(this).css({ 'background': 'url('+featuredImgURL+') 50% 50% no-repeat' }).find('img').hide();
    });
    $('#menu-main li div.dropdown ul.featured li:first-child').each( function() {
        $(this).addClass('ui-corner-top');
    });
    $('#menu-main li div.dropdown ul.featured li:last-child').each( function() {
        $(this).addClass('ui-corner-bottom');
    });
    $('.dropdown').dropdownBorder();
    var config = {
        sensitivity: 2,
        interval: 100,
        over: megaHoverOver,
        timeout: 100,
        out: megaHoverOut
    };
    $("#menu-main li").hoverIntent(config);
});
Cufon.replace('#header #menu-main li div.dropdown ul.featured li h3', { fontFamily: 'HelveticaNeueThin' });
