function HiddenWithIntro() {}
HiddenWithIntro.prototype = {
	init: function(container) {
		this.container = container;
		as.w(".switcher",this.container).click(function(e) {
			e.preventDefault();
			this.container.className = this.container.className.match(/\bhidden-with-intro\b/) ?
			this.container.className.replace(/\bhidden-with-intro\b/gi,"shown-with-intro") :
			this.container.className.replace(/\bshown-with-intro\b/gi,"hidden-with-intro");
		},this);
	}
}

function AgeCheckout() {}
AgeCheckout.prototype = {
	isLeap: function(year) {
		year = Number(year);
		return (year%4 == 0) && (year%100 == 0 ? !(year%400) : true);
	},
	init: function(set) {
		this.daySelect = set.day;
		this.monthSelect = set.month;
		this.yearSelect = set.year;

		this.daySelect && this.monthSelect && this.yearSelect && this.addEventListeners();
	},
	addEventListeners: function() {
		as.e.change(this.monthSelect,this.onMonthChange,this);
		as.e.change(this.yearSelect,this.onYearChange,this);
	},
	onMonthChange: function() {
		if (this.monthSelect.selectedIndex != 0 && this.yearSelect.selectedIndex != 0) {
			this.disableDays();
		}
		else {
			this.enableAllDays();
		}
	},
	onYearChange: function() {
		if (this.monthSelect.selectedIndex != 0 && this.yearSelect.selectedIndex != 0) {
			this.disableDays();
		}
		else {
			this.enableAllDays();
		}
	},
	enableAllDays: function() {
		as.w("option",this.daySelect).each(function() {
			this.disabled = false;
		});
	},
	disableDays: function() {
		as.w("option",this.daySelect).each(function(index) {
			(index != 0) && (this.disabled = true);
		});
		var currentMonth = this.monthSelect.value - 1;
		var currentDay = 1;
		var lastEnabledDay;
		while (new Date(this.yearSelect.value + "/" + this.monthSelect.value + "/" + currentDay).getMonth() == currentMonth) {
			this.daySelect.options[currentDay].disabled = false;
			lastEnabledDay = this.daySelect.options[currentDay];
			currentDay++
		}
		if (this.daySelect.options[this.daySelect.selectedIndex].disabled) {
			lastEnabledDay.selected = true;
		}
	}
}

function InfoBlockHandler() {}
InfoBlockHandler.prototype = {
	init: function(handler) {
		this.handler = handler;
		if (!this.handler.onclick || !this.handler.onclick()) return;
		this.createInfoBlock();
		this.addEventListeners();
	},
	createInfoBlock: function() {
		var info = this.handler.onclick();
		this.infoBlock = as.create("<div class='auto-members'><div class='info'><h3>" + info.name + "</h3><p><span class='number'>"+info.members+"</span> участников</p><p><span class='number'>"+info.posts+"</span> публикаций</p></div></div>");
	},
	addEventListeners: function() {
		as.e.mouseover(this.handler,this.showInfoBlock,this);
		as.e.mouseout(this.handler,this.hideInfoBlock,this);
	},
	showInfoBlock: function() {
		as.append(this.infoBlock,document.body);
		var hcs = as.where(this.handler);
		var ics = as.where(this.infoBlock);
		as.style(this.infoBlock,{
			left: hcs.left + hcs.width/2 - ics.width/2 + "px",
			top: hcs.top - ics.height - 10 + "px"
		});
	},
	hideInfoBlock: function() {
		as.remove(this.infoBlock);
	}
}

