(function() {
	var buttonsHeight = 24;

	jQuery('.jir-buttons')
		//.css('border', 'solid blue 1px')
		.css('height', buttonsHeight);
		
	jQuery('.jir-content')
		//.css('border', 'solid red 1px')
		//.css('height', contentHeight);
		

	JIR = {
		news: [],
		
		cfg: {
			delay: 5000 // 5 sec
		},
		
		init: function(vesti, imgVestiCPath) {
			if(vesti) {
				this.news = vesti;
				this.createLeftButons(imgVestiCPath);
				this.createNavButtons();
				this.createDivs();
			} else {
				var _this = this;
				jQuery.getJSON('topVesti.ajax', {}, function(list) {
					_this.news = list;
					_this.createLeftButons();
					_this.createNavButtons();
					_this.createDivs();
				});
			}
		},
		
		createDivs: function() {
			
			var container = jQuery('.jir-left');
			
			container.css('overflow', 'hidden');
			
			for(var i=0; i<this.news.length; i++) {
				var d = jQuery('<div />')
					.css('display', 'none')
					.appendTo(container);
				var n = this.news[i];
				d.html(this.generateVestContent(n));
				n.container = d;
			}
			
			//jQuery('a.lb-img-gr').lightbox();
			
			this.news[0].container.show();
			this.active = 1;
			this.start(true);
			
			var _this = this;
			jQuery('#jir-btn-play-pause').click(function() {
				if(_this.playing)
					_this.stop();
				else
					_this.start();
			});
			
		},
		
		generateVestContent: function(n) {
			return [
				'<div style="width: 326px; height: 240px;">',
				'<div style="height: 240px">',
					'<a class="lb-img-gr" rel="lightbox[jir.images]" href="', telegraf.siteUrl + '/' + n.path + '/' + n.images[0]+'" title="'+n.introText+'">',
						'<img src="', telegraf.siteUrl + '/' + n.path + '/' + n.images[0]+'?fmt=medium"/>',
					'</a>',
				'</div>',
				'</div>'
			].join('');
		},
		
		createLeftButons: function(imgVestiCPath) {
			
			var buf = ['<ul class="jir-buttons-ul">'];
			for(var i=0; i<this.news.length; i++) {
				var k = i+1;
				var id = 'jir-brojce-' + i;
				buf.push('<li><a id='+id+' style="padding-top:7px" class="jir-brojce-icon'+(i==0?' jir-brojce-active-icon':'')+'" href="javascript:void(0)" onclick="JIR.showVestBtn('+k+')">'+k+'</a></li>');
			}
			
			buf.push('<li style="padding: 2px 0px 0px 60px;"> <a href="'+telegraf.siteUrl+'/'+imgVestiCPath+'" style="color: white; font-size: 11px; font-weight: normal;">Архива</a> </li>');
			buf.push('</ul>');
			
			jQuery('#jir-buttons-left-td-id').html(buf.join(''));
		},
		
		createNavButtons: function() {
			var buf = ['<div style="margin-top: 0px; margin-right: 4px;"><ul class="jir-buttons-ul">',
				'<li><a class="jir-prev-icon" href="javascript:void(0)" onclick="JIR.showPrevBtn()"></a></li>',
				'<li><a class="jir-pause-icon" id="jir-btn-play-pause" href="javascript:void(0)"></a></li>',
				'<li><a class="jir-next-icon" href="javascript:void(0)" onclick="JIR.showNextBtn()"></a></li>',
			'<ul></div>'];
			jQuery('#jir-buttons-nav').html(buf.join(''));
		},
		
		showVestBtn: function(n) {
			if(this.playing) {
				this.stop();
				//this.start(true);
			}
			this.show(n);
		},
		
		showNextBtn: function() {
			if(this.playing) {
				this.stop();
			}
			this.showNext();
		},
		
		showPrevBtn: function() {
			if(this.playing) {
				this.stop();
			}
			this.showPrevious();
		},
		
		showNext: function() {
			var a = this.active || 0;
			if(a==this.news.length) a=0;
			this.show(a+1);
		},
		
		showPrevious: function() {
			var a = this.active;
			if(a==1) a=this.news.length+1;
			this.show(a-1);
		},
		
		show: function(number) {
			if(this.busy) return;
			if(number==0 || number>this.news.length) {
				console.error('invalid number' + number);
				return;
			}
			
			var _this = this;
		
			for(var i=0; i<this.news.length; i++) {
				this.news[i].container.hide();
				jQuery('#jir-brojce-'+i).removeClass('jir-brojce-active-icon');
			}
			
			_this.active = number;
			jQuery('#jir-brojce-'+(number-1)).addClass('jir-brojce-active-icon');
			_this.busy = true;
			_this.news[_this.active-1].container.fadeIn(200, function() {
				_this.busy = false;
			});
			
			
			
		},
		
		start: function(notNow) {
			var _this = this;
			this.stop();
			var loop = function() {
				if(!notNow) _this.showNext(); notNow = false;
				_this._toId = setTimeout(loop, _this.cfg.delay);
			}
			loop();
			_this.playing=true;
			jQuery('#jir-btn-play-pause').removeClass('jir-play-icon');
			jQuery('#jir-btn-play-pause').addClass('jir-pause-icon');
		},
		
		stop: function() {
			if(this._toId) clearTimeout(this._toId);
			this.playing = false;
			//this._toId = false;
			jQuery('#jir-btn-play-pause').removeClass('jir-pause-icon');
			jQuery('#jir-btn-play-pause').addClass('jir-play-icon');
		}
	}
	
})();
