$(document).ready(function(){	
	// Run our test cookie method
	testCookies();

/**
 * Main menu functions
 */
	$("#mainMenu li").mouseover(function(){
		$(this).children("ul" ).css({
			'display':'block'
		});
	});
	$("#mainMenu li").mouseout(function(){
		$(this).children("ul" ).css({
			'display':'none'
		});
	});
	$("#moreWeek ul li:nth-child(5)").css({
		'padding-right':'0',
		'margin-right':'0',
		'border-right':'none'
	});
	$("#subscriptionBox li:last-child").css({
		'border-right':'none'
	});
	$("#footer li:last-child").css({
		'border-right':'none'
	});
	$(".most h6").click(function(){
		$(".most h6").toggleClass("inactive").next("ol").toggle();
	});
	$(".most h6.inactive + ol").toggle();

/**
 * Set up the home page articles/images carousel
 */
	$("#articleImages li a img").each(function( i ) {
		$(this).attr("id","topArticleImg"+(i+1));
	});
	$(".mainStory").each(function( j ) {
		$(this).attr("href","topArticleImg"+(j+1));
	}); 
	$(".mainStory").hover(function(){
		var hrefattr = $(this).attr("href");
		$(".mainStory").removeClass("active").children(".byline").css({
			"color":"#000000"											   
		});
		$(this).addClass("active").children(".byline").css({
			"color":"#de0000"											   
		});
		
		$('#articleImages li a img:not([id="' + hrefattr + '"])').fadeOut(200);
		$('#articleImages li a img[id="' + hrefattr + '"]').fadeIn(300);
	});

	$('#topArticleImg2, #topArticleImg3').fadeOut(100);

/**
 * Comment functions
 */
	$("#commentMessage").keyup(function(){
		if($(this).val().length>300){
			alert('Your message is to long. No more than 300 characters please.');
		}
		if($(this).val().length+1>300){
			thisMessage = $(this).val().substring(0,300)
			$(this).val(thisMessage);
		}
	});

	if($("p").find(":contains('h4,h3,h2')")){
		$("p:empty").remove();
		$(/<p>\.<\/p>/).remove();
	}
	$("a.commentAnchor").click(function () {	
		elementClick = $(this).attr("href")
		destination = $(elementClick).offset().top;
		$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 1100 );
		return false;
	});	
	$(".clearfield1").click(function() {
		document.forms['refined_search'].start_date.value = 'Start Date';
		document.forms['refined_search'].end_date.value = 'End Date';
	});
});

// Set a test cookie to see if cookies are enabled
function setTestCookie() {
	$.cookie('testcookiesenabled', null);
	$.cookie('testcookiesenabled', 'enabled');
}
// Test for the cookie and redirect if they are not
function testCookies() {
	url = 'http://' + document.domain + '/nopage/index/ck:0';

	if (
		window.location != url &&
		$.cookie('testcookiesenabled') == null
	) {
		window.location = url;
	}
}