LW.campaign = LW.campaign || {
	IN_WELCOME_PAGE: false,
	init: function () {
		$('#campaign_welcome .close_greyout, #campaign_welcome .close_campaign_welcome').click(function (event) {
			event.preventDefault();
			LW.campaign.hideWelcome();
		});
		$('#coupon_email').focus(function () {
			var self = $(this);
			if (!self.is('.lookedat')) {
				self.addClass('lookedat');
				self.val('');
				self.css({color: 'black'});
			}
		});
		$('#get_coupon form').submit(function (event) {
			event.preventDefault();
			if ( $('#coupon_email').val() == 'emailaddress@domain.com' || $('#coupon_email').val() == '' ) {
				LW.validation.removeHighlight($('#coupon_email'));
				LW.validation.highlight('#coupon_email', 'Please enter your email address.');
				return false;
			}

			if (!LW.validation.validateInput('#coupon_email', { type: 'email', required: true })) {
				return false;
			}

			var campaign = LW.campaign.getCampaignCookie();
			if ( ! LW.campaign.cookieCheck('LW_CAMPAIGN') ) {
				return false;
			}

			var vars = {
				email: $('#coupon_email').val(), 
				code:  campaign.code
			};

			var amount = campaign.amount;

			$('#get_coupon').fadeOut('slow', function () {
				$('#processing').fadeIn('slow');

				$.postJSON(LW.vars.jsonPath + '/campaign/coupon/add.html', vars, function (res) {
					if (res.error_class) {
						$('#processing').hide();
						$('#get_coupon').show();

						LW.validation.removeHighlight($('#coupon_email'));
						
						if (res.error_class == 'LW::Exception::DuplicateRecord') {
							LW.validation.highlight('#coupon_email', 'This email address has already gotten a coupon.');
						}
						else if (res.error_class == 'LW::Exception::Resource::Unavailable') {
							LW.validation.highlight('#coupon_email', "We're sorry, but we're out of coupons!");
						}
						else if ( res.error_class == 'LW::Exception::Input::Multiple' ) {
							LW.validation.highlight('#coupon_email', "Please check your email address and try again.");
						}
						else if ( res.errors ) {
							LW.DHTML.popupError(LW.error.format(res.errors));
						}
						return;
					}

					LW.campaign.deleteCampaignCookie();
					LW.campaign.setCouponCookie(res.code, amount, vars.email);
		
					LW.signup.coupon_code  = res.code;
			                LW.signup.coupon_email = vars.email;

					$('#processing').fadeOut('slow', function () {
						$('#done').fadeIn('slow');
					});
				});
			});

			return false;
		});

		$('.toggle_coupon_drawer').click(function (event) {
			event.preventDefault();
			LW.campaign.toggleCouponDrawer();
		});

		$('#coupon_finished').click(function (event) {
			event.preventDefault();
			LW.campaign.toggleCouponDrawer();
		});

		$('#signup_from_coupon').click(function (event) {
			event.preventDefault();
			LW.campaign.toggleCouponDrawer();
			LW.signup.show();
		});

		var campaign = LW.campaign.getCampaignCookie();
		var coupon   = LW.campaign.getCouponCookie();

		if (campaign) {
			if (!coupon && !LW.campaign.IN_WELCOME_PAGE) {
				LW.campaign.showCampaignTag(campaign);
			}
		}
		if (coupon) {
			LW.campaign.showCouponTag(coupon);
			LW.signup.coupon_code  = coupon.code;
			LW.signup.coupon_email = coupon.email;
		}
		LW.event.fire('campaign.loaded');
	},
	toggleCouponDrawer: function () {
		var drawer = $('#coupon_drawer');
		if (drawer.is('.open')) {
			drawer.animate(
				{ top: '-71px' },
				'slow',
				'swing'
			);
			var img = drawer.find('#get_coupon_tag .arrow');
			var src = img.src().replace('-up', '-dn');
			img.src(src);
			drawer.removeClass('open');
		}
		else {
			drawer.animate(
				{ top: '0px' },
				'slow',
				'swing'
			);
			var img = drawer.find('#get_coupon_tag .arrow');
			var src = img.src().replace('-dn', '-up');
			img.src(src);
			drawer.addClass('open');
		}

		if ($('#campaign_welcome').is(':visible')) {
			LW.campaign.hideWelcome();
		}
	},
	showWelcome: function (callback) {
		var campaign = LW.campaign.getCampaignCookie();

		$('.coupon_amount').text(campaign.amount);

		LW.DHTML.greyOut('slow');
		$('#campaign_welcome').slideDown('slow', function () {
			$('#coupon_drawer').fadeIn('slow').css({'z-index': 6000});
			$('#thepeak').fadeIn('slow');
			if (callback) {
				callback();
			}
		});
	},
	hideWelcome: function () {
		LW.DHTML.unGreyOut('slow');
		$('#campaign_welcome').slideUp('slow', function () {
			$('#get_coupon_tag').css({'z-index': 4});
		});
	},
	showCampaignTag: function (campaign) {
		$('.coupon_amount').text(campaign.amount);
		$('#coupon_drawer').show();
	},
	showCouponTag: function (coupon) {
		$('.coupon_amount').text(coupon.amount);
		$('#coupon_drawer').show();
		$('#get_coupon').hide();
		$('#done').show();
	},
	hideCouponTag: function () {
		$('#coupon_drawer').hide();
	},
	setCouponCookie: function (code, amount, email) {
		$.cookie("LW_CAMPAIGN_COUPON", [code, amount, email].join('|'), {
			path: '/',
			expires: 90
		});
		LW.campaign.cookieCheck("LW_CAMPAIGN_COUPON");
	},
	getCouponCookie: function () {
		var raw  = $.cookie('LW_CAMPAIGN_COUPON');

		if (raw) {
			var data = {};
			raw = raw.split('|');
			data.code   = raw[0];
			data.amount = parseFloat(raw[1]);
			data.email  = raw[2];
			return data;
		}

		return false;
	},
	deleteCouponCookie: function () {
		$.cookie("LW_CAMPAIGN_COUPON", null, { path: '/' });
	},
	setCampaignCookie: function (code, amount) {
		$.cookie("LW_CAMPAIGN", [code, amount].join('|'), {
			path: '/',
			expires: 90
		});
		LW.campaign.cookieCheck('LW_CAMPAIGN');
	},
	getCampaignCookie: function () {
		var raw  = $.cookie('LW_CAMPAIGN');

		if (raw) {
			var data = {};
			raw = raw.split('|');
			data.code   = raw[0];
			data.amount = parseFloat(raw[1]);
			return data;
		}

		return false;
	},
	deleteCampaignCookie: function () {
		$.cookie("LW_CAMPAIGN", null, { path: '/' });
	},
	cookieCheck: function ( cook ) {
		if ( ! $.cookie(cook) ) {
			LW.DHTML.popupError('You must enable your browser cookies to redeem a Storm coupon.  Please enable cookies and reload this page to redeem your coupon.');
			return false;
		}
		return true;
	}
};

$(function () {
	LW.campaign.init();
});