function PhotoExposer() {};
PhotoExposer.prototype = {
	init: function(handler) {
		this.handler = handler;
		if (!this.handler.onclick || !this.handler.onclick()) return;
		this.getPhotoList();
		this.createPhotoHTMLList();
		this.addEventListeners();
	},
	getPhotoList: function() {
		this.photoList = this.handler.onclick().photos;
	},
	createPhotoHTMLList: function() {
		if(this.photoList.length>1){
			this.photoHTMLList = as.create("<ul class='photo-expose-list'></ul>");
			for (var i=0,l=this.photoList.length;i<l;i++) {
				as.append("<li><a href='" + this.photoList[i].url + "'><img src='" + this.photoList[i].img + "' /></a></li>",this.photoHTMLList);
			}
		} else {
			this.photoHTMLList = as.create("<div style='display:none;'></div>");
		}
	},
	addEventListeners: function() {
	as.e.mousemove(document.body,this.checkMP,this);
	},
	checkMP: function(e) {
		var hcs = as.where(this.handler);
		var lcs = as.where(this.photoHTMLList);



		var x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft);
		var y = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop);

		if (
			(x > hcs.left && x < hcs.left + hcs.width && y > hcs.top && y < hcs.top + hcs.height) ||
			(x > lcs.left && x < lcs.left +lcs.width && y > lcs.top && y < lcs.top + lcs.height)
		) {
			this.showPhotoList();
		}
		else {
			this.hidePhotoList();
		}
	},
	showPhotoList: function() {
		as.append(this.photoHTMLList,document.body);
		var cs = as.where(this.handler);
		as.style(this.photoHTMLList,{
			left: cs.left + "px",
			top: cs.top + "px"
		});
	},
	hidePhotoList: function() {
		as.remove(this.photoHTMLList);
	}
}

function AfishaDateSwitcher() {}
AfishaDateSwitcher.prototype = {
	init: function(table) {
		this.table = table;
		this.cells = as.w("td",this.table);
		this.headers = as.w("th",this.table);
		this.headersLinks = as.w("tr.switcher th a",this.table);
		this.numerateCells();
		this.setCurrent(0);
		this.addEventListeners();
	},
	numerateCells: function() {
		as.w("tr",this.table).each(function() {
			as.w("td",this).each(function(index) {
				this.adIndex = index;
			});
			as.w("th",this).each(function(index) {
				this.adIndex = index;
			});
		});
	},
	setCurrent: function(index) {
		this.cells.each(function() {
			this.className = this.className.replace(/\bcurrent-day\b/gi,"day");
			if (this.adIndex == index) {
				this.className = this.className.replace(/\bday\b/gi,"current-day");
			}
		});
		this.headers.each(function() {
			this.className = this.className.replace(/\bcurrent-day\b/gi,"day");
			if (this.adIndex == index) {
				this.className = this.className.replace(/\bday\b/gi,"current-day");
			}
		});
	},
	addEventListeners: function() {
		this.headersLinks.click(this.trySetCurrent,this);
	},
	trySetCurrent: function(e) {
		e.preventDefault();
		var target = as.parent(e.target,"th");
		this.setCurrent(target.adIndex);
	}
}

function Voter() {}
Voter.prototype = {
	ajax: '/vote',
	init: function(wrapper) {
		this.wrapper = wrapper;
		this.voteLinks = as.w("ul.stars a",this.wrapper);
		this.value = as.$$("span#rating",this.wrapper);

		this.addEventListeners();
	},
	addEventListeners: function() {
		this.voteLinks.click(this.sendRequest,this);
	},
	sendRequest: function(e) {
		e.preventDefault();
		var data = e.target.onclick && e.target.onclick();
		if (!data) return;
		var postString = "";
		for (var i in data) {
			if (i != "url") {
				postString += (i + "=" + data[i] + "&");
			}
		}
		as.ajax({
			url: (data.url || this.ajax),
			callback: this.endVote,
			onerror: this.onError,
			context: this,
			parseJson: true,
			type: "post",
			data: postString
		});
	},
	endVote: function(data) {
		if (!data.result) {
			if (data.error) {
				alert(data.error);
			}
		}
		else {
			as.remove(as.$$("ul",this.wrapper));
			as.remove(as.$$("span.mark",this.wrapper));
			var inte, fraction;
			inte = Math.floor(data.rating);
			fraction = data.rating*10%10;
			this.value.innerHTML = "<strong>" + inte + "</strong>," + fraction;
		}
	},
	onError: function() {

	}
}

