// ###############################
// Класс показа лучших продаж на сайте
// ###############################
// ------- ДОбавить событие клика на рекламу
function cityTopSales(obj, delay, onClick){
	this._obj		= obj;
	this._delay		= delay * 1000;
	this.obj		= $('#' + obj + ' #container');
	this.curr_pos	= 0;
	this.last_pos	= 0;
	this.fOnClick	= onClick || null;
	this.init();
}

cityTopSales.prototype = {
	// Инициализируем объект
	init : function(){
		var oThis	= this;
		var items	= this.obj.find('.item');
		var items_cnt	= items.length;
		this.last_pos	= -items_cnt * 50
		var animation	= false;
		if(items_cnt > 0){
			if(items_cnt > 2){
				var	txt	= '<div class="item">' + $(items[0]).html() + '</div>';
				txt	+= '<div class="item">' + $(items[1]).html() + '</div>';
				this.obj.append(txt);
				animation	= true;
				items	= this.obj.find('.item');
				items_cnt	+= 2;
			}
			for(var i=1; i<items_cnt; i++){
				items[i].style.left = i*50 + '%';
			}
		}
		items.click(function(){oThis.click(this); return false;}).find('a').click(function(){oThis.click(this); return false;});
		if(animation){
			setTimeout(function(){oThis.animation()}, this._delay);
		}
	},
	// анимация перехода объекта
	animation : function(){
		var oThis	= this;
		this.obj.animate({left: this.curr_pos-50 + '%'}, 'slow', 'easeout', function(){
			oThis.curr_pos	-= 50;
			if(oThis.last_pos == oThis.curr_pos){
				oThis.curr_pos = 0;
				oThis.obj.css({left: 0});
			}
			setTimeout(function(){oThis.animation()}, oThis._delay);
		});
	},
	//обрабатываем нажатие клавиш
	click : function(obj){
		if(obj.nodeName == 'A'){
			obj = $(obj).parents('.item');
		}else{
			obj	= $(obj)
		}
		var id	= obj.attr('keywords');
		if(this.fOnClick && id) this.fOnClick(id);
	}
}
