$(document).ready(function(){
	if (readCookie('eighteen')=='ok') { }
	else {
		dimAndBox();
		scrolled();
		$('#dim, #dimclose').live('click',hideDim);
		showDim();
	}
});


function scrolled() {
	var viewportHeight = window.innerHeight ? window.innerHeight : $(window).height();
	var viewportWidth = window.innerWidth ? window.innerWidth : $(window).width();
	dimAndBox(viewportWidth, viewportHeight, $(window).scrollTop());
}

function dimAndBox(vw_, vh_, st_) {
	var h_ = $('#amtech').outerHeight();
	$('#dim').height(h_);
	if (vh_) {
		var bh_ = $('#dimbox').height();
		var bw_ = $('#dimbox').width();
		var posTop = (vh_-bh_)/2;		
		var posLeft = (vw_-bw_)/2;
		$('#dimbox').css({top:(posTop+st_)+'px',left:posLeft+'px'});
	}
}

function copyContents(from, to) {
	$('#'+to).html('<div id="closedim">X</div>');
	$('#'+from).clone(true).appendTo('#'+to);
}

function showDim(showClose) {
	$(window).scroll(scrolled);
	$(window).resize(scrolled);
	scrolled();
	$('#dim, #dimbox').fadeIn(500);
	if (showClose) {
		$('#closedim').fadeIn(500);
	}
	//$('#dimbox').children().removeClass('din');
	scrolled();
}
function hideDim() {
	createCookie('eighteen', 'ok', 666);
	$('#dim, #dimbox').fadeOut(500);
	$(window).unbind('scroll');
	$(window).unbind('resize');
}

/* ---------------------------------------
 * 
 * 
 * 888888888888888888888888888888888888888
 */

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
