// JavaScript Document
jQuery.fn.ass = function(options){
 //настройки по умолчанию
	var options = jQuery.extend({
		time: 1000, // общее время отображения слайда
		speedIn: 1000, // время смены еффекта
		speedOut: 1000, // время смены еффекта
		main: '.slideshow', // основной котейнер слайдов
		cont: '.slide',
		pause: '#pause',
		play: '#play', // слайд
		prevcol: 5,
		autoplay: true
	},options);
	var msw=0;
	var startclick=false;
	var startcol=false;
	var asspp=false;
	var $assse=jQuery(this);
	var $asspe=jQuery(this).parent();
	var asslength=parseInt(jQuery(this).length)-1;
	
	var assarray=new Array();
	for (var i=0; i<=asslength;i++){
		var sw=0;
		sw=parseInt(jQuery(this).eq(i).css('width').replace('px',''));
		msw=parseInt(msw)+parseInt(sw)+2;
		$asspe.css('width',msw)
		$assse.eq(i).find('img').attr('num',i);		
		assarray[i]=new Array ();
	  	assarray[i][0]=jQuery(this).eq(i).find('img').attr('src');
		assarray[i][0]=assarray[i][0].replace('thumb_/','');
		assarray[i][1]=parseInt(i);
		assarray[i][2]=msw;
		//console.log(assarray[i]);
		//console.log(asslength);
	}
	jQuery.fn.addac=function(number){
		$assse.eq(parseInt(number)).addClass('activeSlide');
	}
	jQuery.fn.addrc=function(number){
		$assse.eq(parseInt(number)).removeClass('activeSlide');
	}
	jQuery.fn.addafters=function(number){
		jQuery(options.main).append('<div class="slide"><img src="'+assarray[number][0]+'" alt="Photo" num="'+number+'" /></div>');	
		//console.log(parseInt(number)-1);
		if (startcol){
			jQuery.fn.ss();
		}
	}
	jQuery.fn.addaftersfor=function(col,number){
		for (var i=0;i<col;i++){
			if(parseInt(number)+parseInt(col)<asslength){
				jQuery.fn.addafters(parseInt(number)+parseInt(i));
			}else{
				jQuery.fn.addafters(Math.abs(asslength-(parseInt(number)+parseInt(i))));
			}
		}
		jQuery.fn.addac(number);
		jQuery.fn.ss();
	}
	jQuery.fn.ss=function(){
		if (asspp){
			jQuery(this).stop();
		}else{
		var $el=jQuery(options.main+' '+options.cont+':first');
		var $next=$el.next();
		var num=$el.find('img').attr('num');
		if(assarray[$el.find('img').attr('num')][2]>1000){
			//var pw=assarray[parseInt($el.find('img').attr('num'))][2]
			$asspe.parent().parent().css('position','absolute').css('left',(1000-assarray[parseInt($el.find('img').attr('num'))][2]));
			jQuery('#hsb_hscrollerbar').css('left',Math.abs((1000-assarray[$el.find('img').attr('num')][2]))/(assarray[asslength][2]/1000));
		}else{
			$asspe.parent().parent().css('position','absolute').css('left',0);
			jQuery('#hsb_hscrollerbar').css('left',0);
		}
		$el.fadeIn(options.speedIn,function(){
			startcol=true;
			jQuery.fn.addrc(num);
			jQuery.fn.addac(parseInt(num)+1);
			$next.fadeIn(options.speedIn).delay(options.time);
			jQuery(this).fadeOut(options.speedOut,function(){
				jQuery(this).remove();
				if ((parseInt(num)+parseInt(options.prevcol))<=asslength){	
					jQuery.fn.addafters(parseInt(num)+parseInt(options.prevcol));
				}else{
					jQuery.fn.addafters(Math.abs(asslength-(parseInt(num)+parseInt(options.prevcol))));
				}
			});
		});}
	}
	jQuery(options.play).bind('click',function(){
		jQuery(this).css('display','none');
		jQuery(options.pause).css('display','block');
		jQuery(options.main+' '+options.cont+':not(:first)').remove();
		var num=jQuery(options.main+' '+options.cont+':first').find('img').attr('num');
		jQuery.fn.addaftersfor(parseInt(options.prevcol)-1,parseInt(num)+1);
		asspp=false;
		jQuery.fn.ss();
	})
	jQuery(options.pause).bind('click',function(){
		jQuery(this).css('display','none');
		jQuery(options.play).css('display','block');
		asspp=true;
	})
	jQuery(this).find('a').bind('click',function(){/////////////////////////////////////////////
		jQuery(options.pause).click();
		jQuery('.activeSlide').removeClass('activeSlide');
		var num=jQuery(this).find('img').attr('num');
		jQuery(options.main+' '+options.cont+':not(:first)').remove();
		startcol=false;
		jQuery.fn.addafters(num);
		jQuery.fn.addac(num);
		var $el=jQuery(options.main+' '+options.cont+':first');
		//var num=$el.find('img').attr('num');
		var $next=$el.next();
		$el.fadeOut(options.speedOut).delay(options.time);
		$next.fadeIn(options.speedIn,function(){
				$el.remove();
		});
	})
	if (options.autoplay){
		jQuery(options.pause).css('display','block');			
	}
	setTimeout(function(){jQuery.fn.addaftersfor(options.prevcol,0);},3000);
	/*jQuery.fn.addac(0);*/
	return 0; 	
}
jQuery(document).ready(function(){
jQuery("#backward").bind('click',function(){/////////////////////////////////////////////
var options = jQuery.extend({
		time: 1000, // общее время отображения слайда
		speedIn: 1000, // время смены еффекта
		speedOut: 1000, // время смены еффекта
		main: '.slideshow', // основной котейнер слайдов
		cont: '.slide',
		pause: '#pause',
		play: '#play', // слайд
		prevcol: 5,
		autoplay: true
	},options);

		jQuery('#pause').click();
		var num=jQuery('.activeSlide').find('img').attr('num');
		jQuery('.activeSlide').removeClass('activeSlide');
		jQuery(options.main+' '+options.cont+':not(:first)').remove();
		startcol=false;
		jQuery.fn.addafters(parseInt(num)-parseInt(1));
		jQuery.fn.addac(parseInt(num)-parseInt(1));
		var $el=jQuery(options.main+' '+options.cont+':first');
		//var num=$el.find('img').attr('num');
		var $next=$el.next();
		$el.fadeOut(options.speedOut).delay(options.time);
		$next.fadeIn(options.speedIn,function(){
				$el.remove();
		});
	})
jQuery("#forward").bind('click',function(){/////////////////////////////////////////////
var options = jQuery.extend({
		time: 1000, // общее время отображения слайда
		speedIn: 1000, // время смены еффекта
		speedOut: 1000, // время смены еффекта
		main: '.slideshow', // основной котейнер слайдов
		cont: '.slide',
		pause: '#pause',
		play: '#play', // слайд
		prevcol: 5,
		autoplay: true
	},options);

		jQuery('#pause').click();
		var num=jQuery('.activeSlide').find('img').attr('num');
		jQuery('.activeSlide').removeClass('activeSlide');
		jQuery(options.main+' '+options.cont+':not(:first)').remove();
		startcol=false;
		jQuery.fn.addafters(parseInt(num)+parseInt(1));
		jQuery.fn.addac(parseInt(num)+parseInt(1));
		var $el=jQuery(options.main+' '+options.cont+':first');
		//var num=$el.find('img').attr('num');
		var $next=$el.next();
		$el.fadeOut(options.speedOut).delay(options.time);
		$next.fadeIn(options.speedIn,function(){
				$el.remove();
		});
	})
});

