var entersiteStatus = 0;

$(document).ready(function() {
    if (location.href.indexOf("popUp.html") == -1) {
	    if(!get_cookie("visited"))
	    {
			makeHid();
		    EnterSite();  //Calling Loading Popup Function
	    }
		else{makeVis();}
	     $("#return-home").click(function(event) {
			 
		     event.preventDefault();
		     window.location.href = window.location.href;
        });
	    //User stays on current page
        $("#enter-site-cancel").click(function(event) {

            event.preventDefault();
            cancelPage(); 
		
        });
	    //Allows User to Move to External Web Site
	    $("#enter-site-ok").click(function() { 
			
		    set_cookie();
		    disableEnterSite();
			
		
        }); 
    }
});  

function set_cookie() {
	var expiredays = 1;
	var cookiedate=new Date();
	cookiedate.setDate(cookiedate.getDate()+expiredays);
	document.cookie = "visited = true; expires=" + cookiedate.toGMTString();
}

function get_cookie(visited)
{
   var results = document.cookie.match ( '(^|;) ?' + visited + '=([^;]*)(;|$)' );
   if (results)
   {
		return ( unescape ( results[2] ) );
		
   }
   else
   {
	    return null;
   }
}

//loading popup
function EnterSite() {
	var winH = $(window).height();
	var winW = $(window).width();
	var documentHeight = $(document).height();
	
	//Determine positioning based on user scrolling
	$('#popupEnterSite').css('top',  (winH/2-$('#popupEnterSite').height()/2));
	$('#popupEnterSite').css('left', winW/2-$('#popupEnterSite').width()/2);
	$('#popupEnterSite').load('popUp.html');

	$(window).resize(function() {
	    var newwinW = $(window).width();
	    var newwinH = $(window).height();
	    $('#popupEnterSite').css('left', newwinW / 2 - $('#popupEnterSite').width() / 2);
	    $('#popupEnterSite').css('top', (newwinH / 2 - $('#popupEnterSite').height() / 2));
	});
	
	
	if(entersiteStatus==0) {
	    $("#entersiteBackground").css("height", documentHeight);
		$("#entersiteBackground").fadeIn("slow");
		$("#popupEnterSite").fadeIn("slow");
		
		entersiteStatus = 1;
			
	}
}

//disables popup 
function cancelPage()
{
	if(entersiteStatus==1) 
	{   
		window.location.href = "popUpCancel.html";
		$("#entersiteBackground").fadeOut("slow");
		$("#popupEnterSite").fadeOut("slow");
		entersiteStatus = 0;	
	}   
}

function disableEnterSite() {
	if(entersiteStatus==1) {
		$("#entersiteBackground").fadeOut("slow");
		$("#popupEnterSite").fadeOut("slow");
		entersiteStatus = 0;
		makeVis();
		
	}   
}

function makeVis(){
	document.getElementById('container2').style.visibility = 'visible';	
}

function makeHid(){
	document.getElementById('container2').style.visibility = 'hidden';	
}


