var popupStatus = 0;


$(document).ready(function() {

    
    $('.outside-link').click(function(event) {

		 event.preventDefault();
		var thislink = $(this).attr("href"); //Gets href attribute of the link that the user clicked on
		var docScrollTop = $(document).scrollTop(); //Gets Scroll position
		var clickcoordinate = event.pageY;//Gets position where the click happened
		
        loadPopup(docScrollTop, thislink);  //Calling Loading Popup Function
       

    });

	//User stays on current page
    $("#leaving-site-no").click(function(event) {

        event.preventDefault();
        disablePopup();
        
    });    

	
	//Closes out popup when clicking background
    $("#backgroundPopup").click(function() {

        disablePopup();
        
    });


	
    
    

});  

//loading popup
function loadPopup(docScrollTop, thislink){

    
	var winH = $(window).height();  
	var winW = $(window).width();
	
	//Determine positioning based on user scrolling
	//$('#popupLeaving').css('top',  (winH/2-$('#popupLeaving').height()/2) + docScrollTop);
	$('#popupLeaving').css('top',  200 + docScrollTop);
	$('#popupLeaving').css('left', winW/2-$('#popupLeaving').width()/2);

	$(window).resize(function() {

		var newwinW = $(window).width();
		$('#popupLeaving').css('left', newwinW/2-$('#popupLeaving').width()/2); 

	});

	if(popupStatus==0)
	{


		//$("#backgroundPopup").css({"opacity": "0.7"});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupLeaving").fadeIn("slow");
		popupStatus = 1;
		
		
	}

	//Allows User to Move to External Web Site
	
	$("#leaving-site-ok").click(function() { 
           //event.preventDefault();
	       
	       window.location.href = thislink;
	       disablePopup();
    }); 

    
    $("#leaving-site-close").click(function() { 
           //event.preventDefault();
	       
	       disablePopup();
    }); 
    
    $("#leaving-site-cancel").click(function() { 
           //event.preventDefault();
	       
	       disablePopup();
    }); 


	
}

//disables popup 
function disablePopup()
{
	$('#scholarship-flash').show();
	$('#crohnies-video-player').show();	
	$('#home-flash-container').show();
	if(popupStatus==1) 
	{   
		$("#backgroundPopup").fadeOut("slow");
		$("#popupLeaving").fadeOut("slow");
		popupStatus = 0;
		
	}   

}


