$(document).ready(function() {
//Show the paging and activate its first link
$("#slider .paging").show();
$("#slider .paging a:first").addClass("active");

//Get size of the image, how many images there are, then determin the size of the image reel.
var imageWidth = 600,
	imageSum = $("#slider .image_reel .slider_block").size(),
	imageReelWidth = imageWidth * imageSum,
	play,
	moveImage_reel = false;

var time =  $('#slider .paging a:first').attr('name')*1000;

//Adjust the image reel to its new size
$("#slider .image_reel").css({'width' : imageReelWidth});

//Paging  and Slider Function
rotate = function(){
	
	$('#next_block, #prev_block').unbind('click');
	
    var triggerID = $active.attr("rel"); //Get number of times to slide
    var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide
	
	if(image_reelPosition == ((imageSum-1)*imageWidth) && $direction == 'prev') {
		$('#slider .image_reel .slider_block').clone().addClass('clone').prependTo('#slider .image_reel');
		$('.clone').prependTo('#slider .image_reel');
		
		$("#slider .image_reel").css({
			'width' : (imageReelWidth + imageWidth)*2,
			'left' : '-'+(imageSum*imageWidth)+'px'
		});
		image_reelPosition = (imageSum-1)*imageWidth;
		
	} else if(image_reelPosition == 0 && $direction == 'next') {
		$('#slider .image_reel .slider_block').clone().addClass('clone').appendTo('#slider .image_reel');
		
		$("#slider .image_reel").css({
			'width' : (imageReelWidth + imageWidth)*2,
			'left' : '-'+((imageSum-1)*imageWidth)+'px'
		});
		image_reelPosition = (imageSum)*imageWidth;
	}

    $("#slider .paging a").removeClass('active'); //Remove all active class
    $active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)

    //Slider Animation
    $("#slider .image_reel").animate({
        left: -image_reelPosition
    }, 1000, function() {
		if($('.clone').length > 0) {
			$('#slider .image_reel .slider_block').each(function(){
				if($(this).hasClass('clone')) $(this).removeClass('clone');
				else $(this).remove();
				if($direction == 'next') {
					$("#slider .image_reel").css({
						'width' : imageReelWidth,
						'left' : 0
					});
				}
			});
		}
		clickfunctions();
	});
	
	time = $active.attr('name')*1000;
	clearInterval(play);
	rotateSwitch();
	
}; 

//var time =1000000*1000;
//Rotation  and Timing Event
rotateSwitch = function(){
	
    play = setInterval(function(){ //Set timer - this will repeat itself every 7 seconds
        $active = $('#slider .paging a.active').next(); //Move to the next paging
		$prev = $('#slider .paging a.active'); //Get previous slide
		$direction = 'next';
        if ( $active.length === 0) { //If paging reaches the end...
            $active = $('#slider .paging a:first'); //go back to first
        }
		if($prev.length == 0) {
			$prev = $('#slider .paging a:last'); //get last image as previous
		}
		time = $active.attr('name')*1000;
		
        rotate(); //Trigger the paging and slider function
    }, time); //Timer speed in milliseconds (7 seconds)
};

//On Hover
$("#slider .image_reel .slider_block").hover(function() {
    clearInterval(play); //Stop the rotation
}, function() {
    rotateSwitch(); //Resume rotation timer
});	

//On Click
$("#slider .paging a").click(function() {
    $active = $(this); //Activate the clicked paging
	$direction = 'btn';
    //Reset Timer
    clearInterval(play); //Stop the rotation
    rotate(); //Trigger rotation immediately
    //rotateSwitch(); // Resume rotation timer
    return false; //Prevent browser jump to link anchor
});


/*var is_chrome = /chrome/.test( navigator.userAgent.toLowerCase() );
if(is_chrome == true) {*/
	$(window).blur(function() {
		clearInterval(play);				
	});
	$(window).focus(function() {
		clearInterval(play);
		rotateSwitch();				 
	});
//}


// next and previous buttons and stuff
$('.next_block_btn, .prev_block_btn').hide();
$('#next_block').hover(function(){
	$('.next_block_btn').fadeIn(150);
}, function() {
	$('.next_block_btn').fadeOut(150);
});
$('#prev_block').hover(function(){
	$('.prev_block_btn').fadeIn(150);
}, function() {
	$('.prev_block_btn').fadeOut(150);
});

var clickfunctions = function() {
$('#next_block').click(function() {
	$active = $('#slider .paging a.active').next();
	$direction = 'next';
	if ($active.length === 0) { //If paging reaches the end...
		$active = $('#slider .paging a:first'); //go back to first
	}
	clearInterval(play); //Stop the rotation
    rotate(); //Trigger rotation immediately
   // rotateSwitch(); // Resume rotation timer
	return false;
});

$('#prev_block').click(function() {
	$active = $('#slider .paging a.active').prev();
	$direction = 'prev';
	if ($active.length === 0) { //If paging reaches the end...
		$active = $('#slider .paging a:last'); //go back to first
	}
	clearInterval(play); //Stop the rotation
    rotate(); //Trigger rotation immediately
   // rotateSwitch(); // Resume rotation timer
	return false;
});
}

clickfunctions();


rotateSwitch();
});
