
$(document).ready(function() {
	
	/** This value represents number of pageviews to trigger popup **/
	/** Change as desired **/
	var interval	= 7;


	var one_day		= 1000*60*60*24;
	var today		= (new Date()).getTime();
	var visits		= 0;

	if($.cookie('miraclecenter_page_views') != null)
		visits = $.cookie('miraclecenter_page_views');

	//only increment views count if popup was last opened over one day ago
	if(($.cookie('miraclecenter_last_popup') == null) || ( (today - $.cookie('miraclecenter_last_popup'))/(one_day) >= 5 )){
		visits = parseFloat(visits) + 1;
		$.cookie('miraclecenter_page_views', visits, { expires: 365, path: '/', domain: 'miraclecenter.org', secure: false });
	}

	if (visits == interval)
	{
		if(($.cookie('miraclecenter_last_popup') == null) || ( (today - $.cookie('miraclecenter_last_popup'))/(one_day) >= 1 ))
		{


/***** SUBSCRIPTION POPUP *****/

			var txt = '';
			txt += "<script>";
			txt += "$(document).ready(function() {";
			txt += "$('#sub-postal-yes').click(function(){";
			txt += "$('.sub-dependent').attr('disabled','');";
			txt += "});";
			txt += "$('#sub-postal-no').click(function(){";
			txt += "$('.sub-dependent').val('');";
			txt += "$('.sub-dependent').attr('disabled','disabled');";
			txt += "});";
			txt += "});";
			txt += "</script>";
			txt += "<style>";
			txt += "#subscribe-dialog{font-size:12px; width:630px;} ";
			txt += "#subscribe-dialog td{padding:5px 15px;	text-align:left;}";
			txt += "#subscribe-dialog input{width:210px; padding:5px;}";
			txt += "#subscribe-dialog select{width:100%; padding:5px;}";
			txt += "#subscribe-dialog input.radio{width:10px; padding:0px;}";
			txt += "#subscribe-dialog img{margin:0px;}";
			txt += "</style>";
			txt += "<!--[if lt IE 8]>";
			txt += "<style>";
			txt += "#subscribe-dialog table{text-align:left;} ";
			txt += "#subscribe-dialog td{padding:3px 15px;	text-align:left;}";
			txt += "#subscribe-dialog input{padding:3px;}";
			txt += "#subscribe-dialog select{width:300px; padding:4px;}";
			txt += "</style>";
			txt += "<![endif]-->";
			txt += "<div id='subscribe-dialog' title='Receive The Holy Encounter . . .'>";
			txt += "<div id='donation-heading'>Receive our bimonthly magazine, The Holy Encounter . . .</div>";
			txt += "<form id='subscribe-form'>";
			txt += "<table align='left'>";
			txt += "<tr><td>Email:</td><td><input id='sub-email' name='sub-email' type='text' /></td><td rowspan='6' valign='top'><img src='http://www.miraclecenter.org/images/mayjune11THE.jpg' /></td></tr>";
			txt += "<tr><td colspan='2'>Postal address for print version (US only): &nbsp;&nbsp;<input class='radio' id='sub-postal-yes' name='sub-postal' type='radio' value='1' />Yes &nbsp;&nbsp;<input class='radio' id='sub-postal-no' name='sub-postal' type='radio' value='0' checked />No </td></tr>";
			txt += "<tr><td>First Name:</td><td><input id='sub-first' name='sub-first' class='sub-dependent' type='text' disabled /></td></tr>";
			txt += "<tr><td>Last Name:</td><td><input id='sub-last' name='sub-last' class='sub-dependent' type='text' disabled /></td></tr>";
			txt += "<tr><td>Address (line 1):</td><td><input id='sub-address1' name='sub-address1' class='sub-dependent' type='text' disabled /></td></tr>";
			txt += "<tr><td>Address (line 2):</td><td><input id='sub-address2' name='sub-address2' class='sub-dependent' type='text' disabled /></td></tr>";
			txt += "<tr><td>City:</td><td colspan='2'><input id='sub-city' name='sub-city' class='sub-dependent' type='text' disabled style='width:400px;' /></td></tr>";
			txt += "<tr><td>State:</td><td colspan='2'><input id='sub-state' name='sub-state' class='sub-dependent' type='text' disabled maxlength='2' style='width:400px;' /></td></tr>";
			txt += "<tr><td>Zip Code:</td><td colspan='2'><input id='sub-zip' name='sub-zip' class='sub-dependent' type='text' disabled style='width:400px;' /></td></tr>";
			txt += "<tr><td>Country:</td><td colspan='2'><input id='sub-country' name='sub-country' type='text' value='United States' disabled style='width:400px;' /></td></tr>";
			txt += "</table>";
			txt += "</form>";
			txt += "</div>";
			txt += "<div id='donation-heading'>&nbsp;</div>";

			$.prompt(txt,{
				top: '5%',
				zIndex: '999',
				buttons:{'Yes':true, 'No thanks':false},
				submit: function(v,m){
					
					if(v){												
						var params = $('#subscribe-form').serialize();
						var msg = $.ajax({async:false, url:"../../../../services/subscribe.php?", cache:false, type:'POST', data: params}).responseText;
						alert(msg);
						return true;
					}
					else{
						return true;
					}
					
				},
				opacity: 0.7
			});


/***** DONATION POPUP *****/
/*
			var txt = '';
			txt += "<div id='donation-heading'>Did You Know?</div>";
			txt += "<p class='donation-content'>If everyone who visited our site throughout the year voluntarily gave even a small donation, we could easily cover the costs of this website.</p>";
			txt += "<p class='donation-content'>Our site and all of its great content are absolutely free to everyone. It is our hope that this will never change. As a nonprofit organization, we rely on the generosity of our users to help ensure that we can continue to expand and enhance our vital services for the benefit of Course students around the world. </p>";
			txt += "<p class='donation-content'>Whether you're a first time visitor or a long time user, your gift of any size is deeply appreciated. If you click 'Yes' below, a new window will open up to our Donation Page. Thanks for supporting <em>Miracle Distribution Center!</em></p>";

			$.prompt(txt,{ 
				buttons:{'Yes':true, 'No thanks':false},
				callback: function(v,m,f){
					
					if(v){												
						window.top.location = "https://www.miraclecenter.org/donation/donation.php" ;
					}
					else{
					}
					
				},
				opacity: 0.7
			});
*/			
			$.cookie('miraclecenter_last_popup', today, { expires: 365, path: '/', domain: 'miraclecenter.org', secure: false });
			$.cookie('miraclecenter_page_views', '0', { expires: 365, path: '/', domain: 'miraclecenter.org', secure: false });

		}
	}


});

