$(document).ready(function(){ 
var Drum = new Drum();
Drum.Load(drumItems,'#divDrumMover');
Drum.Select(0);


$('DIV.drumstop').hover(function() {
	drumTimerBlocked = true;
}, function() {
	drumTimerBlocked = false;
});


var drumTimerBlocked = false;
var drumCurrentId = 0;
var drumTimerStopped = false;

setInterval(function() {
	
	if (drumTimerBlocked) return false;
	
	if (drumCurrentId==4 && !drumTimerStopped) {
		Drum.Select(0);
		drumTimerStopped = true;
	}
	if (drumTimerStopped) return false;
	Drum.Select(drumCurrentId++);

}, 4000);

function Drum()
{	
	this.goLeft=function()
	{
		if (!moving)
		{
			moving=true;
			movePrev(4);		
		}
	}
	
	this.goRight=function()
	{
		if (!moving)
		{
			moving=true;
			moveNext(4);
		}
	}
	
	
	this.Select=function(id)
	{
		Select(id);
	}
	
	function Select(id)
	{
		if (!moving)
		{
			pos=currentPos; if (fnext) pos-=3;
								
			if (((id-pos)>3 && (pos-id)<-3) || (id-pos)<(pos-id)){
				moving=true; moveNext(4);
			}
			
			itemToDisplay(id);
			/*highlightDot(id);*/
			highlightItem(id);
		}
	}
	
	
	this.Load=function(_displayItems,_place)
	{
		place=_place;
		displayItems=_displayItems;
		
		
		$(place+" > li.itemDrum").each(function(i){
			DrumArr[i]="<li item='"+i+"' class='itemDrum' id='itemDrum"+i+"'>"+$(this).html()+"</li>";
			//if(i<3)
			//DrumArr[i]=DrumArr[i]+"<li class=space></li>";
			if (i<4)
			RenderArr[i]=DrumArr[i];
		});
		
		render();
	}
	
	
	var displayItems=null;
	
	var DrumArr=new Array();
	
	var RenderArr=new Array();
	
	var place=null;
	
	var elementSize=157;
	
	var fnext=true;
	
	var moving=false;
	
	var dotActive=null;
	
	var itemActive=null;
	var itemActiveIndex=null;
	
	var currentPos=3;
	
	var movingCallBack=null;
	
	
	function itemToDisplay(id)
	{
		var item=displayItems[id];
	
		// Детальная область элемента
		$('#divDrumMainTitle').html('<a href="' + item.url + '">' + item.title + '</a>');
		$('#divDrumMainText').html('<a href="' + item.url + '">' + item.intro + '</a>');
		//$('#imgDrumMain').s('background-image', 'url("' + item.photo + '")').attr('src', item.gotVideo ? 'http://infox.ru/img/temp003-border-video-new.png' : 'http://infox.ru/img/temp003-border-new.png');
		$('#imgDrumMain').attr('src', item.photo);
		$('#linkDrumMain').attr('href', item.url); 
	}
	
	
	function highlightDot(id)
	{
		var drumDotNew = document.getElementById("imgDrumDot" + id);
		if (drumDotNew != dotActive) {
			if (dotActive) {
				dotActive.src = '/img/dot-pas-darkblue.gif';
			}
			drumDotNew.src = '/img/dot-act-darkblue.gif';
			dotActive = drumDotNew;
		}
	}
	
	
	function highlightItem(id)
	{
		var drumItemNew =  $("li#itemDrum" + id);
		if (drumItemNew != itemActive && drumItemNew) {
			if (itemActive) {
				//itemActive.css("background-image" , "url(/images/bg_online.gif)");
				itemActive.attr('class', "itemDrum");
			}
			//drumItemNew.css("background-image" , "url(/images/bg_online_act.gif)");
			drumItemNew.attr('class', "itemDrum_act");
			
			itemActive = drumItemNew;
			itemActiveIndex=id;
		}
	}

	function setItemsHoverEvents()
	{
		$(place+' li.itemDrum').hover(function() {
			var id = this.getAttribute('item');
			Select(id);
			drumTimerBlocked = true;
			drumCurrentId = id;
			return false;			
		}, function() {drumTimerBlocked = false;}).click(function() {
			document.location.href = $(this).find('A').attr('href');			
		});
	}
	
	
	function render()
	{
		var content = RenderArr.join('');
		
		$(place).empty();
		$(place).append(content);
		
		setItemsHoverEvents();
	}
	
	
	function renderWithLeftAlign(c)
	{
		render();
		$(place).css({left:"-"+(c*elementSize)+"px"});
	}
	
	
	function getNext()
	{
		fnext=true;
		
		if (++currentPos>DrumArr.length-1)
			currentPos=0;
			
		return currentPos;
	}
	
	function getPrev()
	{
		fnext=false;
		
		if (--currentPos<0)
			currentPos=DrumArr.length-1;
			
		return currentPos;
	}
	
	
	function getNextElements(c)
	{
		var buffer=new Array();
		
		if (!fnext)
			for(i=0;i<3;i++)
				getNext();
				
		for(i=0;i<c;i++)
			buffer[i]=DrumArr[getNext()];
			
		return buffer;
	}
	
	function getPrevElements(c)
	{
		var buffer=new Array();
		
		if (fnext)
			for(i=0;i<3;i++)
				getPrev();
				
		for(i=0;i<c;i++)
			buffer[i]=DrumArr[getPrev()];
			
		return buffer;
	}
	
	
	function moveNext(c)
	{
		RenderArr=RenderArr.concat(getNextElements(c));
		render();
		
		$(place).animate({left:'-'+(c*elementSize)+'px'},'normal','',function(){
			for(i=0;i<c;i++)
				RenderArr.shift();
				
			render();
			$(place).css({left:'0px'});
			moving=false;
			
			highlightItem(itemActiveIndex);
			
			if (movingCallBack!=null)
				movingCallBack();
		});
	}
	
	function movePrev(c)
	{
		buffer=getPrevElements(c);
		for (i=0;i<c;i++)
			RenderArr.unshift(buffer[i]);
			
		renderWithLeftAlign(c);
		
		
		$(place).animate({left:'0px'},'normal','',function(){
			for (i=0;i<c;i++)
				RenderArr.pop();
				
			render();
			moving=false;
			
			highlightItem(itemActiveIndex);
			
			if (movingCallBack!=null)
				movingCallBack();
		});
	}
}


});
