$(document).ready(function () {
	
	$(".promotionlink").click(function (e) {
		$("#opaque").show();
		var l = Math.floor($(window).width() / 2) - Math.floor($("#specialoffer").width() / 2);
		var h = Math.floor($(window).height() / 2) - Math.floor($("#specialoffer").height() / 2);
		$("#specialoffer").css({'top':h + 'px','left':l + 'px'}).show();
		
		$.post('/home/',{action:'windowopen'},function (data) { }, "text");
		return false;
	});
	
	$("#specialoffer a").click(function () {
		$("#specialoffer").hide();
		$("#opaque").hide();
	});
	
	
	$("form").submit(function () {
		var ok = true;
		var vars = 'action=getpromotion';
		$('input').each(function () {
			if ($(this).val() == '') {
				$(this).addClass('emailnotfilled');
				ok = false;
			} else {
				vars += '&' + $(this).attr('name') + '=' + $(this).val();
			}
		});
		if (ok) {
			//TODO: RESET to original state incase POST did not succeed.  
			$("div","#specialoffer").hide();
			$("#specialoffer").css({'padding-left':'0','padding-right':'0','padding-top':'0','padding-bottom':'0'});
			$("#specialoffer_waiting").show().children().show();
			$.post('/home/',vars,function (data) { 
				switch(data) {
					case 'FAIL':
						alert('An error has occured, page will reload now');
						window.location = '/home/';
						break;
					case 'OK':
						$("#specialoffer").hide().remove();
						alert('You should receive your promo code in your inbox momentarily. Please check your SPAM folder if you can\'t find it.');
						$("#opaque").hide().remove();
						$("#logo a").removeClass("promotionlink").addClass("standardlogo").children("img").remove().andSelf().text('Svveet Design - More Than a Graphic Design Agency - Design &amp; Consulting');
						$("#logo").removeClass("wblogo").addClass("homepage");	
				
						break;
					default:
						alert('Error: ' + data);
						$("div","#specialoffer").show();
						$("#specialoffer_waiting").hide();
						$("#specialoffer").css({'padding-left':'20px','padding-right':'15px','padding-top':'0','padding-bottom':'25px'});
						break;
				}
				return false;
			},"text");
		}
		return false;
	});
});
