var menu,contents;
var tours=[],cats=[],fakeTours=[];
var months=[' января',' февраля',' марта',' апреля',' мая',' июня',' июля',' августа',' сентября',' октября',' ноября',' декабря'];
var months4links=['январь','февраль','март','апрель','май','июнь','июль','август','сентябрь','октябрь','ноябрь','декабрь'];
var allCats=[
	{id:'0',name:'Все',hH:0,spaceAfter:0,emptyDatesText:''},
	{id:'11',name:'Масленица',hH:0,spaceAfter:0,emptyDatesText:''},
	{id:'1',name:'Новинки',hH:0,spaceAfter:1,emptyDatesText:''},
	{id:'2',name:'Автобус, 1 день',hH:0,spaceAfter:0,emptyDatesText:''},
	{id:'3',name:'Эконом',hH:0,spaceAfter:0,emptyDatesText:''},
	{id:'4',name:'Автобус, от 2 дней',hH:0,spaceAfter:0,emptyDatesText:''},
	{id:'5',name:'Железнодорожные',hH:0,spaceAfter:0,emptyDatesText:''},
	{id:'6',name:'Авиа',hH:0,spaceAfter:1,emptyDatesText:''},
	{id:'7',name:'Санкт-Петербург',hH:0,spaceAfter:0,emptyDatesText:'<span style="font-weight:normal;">даты<br>уточняются</span>'},
	{id:'8',name:'Украина',hH:0,spaceAfter:0,emptyDatesText:''},
	{id:'9',name:'Беларусь',hH:0,spaceAfter:1,emptyDatesText:''},
	{id:'10',name:'Новый Год',hH:0,spaceAfter:0,emptyDatesText:''},
	{id:'12',name:'Золотое Кольцо',hH:0,spaceAfter:0,emptyDatesText:''},
	{id:'13',name:'Летние туры',hH:0,spaceAfter:0,emptyDatesText:''},
	{id:'14',name:'Теплоход',hH:0,spaceAfter:0,emptyDatesText:''},
	{id:'15',name:'Праздники лета',hH:0,spaceAfter:0,emptyDatesText:''}
];
function getAllCatsIndex(id){for(var i=0;i<allCats.length;i++)if(id==allCats[i].id)return i;}
function makeDate(dt)
{
	var day,mon,yr,parts,tmp;
	parts=dt.split('.');
	day=parseInt(parts[0],10);
	mon=parseInt(parts[1],10)-1;
	yr=now.getFullYear();
	if(mon<now.getMonth())yr++;
	tmp=new Date(yr,mon,day);
	return tmp;
}
function makeDates(dt)
{
	var allPeriods,period,i,j,start,end,dates=[];
	allPeriods=dt.split(',');
	for(i=0;i<allPeriods.length;i++)
	{
		period=allPeriods[i].replace(/[.]/g,'-').split('-');
		//alert('dt: ' + dt + '; allPeriods[i]: ' + allPeriods[i] + '; period.length: ' + period.length);
		switch(period.length)
		{
		    case 2: // 23.04
	    	    end=makeDate(period[0]+'.'+period[1]);
    		    start=makeDate(period[0]+'.'+period[1]);
		        break;
		    case 3: // 23-24.04
	    	    end=makeDate(period[1]+'.'+period[2]);
    		    start=makeDate(period[0]+'.'+period[2]);
		        break;
		    case 4: // 23.04-24.04
	    	    end=makeDate(period[2]+'.'+period[3]);
    		    start=makeDate(period[0]+'.'+period[1]);
		        break;
		}
		if((now.getTime()<start.getTime())&&(start.getTime()-now.getTime()<23583600000))dates.push({start:start,end:end});
	}
	return dates;
}
var cTour=function(start,end,name,cats){this.start=start;this.end=end;this.name=name;this.cats=cats;}
var cFakeTour=function(name,cats){this.name=name;this.cats=cats;}
function createTours(row)
{
	var i,params,dt,dates,nm,cat;
	params=row.getElementsByTagName('td');
	dt=row.getElementsByTagName('th')[0].innerHTML;
	nm=params[0].innerHTML;
	cat=params[1].innerHTML.split(',');
	if(dt!='')
	{
		dates=makeDates(dt);
		for(i=0;i<cat.length;i++)cat[i]=parseInt(cat[i]);
		for(i=0;i<dates.length;i++)tours.push(new cTour(dates[i].start,dates[i].end,nm,cat));
	}
	else fakeTours.push(new cFakeTour(nm,cat));
}
cTour.prototype.isInCat=function(cat){var i;for(i=0;i<this.cats.length;i++)if(this.cats[i]==cat)return true;return false;}
cTour.prototype.getDate=function()
{
	var res='';
	if(this.start.getTime()==this.end.getTime())res=this.start.getDate()+months[this.start.getMonth()];
	else
	{
		if(this.start.getMonth()==this.end.getMonth())res=this.start.getDate()+'&mdash;'+this.end.getDate()+months[this.end.getMonth()];
		else res=this.start.getDate()+months[this.start.getMonth()]+' &mdash; '+this.end.getDate()+months[this.end.getMonth()];
	}
	return res;
}
cFakeTour.prototype.isInCat=function(cat){var i;for(i=0;i<this.cats.length;i++)if(this.cats[i]==cat)return true;return false;}
var cCat=function(row){var params=row.getElementsByTagName('td');this.name=params[0].innerHTML;if(!!params[1]&&params[1].innerHTML!='')this.id=params[1].innerHTML;}
function init()
{
	var i,j,tour,catLink,hideCat;
	menu=document.getElementById('leftMenu');
	contents=document.getElementById('contents');
	var contRows=contents.getElementsByTagName('tr');
	for(i=0;i<contRows.length;i++)if(contRows[i].className=='cat')cats.push(new cCat(contRows[i]));
	for(i=0;i<contRows.length;i++)if(contRows[i].className=='tour')createTours(contRows[i]);
	tours.sort(sorter);
	for(i=0;i<allCats.length;i++)allCats[i].fullName='<h2>'+allCats[i].name+'</h2>';
	for(i=0;i<cats.length;i++){allCats[getAllCatsIndex(cats[i].id)].hH=1;allCats[getAllCatsIndex(cats[i].id)].fullName=cats[i].name;}
	buildLeftMenu();
}
function sorter(t1,t2){var r;if(t1.start.getTime()<t2.start.getTime())r=-1;else{if(t1.start.getTime()>t2.start.getTime())r=1;else{if(t1.end.getTime()<t2.end.getTime())r=-1;else{if(t1.end.getTime()>t2.end.getTime())r=1;else r=0;}}}return r;}
function buildLeftMenu()
{
	var i,j,monthsDepth=4,mon;
	var html='<table border="0" cellpadding="0" cellspacing="0">';
	html+='<tr id="cat0"><td><a href="/list.shtml?cat=0">Все туры</a></td></tr>';
	html+='<tr><td><a href="neznaika.shtml">Детские туры</a></td></tr>';

	for(i=1;i<allCats.length;i++)
	{
		if(hasTours(allCats[i].id))
		{
			html+='<tr id="cat'+allCats[i].id+'"><td><a href="/list.shtml?cat='+allCats[i].id+'">'+allCats[i].name+'</a></td></tr>';
			for(j=0;j<monthsDepth;j++)
			{
				mon=now.getMonth()+j;if(mon>11)mon-=12;
				if(hasTours(allCats[i].id,mon))html+='<tr id="cat'+allCats[i].id+'-'+mon+'"><td><li><a href="/list.shtml?cat='+allCats[i].id+'-'+mon+'">'+months4links[mon]+'</a></li></td></tr>';
			}
			if(allCats[i].spaceAfter)html+='<tr><td>&nbsp;</td></tr>';
		}
	}
	html+='</table><p>&nbsp;</p>';
	menu.innerHTML=html;
}
function hasTours(cat,mon)
{
	var i,res=false;
	for(i=0;i<tours.length;i++)
		if(tours[i].isInCat(cat))
		{
			if(mon==undefined)
			{
				res=true;
				break;
			}
			else
				if(tours[i].start.getMonth()==mon)
				{
					res=true;
					break;
				}
		}
	for(i=0;i<fakeTours.length;i++)
		if(fakeTours[i].isInCat(cat)&&mon==undefined)
		{
			res=true;
			break;
		}
	return res;
}
function getDates(tourName){var newHTML='',i,used='';for(i=0;i<tours.length;i++){if(tours[i].name==tourName){used+=i+'.';if(newHTML!='')newHTML+=',<br>';newHTML+=tours[i].getDate();}}return {html:newHTML,used:used};}
function markMenu(c)
{
	var i,d,c1,items=menu.getElementsByTagName('tr');
	for(i=0;i<items.length;i++)if(items[i].id!=undefined&&items[i].id!='')if(items[i].id.indexOf('-')!=-1)items[i].className='h';//спрятать месяцы
	for(i=0;i<items.length;i++)if(items[i].id!=undefined&&items[i].id!='')if(items[i].id=='cat'+c)items[i].firstChild.className='active';else items[i].firstChild.className='';//снять пометку неактивных и пометить активный
	if(c.indexOf('-')!=-1){d=c.substr(c.indexOf('-')+1);c=c.substr(0,c.indexOf('-'));}//разобрать на категорию и месяц
	for(i=0;i<items.length;i++)if(items[i].id!=undefined&&items[i].id!=''){c1=items[i].id.substr(3);if(c1.indexOf('-')!=-1){c1=c1.substr(0,c1.indexOf('-'));if(c1==c)items[i].className='';}}//показать месяцы активной категории
}
function chooseCat(c)
{
	markMenu(c);
	var d=-1,i,j,curCat=null,catNameMentioned,newTT='',used='.',fUsed='.',tmp;
	if(c.indexOf('-')!=-1){d=parseInt(c.substr(c.indexOf('-')+1));c=c.substr(0,c.indexOf('-'));}
	if(c=='0')
	{
		newTT='<table border="0" class="list" cellpadding="0" cellspacing="0" width="90%">';
		for(i=0;i<cats.length;i++)
		{
			if(!!allCats[getAllCatsIndex(cats[i].id)].hH)
			{
				curCat=allCats[getAllCatsIndex(cats[i].id)];
				catNameMentioned=false;
				for(j=0;j<tours.length;j++)
				{
					if(used.indexOf('.'+j+'.')==-1)
					{
						if(tours[j].isInCat(curCat.id))
						{
							if(d==-1||(d==tours[j].start.getMonth())||(d==tours[j].end.getMonth()))
							{
								if(!catNameMentioned)
								{
									newTT+='<tr class="cat"><td colspan="2">'+curCat.fullName+'</td><td class="id">'+curCat.id+'</td></tr>\n';
									catNameMentioned=true;
								}
								tmp=getDates(tours[j].name);
								used+=tmp.used;
								newTT+='<tr class="tour"><th>'+tmp.html+'</th><td width="100%">'+(tours[j].isInCat(1)?'<span style="color:red">НОВИНКА!</span> ':'')+tours[j].name+'</td><td class="id">'+tours[j].cats.join(',')+'</td></tr>';
							}
						}
					}
				}
				for(j=0;j<fakeTours.length;j++)
				{
					if(fUsed.indexOf('.'+j+'.')==-1)
					{
						if(fakeTours[j].isInCat(curCat.id)&&d==-1)
						{
							if(!catNameMentioned)
							{
								newTT+='<tr class="cat"><td colspan="2">'+curCat.fullName+'</td><td class="id">'+curCat.id+'</td></tr>\n';
								catNameMentioned=true;
							}
							fUsed+=j+'.';
							newTT+='<tr class="tour"><th>'+curCat.emptyDatesText+'</th><td width="100%">'+(fakeTours[j].isInCat(1)?'<span style="color:red">НОВИНКА!</span> ':'')+fakeTours[j].name+'</td><td class="id">'+fakeTours[j].cats.join(',')+'</td></tr>';
						}
					}
				}
				fUsed='.';
				used='.';
			}
		}
		newTT+='<tr><td colspan="3">&nbsp;</td></tr></table>';
		contents.innerHTML=newTT;
	}
	else
	{
		curCat=allCats[getAllCatsIndex(c)];
		if(curCat!=null)
		{
			newTT='<table border="0" class="list" cellpadding="0" cellspacing="0" width="90%">';
			catNameMentioned=false;
			for(i=0;i<tours.length;i++)
			{
				if(used.indexOf('.'+i+'.')==-1)
				{
					if(tours[i].isInCat(curCat.id))
					{
						if(d==-1)
						{
							if(!catNameMentioned)
							{
								newTT+='<tr class="cat"><td colspan="2">'+curCat.fullName+'</td><td class="id">'+curCat.id+'</td></tr>\n';
								catNameMentioned=true;
							}
							tmp=getDates(tours[i].name);
							used+=tmp.used;
							newTT+='<tr class="tour"><th>'+tmp.html+'</th><td width="100%">'+(tours[i].isInCat(1)?'<span style="color:red">НОВИНКА!</span> ':'')+tours[i].name+'</td><td class="id">'+tours[i].cats.join(',')+'</td></tr>';
						}
						else
						{
							if(d==tours[i].start.getMonth()||d==tours[i].end.getMonth())
							{
								if(!catNameMentioned)
								{
									newTT+='<tr class="cat"><td colspan="2">'+curCat.fullName+'</td><td class="id">'+curCat.id+'</td></tr>\n';
									catNameMentioned=true;
								}
								newTT+='<tr class="tour"><th>'+tours[i].getDate()+'</th><td width="100%">'+(tours[i].isInCat(1)?'<span style="color:red">НОВИНКА!</span> ':'')+tours[i].name+'</td><td class="id">'+tours[i].cats.join(',')+'</td></tr>';
								used+=i+'.';
							}
						}
					}
				}
			}
			for(i=0;i<fakeTours.length;i++)
			{
				if(fUsed.indexOf('.'+i+'.')==-1)
				{
					if(fakeTours[i].isInCat(curCat.id)&&d==-1)
					{
						if(!catNameMentioned)
						{
							newTT+='<tr class="cat"><td colspan="2">'+curCat.fullName+'</td><td class="id">'+curCat.id+'</td></tr>\n';
							catNameMentioned=true;
						}
						fUsed+=i+'.';
						newTT+='<tr class="tour"><th>'+curCat.emptyDatesText+'</th><td width="100%">'+(fakeTours[i].isInCat(1)?'<span style="color:red">НОВИНКА!</span> ':'')+fakeTours[i].name+'</td><td class="id">'+fakeTours[i].cats.join(',')+'</td></tr>';
					}
				}
			}
			newTT+='<tr><td colspan="3">&nbsp;</td></tr></table>';
			contents.innerHTML=newTT;
		}
	}
}


