var popupStatus = 0;
var destinationUrl = null;
var destinationTitle = null;
var referringUrl = null;
var referringTitle = null;

$(document).ready(function() {
    $('.outside-link').click(function(event) {
        event.preventDefault();
        $('#scholarship-flash').hide();
        $('#crohnies-video-player').hide();
        $('#home-flash-container').hide();
        destinationUrl = $(this).attr("href"); //Gets href attribute of the link that the user clicked on
        destinationTitle = $(this).attr("title");
        referringUrl = location.href;
        referringTitle = document.title;
        var docScrollTop = $(document).scrollTop(); //Gets Scroll position
        var clickcoordinate = event.pageY; //Gets position where the click happened
        loadPopup(docScrollTop);  //Calling Loading Popup Function
    });

    //Allows User to Move to External Web Site
    $("#leaving-site-yes").click(function(event) {
        event.preventDefault();
        //$(this).attr("href", thislink);
        _gaq.push(['_setAccount', 'UA-483259-1'], ['_trackEvent', 'CAM External Link', 'Yes Click - ' + destinationTitle, referringUrl + ' || ' + referringTitle]);
        disablePopup();
        setTimeout("popUpRedirect('" + destinationUrl + "')", 1000);
    });

    //User stays on current page
    $("#leaving-site-no").click(function(event) {
        event.preventDefault();
        _gaq.push(['_setAccount', 'UA-483259-1'], ['_trackEvent', 'CAM External Link', 'No Click - ' + destinationTitle, referringUrl + ' || ' + referringTitle]);
        disablePopup();
    });

    //Closes out popup when clicking background
    $("#backgroundPopup").click(function() {
        disablePopup();
    });
});  

//loading popup
function loadPopup(docScrollTop) {
	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('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;	
	} 
}

//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;
	}
}

function popUpRedirect(url)
{
    window.open(url);
}
