var BELOJS = (typeof BELOJS === "object") ? BELOJS : {};

BELOJS.reg = {
	getnow:function() {
		return Number(new Date().getTime());
	},
	init:function(token) {
		younews.init(token);
	},
	pagecount:{
		get:function() {
			var pagecount = Number(younews.readcookie("BELO_PageCount"));
			var countts = younews.readcookie("BELO_CountTS");
			if(pagecount == null || countts == null) {
				BELOJS.reg.pagecount.reset();
				pagecount = 1;
			}
			return pagecount;
		},
		reset:function() {
			var now = BELOJS.reg.getnow();
			younews.setcookie("BELO_PageCount",1);
			younews.setcookie("BELO_CountTS",now);
		},
		increment:function() {
			var pagecount = Number(younews.readcookie("BELO_PageCount"));
			var countts = Number(younews.readcookie("BELO_CountTS"));
			// Get Current Timestamp
			var now = BELOJS.reg.getnow();
			// Expire in 30 Minutes
			var deadtime = (30-((now-countts)/1000/60));
			if(pagecount == null || countts == null || deadtime < 0) {
				BELOJS.reg.pagecount.reset();
				pagecount = 0;
			}
			younews.setcookie("BELO_PageCount",pagecount+1);
		}
	},
	optout: {
		get:function() {
			var optout = younews.readcookie("BELO_OptOut");
			return (optout)?true:false;				
		},
		set:function() {
			younews.setcookie("BELO_OptOut","true",30);
		},
		clear:function() {
			younews.setcookie("BELO_OptOut","");
		}
	}
};

BELOJS.winRefresh = {
	init: function(int) {
		if (typeof(int) === "number") {
			this.refreshInt = int;
			this.startTime = new Date().getTime();
			this.staticInt = int;
			this.run();	
		}
	},
	pause: function() {
		window.clearInterval(this.refreshId);
		this.refreshInt = this.refreshInt - (new Date().getTime() - this.startTime);
    },
	refresh: function() {
		window.location.reload(true);
	},
	restart: function() {
		var fnContext = this;
		this.refreshId = window.setTimeout(function(){
			fnContext.refresh();
			fnContext.refreshInt = fnContext.staticInt;
			fnContext.run();
		}, this.refreshInt);
	},
	run: function() {
		if (this.refreshId !== "undefined") {
			window.clearInterval(this.refreshId);
		}
		var fnContext = this;
		this.refreshId = setTimeout(function() {
			fnContext.refresh();
		}, this.refreshInt);
	}
};
