var MagazineAnimations = {
	initialize: function () {
		this.element = $('#magazine_cover');
		this.elemW = this.element.outerWidth();
		this.elemH = this.element.outerHeight();
		this.element.css('width', this.elemW + 'px');
		this.element.css('height', this.elemH + 'px');
		/*this.news = [
		            '<strong>Плевенлиев измести Борисов по рейтинг</strong> С 6 процента е спаднало доверието към министър-председателя Бойко Борисов през последния месец',
		            '<strong>Удвояваме усвоените евросредства</strong> В края на тази година България ще удвои размера на усвоените средства по европейските програми',
		            '<strong>БАИС проведе кръгла маса "Проблеми на енергийното обновяване на сгради"</strong> На 10 февруари т. г. Българска асоциация за изолации в строителството проведе кръгла маса на тема: "Проблеми на енергийното обновяване на сгради"',
		            '<strong>Лот 1 на "Марица" - с 44 кандидата</strong> Документация за участие в тържната процедура за строително-монтажни работи по Лот 1 на автомагистрала "Марица" до момента е закупена от 44 кандидата'
		      ];*/
		if (this.news.length == 0) return;
		this.cover = this.element.find('div');
		this.animationElements = [];
		for (var i = 0; i < this.news.length; i ++) {
			var el = $(document.createElement('div'));
			el.html('<div>' + this.news[i] + '</div>');
			el.addClass('text');
			this.element.append(el);
			this.animationElements.push(el);
			/*el.css('opacity', 0.01);
			el.css('display', 'block');
			var txtDiv = el.find('div');
			var h = txtDiv.outerHeight();
			txtDiv.css('margin-top', (Math.max(this.elemH - h, 0) / 2) + 'px');
			el.css('opacity', '');
			el.css('display', 'none');*/
		}
		for (var i = 0; i < this.animationElements.length; i ++) {
			this.animationElements[i].css('position', 'absolute');
		}
		this.currentStep = 0; // means we are showing the cover
		this.currentElement = this.animationElements[0];
		setInterval('MagazineAnimations.switchElement()', 4000);
	},
	switchElement: function () {
		this.currentStep ++;
		if (this.currentStep > this.animationElements.length - 1)
			this.currentStep = 0;
		this.transitionStepOut();
		this.currentElement = this.animationElements[this.currentStep];
		this.transitionStepIn();
	},
	transitionStepOut: function () {
		this.currentElement.fadeOut(300);
	},
	transitionStepIn: function () {
		this.currentElement.css('left', this.elemW + 'px');
		this.currentElement.css('display', 'block');
		this.currentElement.animate({left:0}, 500);
		//setTimeout ('MagazineAnimations.slideCurrentElementIfNeeded()', 1000);
	},
	slideCurrentElementIfNeeded: function () {
		var oh = this.currentElement.outerHeight();
		if (oh > this.elemH) {
			this.currentElement.animate({top:this.elemH - oh}, 1000, false);
		}
	}
}
$(window).load(function(){
	MagazineAnimations.initialize();
});
