var $j = jQuery.noConflict();
$j(document).ready(function() {
	
	// remove placeholder labels if input field already populated (e.g from pressing back button)
		var invLabels = ".search-form input, .mailing-list input, .content-mailing-list input";
		
		
		$j(".search-form input, .mailing-list input, .content-mailing-list input").each(function() { 
			if($j(this).val() != '') {
				$j(this).next('label').css('display','none');
			}
		});
	
	// fade out placeholder label when focussed
		$j(invLabels).focus(function(){ 
			$j(this).next('label').fadeOut('fast');
		});
	
	// fade in placeholder label when blurred as long as input value is blank
		$j(invLabels).blur(function(){ 
			var inputVal = $j(this).val();
			if(inputVal == '') {
				$j(this).next('label').fadeIn('fast');
			}
		});
	
	// fade tags placeholder in comments textarea when over 400 chars	
		$j('.compose-comment textarea').keyup(function() { 
			$j(this).next('span').stop().animate();
			var valLength = $j(this).val().length;
			if(valLength > 400) {			
				$j(this).next('span').animate({
					opacity: 0
				},500,function() {
					$j(this).css('display','none');				
				});
			} else {			
				$j(this).next('span').css('display','block');
				$j(this).next('span').animate({
					opacity: 1
				},500);
			}
		});
	
	// latest & popular tabs	
		$j('.latest-popular-cats .filter a').click(function(e) {
			e.preventDefault();			
			var clicked = $j(this).html();			
			if(clicked == 'Popular') {
				$j('.latest-popular-cats ul.latest').fadeOut('fast', function() {
					$j('.latest-popular-cats ul.popular').fadeIn('slow');	
				});				
				$j('.latest-popular-cats ul.filter li.selected').removeClass('selected')
					.next('li').addClass('selected');
			} else {
				$j('.latest-popular-cats ul.popular').fadeOut('fast', function() {
					$j('.latest-popular-cats ul.latest').fadeIn('slow');	
				});
				$j('.latest-popular-cats ul.filter li.selected').removeClass('selected')
					.prev('li').addClass('selected');
			}
		})
	
	// show only x number of categories in sidebar unless more clicked	
	var expanded = false;
	
	function display_categories(num) {	
		var cat_count = 0;
		$j('.latest-popular-cats .categories li').each(function(){	
			if(cat_count>num) {
				$j(this).addClass('hide');
				$j(this).fadeOut('fast');
			}
			cat_count++;			
		});
		
		$j('.latest-popular-cats .categories li.more').fadeIn('fast')
			.removeClass('hide');
	}	
	function expand_categories() {
		var cat_count = 0;
		$j('.latest-popular-cats .categories li.hide').each(function(){	
			$j(this).fadeIn('fast');		
		});		
		$j('.latest-popular-cats .categories li.more a').html('less &uarr;');
		expanded = true;
	}	
	function collapse_categories() {
		$j('.latest-popular-cats .categories li.hide').each(function() {
			$j(this).fadeOut('fast');
		});		
		$j('.latest-popular-cats .categories li.more a').html('more &darr;');
		expanded = false;
	}		
	$j('.latest-popular-cats .categories li.more a').click(function(e) {
		e.preventDefault();
		if(expanded == true) {
			collapse_categories();
		} else {
			expand_categories();
		}
	});			
	display_categories(10);	
	
	// scrollTo	
	$j('a.scroller').click(function(){
		$j.scrollTo(this.hash,2000,{easing:'easeInOutCirc'});
		return false;
	});
	
	// vertically align tweets
	$j('.twitter article').each(function() {
		var thisHeight = $j(this).height();
			var marginTop = (100 - thisHeight) / 2;
			$j(this).css('top',marginTop);
	});
	
	/* twitterScroller	
	function nextTweet() {	
		var active = $j('.twitter article.active');
		var next = $j(active).next();
		var first = $j('.twitter article').first();
					
		$j(active).fadeOut()
			.removeClass('active');
		
		if(next.length != 0) {
			$j(next).stop().fadeIn('slow')
				.addClass('active');
		} else {
			$j(first).stop().fadeIn('slow')
				.addClass('active');
		}
	}	
	setInterval(nextTweet, 6000);
	*/
	
	// twitter fadeRotate
	$j('.twitter article').fadeRotate();
	
});
