jQuery(function(){
	
	initForgotPassword();
	initSubscribeHide();
	initStandardTerms();
	initTopSubscribeSubmit();
	initInactiveSubscribeSubmit();
	initWidgetSubscribeSubmit();
	initFacebox();
	
	// CHECKOUT
	$('#checkout-card-select').change(function() {
		if($(this).val() == "new_card")
			$("#checkout-new-card").show();
		else
			$("#checkout-new-card").hide();
	});
	
	$('#checkout-sign-in-link').click(function() {
		$('#checkout-sign-in-prompt').hide();
		$('#checkout-new-user').hide();
		$('#checkout-sign-in').show();
	});
	
	$('#checkout-new-user-link').click(function() {
		$('#checkout-new-user').show();
	});
	
	// CHECKOUT QUANTITY
	$('#checkout-quantity-select').change(function() {
		var price = $.trim($("#checkout-price").html()); 
		price = parseInt(price.substring(1,price.length));
		var quantity = $(this).val();
		$('#grand_total').text('$'+(price*quantity).toFixed(2));
		$('#subprice').text('$'+(price*quantity).toFixed(2));
		$('#checkout-quantity-hidden').val(quantity);
		$('#checkout-login-quantity-hidden').val(quantity);
	});
	
	// REFUND CATEGORY
	$('#refund_category').change(function() {
		var cat = $(this).val();
		if (cat == "closed" || cat == "denied") {
			$("#refund_warning").show();
		} else {
			$("#refund_warning").hide();
		}
	});

})

function initStandardTerms() {
  $('#standard-terms-link').click(function () { 
    $('#standard-terms-list-item').hide();
    $('.standard-term').show();
  });
}

function initFacebox() {
  $('a[rel*=facebox]').facebox();
}

function dealcoopSubscribe(email, slug, division){
  var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
  var alert_base = '/vendor/alert?slug='+slug+'&division='+division;
  if (!pattern.test(email)) {
    //jQuery.facebox('<h1>Invalid E-mail!</h1><p>Please provide a valid e-mail address and re-submit to begin receiving deal alerts.</p>')
    jQuery.facebox({ ajax: alert_base+'&event=invalid' });
    return false
  }
  data = 'email=' + email + '&slug=' + slug + '&division=' + division;
  
  $.ajax({
        
    //this is the file that processes the data
    url: "/subscriber/create", 
     
    //GET method is used
    type: "GET",
  
    //pass the data         
    data: data,     
     
    //Do not cache the page
    cache: false,
     
    //success
    success: function (ret) {              
      
      if (ret==1) {                  
        //jQuery.facebox('<h1>Subscribed!</h1><p>Now just sit back and relax, deal alerts will be delivered to your inbox soon.</p>')
        jQuery.facebox({ ajax: alert_base+'&event=success' });
      } else {
        //jQuery.facebox('<h1>Something Went Wrong</h1><p>Sorry, please try again. You are so close!</p>')
        jQuery.facebox({ ajax: alert_base+'&event=error' });
      }               
    }
      
  });
  
  return true;
}

function forgotPasswordXhr(email){
  
  $.ajax({
        
    //this is the file that processes the data
    url: "/store/forgot_password_xhr", 
     
    //GET method is used
    type: "POST",
  
    //pass the data         
    data: 'email=' + email,     
     
    //Do not cache the page
    cache: false,
     
    //success
    success: function (ret) {              
      if (ret==1) {                  
				jQuery.facebox('<h2>Success!</h2><p>Please check your e-mail for your new password. Use it to sign-in before purchasing (directly on your left). We highly recommend logging into your Account area and changing your password after purchase.</p>')
      } else {
        jQuery.facebox('<h2>E-mail Not Found</h2><p>We could not find an account in the system with that e-mail address. Please create a new account below while checking out. Thanks!</p>')
      }               
    } 
  });
  return true;

}

function initForgotPassword() {
	$('#get-new-password').click(function() {
		var email = $('#get-new-password-email').val();
		forgotPasswordXhr(email);
	});
}

function initTopSubscribeSubmit(){
  $('#top-subscribe').click(function () { 
    var email = $('#top-email').val();
    var division = $('#top-division').val();
    var slug = $('#top-slug').val();
    if (dealcoopSubscribe(email, slug, division)) {
			// NEW DESIGN
			if ($("#topbar").length) {		
	    	var _holder = jQuery("#topbar"),
			    _main = jQuery('#topbar'),
			    _speedHide = 400;
	        _holder.slideUp( _speedHide );
	        _main.animate({ paddingTop: '-=73px'}, {queue: false, duration: _speedHide});
			}
			// FIRST DESIGN
			if ($('#header .subscribe').length) {				
	    	var _holder = jQuery('#header .subscribe'),
			    _main = jQuery('#wrapper'),
			    _speedHide = 400;
	        _holder.slideUp( _speedHide );
	        _main.animate({ paddingTop: '-=69px'}, {queue: false, duration: _speedHide});
			}
    }
    return false
  });
}
function initInactiveSubscribeSubmit(){
  $('#inactive-subscribe').click(function () { 
    var email = $('#inactive-email').val();
    var slug = $('#inactive-slug').val();
    var division = $('#inactive-division').val();
    dealcoopSubscribe(email, slug, division);
    return false;
  });
}
function initWidgetSubscribeSubmit(){
  $('#widget-subscribe').click(function () { 
    var email = $('#widget-email').val();
    var slug = $('#widget-slug').val();
    var division = $('#widget-division').val();
    dealcoopSubscribe(email, slug, division);
    return false;
  });
}

// initSubscribeHide
function initSubscribeHide(){
	
	var bar_height = 69;
	
	// NEW DESIGN
	if($('#topbar').length) {
		var _holder = jQuery('#topbar'),
			_btnClose = jQuery('.close', _holder),
			_main = jQuery('#topbar'),
			_speedHide = 250;
		bar_height = 72;		
	}
	
	// OLD DESIGN
	if($('#header .subscribe').length) {		
		var _holder = jQuery('#header .subscribe'),
			_btnClose = jQuery('.close', _holder),
			_main = jQuery('#wrapper'),
			_speedHide = 250;
	}
	
	// CLOSE ON CLICK
	_btnClose.click(function(){
		_holder.slideUp( _speedHide );
		_main.animate({ paddingTop: '-='+bar_height+'px'}, {queue: false, duration: _speedHide});
		return false;
	})

}
