$(function(){
    $('#subscribe-button').click(function(){
    	doSubscribe();
    });
    // Blur on focus
    $('a').focus(function() { this.blur(); });
    $('input[@type=text], input[@type=password]').addClass("input");
    
    $("#subscribe-text").focus();
	
	$('a').hover(
		function()
		{
			$(this).addClass("hover");
		},
		function()
		{
			$(this).removeClass("hover");
		}
	);
	$('input#submit').hover(
		function()
		{
			$(this).addClass("hover");
		},
		function()
		{
			$(this).removeClass("hover");
		}
	);
	$('#comment').after('<div id="chars-remaining"><span id="chars-text"><span id="char-count">500</span> Characters Remaining</span></div>');
	$('#comment').keyup(function() {
		if ($(this).val().length > 500){
			$('#chars-remaining #chars-text').addClass('warning');
			$('#chars-remaining #char-count').text("0");
		} else {
			$('#chars-remaining #chars-text').removeClass('warning');
			$('#chars-remaining #char-count').text(500-($(this).val().length));
		}
	});
	$('#amount').before('$ ');
	/*$('#agreeterms-label').click(function() {
        return false;
	});*/
	
	$('label.required').after(' *');
	
	$('a img').parent().addClass('nobackground');
	$('a h1').parent().addClass('nobackground');

	$('.stripe tr:even').addClass('alt');
	
});

function doSubscribe() {
    var dataString = 'subscribe_to_news=true&formName=Subscribe_Form';
    dataString += '&primaryEmail=' + document.getElementById("subscribe-text").value;
    
    $.ajax({
            url: '/contacts/subscribe/index',
            type: 'POST',
            data: dataString,
            success: subscribeSuccess
    });
    
    return false;
}

function subscribeSuccess(data) {
    document.getElementById("subscribe-feedback").innerHTML = "<p>" + data + "</p>";
    document.getElementById("subscribe-text").value = '';
}

// If enter is pressed inside the text filter box, execute filter
function checkEnter(e) { 
	var characterCode;

	if (e && e.which){
		e = e;
		characterCode = e.which;
	}
	else {
		e = event;
		characterCode = e.keyCode;
	}

	if (characterCode == 13){
		doSubscribe();
		return false;
	}
	else{
		return true;
	}

}

function clearSearch(i, def){
    if( $(i).val() == "" ){
        $(i).val(def);
        $(i).addClass('inactive');
    }else if( $(i).val() == def ){
        $(i).val('');
        $(i).removeClass('inactive');
    }else{
        $(i).select();
    }
}
