var POPUP_FLAG = "itm_popup";

function ShowSignup() {
	var member_signup_id = "member_signup";
	DimPage();
	if ($("#"+member_signup_id).length == 0) {
		$("body").append('<div id="'+member_signup_id+'"></div>');
	}
	scroll(0,0);
	$("#"+member_signup_id).each(function() {
		left_pos = (screen.width/2) - (($(this).width())/2);
		$(this).css({'position' : 'absolute', 'top' : "5px", 'left' : left_pos+"px", 'opacity' : '1'});
		$(this).css("display", "none");
		$(this).load("popup.php?t="+time(), "", function() {
			$(".member_signup_closer").bind("click", function() {
				UnDimPage();
			});
		}).fadeIn(1500);
	});
}
function DimPage() {
	$("body").css("overflow", "hidden");
	$("body").prepend('<div id="overlay_div"></div>');
	$("#overlay_div").css({'position' : 'absolute', 'top' : "0px", 'left' : "0px", 'opacity' : '0.4', 'height' : screen.height, 'width' : screen.width});
}
function UnDimPage() {
	$("body").css("overflow", "auto");
	$("#member_signup").css("display", "none");
	$("#optin_popup").fadeOut(700);
	$("#overlay_div").remove();	
	
	if (window["reload_parent"] == 1) {
		location.reload();
	} 
}
function time() {
	return((new Date()).getTime());
}

function doOptinPopup() {
	itm = getCookie(POPUP_FLAG);
	if (itm == null || itm == "" || itm == undefined) {
		showOptinPopup();
	}
}

function showOptinPopup() {
	
	var optin_popup_id = "optin_popup";
	DimPage();
	if ($("#"+optin_popup_id).length == 0) {
		$("body").append('<div id="'+optin_popup_id+'"></div>');
	}
	scroll(0,0);
	$("#"+optin_popup_id).each(function() {
		left_pos = (screen.width/2) - (920/2);
		$(this).css({'position' : 'absolute', 'top' : "5px", 'left' : left_pos+"px", 'opacity' : '1', 'height' : '100%', 'width' : '920px', 'background-color' : '#fff', 'display' : 'none', 'overflow' : 'auto', 'z-index' : '5000', 'border' : '5px #2D2D2D solid'});
		$(this).load("optin_min/index.php", "", function() {
			
			setCookie(POPUP_FLAG, "1", 14, "/", ".theinternettimemachine.com", "");
			
			$(this).prepend('<div id="optin_popup_closer" style="position:absolute; left:820px; top:40px; z-index:5001; cursor:pointer;"><img src="optin/optin_imgs/close.gif"></div>');
			
			$("#optin_popup_closer").bind("click", function() {
				
				//UnDimPage();
				$("#"+optin_popup_id).load("optin_min/optin_confirm.php");
				$("#"+optin_popup_id).css("height", "400px");
				
				window["reload_parent"] = 1;
			});
			
		}).fadeIn(500);
	});
}

function getCookie( check_name ) {
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ ) {
		a_temp_cookie = a_all_cookies[i].split( '=' );
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		if ( cookie_name == check_name ) {
			b_cookie_found = true;
			if ( a_temp_cookie.length > 1 ) {
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found ) {
		return null;
	}
}

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );	
	/*
	number of days
	*/
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}
