/*--------------*/
/*     Hall     */
/*--------------*/

var aHalls = new Array();
function Hall(sHallId, sName, sAddress) {
	this.sID = sHallId;
	this.sName = sName;
	this.sAddress = sAddress;
	return this;
}


/*--------------------*/
/*    Performance     */
/*--------------------*/

var aPerformances = new Array();
function Performance(sPerformanceID, sName, sHallID, sTime, sCategoryID, iYear, iMonth, iDay, sETicket) {
	this.sID = sPerformanceID;
	this.sName = sName;
	this.sHallID = sHallID;
	this.sTime = sTime;
	this.sETicket = sETicket;

	this.sCategoryID = sCategoryID;

	this.iYear = iYear;
	this.iMonth = iMonth;
	this.iDay = iDay;
	

	/* 1 day in milliseconds - 86400000 */
	this.iWeek = Math.floor((Date.UTC(this.iYear, this.iMonth, this.iDay) - (4 * 86400000)) / 86400000 / 7);

	return this;
}

var aPerformanceWeeks = new Array();
function initPerformancesData(sResponseXML) {
	if (sResponseXML && sResponseXML.documentElement) {
		/* there can be several categories, so we need to know whis is active for the performance */
		for (var i = 0; (eCategory = sResponseXML.documentElement.getElementsByTagName('CATEGORY')[i]); i++) {
			var sCategoryID = eCategory.getAttribute('UUID');
			/* go through dates */
			for (var j = 0; (eDate = eCategory.getElementsByTagName('DATE')[j]); j++) {
				var iYear = parseInt(eDate.getAttribute('year'));
				var iMonth = parseInt(eDate.getAttribute('month'));
				var iDay = parseInt(eDate.getAttribute('day'));
				for (var k = 0; (ePerformance = eDate.getElementsByTagName('PERFORMANCE')[k]); k++) {
					var sPerformanceID = ePerformance.getAttribute('UUID');
					var sName = ePerformance.getAttribute('Name');
					var sHallID = ePerformance.getAttribute('HallPlaceUUID');
					var sTime = ePerformance.getAttribute('PerformanceTime');
					var sETicket = ePerformance.getAttribute('ETicket');
					aPerformances[aPerformances.length] = new Performance(sPerformanceID, sName, sHallID, sTime, sCategoryID, iYear, iMonth, iDay, sETicket);
				}
			}
		}
		
		/* get weeks */
		var iCurrentWeek = 0;
		for (var i = 0; i < aPerformances.length; i++) {
			if (aPerformances[i].iWeek != iCurrentWeek) {
				aPerformanceWeeks[aPerformanceWeeks.length] = aPerformances[i].iWeek;
				iCurrentWeek = aPerformances[i].iWeek;
			}
		}
		setUpPaginator();
		
		/* halls - named array */
		for (var i = 0; (eHall = sResponseXML.documentElement.getElementsByTagName('HallPlace')[i]); i++) {
			var sHallId = eHall.getAttribute('UUID');
			if (sHallId) {
				var sName = eHall.getAttribute('Name');
				var sAddress = eHall.getAttribute('Address');
				aHalls[sHallId] = new Hall(sHallId, sName, sAddress);
			}
		}
	}
}



/*-------------------*/
/*     Paginator     */
/*-------------------*/

function setUpPaginator() {
	oPaginator = new Paginator();
}

var aMonthNames = ['января', 'февраля', 'марта', 'апреля', 'мая', 'июня', 'июля', 'августа', 'сентября', 'октября', 'ноября', 'декабря'];

function getWeekTitle(iWeekNumber) {
	var dtWeekStart = new Date((iWeekNumber * 7 + 4) * 86400000);
	var dtWeekEnd = new Date((iWeekNumber * 7 + 10) * 86400000);
	var sWeekTitle = dtWeekStart.getDate();
	if (dtWeekStart.getMonth() != dtWeekEnd.getMonth()) sWeekTitle += '&nbsp;' + aMonthNames[dtWeekStart.getMonth()];
	sWeekTitle += '&nbsp;&mdash; ' + dtWeekEnd.getDate() + '&nbsp;' + aMonthNames[dtWeekEnd.getMonth()];
	return sWeekTitle;
}

