popupNumber = 0;
count = 1;
numberOfNewsItems = 12;
var AnyRotatorVideosPlaying = false
$(function(){
	////////////////////////////////// Expander Stuff///////////////////////////////////////////////////////////////////////////////////////////////////////////////
	$('.scrollOver').hover(function(){
		popupNumber = this.id.charAt(2);
		if($("#PU" + popupNumber).css('display') == 'none'){
			$("#PU" + popupNumber).fadeIn('slow', function(){});
	}},function(){
		popupNumber = this.id.charAt(2);
		if($("#PU" + popupNumber).css('display') == 'block'){
			$("#PU" + popupNumber).fadeOut('slow', function(){});
	}});
	
	setInterval( newsReel, 3000);
	
	///////////////////////////////////ROTATOR STUFF////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	var rCount = 0;
	$("#AdRotater #HiddenImages div.rotatorDivs").each(function() {
		rCount++;
		var source = $(this).attr("src");
		$("#AdRotater .image_reel1").append("<div class='rotatorContent1'>" + $(this).find('.rotatorContent1').html() + "</div>");
		$("#AdRotater .image_reel2").append("<div class='rotatorContent2'>" + $(this).find('.rotatorContent2').html() + "</div>");
		$(".paging").append("<a href='#' rel='" + rCount + "'>" + $(this).find('.rotatorContentName').text() + "</a>");
	});
	var imageWidth = $("#AdRotater .window").width();
	var imageSum = $("#AdRotater .image_reel1 div.rotatorContent1").size();
	var imageReelWidth1 = imageWidth * imageSum *3;
	var imageReelWidth2 = imageWidth * imageSum * 2;
	var imageReelWidth3 = imageWidth * imageSum;
	//Adjust the image reel to its new size
	$("#AdRotater .image_reel1").css({'width' : imageReelWidth1});
	$("#AdRotater .image_reel2").css({'width' : imageReelWidth2});
	$("#AdRotater .image_reel3").css({'width' : imageReelWidth3});
	//Selecting a random image to start with
	$(".paging").show();
	var randomnumber=Math.floor(Math.random()*rCount) + 1;
	$(".paging a[rel='" + randomnumber + "']").addClass("active");
	$("#AdRotater .image_reel1").css("left", -1 * imageWidth * 3 * (randomnumber - 1));
	$("#AdRotater .image_reel2").css("left", -1 * imageWidth * 2 * (randomnumber - 1));
	$("#AdRotater .image_reel3").css("left", -1 * imageWidth * (randomnumber - 1));
	//Paging and Slider Function
	rotate = function(){

AnyRotatorVideosPlaying = false;
$('div').filter(function() {
	if($(this).attr('id') == undefined) {
		return false;
	}else {
		return $(this).attr("id").substring(0,10)=="flowPlayer";
	}
}).each(function(){
	var tempPlayer = $f($(this).attr('id'));
    if(tempPlayer.isPlaying())
		AnyRotatorVideosPlaying = true;
});

		if(!AnyRotatorVideosPlaying){
			var triggerID = $active.attr("rel") - 1; //Get number of times to slide
			var image_reelPosition = triggerID * imageWidth *3; //Determines the distance the image reel needs to slide
			var image_reelPosition2 = triggerID * imageWidth *2;
			var image_reelPosition3 = triggerID * imageWidth;
			$(".paging a").removeClass('active'); //Remove all active class
			$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
			//Slider Animation
			$("#AdRotater .image_reel1").stop(true, false).animate({left: -image_reelPosition}, 2400 );
			setTimeout(function(){$('#AdRotater .image_reel2').stop(true, false).animate({left: -image_reelPosition2}, 2200 );}, 300);
			setTimeout(function(){$('#AdRotater .image_reel3').stop(true, false).animate({left: -image_reelPosition3}, 2000 );}, 600);
		}
	};
	//Rotation and Timing Event
	rotateSwitch = function(){
		play = setInterval(function(){ //Set timer - this will repeat itself every 7 seconds
			$active = $('.paging a.active').next(); //Move to the next paging
			if ( $active.length === 0) { //If paging reaches the end...
				$active = $('.paging a:first'); //go back to first
			}
			rotate(); //Trigger the paging and slider function
		}, 8000); //Timer speed in milliseconds (7 seconds)
	};
	rotateSwitch(); //Run function on launch
	$("#AdRotater").hover(function() {
		clearInterval(play); //Stop the rotation
	}, function() {
		rotateSwitch(); //Resume rotation timer
	});
	//On Click
	$(".paging a").click(function() {
		$active = $(this); //Activate the clicked paging
		//Reset Timer
		clearInterval(play); //Stop the rotation
		rotate(); //Trigger rotation immediately
		//rotateSwitch(); // Resume rotation timer
		return false; //Prevent browser jump to link anchor
	});
}); 

///////////////////////////////////NEWSREEL STUFF////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function newsReel()
{
	$('#NewsItems').animate({marginTop: '-=35px'}, 1000, function(){});
	setTimeout(resetReel, 1500);
}
function resetReel()
{
	if( count == numberOfNewsItems){
		$('#NewsItems').css({'margin-top' : '0px'});
		count = 1;
	}
	else{
		count++;
	}
}
	

