jQuery(document).ready(function($){
	
	/* navigation for IE6 */
	if($.browser.msie && $.browser.version.substr(0,1) == '6'){
		$('#navigationSaab li').hover(function(){
			$(this).addClass('over');
		},function(){
			$(this).removeClass('over');
		});
	}
	
	/* navigation keyboard accessibility */
	$('#navigationSaab 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');
	});
	
	/* nav on */
	$('#navigationSaab > ul > li:not(.spacer) > a').hover(function(){
		$(this).parent().parent().find('> li').removeClass('current');
		$(this).parent().addClass('current');
	},function(){
	
	});
	
	/* tabs */
	$('div.tabbedContainer').each(function(){
		var cont = $(this);
		cont.find('ul.anchors li:first-child').addClass('tabs-selected');
		cont.find('ul.anchors li a').click(function(){
			cont.find('ul.anchors li').removeClass('tabs-selected');
			$(this).parent('li').addClass('tabs-selected');
			
			cont.find('>div:visible').hide();
            var re = /([_\-\w]+$)/i;
            $('#' + re.exec($(this).attr('href'))[1]).show();
			
			return false;
		});
	});
	
	/* counter */
	if($('#postComment').get(0)){
		var charLimit = 1000;
		var code = ', left: <span class="limit">' + (charLimit - $('#postComment fieldset textarea').val().length) + '</span>';
		$('#postComment p.limit-info').append(code);
		
		$('#postComment fieldset textarea').keyup(function(){
			$('#postComment span.limit').html((charLimit - $(this).val().length));
			if($(this).val().length > charLimit){
				$('#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 slidesCurrent = 1;
	var slidesCount = $('#entertainment ul.slides').size();
	var slidesWidth = $('#entertainment div.slidesHolder').width();
	
	if(slidesCount > 1){
		// cloning
		$('#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:last').clone().prependTo("#entertainment div.slidesHolder");
		$('#entertainment ul.slides').eq(1).clone().appendTo("#entertainment div.slidesHolder");
		
		// positioning
		$('#entertainment ul.slides').each(function(i){
			var leftPosition = (slidesWidth * i) - slidesWidth;
			$(this).css({ position: "absolute", width: "100%", top: "0", left: leftPosition });
		});
		
		// click: previous 
		$('#entertainment ul.paging li.prev a').click(function(){
			if(parseInt($('#entertainment ul.slides').eq(0).css("left")) % slidesWidth == 0){
				if(slidesCurrent == 0){
					$('#entertainment ul.slides').each(function(){
						var newLeftPosition = parseInt($(this).css("left"),10) - slidesCount * slidesWidth;
						$(this).css('left',newLeftPosition);
					});
					slidesCurrent = slidesCount;
				}
				
				$('#entertainment ul.slides').each(function(){
					var newLeftPosition = parseInt($(this).css("left"),10) + slidesWidth;
					$(this).animate( { left: newLeftPosition + 'px' }, 'slow' );
				});
				slidesCurrent--;
			}
			return false;
		});
		
		// click: next
		$('#entertainment ul.paging li.next a').click(function(){
			if(parseInt($('#entertainment ul.slides').eq(0).css("left")) % slidesWidth == 0){
				if(slidesCurrent == slidesCount+1){
					$('#entertainment ul.slides').each(function(){
						var newLeftPosition = parseInt($(this).css("left"),10) + slidesCount * slidesWidth;
						$(this).css('left',newLeftPosition);
					});
					slidesCurrent = 1;
				}
				
				$('#entertainment ul.slides').each(function(){
					var newLeftPosition = parseInt($(this).css("left"),10) - slidesWidth;
					$(this).animate( { left: newLeftPosition + 'px' }, 'slow' );
				});
				slidesCurrent++;
			}
			return false;
		});
	}
	
	// editors slider
	var editorHeight =  0;
	$('#editorsSlider ul.slides li').each(function(){
		currentHeight = $(this).height();
		if(currentHeight > editorHeight){
			editorHeight = currentHeight;
		}
	});
	$('#editorsSlider ul.slides li').css('height',editorHeight+'px');
	
	var editorsCurrent = 1;
	var editorsCount = $('#editorsSlider ul.slides li').size();
	var editorsWidth = $('#editorsSlider').width()/4;
	
	if(editorsCount > 4){
		// cloning
		$('#editorsSlider div.slidesWrap').height(editorHeight+16).before('<ul class="paging"><li class="prev"><a href="./">&laquo;</a></li><li class="next"><a href="./">&raquo;</a></li></ul>');
		$('#editorsSlider ul.slides li').clone().appendTo("#editorsSlider ul.slides").clone().appendTo("#editorsSlider ul.slides");
		
		// positioning
		var leftPosition = editorsWidth * editorsCount * -1;
		var fullWidth = editorsCount * editorsWidth * 3;
		$('#editorsSlider ul.slides').css({ position: "absolute", top: "0", left: leftPosition, width: fullWidth });
		
		// click: previous 
		$('#editorsSlider ul.paging li.prev a').click(function(){
			if(parseInt($('#editorsSlider ul.slides').css("left")) % editorsWidth == 0){
				if(editorsCurrent == 0){
					var newLeftPosition = parseInt($('#editorsSlider ul.slides').css("left"),10) - editorsCount * editorsWidth;
					$('#editorsSlider ul.slides').css('left',newLeftPosition);
					editorsCurrent = editorsCount;
				}
				
				var newLeftPosition = parseInt($('#editorsSlider ul.slides').css("left"),10) + editorsWidth;
				$('#editorsSlider ul.slides').animate( { left: newLeftPosition + 'px' }, 350 );
				editorsCurrent--;
			}
			return false;
		});
		
		// click: next
		$('#editorsSlider ul.paging li.next a').click(function(){
			if(parseInt($('#editorsSlider ul.slides').css("left")) % editorsWidth == 0){
				if(editorsCurrent == editorsCount+1){
					var newLeftPosition = parseInt($('#editorsSlider ul.slides').css("left"),10) + editorsCount * editorsWidth;
					$('#editorsSlider ul.slides').css('left',newLeftPosition);
					editorsCurrent = 1;
				}
				
				var newLeftPosition = parseInt($('#editorsSlider ul.slides').css("left"),10) - editorsWidth;
				$('#editorsSlider ul.slides').animate( { left: newLeftPosition + 'px' }, 350 );
				editorsCurrent++;
			}
			return false;
		});
	}
	
	$('#editorsSlider ul.slides li a').hover(function(){
		$(this).parent('li').addClass('over');
	},function(){
		$(this).parent('li').removeClass('over');
	});
	
	// advanced search
	if($('#advs-daterange-5').attr('checked') !== 'checked'){
		$('#advancedSearch fieldset div.radios div.dates select').attr('disabled','disabled');
	}
	$('#advancedSearch span.datechange input').focus(function(){
		if($(this).attr('id') == 'advs-daterange-5'){
			$('#advancedSearch fieldset div.radios div.dates select').attr('disabled','');
		} else {
			$('#advancedSearch fieldset div.radios div.dates select').attr('disabled','disabled');
		}
	});

	// complainments
	$('#commentsList div.comment p.author').before('<p class="complain"><span>Complain about this comment</span></p>');
	
	$('#commentsList div.comment p.complain span').toggle(function(){
		$(this).addClass('active').parents('div.comment').next('form.complainment').addClass('active');
		return false;
	},function(){
		$(this).removeClass('active').parents('div.comment').next('form.complainment').removeClass('active');
		return false;
	});
	
	// sound and vision
	$('#soundAndVision p.more span').click(function(){
		$(this).parent('p').next('.toggled').toggle();
	});
	
	// article font
	$('div.articleRelated:last').append('<div class="fonts">Change font size: <a href="#a" class="a">A</a> <span>|</span> <a href="#aa" class="aa">A</a> <span>|</span> <a href="#aaa" class="aaa">A</a></div>');
	
	
	$('div.articleRelated div.fonts a').click(function(){
		var value;
		switch ($(this).attr('class')) {
			case 'aa':
				value = 'aa';
				break;
			case 'aaa':
				value = 'aaa';
				break;
			default:
				value = 'a';
				break;
		} 
		
		$.cookie('fontsizeAAA', value, { expires: 90, path: '/'});
		
		$('#article div.body').attr('class','body font-'+$(this).attr('class'));
		return false;
	});
	
	if($('#article').size() == 1 && $.cookie('fontsizeAAA') != 'null'){
		$('#article div.body').attr('class','body font-'+$.cookie('fontsizeAAA'));
	}
	
});

// 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
	
	var temp = '<li><a href="' + theTargetLink +'">';
	temp += thePrefix;
	temp += theStorySummary.substring(0,theCurrentLength);
	temp += whatWidget();
	temp += '</a></li>';
	$('div.breakingTicker ul').html(temp);
	
	// 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 '-'; 
	}
}

jQuery(document).ready(function($){
	if($('div.breakingTicker').size() > 0){
		theCharacterTimeout = 25;
		theStoryTimeout     = 5000;

		theSummaries 		= [];
		theHours 			= [];
		theSiteLinks 		= [];

		$('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="/independent.co.uk/styles/javascript.css" />');