$(function(){

	if($('#login').length && $.cookie('suppress_optin') == null && $('.city_state:last').text() != 'your area') { //open automatically for those w/o cookie set (first timers)
		
		$("#newsletter").colorbox({
			open: true, 
			opacity:.75,
			onComplete:function(){
			
				$("#form-subscribe").bind("submit", function(e){
					e.preventDefault();
					handleSubscribe();
				});
				
				var city_state = $('.city_state:last').text();
				if (city_state != "your area") {
					$('.city_state').text(city_state);
					$('input[name=location]').val(city_state);
					$('.example').hide();
					$.fn.colorbox.resize();
				}
				$('.close_opt_in').click(function(e){
					e.preventDefault();
					cookieSubscribeClosed();
					$.fn.colorbox.close()
				})},
				onClosed:function(){cookieSubscribeClosed();}
		});	
	
	} else { //they already have the cookie, dont show the opt-in modal automatically
	
		$("#newsletter").colorbox({
			open: false, 
			opacity:.75,
			onComplete:function(){
			
				$("#form-subscribe").bind("submit", function(e){
					e.preventDefault();
					handleSubscribe();
				});
	
				var city_state = $('.city_state:last').text();
				if (city_state != "your area") {
					$('.city_state').text(city_state);
					$('input[name=location]').val(city_state);
					$('.example').hide();
					$.fn.colorbox.resize();
				}
				$('.close_opt_in').click(function(e){
					e.preventDefault();
					cookieSubscribeClosed();
					$.fn.colorbox.close()
				})},
				onClosed:function(){cookieSubscribeClosed();}
		});
	
	}
		
	$("a.subscribe").attr({"href":$("#newsletter").attr('href')}).colorbox({opacity:.75});
	
	$("#form-subscribe").bind("submit", function(e){
		e.preventDefault();
		handleSubscribe();
	});
	
});


function cookieSubscribeClosed() { //set a cookie so we dont bother them again 
	$.cookie('suppress_optin', 'yes', { expires: 10000, path: '/' });
	$('.example').show();
}

function handleSubscribe() { //process user subscription

	 $.post('/block/subscribe', {
        'email': $('input#email').val(),
        'location': $('input#location').val(),
        'radius': $('select#radius').val()
    }, function(data) {
		
		checkColorBox();
        
        if ($(data).find('success').text() == 'true') {
            //console.log('success');
        	
        	if($("#colorbox:visible").length) {
        		$(isColorBox).addClass("success").html('Thank you for signing up! This window will automatically close in 5 seconds.');
            	setTimeout(function(){
            		$.fn.colorbox.close();
            	},5000)
            } else {
            	$(isColorBox).addClass("success").html('Thank you for signing up!');
            }
        	
        } else {
        	$(isColorBox).html('');
            $(data).find('error').each(function() {
                $(isColorBox).append($(this).text() + "<br />");
                //console.log($(this).attr('field') + ' error = ' + $(this).text());
            });
            $("#subscribe p.error").show();
        }
        
        $(isColorBox).show();
		$.fn.colorbox.resize();
            
    });

}


var isColorBox;
var checkColorBox = function(){
	if(!$("#colorbox:visible").length) {
		isColorBox = "#subscribe p.error";
	} else {
		isColorBox = "#colorbox #subscribe p.error";
	}
}
