$(document).ready(function(){
	$('.holder input, .holder textarea, .holder select').focus(function() {$(this).closest('.holder').addClass('focus');});
	$('.holder input, .holder textarea, .holder select').blur(function() {$(this).closest('.holder').removeClass('focus');});
	$('.holder .required').blur(function() {
		if ($(this).val().replace(/\s+/) == '') {
			$(this).closest('.holder').addClass('error');
			$(this).siblings('.required_text').fadeIn('fast');
		} else {
			$(this).closest('.holder').removeClass('error');
			$(this).siblings('.required_text').fadeOut('fast');
			if ($(this).attr('id').search(/email/) != -1) {
				if ($(this).val().search(/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i) == -1) {
					$(this).closest('.holder').addClass('invalid');
					$(this).siblings('.invalid_text').fadeIn('fast');
				} else {
					$(this).closest('.holder').removeClass('invalid');
					$(this).siblings('.invalid_text').fadeOut('fast');
				}
			}
		}
	});
	up_next_closed = false;
	if ($('.article').length > 0) {
		$(window).scroll(function() {
			var point_of_interest = $('.article').offset().top + $('.article').height() - $(window).height();
			if (up_next_closed == false && $(this).scrollTop() > point_of_interest) {
				$('#up_next').fadeIn('slow');
			}
			if ($(this).scrollTop() < point_of_interest - 200) {
				$('#up_next').fadeOut('fast');
			}
		});
	}

	$('.accordion .title').click(function() {
		$('.accordion .section').removeClass('selected');
	 	$('.accordion .content').slideUp('normal');
		if($(this).next().is(':hidden') == true) {
			$(this).closest('.section').addClass('selected');
			$(this).next().slideDown('normal');
		 } 
	 });
	$('.accordion .content').hide();
	$('.accordion .selected .content').show();
	
	
	$('.action.toggle').each(function() {
		$(this).unbind('click');
		$(this).click(function() {
			var element = $(this)
			var query = ''
			if (element.attr('rel')) {
				query = element.attr('rel');
			} else {
				query = element.attr('data-rel');
			}
			$(query).toggle();
			if (element.hasClass('hide_on_toggle')) {
				element.hide();
			}
			return false;
		})
	});
	initialize_remote_forms();

});

function initialize_remote_forms() {
	$('form.remote').submit(function() {
		var form = $(this);
		if (form.in_progress && arguments[1] != true) { return }

		var method = form.find('input[name=_method]') && form.find('input[name=_method]').length > 0 ? form.find('input[name=_method]')[0].value : 'post'

		if (form.hasClass('single_field') && form.children('textarea').val().trim().length == 0) { return }
		
		$.ajax({
		  type: method,
		  url: form.attr('action'),
		  data: form.serialize(),
		  success: function() {
				form.in_progres = false;
			}
		});
		
		$(this).in_progress = true;
		return false;
	});
}


(function( $ ){
	$.extend( {
		Twitter: function( username, numPosts, fnk ) {
			var info = {};
			
			// If no arguments are sent or only username is set
			if( username == 'undefined' || numPosts == 'undefined' ) {
				return;
			} else if( $.isFunction( numPosts ) ) {
				// If only username and callback function is set
				fnk = numPosts;
				numPosts = 5;
			}
			
			var url = "http://twitter.com/status/user_timeline/"
				+ username + ".json?count="+numPosts+"&callback=?";

			$.getJSON( url, function( data ){
				if( $.isFunction( fnk ) ) {
					fnk.call( this, data );
				}
			});
		}
	});
})( jQuery );