function CheckboxController() {}
CheckboxController.prototype = {
	init: function(form,count) {
		this.form = form;
		this.count = count;
		this.currentCount = 0;
		this.checkboxes = as.w("input[type='checkbox']",this.form);
		this.countFilled();

		this.addEventListeners();
	},
	addEventListeners: function() {
		this.checkboxes.change(this.onChange,this);
	},
	countFilled: function() {
		var currentCount = this.currentCount, count = this.count;
		this.checkboxes.each(function() {
			if (this.checked) {
				currentCount++;
				if (currentCount > this.count) {
					this.checked = false;
					currentCount--;
				}
			}
		});
		this.currentCount = currentCount;
		this.onChange();
	},
	disable: function() {
		this.checkboxes.each(function() {
			this.disabled = !this.checked;
		});
	},
	enable: function() {
		this.checkboxes.each(function() {
			this.disabled = false;
		});
	},
	onChange: function(e) {
		if (e && e.target) {
			this.currentCount += (e.target.checked ? 1 : -1);
		}
		if (this.currentCount >= this.count) {
			this.disable();
		}
		else {
			this.enable();
		}
	}
}

var Tools = {
	init: function() {
		this.validateStandardForms();
		if (typeof initEditor == "function") {
			initEditor();
		}
		this.initVoters();
		this.initRelax();
		this.initAnswers();
		this.initPhotoExpose();
		this.initInfoBlockHandlers();
		this.initAddSiteForm();
		this.initAgeCheckout();
		this.initHiddensWithIntro();
	},
	validateStandardForms: function() {
		var formsToValidate = as.w("form.to-validate");
		formsToValidate.each(function() {
			new ASFormValidator().init(this);
		});
	},
	initVoters: function() {
		as.w("div.rate").each(function() {
			new Voter().init(this);
		});
	},
	initRelax: function() {
		as.w("table.afisha-date").each(function() {
			new AfishaDateSwitcher().init(this);
		});
	},
	initAnswers: function() {
		as.w("div.comments div.comment h4 a").click(function(e) {
			var ta = as.$$("div.commentform textarea",as.parent(this,"div#main"));
			if (!ta || !ta.aseditor) return;
			var data = this.onclick && this.onclick();
			if (!data) return;
			var quoteText = "<span class='answ'>Ответ на сообщение " + data.name + " от " + data.date + "</span>";
			ta.aseditor.appendContent(quoteText);
			ta.aseditor.setEnd();
		});
	},
	initPhotoExpose: function() {
		as.w('.photo-expose').each(function() {
			new PhotoExposer().init(this);
		});
	},
	initInfoBlockHandlers: function() {
		as.w("a.info-block-handler").each(function() {
			new InfoBlockHandler().init(this);
		});
	},
	initAddSiteForm: function() {
		as.w("form.addsite").each(function() {
			new CheckboxController().init(this,5);
		})
	},
	initAgeCheckout: function() {
		as.w("form.profile-form").each(function() {
			new AgeCheckout().init({
				day: as.$$("select[name='day']",this),
				month: as.$$("select[name='month']",this),
				year: as.$$("select[name='year']",this)
			});
		});
	},
	initHiddensWithIntro: function() {
		as.w(".hidden-with-intro").each(function() {
			new HiddenWithIntro().init(this);
		});
	}
}

as.controller = {};
as.controller.create = function(constructor, cn, tn) {
	as.w(tn+"."+cn).each(function() {
		try {
			new window[constructor](this).init(this);
		}
		catch(e) {}
	})
}
as.getBCN = function(cn,tn,container) {
	return as.$(tn+"."+cn,container);
}
as.getBTN = function(tn,container) {
	return as.$(tn,container);
}
as.ready.add = as.ready;

as.ready(function() {
	Tools.init();
});


//На меню второго уровня вешаем заплатку, чтобы не мигало в ИЕ
//Вешаем обработчики событий
function addEvent(elem, type, handler){
	if (elem.addEventListener){
		elem.addEventListener(type, handler, false)
	} else {
		elem.attachEvent("on"+type, handler)
	}
} 
addEvent(window, 'load', qwe);


function qwe()
{
	var asd=document.getElementById('mainmenu'); 
	if (!asd) return;
	asd.onmousemove=stopEvent;
}

function stopEvent(e)
{
	e = e || window.event    
	e.stopPropagation ? e.stopPropagation() : (e.cancelBubble=true)
}

