/**
 * @author dejan
 */
$.fn.domCarousel = function(options){
		var container = this;
		var itemClass = (options.itemClass)?(options.itemClass):("dc-item");
		var ti = (options.transitionInterval)?(options.transitionInterval):(10000);
		var startLabel = (options.startLabel)?(options.startLabel):("Старт");
		var stopLabel = (options.stopLabel)?(options.stopLabel):("Сопри");
		var backLabel = (options.backLabel)?(options.backLabel):("Назад");
		var forwardLabel = (options.forwardLabel)?(options.forwardLabel):("Напред");
		var displayButtons = (options.displayButtons)?(options.displayButtons):(true);
		var buttonClass = (options.buttonClass)?(options.buttonClass):('button');
		
		var intervalID = "";
		var mutex = false;
		var min_height = 0;
		
		
		
		$(container).find("."+itemClass+":gt(0)").hide();
		
		$(container).css("position","relative");
		$(container).css("margin-top","20px");
		$(container).find("."+itemClass).css("margin","0px");
		var currentIndex = 0;
		
		container.changeItem = function(){
			if(!mutex){
				mutex=true;
				$(container).find("."+itemClass+":eq("+currentIndex+")").hide("slide",{direction: 'left'},300,function(){
					nextToShow = (currentIndex == $(container).find("."+itemClass).length-1)?(0):(currentIndex+1);				
					$(container).find("."+itemClass+":eq("+nextToShow+")").show("slide",{direction: 'right'},300,function(){
						currentIndex = nextToShow;
						mutex=false;					
					});
				});
			}				
		}
		
		container.changeBack = function(){
			if (!mutex) {
				mutex = true;
				$(container).find("." + itemClass + ":eq(" + currentIndex + ")").hide("slide", {
					direction: 'right'
				}, 300, function(){
					nextToShow = (currentIndex == 0) ? ($(container).find("." + itemClass).length - 1) : (currentIndex - 1);
					$(container).find("." + itemClass + ":eq(" + nextToShow + ")").show("slide", {
						direction: 'left'
					}, 300, function(){
						currentIndex = nextToShow;
						mutex = false;
					});
				});
			}
		}
		
		container.play = function(){
			
			if(!intervalID){
				$(container).find("input.dc_button_ss").val(stopLabel);
				$(container).find("input.dc_button_ss").click(function(){
					container.stop();
				});
				intervalID = setInterval(function(){
					container.changeItem();
				},ti);
			}
		}
		
		container.stop = function(){
			if(intervalID){
				$(container).find("input.dc_button_ss").val(startLabel);
				$(container).find("input.dc_button_ss").click(function(){
					container.play();
				});
				clearInterval(intervalID);
				intervalID = null;
			}	
		}
		
		if(displayButtons){
			$(container).append('<span style="position:absolute;right:5px;top:-28px;"><input type="button" class="dc_button_back '+buttonClass+'" value="'+backLabel+'"/>&nbsp;<input type="button" class="dc_button_ss '+buttonClass+'" value="'+stopLabel+'"/>&nbsp;<input class="dc_button_forward '+buttonClass+'" type="button" value="'+forwardLabel+'" /></span>');
			
			$(container).find("input.dc_button_back").click(function(){
				container.stop();
				container.changeBack();
			});
			
			$(container).find("input.dc_button_forward").click(function(){
				container.stop();
				container.changeItem();
			});
		}
		container.play();
}
