/**
 * @author craigkaminsky
 */
$( document ).ready(
	function(){
		$( "#state" ).change( 
			function(){
				if ( $('#state').val() != 0 ) {
					$.getJSON('/communities/by_state', {
						state: $('#state').val()
					}, 
					// add communities to the display region, clearing existing content first
					function(data, text_status){
						if( data.length > 0 ){
							$('div.items').hide( 'slow' );
							$('div.items').html('');
							$('div.items').before('<div class="loader">Loading &hellip; <img src="/img/ajax/loading_bar.gif" alt="Loading" border="0"/></div>');
							
							for( i = 0; i < data.length; i++ ){
								html = '<div class="communityListing"><div class="listingTitle">';
								html += '<a href="/communities/view/' + data[i].Community.id + '" class="listingBlurbReadMore">';					
								html += data[i].Community.name;
								html += '</a></div><div class="listingBlurb">';
								html += '<span class="communityDescription">';
								html += data[i].Community.description.substring(0,130);
								html += '</span> &hellip; '
								html += '<span class="listingBlurbReadMore"><a href="/communities/view/' + data[i].Community.id + '" class="listingBlurbReadMore">';
								html += 'MORE</a></span></div></div>';
								// now add it to the items div
								$( html ).appendTo( 'div.items' );
							}
							
							$('span.communityDescription').stripHtml();
							
							$( 'div.items' ).show( 'fast', function(){
								$( 'div.loader' ).remove();
								$( 'div.communityListing' ).each(
									function( i, elem ){
										$(elem).fadeIn( 1000 );
									}
								);
							} );							
						}
					});
				}
			} 
		);
	}
);