
function createWrapper()
{
	var _body = $(".index");
	
		if($("#popupWrapper").attr("id")) $("#popupWrapper").remove();
		_body.prepend('<div id="popupWrapper"></div>');
		
		var popupWrapper = $("#popupWrapper");
		popupWrapper.css({display:"none", height:$(window).height()+"px", width:$(window).width()+"px", background:"#000000", position:"fixed", opacity:"0.5", display:"none", zIndex:"10"});
		
}

function createPopupBox()
{
	var popupWrapper = $("#popupWrapper");
	
	popupWrapper.after('<div id="popupBox"><img id="close-btn" src="./images/close.png" /><div id="responseDiv"></div></div>');
	
	var popupBox = $("#popupBox");
	var closeBtn = $("#close-btn");
	var responseDiv = $("#responseDiv");
	
	popupBox.css({position:'fixed', display:"none", zIndex:"11", width:"400px", height:"160px", background:"url(./images/ajax-loader.gif) no-repeat center center #FFFFFF", border:"1px solid #777070"});
	popupBox.css({"margin-top":($(window).height() - 160)/2  + 'px', left:($(window).width() - 400)/2  + 'px'});
	
	responseDiv.css({float:"left", height:"80px", width:"300px", background:"url(./images/info.png) no-repeat #FFFFFF 20px 15px", display:"none", "text-align":"left","padding":"20px 20px 10px 80px", "font-size":"12px", "color":"#777070"});
	
	closeBtn.css({float:"right", marginTop:"-10px", marginRight:"-10px"});
	closeBtn.bind("click", function(){togglePopupBox('hide')});
	closeBtn.bind("mouseover", function(){$(this).css({cursor:"pointer"})});
	
	popupBox.fadeIn('fast');
}

function togglePopupBox(trigger)
{
	if(trigger=="show")
	{
		createWrapper();
		$("#popupWrapper").fadeIn("fast",function(){createPopupBox()});
	}
	else
	{
		var popupWrapper = $("#popupWrapper");
		var popupBox = $("#popupBox");
		
		popupWrapper.fadeOut("fast", function()
		{
			if($("#popupWrapper").attr("id")) $("#popupWrapper").remove();
		});
		
		popupBox.fadeOut("slow", function()
		{
			if($("#popupBox").attr("id")) $("#popupBox").remove();
		});			
	}
}

function initEvents()
{
	var inputCollect = $("#popupbox-form input[type=text]");
	
	jQuery.each(inputCollect, function()
	{
		$(this).bind('click', function()
		{
			if($(this).val().trim()=="email" || $(this).val().trim()=="name")
			{
					$(this).val("");
			}				
		});
		
		$(this).bind('blur', function()
		{
			if($(this).val().trim()=="")
			{
					if($(this).attr("name")=="email") $(this).val("email");
					else $(this).val("name");
			}				
		});		
	});
	
	$("#popupbox-form input[type=button]").bind('click', function(){submitEmail()});

}

function submitEmail()
{	
	togglePopupBox('show');
	$.post("store-data.php", $("#popupbox-form").serialize(),function(data)
	{
		$("#responseDiv").html(data);
		$("#responseDiv").fadeIn("fast");
	});
}

function verifyCode()
{	
	togglePopupBox('show');
	$.get("verify.php", {vcode:$("#vcode").val()},function(data)
	{
		$("#responseDiv").html(data);
		$("#responseDiv").fadeIn("fast");
	});
}

$(document).ready(function()
{
	initEvents();
	
	if($("#vcode").attr("id"))
	{
		if($("#vcode").val()!="")
		{
			verifyCode();
		}
	}
});


