/*
* Adds a function to the window onunload event
*/
function addUnloadEvent(func) {
  var oldOnunload = window.onunload;
  if (typeof window.onunload != 'function') {
    window.onunload = func;
  }
  else {
    window.onunload = function() {
      oldOnunload();
      func();
    }
  }
}

function setConfirmUnload(on) {	
     window.onbeforeunload = (on) ? unloadMessage : null;
}

function showConfirm(){
	var yes = false;
		confirm('Close this window?'); 
		if(yes) stopCountdown();			
};

function unloadMessage() {   
     return 'You have entered new data on this page.  If you navigate away from this page without first saving your data, the changes will be lost.';
}

function stopCountdown() { 
	window.close(); 
}

function goCountdown(countID, secs) {
    var countObj = document.getElementById(countID);
    if (countObj) {
      if (0 < secs) {         
	  	countObj.innerHTML = secs; 
	  	alert("in countdown");         
	  	setTimeout("goCountdown('"+countID+"',"+(secs-1)+")", 1000);       
	  } else stopCountdown(countID);     
	}   
}

function oto(otoName) {
  	var cookie = "oto"+escape(otoName)+"=";
  		if (0 > document.cookie.indexOf(cookie)) {
    		var d = new Date();
    		d.setMonth(d.getMonth() + 120);
    		document.cookie = cookie+"1; expires="
      		+d.toGMTString()+"; path=/;";
    	return true;
  } else return false;
}

function expiration(divId, timeframe, redirect){
	var today = new Date();
	//var expiry = new Date(year, month - 1, day);
	//var expiryDays = parseInt((expiry - today) / 86400000);
	//var expiryDate = expiry.toDateString();
	var IFR = document.getElementById(divId);
	var oldtime = new Date();
	var timelimit = new Date(oldtime.getTime() + timeframe*60000);
	var newtime = new Date();
	var expiryTime = parseFloat(newtime.getMinutes() - oldtime.getMinutes());
	alert(newtime.getTime()+':'+timelimit);
	//if (IFR) 
		
		if (newtime.getTime() > timelimit) {
			alert('This offer is still valid for  '+ expiryDays + ' days and will expired on ' +	expiryDate);
			jQuery('#specialOffer').html('');
			jQuery('#specialOffer').html('<h4>This offer is still valid for <strong><i>' +
			expiryTime + ' </i>days</strong> and will expired on <strong><i> ' +
			expiryDate +'</i></strong></h4>');
			Cufon.refresh();
		}
		else {
			jQuery('#question').find('h1').text('Offer has expired! Redirect to page. Click "Yes" to Continue');
			Cufon.refresh();
			//jQuery.blockUI({ message: 'Offer has expired! Redirect to page.Continue?', css: { width: '275px' } }); 
			
		}
}
 
function hello(selector){
	//alert('hello world');
		if ( oto(selector) )
	    expiration(selector, 2007, 2, 2); // set your expiry time here
	    if(!expiration){
	    	var path = 'http://www.8020newmedia.com/the-consultants-handbook/'; //your full path redirect location
	    	//window.location.href = path;
  		}
}

jQuery(document).ready(function() {
	
	/* Temporary hide current Aweber form */
	jQuery('#multimedia-box').hide('fast');
	
	/* Tabs */
	jQuery('#tabs > ul').tabs({ fx: { height: 'toggle', opacity: 'toggle' } });
	
	// window close event
	var path = window.location.href;
	var pathPage1 = 'http://www.8020newmedia.com/';
	var pathPage2 = 'http://www.8020newmedia.com/check-out-our-limited-offer/';
	var pathPage3 = 'http://www.8020newmedia.com/the-consultants-handbook/';
	
	if (path == pathPage1 ||
	path == pathPage2 ||
	path == pathPage3) {
		jQuery(document).mousemove(function(e){
			if (e.pageY <= 5) {				
				jQuery('#exit-content').modal({
					onOpen: modalOpen,
					onClose: simplemodal_close
				});
			}
			Cufon.refresh('h1,h3');
		});
	}
	
	function modalOpen (dialog) {
		dialog.overlay.fadeIn('fast', function () {
			dialog.container.fadeIn('fast', function () {
				dialog.data.hide().slideDown('fast');
			});
		});
	}
	
	function simplemodal_close (dialog) {
	dialog.data.fadeOut('fast', function () {
		dialog.container.hide('fast', function () {
			dialog.overlay.slideUp('fast', function () {
				jQuery.modal.close();
				});
			});
		});
	}
	
});

