/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);

// JavaScript Document
(function($) {
	$.fn.cbb = function(el){
		if($(this).parent().hasClass('.i3')||$(this).parent().parent().hasClass('.i3')){return;}
		if($(this).parent().is('a')){var root=$(this).parent();}
		else{var root=$(this);}
		root.wrap('<div class="cb" />').wrap('<div class="i1" />').wrap('<div class="i2" />').wrap('<div class="i3" />');
		$(this).closest('div.i1').after('<div class="bb"><div></div></div>')
		$(this).closest('div.i1').before('<div class="bt"><div></div></div>')
		$(this).closest('.cb').width($(this).width()+36);
	};
})(jQuery);

$(document).ready(function(){							 

	$(".hihslide-body table").attr('width','100%');

	$.hovering = false;
	
	var config = {    
     timeout: 500 // number = milliseconds delay before onMouseOut    
	};

	$("#MegaMenu").hover(
    function() {
      $.hovering = true;
	  //stop the hoverintent function from working
    },
    function() {
      $.hovering = false;
	  $("#MegaMenu").hide();
	  $(".activeMenu").toggleClass("activeMenu");
    });


	function MMOpen(){
				// tag this link to find again later



			$(this).toggleClass("activeMenu").removeAttr("title");
			var file = $(this).children(":first").attr('id').split("-")[1];
			$(this).append("<div class='MegaMenu' id='"+file+"'><div class='Content'></div></div>");
			var Menu = $("#"+ file);
			var pos = $(this).position();
			var page = $(".page").offset();	
			
			var dif =  (pos.left + Menu.width()) - ($(".page").width());
			if( dif > 0){
				dif = dif + 10;
			}
			else{ 
				dif = 0;
			}

			
			// get file from server
			$(".MegaMenu .Content").load('/menus/'+file+'.html',function(){
				//find if menu will be outside the design

				 Menu.show();

				//display the menu
				Menu.css({
				  "top": pos.top + 26 + "px",
				  "left": (pos.left - dif - 10) + "px"

				})
/*				$("#MegaMenu").show("fast");
				
				*/
			})
	}
	
	function MMClose(){
				//mouse out so  hide the menu again
				$(".MegaMenu").remove();
				$(".activeMenu").toggleClass("activeMenu");
	}

	  var MMconfig = {
		   sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
		   interval: 25, // number = milliseconds for onMouseOver polling interval
		   over: MMOpen, // function = onMouseOver callback (REQUIRED)
		   timeout: 15, // number = milliseconds delay before onMouseOut
		   out: MMClose // function = onMouseOut callback (REQUIRED)
	  };

	$("#MegaMenu").css({'opacity':'0'});
	$("#MainMenu li.MM").hoverIntent(MMconfig);

/*	
	//set up function
	$("#MainMenu .MM").hoverIntent(
		// function for hover over
		function(){

	    },
		// function for hover out
		function(){
		}
	)
*/
	//fancy border
    $(".Frame").each(function(){
        $(this).cbb();
    });

	//google analytics.
	if (typeof _gaq != 'undefined') {
		var GoGA = true;
	}

	//  
	var filetypes = /\.(zip|exe|pdf|doc*|xls*|ppt*|mp3)$/i; 
	
	$('#dnn_ContentPane a[href]').each(function(){  
     var href = $(this).attr('href');  
     if ((href.match(/^https?\:/i)) && (!href.match(document.domain))){  
         $(this).click(function() {  
             var extLink = href.replace(/^https?\:\/\//i, '');
			 _gaq.push(['_trackEvent', 'Link', 'External', extLink]);
         });
		 $(this).addClass('externalLink');
	}
	 else if (href.match(/^mailto\:/i)){  
		 $(this).click(function() {  
			 var mailLink = href.replace(/^mailto\:/i, '');  
			 _gaq.push(['_trackEvent', 'Link', 'Email', mailLink]);
		 });  
	 }  
	 else if (href.match(filetypes)){  
  	     var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : "undefined";  
		 $(this).click(function() {  
			 var filePath = href.replace(/^https?\:\/\/(www.)duhaime\.org\//i, '');
			 _gaq.push(['_trackEvent', 'Link', 'Download - ' + extension, filePath]);
		 }); 
		 $(this).addClass(extension); 
		}
	});
     	
	$('a[href*=#top]').click(function(){$('html,body').animate({scrollTop: 0},400)});

//find if link is to current page
	$('a').filter(
		'[href='+window.location.href+'],[href='
		+window.location.pathname+window.location.search.substring()+
		']'
	).addClass("current");

	$('#main h1:first').addClass('first');

});



