

$(document).ready(function() {
	/////// ROTATOR SCRIPT ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

	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){
			//Get number of times to slide
			var triggerID = $active.attr("rel") - 1; 
			//Determines the distance the image reel needs to slide
			var image_reelPosition = triggerID * imageWidth *3; 		
			var image_reelPosition2 = triggerID * imageWidth *2;
			var image_reelPosition3 = triggerID * imageWidth;
			//Remove all active class
			$(".paging a").removeClass('active');
			//Add active class (the $active is declared in the rotateSwitch function)
			$active.addClass('active'); 								
			
			//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(){
		//Set timer - this will repeat itself every 7 seconds
		play = setInterval(function(){ 	
			//Move to the next paging
			$active = $('.paging a.active').next(); 
			//If paging reaches the end...
			if ( $active.length === 0) { 
				//go back to first
				$active = $('.paging a:first'); 						
			}
			//Trigger the paging and slider function
			rotate(); 
		//Timer speed in milliseconds (7 seconds)
		}, 8000); 														
	};
	rotateSwitch(); 
		
	//Run function on launch
	$("#AdRotater").hover(function() {
		//Stop the rotation
		clearInterval(play); 														
	}, function() {
		//Resume rotation timer
		rotateSwitch(); 															
	});

	//On Click
	$(".paging a").click(function() {
		//Activate the clicked paging
		$active = $(this); 															
		//Reset Timer
		//Stop the rotation
		clearInterval(play);
		//Trigger rotation immediately		
		rotate(); 
		// Resume rotation timer
		//rotateSwitch(); 	
		//Prevent browser jump to link anchor		
		return false; 																
	});
	 
	/////// END ROTATOR SCRIPT ////////////////////////////////////////////////////////////////////////////////////////////////////////////




// SCROLL FEATURE SCRIPT
	// Left Scroll Script	
		var hasClicked = 0;
		//disable left scroll buttons
		$('#ScrollItemsScrollLeft').addClass('disabled');
		$('#ScrollItemsScrollLeft').click(function () {
		// Set Variables For The Left Button
		var framebuffer = 0
		// display size should be display area of scroll items plus the right-margin of one scroll item..
		var displaysize = framebuffer + 537;
		var displayframes = 3;
		var framesize = displaysize / displayframes;
		// var scrollitems = 18;
		var scrollitems = $('.calendarItem').size();
		var framesperscroll = 3;
		var scrollIncrement = framesize * framesperscroll;
		var scrollLeftValue = $(this).next().scrollLeft();
		if(scrollLeftValue == undefined) {
			scrollLeftValue = 0;
		}
		// Left Scroll Process
		if (scrollLeftValue > 0 && hasClicked == 0) {
			hasClicked = 1;
			$('#ScrollItemsScrollRight').removeClass('disabled').css("background", "url(http://resource.deyogroup.com/349/button_right_green.png) no-repeat");
			if (scrollLeftValue - scrollIncrement <= 0) {
				$(this).next().animate({scrollLeft: 0}, 1000, 'easeInOutQuart', function () {
					hasClicked = 0;
					$('#ScrollItemsScrollLeft').addClass('disabled').css("background", "url(http://resource.deyogroup.com/349/button_left.png) no-repeat");
				});
			}
			else {
				$(this).next().animate({scrollLeft: scrollLeftValue - scrollIncrement}, 1000, 'easeInOutQuart', function () {hasClicked = 0;});
			}
		}
	});
	// End Left Scroll Script
	// Right Scroll Script	
	$('#ScrollItemsScrollRight').click(function () {
		// Set Variables For The Right Button
		var framebuffer = 0;
		var displaysize = framebuffer + 537;
		var displayframes = 3;
		var framesize = displaysize / displayframes;
		// var scrollitems = 18;
		var scrollitems = $('.calendarItem').size();
		var framesperscroll = 3;
		var scrollIncrement = framesize * framesperscroll;
		var scrollLeftValue = $(this).prev().scrollLeft();
		if(scrollLeftValue == undefined) {
			scrollLeftValue = 0;
		}
		var maxScroll = (framesize) * (scrollitems-displayframes)
		// Right Scroll Process
		if (scrollLeftValue + scrollIncrement < maxScroll && hasClicked == 0) {
			hasClicked = 1;
			$('#ScrollItemsScrollLeft').removeClass('disabled').css("background", "url(http://resource.deyogroup.com/349/button_left_green.png) no-repeat");
			$(this).prev().animate({scrollLeft: scrollLeftValue+ scrollIncrement}, 1000, 'easeInOutQuart', function () {hasClicked = 0;});
		}
		else if (scrollLeftValue < maxScroll && hasClicked == 0) {
			hasClicked = 1;
			$('#ScrollItemsScrollLeft').removeClass('disabled').css("background", "url(http://resource.deyogroup.com/349/button_left_green.png) no-repeat");
			$(this).prev().animate({scrollLeft: maxScroll}, 1000, 'easeInOutQuart', function () {
				hasClicked = 0;
				$('#ScrollItemsScrollRight').addClass('disabled').css("background", "url(http://resource.deyogroup.com/349/button_right.png) no-repeat");
			});
		}
	});
	// End Right Scroll Script
// END SCROLL FEATURE SCRIPT


















});

