$(function() {
	$.fn.slider = function(options){ 	

	var prefix = options.sliderPrefix; //#slider1 
	//alert(prefix);
	
	var totalPanels			= $(prefix+"_scrollContainer").children().size();
		
	var regWidth			= $(".panel").css("width");
	var regImgWidth			= $(".panel img").css("width");
	var regTitleSize		= $(".panel h2").css("font-size");
	var regParSize			= $(".panel p").css("font-size");
	
	var movingDistance	    = 180;
	
	var curWidth			= regWidth;
	var curImgWidth			= regImgWidth;
	var curTitleSize		= regTitleSize;
	var curParSize			= regParSize;

	var $panels				= $(prefix+' '+prefix+'_scrollContainer > div');
	var $container			= $(prefix+' '+prefix+'_scrollContainer');

	$panels.css({'float' : 'left','position' : 'relative'});
    
	$(prefix).data("currentlyMoving", false);

	$container
		.css('width', ($panels[0].offsetWidth * $panels.length) + 100 )
		.css('left', "-350px");

	var scroll = $(prefix+' .scroll').css('overflow', 'hidden');

	function returnToNormal(element) {
		$(element)
			.animate({ width: regWidth })
			.find("img")
			.animate({ width: regImgWidth })
		    .end()
			.find("h2")
			.animate({ fontSize: regTitleSize })
			.end()
			.find("p")
			.animate({ fontSize: regParSize });
	};
	
	function growBigger(element) {
		$(element)
			.animate({ width: curWidth })
			.find("img")
			.animate({ width: curImgWidth })
		    .end()
			.find("h2")
			.animate({ fontSize: curTitleSize })
			.end()
			.find("p")
			.animate({ fontSize: curParSize });
	}
	
	//direction true = right, false = left
	function change(direction) { 
	   
	    //if not at the first or last panel

if(direction && !(curPanel < totalPanels-4)) {
$(prefix+"_right").css({"background" : "url(/bitrix/templates/.default/components/bitrix/news.list/slider_new/images/arrow_gal_right_inactive.gif) 0 0 no-repeat" } ); } else {
$(prefix+"_right").css({"background" : "url(/bitrix/templates/.default/components/bitrix/news.list/slider_new/images/arrow_gal_right.gif) 0 0 no-repeat" } );
} 
if(!direction && (curPanel <= 2)) {
$(prefix+"_left").css({"background" : "url(/bitrix/templates/.default/components/bitrix/news.list/slider_new/images/arrow_gal_left_inactive.gif) 0 0 no-repeat" }); } else {
$(prefix+"_left").css({"background" : "url(/bitrix/templates/.default/components/bitrix/news.list/slider_new/images/arrow_gal_left.gif) 0 0 no-repeat" });
}

		if((direction && !(curPanel < totalPanels-3)) || (!direction && (curPanel <= 1))) { return false; }	
        
        //if not currently moving
        if (($(prefix).data("currentlyMoving") == false)) {
            
			$(prefix).data("currentlyMoving", true);
			
			var next         = direction ? curPanel + 1 : curPanel - 1;
			var leftValue    = $(prefix+"_scrollContainer").css("left");
			var movement	 = direction ? parseFloat(leftValue, 10) - movingDistance : parseFloat(leftValue, 10) + movingDistance;
		
			$(prefix+"_scrollContainer")
				.stop()
				.animate({
					"left": movement
				}, function() {
					$(prefix).data("currentlyMoving", false);
				});
			
			returnToNormal(prefix+"_panel_"+curPanel);
			growBigger(prefix+"_panel_"+next);
			
			curPanel = next;
			
			//remove all previous bound functions
			$(prefix+"_panel_"+(curPanel+1)).unbind();	
			
			//go forward
			$(prefix+"_panel_"+(curPanel+1)).click(function(){ change(true); });
			
            //remove all previous bound functions															
			$(prefix+"_panel_"+(curPanel-1)).unbind();
			
			//go back
			$(prefix+"_panel_"+(curPanel-1)).click(function(){ change(false); }); 
			
			//remove all previous bound functions
			$(prefix+"_panel_"+curPanel).unbind();
		}
	}
	
	// Set up "Current" panel and next and prev
	var curPanel = 3;
	growBigger(prefix+"_panel_"+curPanel);	
	
	$(prefix+"_panel_"+(curPanel+1)).click(function(){ change(true); });
	$(prefix+"_panel_"+(curPanel-1)).click(function(){ change(false); });
	
	//when the left/right arrows are clicked
	$(prefix+"_right").click(function(){ change(true); });	
	$(prefix+"_left").click(function(){ change(false); });
	
	
}
	
});	
