/* Для галереи авто */
var showDuration = 5000;
var autoImages;
var currentIndex = 0;

window.addEvent('domready', function() {
	if($('latestAdverts')) {
		autoImages = $$('#latestAdverts .slideshowThumbnail');

		autoImages.each(function(element, index) {
			element.fade('hide');
		});
		playGallery();
		playGallery.periodical(showDuration);
	}
	
	if($('gallery')) {
		var gallery = new GalleryLister('gallery');
		gallery.load();
	}
	if($('latestReceipts')) {
		var lady = new GalleryLister('latestReceipts');
		lady.load();
	}
	if($('latestGames')) {
		var latestGames = new GalleryLister('latestGames');
		latestGames.hi = 110;
		latestGames.load('vertical');
	}
	if($('topGames')) {
		var topGames = new GalleryLister('topGames');
		topGames.hi = 110;
		topGames.load('vertical');
	}
});

function playGallery() {
	autoImages[currentIndex].fade('out');
	autoImages[currentIndex = currentIndex < autoImages.length - 1 ? currentIndex+1 : 0].fade('in');
}


var GalleryLister = new Class({
	getOptions: function(){
		return {
			transition: Fx.Transitions.Back.easeOut,
			loop: true
		};
	},
	initialize: function(container, options){
		this.setOptions(this.getOptions(), options);
		
		this.transition = Fx.Transitions.Linear;
		
		this.container = $(container);
		this.begin = 0; // Позиция начального элемента
		this.wi = 240; // Ширина элемента галереи
		this.hi = 0;
		this.show = -1;
		this.cc = 0; // Количество элементов в галерее
		this.end = 0; // Позиция последнего элемента
		this.elementWidth;
		this.liCount = 0;
		this.ml = 0; // Текущая позиция
		this.layer;
		
		this.myNewEffect;
		
		
	},
	load: function(type){
		if(!type) {
			this.object = $$('#'+this.container.id+' .cont_tr');
			this.object = this.object[0];
		
			for (var childItem in this.object.childNodes) {
				if (this.object.childNodes[childItem].nodeType == 1)
					this.liCount++;
			}
			this.cc = this.liCount;
			this.end = this.wi * this.liCount;
			this.layer = $$('#'+this.container.id+' .cont_table')[0];
			
			this.elementWidth = this.wi;//$$('#'+this.container.id+' .galleryIn')[0].offsetWidth;
			
			this.ml = (this.layer.style.marginLeft == "") ? 0 : this.layer.style.marginLeft;
			this.myNewEffect = new Fx.Tween(this.layer, {duration: 500, transition:this.transition});
			
			$$('#'+this.container.id+' .arrow_l')[0].addEvent('click', function(event) {
				this.move('left');
				return false;
			}.bind(this));
			$$('#'+this.container.id+' .arrow_r')[0].addEvent('click', function(event) {
				this.move('right');
				return false;
			}.bind(this));
		}
		else if(type == 'vertical') {
			this.object = this.container;
			this.liCount = $$('#'+this.object.id+' tr td').length;
			this.cc = this.liCount;
			
			this.layer = $$('#'+this.container.id+' .cont_table')[0];
			
			this.elementWidth = this.hi;// = $$('#'+this.object.id+' tr td')[1].getStyle('height').substr(0, $$('#'+this.object.id+' tr td')[1].getStyle('height').length-2);

			this.end = this.hi * this.liCount;
			
			this.ml = (this.layer.style.marginTop == "") ? 0 : this.layer.style.marginTop;
			this.myNewEffect = new Fx.Tween(this.layer, {duration: 500, transition:this.transition});
			
			$$('#'+this.container.id+' .arrow_l')[0].addEvent('click', function(event) {
				this.moveVertical('top');
				return false;
			}.bind(this));
			$$('#'+this.container.id+' .arrow_r')[0].addEvent('click', function(event) {
				this.moveVertical('bottom');
				return false;
			}.bind(this));
		}
	},
	move: function(direction) {
		var ww = this.elementWidth;
		var ee = this.end;
		if(this.layer.style.marginLeft.length > 0)
			this.ml = this.layer.style.marginLeft.substr(0, this.layer.style.marginLeft.length-2);
		else
			this.ml = 0;
		
		
		if(direction == 'right') {
			if(ee-(-this.ml+ww) > 0 && ee-(-this.ml+ww) >= this.wi)
				this.myNewEffect.start('margin-left', this.ml-this.wi);
			else if(ee-(-this.ml+ww) > 0 && ee-(-this.ml+ww) < this.wi)
				this.myNewEffect.start('margin-left', -(ee-(-this.ml+ww)));
			else
				this.myNewEffect.start('margin-left', 0);
		}
		else if(direction == 'left') {
			if(this.ml < 0 && -this.ml >= this.wi)
				this.myNewEffect.start('margin-left', (this.ml*1)+this.wi);
			else if(this.ml < 0 && -this.ml < this.wi)
				this.myNewEffect.start('margin-left', -this.ml);
			else
				this.myNewEffect.start('margin-left', -ee+this.wi);
		}
	},
	moveVertical: function(direction) {
		var ww = this.elementWidth;
		var ee = this.end;
		if(this.layer.style.marginTop.length > 0)
			this.ml = this.layer.style.marginTop.substr(0, this.layer.style.marginTop.length-2);
		else
			this.ml = 0;
		
		if(direction == 'top') {
			if(ee-(-this.ml+ww) > 0 && ee-(-this.ml+ww) >= this.hi)
				this.myNewEffect.start('margin-top', this.ml-this.hi);
			else if(ee-(-this.ml+ww) > 0 && ee-(-this.ml+ww) < this.hi) {
				alert("Ok");
				this.myNewEffect.start('margin-top', -(ee-((-this.ml*1)+ww)));
			}
			else
				this.myNewEffect.start('margin-top', 0);
		}
		else if(direction == 'bottom') {
			
			if(this.ml < 0 && -this.ml >= this.hi)
				this.myNewEffect.start('margin-top', (this.ml*1)+this.hi);
			else if(this.ml < 0 && -this.ml < this.hi)
				this.myNewEffect.start('margin-top', -this.ml);
			else
				this.myNewEffect.start('margin-top', -ee+this.hi);
		}
	}
});
GalleryLister.implement(new Options);
GalleryLister.implement(new Events);

