$(document).ready(function() {
	$("a.trigger").click(function() {
		$(this).parent().children("div.theList").stop().fadeToggle();
		$(this).parent().toggleClass('active');
	});
	
	//set the subnav height
	var subbavcontentH = $("#subnav_content").height();
	var maincontentH = $("#contentleft").height();
	var mainHeight = $("#main").height();

	if(subbavcontentH > maincontentH ) {
		$("#subnav").css({
			height: '100%',
			minHeight: mainHeight	
		});
		
	} else {
		$("#subnav").css({
		height: subbavcontentH,
		minHeight: mainHeight	
		});
	}

	
	
	
	//twitter
	getTwitters('tweet', { 
	  id: 'BTDmfg',
	  count: 4, 
	  enableLinks: true, 
	  ignoreReplies: true, 
	  clearContents: true,
	  template: '"%text%" <a href="http://twitter.com/%user_screen_name%/statuses/%id_str%/">%time%</a>'
	});
	
	
	// MAP SCRIPTS
	$("#usMap").delegate('area', 'mouseenter',(function(e) {
		// need to get x and y coordinates of mouse
		// but need fix for IE 8 and below
		if(/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
			 var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
			 if (ieversion<=9) {
			 	var tempX = e.pageX - this.offsetLeft;
				var tempY = e.pageY - this.offsetTop;
			  	var x = tempX / 2;
				var y = tempY / 2; 
			}
		} else {
		 	var x = e.layerX - this.offsetLeft;
			var y = e.layerY - this.offsetTop;
		}			

		// hide anythig showing
		$('.show').fadeOut().removeClass('show');
		
		// clear out anything thats open
		if($(".mapDetail").css('display') != 'none') {
			$(".mapDetail").fadeOut();
		}

		// get the id of the area that was clicked
		var id = $(this).attr('class');
		
		// find the div with an id of the same class as var(id)
		var div = $("#"+id);
		
		// set its visible class
		div.addClass('show');
		
		// set the css
		 var cssObj = {
		  'top' : y+'px',
		  'left' : x+'px'
		}

		div.css(cssObj);
		
		// make it visible
		div.fadeIn();		
		
	})).delegate('area', 'mouseleave',(function(e) {
		// nothing for now
		})
	);
	
	$("#usMap").delegate('a.closeBtn','click',function(e) {
		// hide anythig showing
		$('.show').fadeOut().removeClass('show');
		
		// clear out anything thats open
		if($(".mapDetail").css('display') != 'none') {
			$(".mapDetail").fadeOut();
		}		
  		
  	});
	
	$("#usMap").delegate('.bubbleInfo','click',function(e) {
		// hide anythig showing
		$('.show').fadeOut().removeClass('show');
		
		// clear out anything thats open
		if($(".mapDetail").css('display') != 'none') {
			$(".mapDetail").fadeOut();
		}		
  		
  	});
	
	// INTERNATIONAL MAP SCRIPTS
	$("#intMap").delegate('area', 'mouseenter',(function(e) {
		// need to get x and y coordinates of mouse
		// but need fix for IE 8 and below
		if(/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
			 var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
			 if (ieversion<=9) {
			 	var tempX = e.pageX - this.offsetLeft;
				var tempY = e.pageY - this.offsetTop;
			  	var x = tempX;
				var y = tempY / 2; 
			}
		} else {
		 	var x = e.layerX - this.offsetLeft;
			var y = e.layerY - this.offsetTop;
		}			

		// hide anythig showing
		$('.show').fadeOut().removeClass('show');
		
		// clear out anything thats open
		if($(".mapDetail").css('display') != 'none') {
			$(".mapDetail").fadeOut();
		}

		// get the id of the area that was clicked
		var id = $(this).attr('class');
		
		// find the div with an id of the same class as var(id)
		var div = $("#"+id);
		
		// set its visible class
		div.addClass('show');
		
		// set the css
		 var cssObj = {
		  'top' : y+'px',
		  'left' : x+'px'
		}

		div.css(cssObj);
		
		// make it visible
		div.fadeIn();		
		
	})).delegate('area', 'mouseleave',(function(e) {
		// nothing for now
		})
	);
	
	$("#intMap").delegate('a.closeBtn','click',function(e) {
		// hide anythig showing
		$('.show').fadeOut().removeClass('show');
		
		// clear out anything thats open
		if($(".mapDetail").css('display') != 'none') {
			$(".mapDetail").fadeOut();
		}		
  		
  	});
	
	$("#intMap").delegate('.bubbleInfo','click',function(e) {
		// hide anythig showing
		$('.show').fadeOut().removeClass('show');
		
		// clear out anything thats open
		if($(".mapDetail").css('display') != 'none') {
			$(".mapDetail").fadeOut();
		}		
  		
  	}); 
  	
  	/************************************
  	 *         News Archives            * 
  	 ************************************/
  	// get the class of the first month in the sidebar and set it active
  	var currentArchiveDate = $(".newsArchives #subnav li").first().attr('class');
  	console.log(currentArchiveDate);
  	$(".newsArchives #subnav li a").first().addClass('current');
  	
  	// show the initial articles for the first nav selection
  	$(".newsArchives #"+currentArchiveDate).css('display','block');
  	
  	// reset currentArchiveDate variable to the clicked on subnav date
  	$(".newsArchives #subnav li a").click(function() {
  		// remove the current class from ALL items
  		$("#subnav li a").removeAttr('class');
 		
 		// set the variable to the current date
  		currentArchiveDate = $(this).parent().attr('class');
  		
  		//hide all articles
  		$(".archiveBlock").hide();
  		
  		//show the currently selected date
  		$("#"+currentArchiveDate).fadeIn();
  		
  		// add the current class to the clicked element for highlighting
  		$(this).addClass('current');
  	});


});
