jQuery(function () {
	startSlider();	
	carets();
	styling();
});

function styling() {
	//Things i could not get done in the stylesheet
	
	// Remove bold styling that was applied in the html
	jQuery(".poll thead td").css('font-weight','normal');
	
	// Add lines in between news entries
	jQuery("<hr class='newsseparator' />").insertAfter(".newsfeed li").appendTo('.blog div:not(:last) .article_separator');
	
	// Move source in newsfeed page down to next line
	jQuery(".newsfeed li a").each( function () {
		this.innerHTML = this.innerHTML.replace(/([^(]*)(\(.*\))/, "$1 <br /><div class='source'>$2</div>");
	});
		
	// wrap menu elements in divs so that they can be formatted
	jQuery('#mainmenu .mainlevel').wrap("<div class='menuelement'></div>");
	jQuery('.menuelement').wrap('<div class="bigmenuelement"></div>');
	// Menu background manipulations, should probably be chained
	jQuery('.bigmenuelement:not(:has(span, #active_menu))').mouseover(function () {
												  jQuery(this).css('background-image', 'url(/images/nav_roll.png)'); });
	
	jQuery('.bigmenuelement:not(:has(span, #active_menu))').mouseout(function () {
												 	jQuery(this).css('background-image','none'); });
	
	jQuery('.bigmenuelement:not(:has(span))').mousedown(function () { jQuery(this).css('background-image','url(/images/nav_sel.png)');});
	// Allow linking from whole menu item area, and not just the link
	jQuery('.bigmenuelement:not(:has(span))').click(function () { 
															 location.href=jQuery(this).find('a').attr('href'); });
	// Add some spacing in contact forms
	jQuery(".forme").attr('cellspacing', '10');
	
	// align poll options
	jQuery('.poll td, .poll div').attr('align','left');
	
	// Poll options whitespace
	jQuery('[class^=sectiontable]:first-child').css('padding-right','10px');
	
	// Add a lining element to headlines so that it displays properly in all browsers
	jQuery('table.moduletable_thefeed, #allnews').wrap('<div class="headlinespadding"></div>');
	jQuery('.headlinespadding').append("<div class='clear'></div>");
	
	// Set the current menu item to different background
	jQuery('.bigmenuelement:has(#active_menu)').css('background-image',"url(/images/nav_sel.png)");
	
	// Make links that are not in menus open in new pages
	jQuery('#topcontent a:not(.mainlevel)').attr('target','_blank');
	
	// Less crowding of poll results
	jQuery('.pollstableborder').attr('cellspacing','5')
		// Hide summary results on polls
		.siblings().hide();
	jQuery('#bottomcontent .moduletable:last-child').css('margin-right', '0px');
	jQuery('#bottomcontent .moduletable:last-child h3').css('color', '#C1272D');
	
	
	
}

function carets () {
	// Add Carets to headings
	jQuery("<div class='caret'>></div>").prependTo("h3");
}



function startSlider() {
	// Hides all the un-nessecary parts of the newsfeed and initialzes the slider
	jQuery("table.moduletable_thefeed").children().children().hide();
	jQuery(".newsfeed_thefeed").parent().parent().show();
	jQuery(".newsfeed_thefeed").attr("style","padding: 15px; padding-top: 25px;");
	jQuery(".newsfeed_thefeed").children().hide().find('img').hide();
	showNewsItem(1);
}

function showNewsItem(index) {

	var num = jQuery(".newsfeed_thefeed").children().length;
	var $it = jQuery(".newsfeed_thefeed").children().slice(index, index+1);
	var $prev;
	
	if(index == 0) {
		$prev = jQuery(".newsfeed_thefeed").children().slice(num-1, num);
	}
	else
	{	
		$prev = $it.prev();
	}
	$prev.fadeOut("normal", function () {
									
		$it.fadeIn("normal");//.css('padding', '10px');
	});
	window.setTimeout(function () {
		showNewsItem((index+1) % num);
	}, 6000);  /* HARDCODED */
}

function H(selector) {
	jQuery(selector).css('background','blue');
}

function submitContact() {
	document.contactform.submit();
}
