var inLinePUDone = false;
var inLinePUCode = 'inLinePU11';
var inLinePUCodeB = 'inLinePU811';
var inLineTimeStart = 0;
var inLineDelayCancel = false;

function startInLinePU(offset) {
	window.setTimeout("runTimer()",5000);
	inLineTimeStart = getUTime() - offset;
}

function runTimer() {
	if (inLinePUDone==false) {
		if ($.cookie(inLinePUCode) == null && $.cookie(inLinePUCodeB) == null) {
			var inLineTimeNow = getUTime();
			if (inLineTimeNow - inLineTimeStart > 60) {
				inLinePUDone = true;
				inLinePU(1);
			} else {
				window.setTimeout("runTimer()",5000);

			}
		} else {
			inLinePUDone = true;
		}
	}
}

function inLinePU(opt) {
	if (opt==1) {
		checkForDodgyIE6Elements("hidden");

		positionInLinePU();

		$('#fade').show();
		$('#popupdiv').slideDown(500);

		$(window).resize(function(){
		try {
			$('.nockBack').css({
				'height': $(document).height(),
				'width': $(window).width()
			});
			positionInLinePU();
			} catch(e){}
		});

		$(window).scroll(function(){
		try {
			$('.nockBack').css({
				'height': $(document).height(),
				'width': $(window).width()
			});
			positionInLinePU();
			} catch(e){}
		});

	} else {
		if (!inLineDelayCancel) {
			$('#popupdiv').slideUp(500,function(){
				$('#fade').hide();
			});
        	checkForDodgyIE6Elements("visible");
			$(window).unbind("resize");
			$(window).unbind("scroll");
			if (opt==2) {
				// user clicked close
				$.cookie(inLinePUCode, '1', {expires: 1825});
				inLinePUDone = true;
				inLineDelayCancel = true;
			}
		}
	}
}

function positionInLinePU() {
	var fadeHeight = $(document).scrollTop() + $(window).innerHeight();
	$('#fade').css('height',fadeHeight+'px');

	var posTop = $(document).scrollTop() + ($(window).innerHeight() / 2) - ($('#popupdiv').height() / 2);
	var posLeft = $(document).scrollLeft() + ($(window).innerWidth() / 2) - ($('#popupdiv').width() / 2);
	$('#popupdiv').css('top',posTop+'px');
	$('#popupdiv').css('left',posLeft+'px');
}

function delayHideInLinePU() {
	window.setTimeout("inLinePU(2)",5000);
}

function checkForDodgyIE6Elements(strVisibility) {
    if (isSetAsIE6) {
        $("select").css("visibility", strVisibility);
        $("input:radio").css("visibility", strVisibility);
        $("input:checkbox").css("visibility", strVisibility);
    }
}

function getUTime() {
	return Math.floor(new Date().getTime()/1000);
}
