var iebool = false;
var safbool = false;

var sw = null;
var sh = null;

function ageVerified(ie) {
	var age_holder = document.getElementById("agehold");
	var remove_obj = age_holder.firstChild;
	
	age_holder.removeChild(remove_obj);
	age_holder.style.width = "0px";
	age_holder.style.height = "0px";
	
	if (!ie) {
		document.body.style.overflow = "scroll";
		document.onmousewheel = function() { return true; };
	} else {
		document.documentElement.style.overflow = "scroll";
	}
	
	setCookie(1);
}

function setCookie(expiredays) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = "age_verified=" + ((expiredays==null) ? "" : "expires=" + exdate.toGMTString()) + ";";
}

function getCookie() {
	if (document.cookie.length > 0) {
		var cookiecheck = document.cookie.indexOf("age_verified=");
		if (cookiecheck != -1) {
			return true;
		} else {
			return false;
		}
	}
}

function ageVer() {
	var age_cookie = getCookie();
	if (age_cookie == "" || age_cookie == null) {
		document.body.style.overflow = "hidden";
		document.onmousewheel = function() { return false; };
		
		var age_bg = document.createElement("div");
		var age_ver = document.createElement("div");
		
		var enter_lnk = document.createElement("a");
		var exit_lnk = document.createElement("a");
		
		age_bg.setAttribute("id", "av_bg");
		age_bg.setAttribute("class", "av_mask");
		
		age_ver.setAttribute("id", "av_ct");
		age_ver.setAttribute("class", "av_content");
		
		enter_lnk.setAttribute("class", "enter_a");
		enter_lnk.setAttribute("href", "#");
		enter_lnk.setAttribute("onmousedown", "ageVerified(false)");
		
		exit_lnk.setAttribute("class", "exit_a");
		exit_lnk.setAttribute("href", "http://www.google.com");
		
		document.getElementById("agehold").appendChild(age_bg);
		document.getElementById("agehold").setAttribute("class", "age_hold");
		
		age_bg.appendChild(age_ver);
		age_ver.appendChild(enter_lnk);
		age_ver.appendChild(exit_lnk);
		
		age_ver.style.top = ((sh / 2) - 160) + "px";
		age_ver.style.left = ((sw / 2) - 220) + "px";
	}
}

function ageVer_ie() {
	var age_cookie = getCookie();
	if (!age_cookie) {
		document.documentElement.style.overflow = "hidden";
		
		var age_bg = document.createElement("<div id='av_bg' class='av_mask'>");
		var age_ver = document.createElement("<div id='av_ct' class='av_content'>");
		
		var enter_lnk = document.createElement("<a class='enter_a' href='#' onmousedown='ageVerified(true)'>");
		var exit_lnk = document.createElement("<a class='exit_a' href='http://www.google.com'>");
		
		document.getElementById("agehold").appendChild(age_bg);
		document.getElementById("agehold").setAttribute("class", "age_hold");
		
		age_bg.appendChild(age_ver);
		age_ver.appendChild(enter_lnk);
		age_ver.appendChild(exit_lnk);
		
		age_ver.style.top = ((sh / 2) - 160) + "px";
		age_ver.style.left = ((sw / 2) - 220) + "px";
	}
}
// Dean Edwards/Matthias Miller/John Resig

function init() {
	// quit if this function has already been called
	if (arguments.callee.done) return;

	// flag this function so we don't do the same thing twice
	arguments.callee.done = true;

	// kill the timer
	if (_timer) clearInterval(_timer);
	
	// do stuff
	sw = window.innerWidth != null? window.innerWidth: document.body != null? document.documentElement.clientWidth:null;
	sh = window.innerHeight != null? window.innerHeight: document.body != null? document.documentElement.clientHeight:null;
	
	if (iebool) {
		ageVer_ie();
	} else {
		ageVer();
	}
};

/* for Mozilla/Opera9 */
if (document.addEventListener) {
	document.addEventListener("DOMContentLoaded", init, false);
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
	iebool = true;
	document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
	var script = document.getElementById("__ie_onload");
	script.onreadystatechange = function() {
		if (this.readyState == "complete") {
		init(); // call the onload handler
	}
};
/*@end @*/

/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
	safbool = true;
	var _timer = setInterval(function() {
		if (/loaded|complete/.test(document.readyState)) {
	  	init(); // call the onload handler
		}
	}, 10);
}

window.onload = init;
