$(document).ready(function() {
	
	// Add JS class
	$('body').addClass('js');
	
	// Add the controls
	$('<div id="controls"></div>').insertBefore('#content-wrapper');
	$('<a href="#" id="prev-slide">Previous</a>').appendTo('#controls');
	$('<a href="#" id="next-slide">Next</a>').appendTo('#controls');
	
		
	// Add div to apply slideshow mask
	$('<div class="mask"></div>').insertAfter('#slideshow ul');
	

	// Slideshow
	$('#slideshow ul').cycle({ 
		fx:     'scrollHorz', 
		speed:  'fast', 
		timeout: 8000, 
		next:   '#next-slide', 
		prev:   '#prev-slide' 
	});
	
	
	// Close toggle
	//$('#content-toggle').addClass('closed');	
	
	
	// Add news toggle
	$('<a href="#" id="toggle-button">Toggle news</a>').insertBefore('#content-toggle .buttons');	
	
	
	//Homepage Cookie Alert
	//alert($.cookie('content-toggle'));
	
				
	// Toggle footer nav
	$('#toggle-button').click(function () {					
		$(this).closest('#content-toggle').toggleClass('open');
		$.cookie('content-toggle', $('#content-toggle').attr('class'), { expires: 7, path: '/', secure: false });
		//alert($.cookie('content-toggle'));
		if($(this).closest('#content-toggle').hasClass('open')) {
			$(this).closest('#content-toggle').stop().animate({ bottom:'+=167px' },"slow")
		} else {
			$(this).closest('#content-toggle').stop().animate({ bottom:'-=167px' },"slow")
		}
		return false;
	});		
	
	
	// Homepage Cookie Behvior
	if ($.cookie('content-toggle')== 'open') {
		$('#content-toggle').addClass('open');
		$('#content-toggle').css('bottom','0');
	}
	
	
	// Buttons
	$('.buttons ul')
	.after('<div id="button-nav">')
	.cycle({
		fx:     'scrollUp', 
		speed:  'fast', 
		timeout: 4444, 
		pager:  '#button-nav' 
	});
	
	
	// Limit the character count on news captions
	$('.allschool li p').each(function () {
		$(this).text($(this).text().substring(0, $(this).text().indexOf(' ', 70)) + "...");
	});
		
		
	// Add news paging
	$('<div id="news-nav"></div>').insertAfter('.allschool ul');	
			
			
	// News List
	$('.allschool ul')
	//.after('<div id="news-nav">')
	.cycle({
		fx:     'scrollUp', 
		speed:  'fast', 
		timeout: 11111, 
		after:     onAfter,
		pager:  '#news-nav',
		after:     onAfter
	});
	function onAfter(curr,next,opts) {
		var caption = (opts.currSlide + 1) + ' <span>of</span> ' + opts.slideCount;
		$('.caption').html(caption);
	}
	
});
