jQuery(document).ready(function(){
	ENscroller.init();
});

ENscroller = { //settings or configration 
	settings: {
		speed: 3000, //scrolling speed
		fadingSpeed: 100, // *****Depreciated *****  fadding speed
		animationInterval : 100,
		direction : 'right', // default scroll direction (left or right)
		visibleImgQty: '3', // *****Depreciated *****  visible image quantity inside of scroller
		readyToPlay: false, // 
		fastPlay: false, // 
		readyToScroll: false,
		scrollWidth: 1000, // *****Depreciated ***** scroll width
		imgWidth: 165 // scroll per item/images scroll 
	},
	init: function(){ // inter in script 
		setInterval( "ENscroller.getStart()", ENscroller.settings.animationInterval );  // call continue function after defined time session
		ENscroller.getStart();
	},
	getStart: function(){ // process to scrolling 
		if(ENscroller.settings.readyToPlay){ 
			if(ENscroller.settings.direction == 'left'){ 
				ENscroller.scrollLeft();
			}else{
				ENscroller.scrollRight();
			}
		}else{
			if(!ENscroller.settings.readyToScroll){				
					ENscroller.setPosition();				
			}
		}
	},
	setPosition: function(){		
		scrollItems = jQuery("#scrollerArea").find("li");
		scrollItems.each(function(){
			jQuery(this).css({background: 'url(images/scroll_img/'+jQuery(this).attr("rel")+')'});
		});
		scrollerContent = jQuery("#scrollerArea").find("ul");
		scrollerContent.css({width: (scrollItems.length*ENscroller.settings.imgWidth)+'px'});
		jQuery("#scrollerArea").prepend(scrollerContent.clone());
		jQuery("#scrollerArea").prepend(scrollerContent.clone());
		
		leftPos = scrollItems.length*ENscroller.settings.imgWidth-scrollItems.length*ENscroller.settings.imgWidth*2;
		jQuery("#scrollerArea").find("ul").each(function(){
			jQuery(this).css({left: leftPos+'px'});
			leftPos = leftPos+parseInt(jQuery(this).css("width"));
		});
		ENscroller.settings.readyToPlay = true;
		ENscroller.settings.readyToScroll = true;
		ENscroller.bindClicks();
	},
	bindClicks: function(){
		jQuery("#scrollerArea").prepend("<div id='DummyScrollLeft'><div id='scrollLeft'></div></div>");
		jQuery("#scrollerArea").append("<div id='DummyScrollRight'><div id='scrollRight'></div></div>");
		jQuery("#scrollLeft").click(function(){
			ENscroller.settings.fastPlay  = true;
			ENscroller.settings.direction = 'left';
			ENscroller.settings.readyToPlay = true;
			jQuery("#scrollerArea").find("ul").each(function(){
				jQuery(this).stop(false, true);
			});
		});
		jQuery("#scrollRight").click(function(){
			ENscroller.settings.fastPlay  = true;
			ENscroller.settings.direction = 'right';
			ENscroller.settings.readyToPlay = true;
			jQuery("#scrollerArea").find("ul").each(function(){
				jQuery(this).stop(false, true);
			});
		});	
	},
	scrollRight: function() {		
		ENscroller.settings.readyToPlay = false;
		
		if(ENscroller.settings.fastPlay){
			aniSpeed = ENscroller.settings.speed/3;
			ENscroller.settings.fastPlay = false;
		}else{
			aniSpeed = ENscroller.settings.speed;
		}

		jQuery("#scrollerArea").find("ul").each(function(n){
			jQuery(this).animate({left: parseInt(jQuery(this).css("left")) - ENscroller.settings.imgWidth}, aniSpeed, 'linear',  function(){	
				if(parseInt(jQuery(this).css("left")) <= parseInt(jQuery(this).width())-parseInt(jQuery(this).width()*3)){					
					newPos = leftPos-parseInt(jQuery(this).width());					
					jQuery(this).css({left: newPos+'px'});					
				}
				if(n == 2){
					ENscroller.settings.readyToPlay = true;
				}
			});
		});

	},
	scrollLeft: function() {		
		ENscroller.settings.readyToPlay = false;

		if(ENscroller.settings.fastPlay){
			aniSpeed = ENscroller.settings.speed/3;
			ENscroller.settings.fastPlay = false;
		}else{
			aniSpeed = ENscroller.settings.speed;
		}

		jQuery("#scrollerArea").find("ul").each(function(n){
			
			jQuery(this).animate({left: parseInt(jQuery(this).css("left")) + ENscroller.settings.imgWidth}, aniSpeed, 'linear',  function(){	
				if(parseInt(jQuery(this).css("left")) >= parseInt(jQuery(this).width())*2){					
					newPos = parseInt(jQuery(this).width())-parseInt(jQuery(this).width())*2;					
					jQuery(this).css({left: newPos+'px'});					
				}
				if(n == 2){
					ENscroller.settings.readyToPlay = true;
				}
			});
		});

	}
}

