// Internet Strateges (UK) Ltd 
// Clerk Maxwell News Scroller > Developed by Kevin Mitchell

// Function slideSwitch()
// This function performs the changes of images, Item Idents and Footer Txt Idents

// Function myInterval()
// This function sets the interval at which the slideSwitcher should run

// Within the jQuery, there are events to handle...
// Mouseenter #newsCont which 'pauses' the slideSwitcher
// Mouseleave #newsCont which starts slideSwitcher again
// Mouseenter .newsItem which idents (identifies) current moused item
// Mouseleave .newsItem which resets the ident to the last item from the slideSwitcher

$(document).ready(function(){
	
	// Set Interval Timer Function
	function myInterval() {
		$setInv = setInterval( "slideSwitch()", 3500 );
	}
	myInterval(); // Run the Interval Function
	
	// If Mouseover of News Container, Clear the Timer
	$('#newsCont').mouseenter(function(){
		clearInterval($setInv);
	});	
	// On Mouseleave, Restart the Timer
	$('#newsCont').mouseleave(function(){
		myInterval();
	});
	
	// If Mouseover News Item Ident, Clear Timer and Show This Item
	$('.newsItem').mouseenter(function(){
		// Clear Sel CSS
		$(".newsItem").removeClass('newsItemSel');
		$(".itmNum").removeClass('itmNumSel');
		// Set Selected Item Ident
		$(this).addClass('newsItemSel');
		// Get This Items ID
		var $myItem = $(this).attr('id');
		// Split ID into Useable Integer
		$myItem = $myItem.split("itm",2);
		$myItem = $myItem[1];
		// Hide All Images, Show Sel Image Using ID Integer
		$('#newsIMG').children().hide();
		$('#newsIMG img:nth-child('+$myItem+')').show();
		// Show Txt Footer Ident
		$("#itmNum"+$myItem).addClass('itmNumSel');
	});
	
	// On Mouse Leave of News Item Ident Button
	$('.newsItem').mouseleave(function(){
		// Remove Sel Class
   		$(".newsItem").removeClass('newsItemSel'); // News Item Ident
		$(".itmNum").removeClass('itmNumSel'); // Footer Txt Ident
		// Show All Images
		$('#newsIMG').children().show();
		// Set Last Item in Interval slideSwitch to Sel for Seamless Mouseleave
		$('#newsIMG img:nth-child('+$swapID+')').show(); // Image
		$("#itm"+$swapID).addClass('newsItemSel'); // News Item Ident
		$("#itmNum"+$swapID).addClass('itmNumSel'); // Footer Txt Ident
	});

});
	
// News Slider Function
function slideSwitch() {
	
	// Set Image Element
	var $active = $('#newsIMG IMG.active');
	// Set Last Item Default
	if ( $active.length == 0 ) $active = $('#newsIMG IMG:last');
	
	// Get Current ID
	$curID = $active.attr("name");
	// Get Next ID
	$swapID = $active.next().attr("name");
	if($swapID == null) {$swapID='1';}
	
	// Cycle Image
	// IF There is Another Image Next, Proceed to Next IMG
	var $next =  $active.next().length ? $active.next()
		// Otherwise, Start Again With 1st IMG
		: $('#newsIMG IMG:first');
	// Add Last Class to Active IMG (Swaps Z-Index Property, Under)
	$active.addClass('last-active');
	// Set Next Item Opacity to 0 (invisible)
	$next.css({opacity: 0.0})
	// Add Active Class to Next IMG (Swaps Z-Index Property, Top)
	.addClass('active')
	// Animate Next Image Into View (Visible)
	.animate({opacity: 1.0}, 500, function() {
		// Remove Added Classes
		$active.removeClass('active last-active'); 
	});
		

	$(".headertitles").children('li').eq($currentheader-1).fadeOut(800, function() {
		// Show Current
		$(".headertitles").children('li').eq($currentheader).fadeIn(800);																				  
		// Set Ident
		$(".homeId").removeClass('homeIdSel'); // Remove
		$("#homeId"+$currentheader).addClass('homeIdSel'); // Add Selected
		//alert($currentheader);
	});
	
	// Item Ident and Txt Footer Ident Swaps
	// Remove Previous Idents Sel CSS
	$(".newsItem").removeClass('newsItemSel'); // News Item Ident
	$(".itmNum").removeClass('itmNumSel'); // Footer Txt Ident
	// Set Next Idents Sel CSS
	$("#itm"+$swapID).addClass('newsItemSel'); // News Item Ident
	$("#itmNum"+$swapID).addClass('itmNumSel'); // Footer Txt Ident
	
}