/**
 * @revision $Id$
 * @author: Beau Bishop  <beau[at]arcamax[dot]com>
 * ArcaMax Publishing 2010-09-08
 *
 */

$(document).ready(function(e){
	
	function showInterstitial () {
		return (parseInt($.cookie('amx_ic')) == 2) ? true : false;
	}
	
	function incrementInterstitialCookie () {
		var ic = $.cookie('amx_ic');
		var bits = ic.split('|');
		var exp = new Date(bits[1]);
		setInterstitialCookie(parseInt(bits[0]) + 1 + '|' + bits[1], exp);
	}
	
	function setInterstitialCookie (val, exp) {
		$.cookie('amx_ic', val, { expires: exp, path: '/' });
	}
		
	// grab all non-ard links
	//var links = $("a:not([href*='/ard/']):not([href$='cgi-bin/reg']"); 
	var links = $("a:not([href$='cgi-bin/reg']):not([href*='/ard/'])");

	links.click(function(evt){
		incrementInterstitialCookie();
		if ( showInterstitial()) {
			var redirect = $(this).attr('href');
			var ts = new Date().getTime();
			var adurl = 'http://www.arcamax.com/ard/6734119';
			var modalhtml = '<div id="amx-modal">'
						  + '<img id="ic_logo" src="/files/amx-ui/arcamax-logo-sm.png" alt="" />'
						  + '<div class="modal-timer"></div>'
						  + '<p><small>a d v e r t i s e m e n t</small></p>'
						  + '<iframe frameborder="0" name="midoffer" style="margin:0px auto;"  scrolling="auto" width="740" height="340" src="' + adurl + '"></iframe>'
						  + '<br /><br /><a id="ic_cont" href="'+redirect+'">Continue &raquo;</a>'
						  + '</div>';
			
			$('#amx').prepend(modalhtml);
			$('#amx-modal').modal({
				opacity: 100,
				onShow: function () {
 					$('#amx-modal .modal-timer').countdown({
 						until: +10,
 						layout: 'Page will redirect in {sn} seconds...',
 						expiryUrl : redirect
 					});
				},
				onClose: function () {
					$(location).attr('href', redirect);
				}
			});
			return false;
		}
	 	return true;
	});


	 // sets/increments interstitial cookie on page load
	(function () {
	 	var ic = $.cookie('amx_ic');
		if (ic == null) {
			var days = 1 * 1000 * 60 * 60 * 24;  //< 1 day
			var exp = new Date();
			    exp.setTime(exp.getTime() + days);
			setInterstitialCookie('1|'+exp, exp);
		} 
		else {
			var bits = ic.split('|');
			var exp = new Date(bits[1]);
			setInterstitialCookie(parseInt(bits[0]) + 1 + '|' + bits[1], exp);
		}
	})();


});
