$(document).ready(function(){
	/* navigation keyboard accessibility */
	$('#navigation a').focus(function(){
		$(this).css('text-decoration','underline');
		$(this).parents('ul').eq(0).addClass('over');
		$(this).parent('li').find('ul').eq(0).addClass('over');
	}).blur(function(){
		$(this).css('text-decoration','none');
		$(this).parents('ul').eq(0).removeClass('over');
		$(this).parent('li').find('ul').eq(0).removeClass('over');
	});
	
	/* tabs */
	$('div.tabbedContainer').tabs();
	
	/* counter */
	if($('#postComment').get(0)){
		var code = ', left: <span class="limit">' + (1000 - $('#postComment fieldset textarea').val().length) + '</span>';
		$('#postComment p.limit-info').append(code);
		
		$('#postComment fieldset textarea').keyup(function(){
			$('#postComment span.limit').html((1000 - $(this).val().length));
			if($(this).val().length > 1000){
				$('#postComment span.limit').addClass('warning');
			} else {
				$('#postComment span.limit').removeClass('warning');
			}
		});
	}
	
	// searchbox
	$('fieldset.searchBox span.mainInput input').val('Search');
	$('fieldset.searchBox span.mainInput input').focus(function(){
		if($(this).val() == 'Search'){
			$(this).val('');
		}
	}).blur(function(){
		if($(this).val() == ''){
			$(this).val('Search');
		}
	});
	

	// entertainment slides
	var slides_current = 1;
	var slides_lenght = $('#entertainment ul.slides').size();
	if(slides_lenght > 1){
		$('#entertainment div.slidesHolder').addClass('slidesHolderCSS').before('<ul class="paging"><li class="prev"><a href="./">&laquo;</a></li><li class="next"><a href="./">&raquo;</a></li></ul>');
		$('#entertainment ul.slides').each(function(i){
			var left_position = 456 * i;
			$(this).css({ position: "absolute", width: "100%", top: "0", left: left_position });
		});
		$('#entertainment ul.paging li.prev a').click(function(){
			if(slides_current > 1){
				if(parseInt($('#entertainment ul.slides').eq(0).css("left")) == 0 || parseInt($('#entertainment ul.slides').eq(0).css("left")) % 456 == 0){
					$('#entertainment ul.slides').each(function(){
						$(this).animate(
							{
								style: 'left: ' + (parseInt($(this).css("left")) + 456) + 'px'
							},
							'slow'
						);
					});
					slides_current--;
				}
			}
			return false;
		});
		$('#entertainment ul.paging li.next a').click(function(){
			if(slides_current < slides_lenght){
				if(parseInt($('#entertainment ul.slides').eq(0).css("left")) == 0 || parseInt($('#entertainment ul.slides').eq(0).css("left")) % 456 == 0){
					$('#entertainment ul.slides').each(function(){
						$(this).animate(
							{
								style: 'left: ' + (parseInt($(this).css("left")) - 456) + 'px'
							},
							'slow'
						);
					});
					slides_current++;
				}
			}
			return false;
		});
	}
});

// NEWS TICKER

// Ticker startup
function startTicker() {
	
	// Define run time values
	theCurrentStory     = -1;
	theCurrentLength    = 0;
	
	runTheTicker();
}

// Ticker main run loop
function runTheTicker(){
	var myTimeout;  
	
	// Go for the next story data block
	if(theCurrentLength == 0){
		theCurrentStory++;
		theCurrentStory		= theCurrentStory % theItemCount;
		theStorySummary		= theSummaries[theCurrentStory].replace(/&quot;/g,'"');		
		theTargetLink		= theSiteLinks[theCurrentStory];
		thePrefix 	     	= "<span>" +theHours[theCurrentStory] + "</span> ";
	}
	
	// Stuff the current ticker text into the anchor
	
	$('div.breakingTicker ul').html(
		'<li><a href="' + theTargetLink +'">' + 
		thePrefix + 
		theStorySummary.substring(0,theCurrentLength) + 
		whatWidget() +
		'</a></li>'
	)
	
	// Modify the length for the substring and define the timer
	if(theCurrentLength != theStorySummary.length){
		theCurrentLength++;
		myTimeout = theCharacterTimeout;
	} else {
		theCurrentLength = 0;
		myTimeout = theStoryTimeout;
	}
	
	// Call up the next cycle of the ticker
	setTimeout("runTheTicker()", myTimeout);
}

// Widget generator
function whatWidget(){
	if(theCurrentLength == theStorySummary.length){ 
		return ''; 
	}
	
	if((theCurrentLength % 2) == 1){ 
		return '_';
	} else { 
		return '-'; 
	}
}

$(document).ready(function(){
	if($('div.breakingTicker').size() > 0){
		theCharacterTimeout = 25;
		theStoryTimeout     = 5000;

		theSummaries 		= new Array();
		theHours 			= new Array();
		theSiteLinks 		= new Array();

		$('div.breakingTicker ul').addClass('loaded');
		
		$('div.breakingTicker ul li a').each(function(){
			theSummaries[theSummaries.length] = $(this).get(0).childNodes[1].nodeValue.replace(/^\s*|\s*$/g,"");
			theHours[theHours.length] = $(this).find('span').html();
			theSiteLinks[theSiteLinks.length] = $(this).attr('href');
		});
		
		theItemCount = theSummaries.length;

		startTicker();
	}
});

document.write('<link rel="stylesheet" type="text/css" media="screen" href="styles/javascript.css" />');