$(document).ready(function(){
	
	var click_check = '';
	
	if ((navigator.userAgent.indexOf('iPad') != -1)) {
		//reset viewport for ipad
		document.getElementById("viewport").setAttribute('content', 'width = 1000');
	}
	
	// print button
	$("#print").click(function(e){
 		window.print();
		e.preventDefault();
	});
	
	
	// highlight top level navigation
	// don't think we need this any more
	/*
	try {
		var section = $("#nav").attr('class');
 		$("#"+section + " > a").addClass('active');
	}catch(e){}
	*/
	
	$(".top_level").click(function(e){
		
		// prevents link cancellation on mobile site by checking width
		
		if ($(window).width() > 600) {
			
			//check if iPad
			
			if ((navigator.userAgent.indexOf('iPad') != -1)) {
				
				var p = $(this).parent().attr("id");

				
				// check if variable matches if not cancel link
				if (click_check != p) {
												
					click_check = $(this).parent().attr("id"); // set this to parent ID so next click will launch the page
					
					//Cancel the link behaviour
					e.preventDefault();
					
					return false;
					
				}
				
			}
		
		}

		
	});
	

	// popup windows on links
	$(".popup").click(function(e){
		
		// only provide a popup on the full site
		
		if ($(window).width() > 480) {  		
			//Cancel the link behavior
			e.preventDefault();
			
			//get url
			var exit_url = $(this).attr('href');
			
			// set popup exit url to match
			$("#exit_url").attr('href', exit_url);
			
			//launch popup
			$("#popup").popUp(exit_url);
			
			return false;
		}
	});
	
	// close pop up
	$(".close_popup").click(function(e){
		$("#popup").popUpClose();		
	});
		
	
	//mobile only js below
	
	$("#header").removeClass("search");
	
	//Check to see if header has search class on it, toggle accordingly

	$("#search_link a").click(function(e){
	
		if ($("#header").is('.search')) {
			$("#header").removeClass("search");
		} else {
			$("#header").addClass("search");
		}
			
	});


});


// Popup windows for leaving the site
(function( $ ){
	
	$.fn.popUp = function(element) {
		
		var $this = $(this);
		
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
						
		//Set height and width to mask to fill up the whole screen
		$("#mask").css({'width':maskWidth,'height':maskHeight});
						
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
				
		//transition effect    
		$('#mask').fadeTo("slow",0.3); 
				
		//Set the popup window to center
		$this.css('top',  winH/2-$(this).height()/2);
		$this.css('left', winW/2-$(this).width()/2);	
				
		$this.fadeIn("1000");

	}
	
	$.fn.popUpClose = function(element) {
					
		//transition hide effect    
		$('#mask').fadeOut("1000"); 								
		$(this).fadeOut("1000");
		
	}	
	
})( jQuery );

//end Popup

