//------------------------------------
//	POPUP.JS
//	Author: 	Christian Thomas
//	Version:	0.1
//------------------------------------

$(function() {

	// Reset Cookie
	// $.cookie('lowry-popup', null);

	c = $.cookie('lowry-popup');
	
	// Only perform functions if cookie is not set to complete
	if(c != 'complete'){
		
		// Show the voucher after 3000ms
		showVoucher = $.timer(3000, function() {

			$('div#overlay').fadeIn(function(){

				$(this).find('input:first').focus();

			});

		});
	
		// CLOSE
		$('#reward_popup .close').live('click', function(){

			$(this).closest('#overlay').fadeOut();

			// Set cookie if user closes voucher
			$.cookie('lowry-popup', 'complete', { expires: 7 });

			return false;
		});

		// SUBMIT
		$('#reward_popup form').submit(function(){
			
			$this = $(this);
			
			$this.find('input').each(function(){

				if($(this).val() == '' || $(this).val().length <=2){
					$(this).addClass('error');
				}else{
					$(this).removeClass('error');
				}

				if($(this).attr('type') == 'email') {
				
					email = checkemail($(this).val());

					if(email == false || email == null){
						$(this).addClass('error');
					}

				}
				
			});

			// If there's no errors... Continue...
			if(!$this.find('.error').length){
				
				$.ajax({
					url: $this.attr('action') + '?callback=?',
					crossDomain: 'true',
					data: $this.serialize(),
					dataType: 'jsonp',
					success: function(data, test, test2){
						
						$fs = $('#reward_popup form fieldset');

						$fs.css({
							height: $fs.height()
						}).animate({
							opacity: 0
						}, 200, function(){
							
							$fs.html('<h2>Thank you</h2><p>We&rsquo;ve sent you an email with details on <br />Lowry&rsquo;s Loyalty Scheme</p><a href="#" class="close">Close this window</a>').animate({
								opacity: 1
							});

						})

					},
					error: function(data){
						console.log("ERROR: "+data);
					}
				});
				
			}
			
			return false;
			
			
		});
		
	}
	
});

//////////////////////////
// REGULAR EXPRESSIONS

// EMAIL

function checkemail(email_address) {
	email_address = email_address.toLowerCase();
	return email_address.length > 5 && email_address.match(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/);
}

//////////////////////////
// EASING

jQuery.extend(jQuery.easing,{
	easeInOutExpo: function(x,t,b,c,d){
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	},
	easeInExpo: function (x, t, b, c, d) {
		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	},
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	}
});


//////////////////////////
// TIMERS

jQuery.timer = function(time,func,callback){
	var a = {timer:setTimeout(func,time),callback:null}
	if(typeof(callback) == 'function'){a.callback = callback;}
	return a;
};

jQuery.clearTimer = function(a){
	clearTimeout(a.timer);
	if(typeof(a.callback) == 'function'){a.callback();};
	return this;
};


//------------------------------------
//
//	Cookie plugin
//
//	Copyright (c) 2006 Klaus Hartl (stilbuero.de)
//	Dual licensed under the MIT and GPL licenses:
//	http://www.opensource.org/licenses/mit-license.php
//	http://www.gnu.org/licenses/gpl.html
//
//------------------------------------

eval(function(p,a,c,k,e,d){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--){d[e(c)]=k[c]||e(c)}k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}('o.5=q(b,9,2){7(h 9!=\'x\'){2=2||{};7(9===j){9=\'\';2=$.v({},2);2.3=-1}4 3=\'\';7(2.3&&(h 2.3==\'m\'||2.3.l)){4 6;7(h 2.3==\'m\'){6=t u();6.s(6.w()+(2.3*r*p*p*z))}k{6=2.3}3=\'; 3=\'+6.l()}4 8=2.8?\'; 8=\'+(2.8):\'\';4 a=2.a?\'; a=\'+(2.a):\'\';4 c=2.c?\'; c\':\'\';d.5=[b,\'=\',E(9),3,8,a,c].y(\'\')}k{4 e=j;7(d.5&&d.5!=\'\'){4 g=d.5.F(\';\');D(4 i=0;i<g.f;i++){4 5=o.C(g[i]);7(5.n(0,b.f+1)==(b+\'=\')){e=B(5.n(b.f+1));G}}}A e}};',43,43,'||options|expires|var|cookie|date|if|path|value|domain|name|secure|document|cookieValue|length|cookies|typeof||null|else|toUTCString|number|substring|jQuery|60|function|24|setTime|new|Date|extend|getTime|undefined|join|1000|return|decodeURIComponent|trim|for|encodeURIComponent|split|break'.split('|'),0,{}))


	