function Paginator() {
	var oThis = this;
	
	this.eTable = document.getElementById('paginatorTable');
	this.ePages = document.getElementById('paginatorPages');

	if (this.eTable && this.ePages && aPerformanceWeeks.length) {
		this.showControls();
		this.aPages = new Array();

		this.displayFunction = null;

		var dtNow = new Date();
		var iCurrentWeek = Math.floor((Date.UTC(dtNow.getFullYear(), dtNow.getMonth(), dtNow.getDate()) - (4 * 86400000)) / 86400000 / 7);

		var bCurrentPageFound = false;
		this.iCurrentPage = 0;

		for (var i = 0; i < aPerformanceWeeks.length; i++) {
			this.aPages[i] = new PaginatorPage(i + 1, oThis);
			if (
				(iCurrentWeek == aPerformanceWeeks[i]) ||
				(iCurrentWeek < aPerformanceWeeks[i] && !bCurrentPageFound)
			) {
				bCurrentPageFound = true;
				this.iCurrentPage = i;
			}
		}

		/* back */
		this.ePrevLink = document.getElementById('prevPage');
		if (this.ePrevLink) this.ePrevLink.onclick = function() { oThis.showPrevPage(oThis); }
		/* next */
		this.eNextLink = document.getElementById('nextPage');
		if (this.eNextLink) this.eNextLink.onclick = function() { oThis.showNextPage(oThis); }

		this.ePagesSwitchBlock = document.getElementById('pagesBlock');
		
		this.setLinks();

		return this;
	} else {
		this.hideControls();
		return null;
	}
}

Paginator.prototype.setLinks = function() {
	if (this && !isNaN(this.iCurrentPage)) {
		if (this.ePages) this.ePages.innerHTML = '<span style="color: black;">' + getWeekTitle(aPerformanceWeeks[this.iCurrentPage]) + '</span>';
		/* back */
		if (this.ePrevLink) this.ePrevLink.className = (this.iCurrentPage > 0)? 'pseudo-link' : 'pseudo-link disabled';
		/* next */
		if (this.eNextLink) this.eNextLink.className = (this.iCurrentPage + 1 < this.aPages.length)? 'pseudo-link' : 'pseudo-link disabled';
		
		if ((this.ePrevLink.className == 'pseudo-link disabled')&&(this.eNextLink.className == 'pseudo-link disabled'))
		{			
			if (this.ePagesSwitchBlock)
			{
				this.ePagesSwitchBlock.style.display = 'none';
				this.ePagesSwitchBlock.style.height = '0px';
			}
		}
	}
}

Paginator.prototype.showControls = function() {
	if (this && this.eTable) this.eTable.className = '';
}

Paginator.prototype.hideControls = function() {
	if (this && this.eTable) this.eTable.className = 'hidden';
}

Paginator.prototype.showPage = function(iPageNumber) {
	if (this) {
		this.iCurrentPage = iPageNumber;
		this.setLinks();
		if (this.displayFunction) this.displayFunction();
	}
}

Paginator.prototype.showPrevPage = function(oPaginator) {
	if (oPaginator) {
		if (oPaginator.iCurrentPage > 0) {
			oPaginator.iCurrentPage--;
			oPaginator.setLinks();
			if (oPaginator.displayFunction) oPaginator.displayFunction();
		}
	}
}

Paginator.prototype.showNextPage = function(oPaginator) {
	if (oPaginator) {
		if (oPaginator.iCurrentPage +1 < oPaginator.aPages.length) {
			oPaginator.iCurrentPage++;
			oPaginator.setLinks();
			if (oPaginator.displayFunction) oPaginator.displayFunction();
		}
	}
}


/* Paginator Page */

function PaginatorPage(iNumber, oPaginator) {
	if (oPaginator && oPaginator.ePages) {
		var oThis = this;

		this.iNumber = iNumber;

		this.oPaginator = oPaginator;

		return this;
	} else return null;
}
