$(document).ready(function(){
	// Set up the cookies and interstitial:
	if( undefined == $.cookie('views') ){
		// Set the cookie:
		$.cookie('views', '1', { expires: 3, path: '/', domain: 'premier1supplies.com' });
	} else{
		if(parseInt($.cookie('views')) < 5){
			// Increment the cookie:
			$.cookie('views', (parseInt($.cookie('views')) + 1), { expires: 3, path: '/', domain: 'premier1supplies.com' });
		}
	
		// Determine if we should show the interstitial:
		if(parseInt($.cookie('views')) === 5 && undefined == $.cookie('action')){
			$.cookie('views', 1, { expires: 14, path: '/', domain: 'premier1supplies.com' });
			//This will default the cookie action  to "shown" and the interstitial will not be shown again for 14 days
			//This implies the user exited the form in some way other than a standard option (such as refreshing the page or visiting another page)
			$.cookie('action', 'shown', { expires: 14, path: '/', domain: 'premier1supplies.com' });
			// Show the interstitial:
			var interstitial = $('#interstitial').dialog({
				dialogClass: "module",
				draggable: false,
				modal: true,
				resizable: false,
				width: '550',
				open: function() {
					try {
						pageTracker._trackEvent("Interstitial", "Show");
					} catch(err) {}
					$.cookie('action', 'viewed', { expires: 1825, path: '/', domain: 'premier1supplies.com' });
					$('.ui-widget-overlay').click(function() {
						// If clicked outside interstitial, show again in 14 days
						$(interstitial).dialog("close");
						$.cookie('action', 'clickbehind', { expires: 14, path: '/', domain: 'premier1supplies.com' });
						try {
							pageTracker._trackEvent("Interstitial", "Click Behind");
						} catch(err) {}
						return false;
					});
					$(".module .close_modal").click(function() {
						$(interstitial).dialog("close");
						// if no thanks is clicked once, show again in 6 months. if twice, show again in 5 years
						if(undefined == $.cookie('nothanks_responses')){
							$.cookie('action', 'nothanks', { expires: 180, path: '/', domain: 'premier1supplies.com' });
							$.cookie('nothanks_responses', '1', { expires: 1825, path: '/', domain: 'premier1supplies.com' });
						} else {
							$.cookie('action', 'nothanks', { expires: 1825, path: '/', domain: 'premier1supplies.com' });
						}
						try {
							pageTracker._trackEvent("Interstitial", "No Thanks");
						} catch(err) {}
						return false;
					});
					// if maybe later is clicked, show again in 14 days
					$(".module .dismiss_modal").click(function() {
						$(interstitial).dialog("close");
						$.cookie('action', 'maybelater', { expires: 14, path: '/', domain: 'premier1supplies.com' });
						try {
							pageTracker._trackEvent("Interstitial", "Maybe Later");
						} catch(err) {}
						return false;
					});
				}
			
			});
		}
	} 
		// Determine if we should show the interstitial:
		if($.cookie('first') != 'shown'){
		}
});
