
var POPUP_DEBUG = false;

function show_popup() {
	// Set the cookie before we open the window:

	var d = new Date();
	Cookie.set( 'cookie_asked', d.getTime() / 1000, 168 );

	var w = window.open( 'http://www.sgma.com/popup/', '_blank', 'width=600,height=500' );
}

function test_show_popup() {
	if ( ! parseInt( Cookie.get( 'cookie_asked' ) ) ) {
		show_popup();
	}
}

function cookie_init() {
	var d = new Date();
	var cookie_session_start_time = Math.floor( d.getTime() / 1000 );

	if ( POPUP_DEBUG ) {
		alert('initing cookie_session_start_time to '   + cookie_session_start_time);
	}

	Cookie.set( 'cookie_session_start_time', cookie_session_start_time );
	Cookie.set( 'cookie_page_count', 1 ); // 1 page visited so far
	Cookie.set( 'cookie_asked', 0 ); // never asked so far
}

// Assumes cookie.js has already been included
function cookie_check() {
	var d = new Date();

	if ( ! 0 && ! 0 ) {
		if ( POPUP_DEBUG ) {
			alert( 'page_count_limit and minutes_limit are both 0, so returning false!' );
		}

		return false;
	}

	var cookie_session_start_time = parseInt( Cookie.get( 'cookie_session_start_time' ) );

	var hours_on_site = 0; // assume
	var minutes_on_site = 0; // assume

	if ( cookie_session_start_time ) {
		hours_on_site = ( Math.floor( d.getTime() / 1000 ) - cookie_session_start_time ) / ( 60*60 );
		minutes_on_site = ( Math.floor( d.getTime() / 1000 ) - cookie_session_start_time ) / 60;
	} else {
		cookie_init();
	}

	if ( POPUP_DEBUG ) {
		alert( 'Will re-init in ' + ( 168 - hours_on_site ) + ' hour(s)' );
	}

	// If they've been here more than HOURS_THRESHOLD, assume it's a new session:
	if ( hours_on_site > 168 ) {
		cookie_init();
	}

	// If they've already seen the pop-up, get out of here:
	var cookie_asked = parseInt( Cookie.get( 'cookie_asked' ) );
	if ( cookie_asked ) {
		if ( POPUP_DEBUG ) {
			alert( 'Already asked: "' + cookie_asked + '"' );
			return false;
		}
	}

	if ( POPUP_DEBUG ) {
		alert('on site: ' + hours_on_site + ' hrs' );
	}

	var cookie_page_count = parseInt( Cookie.get( 'cookie_page_count' ) );

	if ( POPUP_DEBUG ) {
		alert('page count: ' + cookie_page_count );
	}

	if ( POPUP_DEBUG ) {
		if ( 0 && ( minutes_on_site >= 0 ) ) {
			alert( 'minutes_on_site (' + minutes_on_site + ') >= 0 so showing popup!' );
		}

		if ( 0 && ( cookie_page_count >= 0 ) ) {
			alert( 'cookie_page_count (' + cookie_page_count + ') >= 0 so showing popup!' );
		}
	}

	// If we're past the threshold for # of minutes or # of page views, show the popup:

	if (
		( 0 && ( minutes_on_site >= 0 ) )
			||
		( 0 && ( cookie_page_count >= 0 ) )
	) {
		test_show_popup();
	}

	// Increment page count and store it in the cookie:
	cookie_page_count++;
	Cookie.set( 'cookie_page_count', cookie_page_count );

	// If still not yet asked, set timeout:
	if ( ! parseInt( Cookie.get( 'cookie_asked' ) ) ) {
		if ( POPUP_DEBUG ) {
			alert( 'will show popup in ( 0 - ' +  minutes_on_site + ' ) = ' + ( 0 - minutes_on_site ) + ' minute(s)' );
		}

		var timeout = ( 0 - minutes_on_site ) * 60*1000;

		if ( POPUP_DEBUG ) {
			alert( 'Setting timeout for ' + ( timeout / 1000 ) + ' second(s) from now' );
		}

		setTimeout( 'test_show_popup()', timeout );
	}

}

cookie_check();

// EOF
