$(document).ready(function() {
        function megaHoverOver(){
                $(this).find(".mainitem").addClass('over');
				
                Cufon.replace(
						'.mainitem',
						{ fontFamily: 'HeliosCond',hover: true }//ul#topnav li a.mainitem,
				);
                $(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("div.border1").children().each(function() {
										var bl = parseInt($(this).css('border-left'));if(isNaN(bl))bl=0;
										var pl = parseInt($(this).css('padding-left'));if(isNaN(pl))pl=0;
										var pr = parseInt($(this).css('padding-right'));if(isNaN(pr))pr=0;
										var br = parseInt($(this).css('border-right'));if(isNaN(br))br=0;
                                        rowWidth += bl+
													pl+
													$(this).width()+
													pr+
													br;	

                                });
                        };
                })(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(".sub").css({'width' :biggestRow});
                       // $(this).find("#p1").css({'width' :biggestRow});
                       // $(this).find(".snow").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});
                       // $(this).find("#p1").css({'width' : rowWidth});
                       //  $(this).find(".snow").css({'width' : rowWidth});

                }
                var collage_padding = parseInt($(this).find('.collage').css('padding-right'));
                var border_width = collage_padding + rowWidth-43;
                //collage_padding = collage_padding + 1;
               // alert( collage_padding);
			    $(this).find(".border1").css({'width' : border_width });
				$(this).find(".sub").css({'width' :  border_width+66});
				$(this).find(".collage").css({'width' :  rowWidth+23});
               

                
             
              //  $("#p1").css({'height' : height2});
              //  $(".snow").css({'height' : height2});

               
        }

        function megaHoverOut(){
		$(this).find(".sub").hide();
		var el = $(this).find(".mainitem");
		if ($.browser.msie) {
			$(this).find(".mainitem").removeClass('over');
		}else
		for (i=0;i<el.length;i++) {
			if(el[i].className!='mainitem over current') el[i].removeClass('over');
		}
		Cufon.replace(
			'.mainitem',
			{ fontFamily: 'HeliosCond',hover: true }//ul#topnav li a.mainitem,
		);
        }


        var config = {
                 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
                 interval: 50, // number = milliseconds for onMouseOver polling interval 100
                 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
                 timeout: 200, // number = milliseconds delay before onMouseOut
                 out: megaHoverOut // function = onMouseOut callback (REQUIRED)
        };

        $("ul#topnav li .sub").css({'opacity':'0'});
        $("ul#topnav li").hoverIntent(config);

        /*http://www.sohtanaka.com/web-design/examples/mega-dropdowns/*/
});



