// JavaScript Document
//todo
function todo(){
	this.action=new Array();
	this.execute=function(){for(var i in this.action){
	//	try{
			todo.action[i]();
	//	}catch(er){alert('todo >> "'+er.message+'" in "'+er.fileName+'" on "'+er.lineNumber+'"');}
	}};
	var onLoadAction=function(o){return function(){o.execute();}}(this);
	if(typeof document.attachEvent!='undefined')window.attachEvent('onload',onLoadAction);
	else window.addEventListener('load',onLoadAction,false);
};
todo.prototype.get=function(id){return document.getElementById(id);};
todo.prototype.create=function(tag,attrs,text,style){
	var e=document.createElement(tag);
	if(attrs)for(var i in attrs)switch(i){
		case 'class': e.className=attrs[i];break;
		case 'id': e.id=attrs[i];break;
		default: e.setAttribute(i,attrs[i]);break;
	};
	if(text)e.appendChild(document.createTextNode(text));
	if(style)for(i in style){try{e.style[i]=style[i];}catch(er){}};
	return e;
};
todo.prototype.onload=function(func){this.action[this.action.length]=func;};
todo.prototype.importScript=function(src){document.getElementsByTagName('head')[0].appendChild(this.create('script',{'type':'text/javascript','src':src}));};
todo=new todo();
//main code
function getAjaxLoader(){
	return '<div style="text-align:center;padding:30px 0;"><img src="images/ajax-loader.gif" alt="" width="128" height="15"/></div>';
}
function isEmail(email){
	var pattern = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	return pattern.test(email);
}
function stopEvent(event){
	var e=event ? event : window.event;
	if(e.stopPropagation)e.stopPropagation();else e.cancelBubble=true;
	if(e.preventDefault)e.preventDefault();else e.returnValue=false;
}
function initMenu(){
	var menuHover=function(e){
		if(document.body.onmouseover){
			document.body.onmouseover();
			document.body.onmouseover=null;
		}
		if(e.tagName.toLowerCase()!='dt' || e.className=='selected')return;
	};
	var menu=todo.get('menu_left'),itm=menu.firstChild,curItem;
	menu.onmouseover=stopEvent;
	do{
		if(itm.nodeName.toLowerCase()=='#text')continue;
		itm.onmouseover=function(){menuHover(this)};
		if(itm.tagName.toLowerCase()=='dt'){curItem=itm;continue;}
		else if(!curItem || itm.tagName.toLowerCase()!='dd')continue;
		itm.onmouseover=stopEvent;
		var submenu=itm.getElementsByTagName('dl')[0];
		submenu.style.visibility='hidden';
		curItem.submenu=submenu;
		curItem.onmouseover=function(event){
			menuHover(this);
			this.submenu.style.visibility='visible';
			document.body.onmouseover=function(o){return function(){o.style.visibility='hidden';this.onmouseover=null;}}(this.submenu);
		}
	}while(itm=itm.nextSibling);
};
function informer(mode){
	this.cont=todo.get('informer');
	this.mode=mode;
	this.bookmark=[];
	try{
		this.bookmark[0]=todo.get('informer_bookmark_1');
		this.bookmark[0].informer=this;
	}catch(er){};
	try{
		this.bookmark[1]=todo.get('informer_bookmark_2');
		this.bookmark[1].informer=this;
	}catch(er){};
	this.createRecord=function(date,text,url,isLast){
		var p=this.cont.appendChild(todo.create('p',isLast ? {'class':'last'} : null));
		p.appendChild(todo.create('span',{'class':'date'},date));
		if(url)p.appendChild(todo.create('a',{'href':url},text));
		else p.appendChild(document.createTextNode(text));
	};
	this.loadData=function(section){
		this.lastSection=section;
		var ajax=new Ajax();
		ajax.setPOST('mode',section);
		ajax.request('informer.php',this);
		this.cont.innerHTML=getAjaxLoader();
	};
	this.processResult=function(text,xml){
		this.cont.innerHTML='';
		if(!xml)return;
		var info=xml.getElementsByTagName('info')[0],
			r=info.getElementsByTagName('row');
		for(var i=0,l=r.length;i<l;i++){
			switch(this.lastSection){
				case 'action_plan':
					var tmp=r[i].getElementsByTagName('announce')[0];
					if(tmp && tmp.firstChild)this.createRecord(tmp.firstChild.data,r[i].getAttribute('title'),null,l-1==i);
					break;
				case 'fair_plan':this.createRecord(r[i].getElementsByTagName('announce')[0].firstChild.data,r[i].getAttribute('title'),null,l-1==i);break;
				default:this.createRecord(r[i].getAttribute('date'),r[i].getAttribute('title'),'?id=home&row='+r[i].getAttribute('id'),l-1==i);
			}
		}
		switch(this.lastSection){
			case 'action_plan':todo.get('bookmarks_image').src='images/informer_bookmarks_1.gif';break;
			case 'fair_plan':todo.get('bookmarks_image').src='images/informer_bookmarks_1_s.gif';break;
			default: todo.get('bookmarks_image').src='images/informer_bookmarks_2.gif';
		}
	}
	switch(this.mode){
		case 'home':
			this.loadData('action_plan');
			this.bookmark[0].onclick=function(){this.informer.loadData('action_plan');return false;}
			this.bookmark[1].onclick=function(){this.informer.loadData('fair_plan');return false;}
			break;
		default:
			this.loadData('news');
			this.bookmark[0].onclick=function(){this.informer.loadData('news');return false;}
			//this.bookmark[1].onclick=function(){window.location.href='/';return false;}
	}
};
function initSubscribeForm(){
	var b=todo.get('subscribe_submit');
	if(!b)return;
	b.onclick=function(){
		var email=todo.get('subscribe_email').value;
		if(!isEmail(email)){
			alert('Вы ввели неверный адрес электронной почты.');
			return false;
		}
		this.processResult=function(text,xml){if(xml)alert(xml.getElementsByTagName('message')[0].firstChild.data);else alert(text);};
		var ajax=new Ajax();
		ajax.setPOST('ajax','1');
		ajax.setPOST('subscribe_email',email);
		ajax.request('index.php',this);
		todo.get('subscribe_email').value='';
		return false;
	}
};
function initPollForm(){
	var b=todo.get('poll_submit');
	if(!b)return;
	b.onclick=function(){
		var f=todo.get('poll_form'),r=f.answer,answer=null;
		for(var i=0,l=r.length;i<l;i++)if(r[i].checked)answer=r[i].value;
		if(!answer){
			alert('Вы не выбрали вариант ответа.');
			return false;
		};
		this.processResult=function(text,xml){
			if(!xml){alert(text);return;};
			var poll=todo.get('poll');
			var parent=poll.parentNode;
			parent.removeChild(poll);
			parent.innerHTML=parent.innerHTML+xml.getElementsByTagName('html')[0].firstChild.data;
			alert(xml.getElementsByTagName('message')[0].firstChild.data);
		};
		var ajax=new Ajax();
		ajax.setPOST('ajax','1');
		ajax.setPOST('poll',f.poll.value);
		ajax.setPOST('answer',answer);
		ajax.request('index.php',this);
		todo.get('subscribe_email').value='';
		return false;
	}
};
/* убираем изменение размера шрифта
function initFontSize(){try{
	todo.get('smaller').onclick=function(){
		var b=todo.get('midlbar')
			fs=parseFloat(b.style.fontSize);
		if(fs>0.5)b.style.fontSize=(fs-0.1)+'em';
		else if(!fs)b.style.fontSize=(1-0.1)+'em';
		return false;
	}
	todo.get('bigger').onclick=function(){
		var b=todo.get('midlbar')
			fs=parseFloat(b.style.fontSize);
		if(fs<2)b.style.fontSize=(fs+0.1)+'em';
		else if(!fs)b.style.fontSize=(1+0.1)+'em';
		return false;
	}
}catch(er){}};
*/
function initArchive(month,year){
	if(!arc || !todo.get('midlbar'))return false;
	/* создаем заголовок */
	todo.get('midlbar').appendChild(todo.create('h2',null,'Архив новостей',{'padding':'20px 0 0 0'}));
		/* возвращает римские цифры от 1 до 12 */
	var getLatinMonth=function(month){switch(month){case 1:return 'I';case 2:return 'II';case 3:return 'III';case 4:return 'IV';case 5:return 'V';case 6:return 'VI';case 7:return 'VII';case 8:return 'VIII';case 9:return 'IX';case 10:return 'X';case 11:return 'XI';case 12:return 'XII';};return '';},
		/* действие при выборе месяца */
		monthAction=function(){
			for(var i=1;i<=12;i++){
				this.parentNode.aMonth[i].className='empty';
				this.parentNode.aMonth[i].onclick=function(){return false;};
			}
			this.className='selected';
			window.location.href='?year='+this.currentYear+'&month='+this.month;
			return false;
		},
		/* действие при выборе года */
		yearAction=function(){
			for(var i in this.parentNode.aYears)this.parentNode.aYears[i].className=null;
			this.className='selected';
			this.parentNode.currentYear=parseInt(this.innerHTML)-2000;
			this.parentNode.shiftYear=0;
			this.parentNode.initMonth(this.parentNode.currentYear);
			return false;
		},
		/* создаем таблицу */
		table=todo.get('midlbar').appendChild(todo.create('table',{'class':'archive','border':0})),
		tbody=table.appendChild(todo.create('tbody')),
		tr=tbody.appendChild(todo.create('tr')),
		tdYears=tr.appendChild(todo.create('td',null,null,{'padding':'10px 10px 20px 0'})),
		tdMonth=tr.appendChild(todo.create('td',null,null,{'padding':'10px 0 20px 0','fontFamily':'"Times New Roman", Times, serif'}));
	tdMonth.aMonth=[];
	tdYears.aMonth=[];
	tdYears.aYears={};
	/* установка нового месяца */
	tdYears.initMonth=function(year,month){
		for(var i=1;i<=12;i++){
			this.aMonth[i].className=arc[year][i] ? null : 'empty';
			this.aMonth[i].onclick=arc[year][i] ? monthAction : function(){return false;};
			this.aMonth[i].currentYear=year;
		};
		if(month)this.aMonth[month].className='selected';
	};
	/* годы */
	tdYears.initYears=function(year,shift){
		this.innerHTML='';
		this.arYears=[];
		for(var i=0;i<arc.length;i++){if(arc[i]){
			this.arYears[this.arYears.length]=i;
			if(year==i){
				this.currentYear=year;
				this.currentYearIndex=this.arYears.length-1;
		}}};
		this.shiftYear=shift;
		this.tripleNum=Math.ceil(this.arYears.length/3);
		this.currentYearTriple=this.tripleNum-Math.ceil((this.arYears.length-this.currentYearIndex)/3);
		this.currentTriple=this.currentYearTriple+this.shiftYear;
		if(this.currentTriple>0){
			var b=this.appendChild(todo.create('img',{'src':'images/bullet_4.gif','width':11,'height':5,'alt':'<'},null,{'border':0,'cursor':'pointer','cssFloat':'left','styleFloat':'left','margin':'5px 0 0 0'}));
			b.$=this;
			b.onclick=function(){this.$.initYears(this.$.currentYear,this.$.shiftYear-1);};
		};
		
		var ost=(this.arYears.length-this.currentYearIndex)%3;
		if(!ost)ost=3;
		var startIndex=(this.currentYearIndex+(ost-3))+this.shiftYear*3;
		if(startIndex<0)startIndex=0;
		else if(startIndex>=this.arYears.length)startIndex=this.arYears.length-4;
		var endIndex;
		if(startIndex==0)endIndex=this.arYears.length%3;
		else if(startIndex+3>this.arYears.length)endIndex=this.arYears.length;
		else endIndex=startIndex+3;
		for(var i=startIndex; i<endIndex;i++){
			this.aYears['year'+i]=tdYears.appendChild(todo.create('a',{'href':'javascript:'},2000+this.arYears[i],{'width':'3em','margin':'0 1px 2px 1px','textAlign':'center','textDecoration':'none','display':'block','cssFloat':'left','styleFloat':'left','fontSize':'12px;'}));
			this.aYears['year'+i].onclick=yearAction;
			if(this.currentYear==this.arYears[i])this.aYears['year'+i].className='selected';
		};
		if(this.currentTriple<this.tripleNum-1){
			var b=this.appendChild(todo.create('img',{'src':'images/bullet_2.gif','width':11,'height':5,'alt':'>'},null,{'border':0,'cursor':'pointer','cssFloat':'left','styleFloat':'left','margin':'5px 0 0 0'}));
			b.$=this;
			b.onclick=function(){this.$.initYears(this.$.currentYear,this.$.shiftYear+1);};
		}
	};
	/* создаем ссылки на месяцы */
	for(i=1;i<=12;i++){
		tdYears.aMonth[i]=tdMonth.appendChild(todo.create('a',{'href':'javascript:'},getLatinMonth(i),{'width':'1.5em','margin':'0 1px 2px 1px','textAlign':'center','textDecoration':'none','display':'block','cssFloat':'left','styleFloat':'left','fontSize':'12px;'}));
		tdMonth.aMonth[i]=tdYears.aMonth[i];
		tdYears.aMonth[i].month=i;
	};
	tdYears.initYears(year,0);
	tdYears.initMonth(year,month);
	return true;
}

todo.onload(initMenu);
todo.onload(initSubscribeForm);
todo.onload(initPollForm);
/* todo.onload(initFontSize); */
todo.importScript('ap/js/ajax.js');
todo.importScript('js/swfobject.js');