var CtJournalItem = Class.create({

	item: null,
	showHideSummaryBtn: null,
	ident: null, //ex: i123 | a321
	summaryContainer: null,
	//cookieTtl: null,

	initialize: function(element, isUseCookie) {

		//this.cookieTtl = new Date();
		//this.cookieTtl.setHours(this.cookieTtl.getHours() + 1);

		this.item = element;

		try {
			this.showHideSummaryBtn = this.item.down('a.ctJournal-summary-btn');
			this.summaryContainer = this.item.down('div.ctJournal-summary');
		} catch(ex) {
			return;
		}
		this.showHideSummaryBtn.observe('click', this.showHideSummary.bindAsEventListener(this));
		try {
	   	    this.ident = this.showHideSummaryBtn.readAttribute('rel');
			this.showHideSummaryBtn.writeAttribute('rel', '');
		} catch(ex) {
			return;
		}

		// process cookie
		// getCookie(this.ident) == 1: force hide
		/*if (isUseCookie && getCookie(this.ident) == 1) {
			this.effectShow(0);
		}

		if (isUseCookie && getCookie(this.ident) != 1) {
			this.effectHide(0);
		}*/

	},

	/**
	 *
	 * @param event
	 */
	showHideSummary: function(event) {

		Event.stop(event);

		if (this.summaryContainer.visible()) {
			this.effectHide(0.1);
		} else {
			this.effectShow(0.1);
		}

	},


	effectShow: function(duration) {
		var queues = Effect.Queues.get(this.ident).effects.length;

		if (queues > 0) {
			return;
		}


		new Effect.BlindDown(this.summaryContainer, {
			duration: duration,
			queue: {position: 'end', scope: this.ident}
		});

		this.showHideSummaryBtn.addClassName('front-contract-icon');
		this.showHideSummaryBtn.update('Hide Summary');

		//setCookie(this.ident, 1, this.cookieTtl, '/', null, null);

		Event.fire(this.item, 'CtJournalItem:setSummaryVisibility', {});
	},


	effectHide: function(duration) {

		var queues = Effect.Queues.get(this.ident).effects.length;

		if (queues > 0) {
			return;
		}

		new Effect.BlindUp(this.summaryContainer, {
			duration: duration,
			queue: {position: 'end', scope: this.ident}
		});

		this.showHideSummaryBtn.removeClassName('front-contract-icon');
		this.showHideSummaryBtn.update('Show Summary');

		//setCookie(this.ident, 0, this.cookieTtl, '/', null, null);

		Event.fire(this.item, 'CtJournalItem:setSummaryVisibility', {});
	},

	isSummaryVisible: function() {
		return this.showHideSummaryBtn.hasClassName('front-contract-icon');
	},

	x: null
});



var CtJournal = new (Class.create({

	itemList: null,
	expandContractAllBtn: null,

	initialize: function() {
		this.itemList = [];
		Event.observe(document, 'dom:loaded', function() {
			this.refresh();
		}.bind(this));

		Event.observe(document, 'click', function () {
			for (var i = 0; i < this.itemList.length; i++) {
				this.itemList[i].item.removeClassName('article-selected');
			}
		}.bind(this));
	},

	refresh: function(htmlElement) {

		// если на странице есть минимум одна выделенная
		// статья, игнрорировать сохранённые в куках
		// признаки открыт/закрыт, открыть только выделенную
		/*var isUseCookie;
		try {
			var selectedArticle = $$('.article-selected');
			isUseCookie = !(selectedArticle.length > 0);
			if ($$('body')[0].down('div.journal-container').hasClassName('search-result')) {
				isUseCookie = false;
			}

		} catch(ex) {
			isUseCookie = true;
		}*/

		var elements = [];
		if (htmlElement) {
			elements = $(htmlElement).select('.ctJournal-item');
		} else {
			elements = $$('.ctJournal-item');
		}

		for (var i = 0; i < elements.length; i++) {
			elements[i].removeClassName('ctJournal-item');
			this.itemList[i] = new CtJournalItem(elements[i], /*isUseCookie*/0);
	        elements[i].observe('click', this.clickItemHandler.bindAsEventListener(this, i));
			elements[i].observe('CtJournalItem:setSummaryVisibility', this.updateExpandContractAllBtn.bindAsEventListener(this));

		}

		try {
			this.expandContractAllBtn = ($$('body')[0]).down('a.ctJournal-expand-all');
			this.expandContractAllBtn.observe('click',
				this.expandContractAll.bindAsEventListener(this));
		} catch(ex) {
		}

		try {
			var selectedArticle = ($$('body')[0]).down('.article-selected');
			Effect.ScrollTo(selectedArticle, {duration : '0.5'});
		} catch(ex) {
		}

		this.updateExpandContractAllBtn();

	},


	updateExpandContractAllBtn: function() {

		if (!this.expandContractAllBtn) {
			return;
		}

		var contract = -1;
		for (var i = 0; i < this.itemList.length; i++) {
			if (contract = this.itemList[i].isSummaryVisible()) {
				break;
			}
		}
		
		if (contract) {
			this.expandContractAllBtn.addClassName('front-contract-icon');
			this.expandContractAllBtn.update('Contract All');
		} else {
			this.expandContractAllBtn.removeClassName('front-contract-icon');
			this.expandContractAllBtn.update('Expand All');
		}
	},


	expandContractAll: function(event) {

		Event.stop(event);
		if (this.expandContractAllBtn.hasClassName('front-contract-icon')) {

			this.expandContractAllBtn.removeClassName('front-contract-icon');
			this.expandContractAllBtn.update('Expand All');

			for (var i = 0; i < this.itemList.length; i++) {
				this.itemList[i].effectHide(0.1);
			}
		} else {
			this.expandContractAllBtn.addClassName('front-contract-icon');
			this.expandContractAllBtn.update('Contract All');

			for (var i = 0; i < this.itemList.length; i++) {
				this.itemList[i].effectShow(0.1);
			}
		}
	},

	clickItemHandler: function(event, idItem) {

		try {
			var element = Event.element(event);
		} catch (ex) {
			return;
		}

		if (element.tagName != 'DIV') {
			return;
		}

		Event.stop(event);

		if (this.itemList[idItem].item.hasClassName('issue-item-archive')) {
			return;
		}

		for (var i = 0; i < this.itemList.length; i++) {
			if (i != idItem) {
				this.itemList[i].item.removeClassName('article-selected');
			}
		}
		this.itemList[idItem].item.addClassName('article-selected');
	},

	x:null
}));


