// ================== SITE REGISTRATION & ROADBLOCK ADVERTISEMENT ================== //

//  ------- ON READY  ------- //

$(document).ready(function() {

	// check for active CCE OAS objects
	// var activeRoadblock set in metaheader.inc macro "roadblockAd"
	if (activeRoadblock === 1 && activeRoadblockCookie == null){
		roadBlock();
	}

	// if not logged in, display registration popup and set REGPROMPT cookie
	setRegistrationPromptCookie();

	// count pages if not logged-in
	if (activeLoginCookie == null){
		cookieCount();
	}

});

// ------- COOKIES ------- //

var activeLoginCookie = $.cookie('CSDemos');
var pageCount = $.cookie('PC');
var newcount = parseInt(pageCount) + 1;
var activeRegistrationPromptCookie = $.cookie('REGPROMPT');
var activeRoadblockCookie = $.cookie('RBAD');

// ------- FUNCTIONS  ------- //

function cookieCount() {
	if (pageCount == null) {
		$.cookie('PC', '1', { path: '/', domain: 'investmentnews.com', expires: 1, secure: false });
	}
	else {
		$.cookie('PC', newcount, { path: '/', domain: 'investmentnews.com', expires: 1, secure: false });
	}
}
function setRegistrationPromptCookie() {
	if (activeLoginCookie == null) { // user not logged-in
		if (activeRegistrationPromptCookie == null){ // registration has not already been displayed
			if (pageCount == 5){ // after 6 pageviews
				if ((activeRoadblockCookie == 1) || (activeRoadblock == 0)) {
					registrationPopup(); // activate popup
					$.cookie('REGPROMPT', '1', { path: '/', domain: 'investmentnews.com', expires: 1, secure: false }); // set cookie
				}
			}
		}
	}
}
function registrationPopup() {
	var newRegisterURL = $('a.register').attr('href') + window.location.href
	var newForgotURL = $('a.forgotpass').attr('href') + window.location.href

	$('a.register').attr('href', newRegisterURL);
	$('a.forgotpass').attr('href', newForgotURL);
	$('input.currenturl').val(window.location.href);
	$.fn.colorbox({href:"#login_popup", inline:true, open:true, scrolling:false});
}

// roadblock advertisement
function roadBlock() {
	$.fn.colorbox({iframe:true, scrolling:false, innerWidth:"640px", innerHeight:"480px", initialHeight:"400", href:"http://www.investmentnews.com/misc/roadblock.html", overlayClose:false, onOpen:function close() {
		setTimeout("closeRoadblock()",13000);
		$('#closeRoadblock').fadeIn('slow');
		}, onCleanup:function closeTopBar() {
		$('#closeRoadblock').fadeOut('fast');
	}
	});
	$.cookie('RBAD', '1', { path: '/', domain: 'investmentnews.com', expires: 1, secure: false });
}
function closeRoadblock() {
	parent.$.fn.colorbox.close()
	$('#closeRoadblock').fadeOut('fast');
}	

