jQuery(document).ready(function($){
	
	//Enable external links
	$('a[rel~=external]').attr('target','_blank');
	
	//Setup form field with default value
	function setFieldValue(id,value){
		$('#'+id).val(value).focus(function(){
			if( $(this).val() == value)
				$(this).val('');
		}).blur(function(){
			if( $(this).val() == '')
				$(this).val(value);
		});
	}
	setFieldValue("phrase","Whatcha' Looking For?");

	//Setup top menu
	$('#header > ul > li > a').append("<span class='menu-hover'></span>").each(function(){
		var linkBackgroundImage = $(this).css('backgroundImage');
		var $span = $('> span.menu-hover',this).css('opacity',0).css('backgroundImage',linkBackgroundImage);
		$(this).parent().hover(
			function(){
				$span.stop().fadeTo(200,1);
			},
			function(){
				$span.stop().fadeTo(200,0);
			}
		);
	});
	
	//Align drop down menus
	$('#header > ul li:has(ul)').each(function(){		
		var topWidth = $(this).width() - parseInt( $('a:first',this).css('marginRight') );
		var topHalfWidth = Math.round( topWidth / 2 );
		var bottomHalfWidth = Math.round( $('ul:first',this).width() / 2 );
		var offset = Math.abs(topHalfWidth - bottomHalfWidth);		
		$('ul:first',this).css('left','-'+offset+'px');		
	});
	
	//Setup content sidebar height
	if( app != 'home' ){
		var contentHeight = $('#content-body').height();
		var contentSidebarHeight = $('#content-sidebar').height();
		if( parseInt(contentSidebarHeight) < parseInt(contentHeight) )
			$('#content-sidebar').height(contentHeight);
	}
	
	//If home page...
	if( app == 'home' ){
		
		//Setup schools scroller on home page
		$('#schools ul li').prepend('&nbsp;&nbsp;&bull;&nbsp;&nbsp;');
		//$('#schools-scroller').marquee();
		
		//Setup feature rotation
		$('#masthead-features').cycle({ 
			timeout: 8000
		});
		$('.masthead-clip').each(function(i,item){
			$(this).click(function(){
				$('#masthead-features').cycle(i);
				return false;
			});
		});
		
		//Color box
		$("a[rel='prettyPhoto']").colorbox();
		
	}
	
	//Setup interior page shadow
	if( app != 'home' ) $('#content').append('<div id="shadow">&nbsp;</div>');
	
	//Setup page accordians
	$('.accordian').accordian();

	//Get latest tweet
	$.ajax({
		type: 'GET',
		url: 'http://twitter.com/statuses/user_timeline/CIAAForLife.json?count=1',
		dataType: 'jsonp',
		success: function(json){
			$.each(json, function(i, item){
				$('#latest-tweet').html(item.text);
			});
		}
	});
		
});

(function($) {
    // Hides the text under each list item and shows
	// it when the header is clicked.
    $.fn.accordian = function() {
        // Iterate over each element
        return this.each(function() {
            $this = $(this);
            
            // Cache the list of items
            var items = $this.children('li');
			var answers = $('.answer', items);
            
            items.each(function() {
				// Cache the answer
				var answer = $('.answer', this);
                // And hide it
                answer.hide();
                
                // Set up the click event on the header
                $(this).children('h2').wrapInner('<a href="#"></a>').children('a:first').click(function(){
					$(this).toggleClass('open');
					answer.slideToggle(400);
					return false;
				});
				
            });
        });
    };
})(jQuery);