var NewsletterSignup = {
  init: function(delay){
    this.delay = delay;
    this.expire = new Date; this.expire.setFullYear(this.expire.getFullYear()+10);
    this.visit = Cookie.get('nlsv') || Cookie.set('nlsv', 0, {'expires': this.expire, 'path':'/q'});
    var now = (new Date).getTime();
    var timestamp = Cookie.get('nlst') || Cookie.set('nlst', now, {'path':'/'});
    var duration = (now - timestamp);
    
    if ((duration < delay) && 
        (this.visit < 2) &&
        (document.location.protocol === 'http:')){
          setTimeout('NewsletterSignup.show()', (delay-duration));
    }
  },
  show: function(){
    if ($('#TB_overlay').is(':visible')){
      Cookie.set('nlst', (new Date).getTime(), {'path':'/'})
      NewsletterSignup.init(this.delay);
    }else{
    	if (Math.floor(Math.random()*2)) {
    		tb_show('', '#TB_inline?width=632&height=400&inlineId=nls&modal=true'); // thick-box
    		$('#TB_window').css({'background-color': 'transparent', 'border': 'none'});
    		pageTracker._setCustomVar(1,"Saw Newsletter Pop","Yes",2);
    		pageTracker._trackEvent('Newsletter','Popup Display');   		
    	} else {
    		pageTracker._setCustomVar(1,"Saw Newsletter Pop","No",2);
    		pageTracker._trackEvent('Newsletter','No Pop Display');   		
    	}
    	
    }
    
    Cookie.set('nlsv', (parseInt(this.visit)+1), {'expires': this.expire, 'path':'/'});
  },
  submit: function() {
	  if (this.validEmail($('#nls-emailaddress').val()))
	  {
		  $('#nls-error').html('');
		  $.post('/newsletter/signup', {email: $('#nls-emailaddress').val()}, function(data){ $('.nls .body').html(data); $('.nls .bottom').html(''); });
	  }
	  else $('#nls-error').html('The email address you entered is invalid.  Please verify your email address below.');
  },
  validEmail: function(emailAddress){  
		   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);  
		   return pattern.test(emailAddress);   
  },
  dismiss: function(){
    tb_remove(); // thick-box
  }
};

var Cookie = {
  get: function(name) {
    var name = escape(name) + '=';
    if (document.cookie.indexOf(name) >= 0) {
      var cookies = document.cookie.split(/\s*;\s*/);
      for (var i = 0; i < cookies.length; i++)
        if (cookies[i].indexOf(name) == 0)
          return unescape(cookies[i].substring(name.length, cookies[i].length));
    }
    return null;
  },

  set: function(name, value, options) {
    var newcookie = [escape(name) + "=" + escape(value)]
    if (options) {
      if (options.expires) newcookie.push("expires=" + options.expires.toGMTString());
      if (options.path)    newcookie.push("path=" + '/');
      if (options.domain)  newcookie.push("domain=" + options.domain);
      if (options.secure)  newcookie.push("secure");
    }
    document.cookie = newcookie.join('; ');
    return escape(value);
  }
}
