(function($) {

    $.fn.sliding = function(options) {
        return this.each(function() {   
            $.sliding(this, options);
        });
    };

    $.sliding = function(container, options) 
	{
        var settings = {
        	'animationtype':    'fade', //fade//scroll//slide//
            'speed':            'slow',
            'type':             'sequence', //sequence//random//
            'timeout':          3000,
            'containerheight':  'auto',
            'children':         null,
			'quan':				2
        };
		
        if (options)
            $.extend(settings, options);
       
        if (settings.children === null)
            var elements = $(container).children();
		else
            var elements = $(container).children(settings.children);
		
	   if (settings.quan != null)
	   {	
        if (elements.length > 1) 
		{
            $(container).css('position', 'relative').css('height', settings.containerheight).css('overflow','hidden').css('width', String($(elements[0]).attr("width")));
			
			var oldPath = $(elements[0]).attr('src');
			var rootPath = oldPath.slice(0,oldPath.lastIndexOf('1.jpg'));

			$(elements[1]).css('z-index', String(elements.length-1)).css('position', 'absolute').css('left','0').css('top','0').hide();
			$(elements[0]).css('z-index', String(elements.length)).css('position', 'absolute').css('left','0').css('top','0').hide();
 
		   //вызов смены слайдов по порядку
		   if (settings.type == "sequence") 
		   {  
				setTimeout(function() 
					{
						$.sliding.next(elements, settings, 2, 1, rootPath);
					}, settings.timeout);
				$(elements[0]).show();
           } 
		   
		   //вызов смены слайдов в случайном порядке
		   if (settings.type == "random") 
		   {  
				var last = Math.ceil((Math.random()*10000) % settings.quan);
				$(elements[0]).attr({src: rootPath + last + '.jpg'});
				$(elements[0]).show();
				var current = Math.ceil((Math.random()*10000) % settings.quan);
				while (last == current)
				{
					current = Math.ceil((Math.random()*10000) % settings.quan);
				}
				
				setTimeout(function() 
					{
						$.sliding.next(elements, settings, current, last, rootPath);
					}, settings.timeout);
				
           } 
	
        } 
	   } else alert('Количество слайдов не указано');
    };
	
	 $.sliding.next = function(elements, settings, current, last, rootPath) 
	 {
	 /////////////////////--Проявление--//////////////////////////////////
        if (settings.animationtype == 'fade') 
		{
			var newPath = rootPath + current + '.jpg';
			var tmpimg = new Image;
			
			
			$(elements[1]).css('z-index', String(elements.length)).css('position', 'absolute').css('left','0').css('top','0').hide();
			
			
			$(tmpimg).load( function ()
				{
					$(elements[1]).attr({src: newPath});
					
					$(elements[1]).fadeIn(settings.speed, function() 
						{
							removeFilter($(this)[0]);
						});
				
				});
				$(tmpimg).attr({src: newPath});
				$(elements[0]).attr({src: rootPath + last + '.jpg'});
        } 
	///////////////////--Перелистывание--////////////////////////////////////////////
	if (settings.animationtype == 'slide') 
		{
			var newPath = rootPath + current + '.jpg';
			var tmpimg = new Image;
			
			
			$(elements[1]).css('z-index', String(elements.length)).css('position', 'absolute').css('left','0').css('top','0').hide();
			
			
			$(tmpimg).load( function ()
				{
					$(elements[1]).attr({src: newPath});
					
					$(elements[1]).slideDown(settings.speed);
				
				});
				$(tmpimg).attr({src: newPath});
				$(elements[0]).attr({src: rootPath + last + '.jpg'});
        } 
		
	///////////////////--Сдвиг--////////////////////////////////////////////
	if (settings.animationtype == 'scroll') 
		{
			var newPath = rootPath + current + '.jpg';
			var tmpimg = new Image;
			
			$(elements[1]).css('z-index', String(elements.length)).css('position', 'absolute').css('left','0').css('top','0').hide();
			$(elements[0]).show();
			
			$(tmpimg).load( function ()
				{
					$(elements[1]).attr({src: newPath});
					
					
					$(elements[1]).css({marginLeft:String($(elements[0]).attr("width"))}).fadeIn();
					$(elements[0]).animate({"left": -String($(elements[0]).attr("width"))}, "easein", function()
								{
									$(elements[0]).hide();
								});
					
					$(elements[1]).animate({"left": -String($(elements[0]).attr("width"))}, "easein");
					
				
				});
				$(tmpimg).attr({src: newPath+"?"+Math.random()*1000});
				$(elements[0]).attr({src: rootPath + last + '.jpg'});
				$(elements[0]).css({"left": String($(elements[0]).attr("width"))});
        } 
		
	//переход к следующему слайду	
		if (settings.type == "sequence") 
		{
            if ((current + 1) <= settings.quan) {
                current = current + 1;
                last = current - 1;
            } 
			else 
			{
                current = 1;
                last = settings.quan;
            }
        }
		else 
			if (settings.type == "random") 
			{
				$(elements[0]).attr({src: rootPath + last + '.jpg'});
				$(elements[0]).show();
				last = current;
				do
				{
					current = Math.ceil((Math.random()*10000) % settings.quan);
				}
				while (current == last);
			} 
			else
				alert('Тип смены изображений может быть только \'sequence\', \'random\' или \'random_start\'');
		
		setTimeout((function() 
				{
					$.sliding.next(elements, settings, current, last, rootPath);
				}), settings.timeout);
    };

})(jQuery);

// **** remove Opacity-Filter in ie ****
function removeFilter(element) 
{
	if(element.style.removeAttribute)
	{
		element.style.removeAttribute('filter');
	}
}

