var divSource=[];
var $next=null;
var counter=0;
var intervalId=null;
var startRotation = null;
var totImage=0;
var courrentDot=null;
var hovePath="";
var normalPath="";


$(document).ready( function(){
				  
				  $('.slider').find('.slides').each(function(n){
																	   		
																divSource[n]=$(this).html();	   	   
																	   
																});
				  $(".banner_buttons").find(".dot").each(function(n){
																  		$(this).attr("serial",n);
																  });
				  
				  totImage=divSource.length;
				  currentDot=$(".banner_buttons").find(".dot").eq(0);
				  hoverPath=currentDot.attr("src");
				  normalPath=$(".banner_buttons").find(".dot").eq(1).attr("src");
				 
				  setStructure();
				  setControl();
				  play();
				 });

function setStructure()
{
	$('.slider').html("");
	
	var CONTENT="<div class='prev'>"+divSource[0]+"</div><div class='next'></div>"
	
	$(".slider").html(CONTENT);
	$next=$('.next');
	$next.hide();
	
}

function imageAction(num)
{
			setCurrent($(".banner_buttons").find(".dot").eq(num));
			currentDot=$(".banner_buttons").find(".dot").eq(num);
	
	$('.next').html("");
	
	$('.next').html(divSource[num]);
	$('.prev').fadeOut();
	$('.next').fadeIn(function(){
							 
								$('.prev').html(divSource[num]);
								$('.prev').show();
							  	$(this).hide();
							  })
}

function setControl()
{
	$('.dot').bind('click',mouseAction);
	$('.lA').bind('click',mouseAction);
	$('.rA').bind('click',mouseAction);
}


function mouseAction(e)
{
	switch($(this).attr('class'))
	{
		case 'dot':
			var num = Number($(this).attr('serial'));
			
			stopPlay();
			counter=num;
			
			imageAction(counter);
			callRotation();
		break;
		
		case 'rA':
			if(counter<totImage-1)
			{	
				counter++;
			}
			else
			{
			counter=0;
			}
			stopPlay();
			
			imageAction(counter);
			callRotation();
		break;
		
		case 'lA':
			if(counter>0)
			{
				counter--;
			}
			else
			{
			counter=totImage-1;
			}
			stopPlay();
			
			imageAction(counter);
			callRotation();
		break;
	}

}

function setCurrent(mc)
{
	if(currentDot!=null)
	{
		currentDot.attr('src',normalPath);
	}
	mc.attr('src',hoverPath);

}




function changeSlide()
{
	 counter++;
 
	if(counter==3)
	{
		counter=0;
	}
	imageAction(counter);
}

function stopPlay()
{
	clearInterval(intervalId);
	if(startRotation!=null)
	{
	clearInterval(startRotation);
	}
}

function play()
{
	if(startRotation!=null)
	{
		clearInterval(startRotation);
	}
intervalId=setInterval(changeSlide,7000);

}

function callRotation()
{
	startRotation = setInterval(play,13000);
}
