// JavaScript Document

var image_rotation_count;
var current_image;
var int;

var main;

jQuery(document).ready(function($){

   //Front Page Rotation Section
   
   //Get Number of Posts
   image_rotation_count = $(".ani_holder .post").length;
   current_image = 0;
   main = $;
   
   start_rotation();
   
   
   $('.animation_con .prev_button').click(function() {
	
	clearInterval(int);
	var newID = return_prev_image();
	var marginOffset = return_margin_offset($, newID);
	 
	 image_move($, newID, marginOffset);

   });
   
   $('.animation_con .next_button').click(function() {
		
		clearInterval(int);
		
		var newID = return_next_image();
		var marginOffset = return_margin_offset($, newID);
		
		//alert(marginOffset);
	 
	 image_move($, newID, marginOffset);
		
   });
   
   //alert(image_rotation_count);

   
 });

function start_rotation(){
	int = setInterval("move_next()", 8000);
}

function move_next(){
	var newID = return_next_image();
	var marginOffset = return_margin_offset(main, newID);

	image_move(main, newID, marginOffset);
}

function image_move($,newID, marginOffset){
	$('.ani_holder').animate({
		marginLeft: marginOffset
	 }, {
		duration: 2000, 
		complete: function() {
		  current_image = newID;
		  updateArrows($);
		}
	  });
}

function updateArrows($){
	if(current_image == 0){
		$('.animation_con .prev_button').fadeOut( 500 );
	} else {
		if($('.animation_con .prev_button').css('display') == 'none'){
			$('.animation_con .prev_button').fadeIn( 500 );	
		}
	}
	
	if(current_image == (image_rotation_count -1)){
		$('.animation_con .next_button').fadeOut( 500 );
	} else {
		if($('.animation_con .next_button').css('display') == 'none'){
			$('.animation_con .next_button').fadeIn( 500 );	
		}
	}
}

function return_next_image(){
	if(current_image == 0 && image_rotation_count > 1){
		return 1;
	} else if (current_image < (image_rotation_count - 1)) {
		return current_image + 1;
	} else {
		return 0;	
	}
}

function return_prev_image(){
	if(current_image == 0 && image_rotation_count > 1){
		return image_rotation_count - 1;
	} else if (current_image > 0) {
		return current_image - 1;
	} else {
		return 0;
	}
}

function return_margin_offset($, imageNumber){
	var imageObjWidth = $('.animation_con').css('width'); 
	imageObjWidth = (imageObjWidth.substring(0,imageObjWidth.length-2));
	return (imageNumber * imageObjWidth) * -1;
}

