﻿var PostCalendarHandler = Class.create();
PostCalendarHandler.prototype = {
		
		initialize : function (holder) {
			this.years	 		= new Array();
			this.currentYear	= null;
			
			var years = holder.childNodes;
			
			if (!years)
				throw new Error('Некорректная структура календаря публикаций');
				
			this.addBehavior(years);
		},
		
		addBehavior : function (years) {
			for (var i = 0; i < years.length; i++) {
				var year = new PostCalendarYearHandler(years[i]);
				
				year.onActivate = this.yearSelect.bind(this, year);
				
				if (year.active)
					this.selected = year;
				
				this.years.push(year);
			}
		},	
		
		yearSelect : function (year) {
			if (this.selected)
				this.selected.deactivate();
				
			this.selected = year;
		}
		
}


var PostCalendarYearHandler = Class.create();
PostCalendarYearHandler.prototype = {
	
	initialize : function (holder) {
		// const
		this.SELECTED_HOLDER_CLASSNAME = 'selected';
		this.SELECTED_TITLE_CLASSNAME = 'selected';
		
		// private members
		this.holder		= null;
		this.titleEl	= null;
		this.monthsEl	= null;
		
		
		// public members
		this.active		= false;
		
		
		// events
		this.onActivate	= null;


		// ctor
		this.holder = $(holder);
		
		if (this.holder.hasClassName(this.SELECTED_HOLDER_CLASSNAME))
			this.active = true;
		
		var divs = holder.getElementsByTagName('div');
		var uls = holder.getElementsByTagName('ul');
		
		if (divs.length != 1)
			throw new Error('Некорректная структура календаря публикаций');

		if (uls.length != 1)
			throw new Error('Некорректная структура календаря публикаций');

		this.titleEl = $(divs[0]);
		this.monthsEl = $(uls[0]);
		
		this.addBehavior();
	},
	

	// public methods
	
	deactivate : function () {
		this.holder.removeClassName(this.SELECTED_TITLE_CLASSNAME);
		this.active = false;
	},
	
	
	// private methods

	addBehavior : function () {
		this.titleEl.innerHTML = '<span>'+this.titleEl.childNodes[0].innerHTML+'</span>';
		this.titleEl.observe('mouseover', this.activate.bind(this));
	},
	
	activate : function () {
		if (this.active) return;
		
		this.holder.addClassName(this.SELECTED_TITLE_CLASSNAME);
		
		if (this.onActivate)
			this.onActivate();
			
		this.active = true;
	}	
	
}
