
/**
 * Main UB program class
 */
UB.EventsGrid = new Class({
	Extends: UB,
	
	uids: [],
	element: null,
	pagerElement: null,

	events: null,
	
	pager: null,
	changer: null,
	request: null,
	
	pageNum: null,
	pageEvents: null,
	
	options: {
		addColorWrapper: false,
		pager: {
			perPage: 5
		}
	},
	
	fx: {
		hideDefaultView: null,
		hideFilteredView: null
	},
	
	initialize: function (uids, element, pagerElement, options) {
		// set options
		this.parent(options);
		
		// init els
		this.element = $(element);
		this.pagerElement = $(pagerElement);

		// init opbjects
		this.changer = new MTAnimatedContentChanger(this.element, {});
		
		// build
		this.build(uids);
		
		// show/load first page
		if (this.options.initialPage 
			&& (this.pager.getPagesCount() >= this.options.initialPage)
		) {
			this.showPage(this.options.initialPage);
		}
	},
	
	build: function (uids) {
		// remove old
		this.destroyEvents();
		
		// empty entire element
		this.element.empty();
		
		// set the new uids
		this.uids = uids? uids : [];
		
		// init pager
		this.pager = new MTPager(this.uids, this.pagerElement, this.pageChangeHandler.bind(this), this.options.pager);
	},
	
	getPagesCount: function () {
		return this.pager.getPagesCount();
	},
	
	showPage: function (pageNum, onComplete, onCompleteIfNone) {
		if (this.isRunning) {
			return;
		}
		this.isRunning = true;
		var oThis = this;
		return this.pager.gotoPage(pageNum, function () {
			if (onComplete) {
				onComplete.run([], this);
			}
			oThis.isRunning = false;
		}, onCompleteIfNone);
	},
	
	getCurrentPage: function () {
		return this.pager.getCurrentPage();
	},
	
	
	hasEvent: function (uid) {
		return this.uids.indexOf(uid) == -1? false : true;
	},
	
	showEvent: function (uid, onComplete) {
		var pageNum = this.pager.findPageNum(uid, true);
		if (! pageNum) {
			return;
		}
		this.showPage(pageNum, onComplete);
	},
	
	pageChangeHandler: function (pageNum, pageElements) {
		// save current page data
		this.pageNum = pageNum;
		this.pageEvents = pageElements;
		
		// cancel prev. requests
		if (this.request) {
			this.request.cancel();
		}
		
		// request new events data
		this.request = this.ajaxRequest('getEvents', {uids: pageElements}, function (results) {
			console.log(results);
			if (this.element.isVisible()) {
				this.changer.change( this.replaceEventsContent.bind(this, results) );
			} else {
				this.replaceEventsContent(results);
			}
		}.bind(this));
		
		// set pager 
		return false;
	},
	
	
	replaceEventsContent: function (newEvents) {
		// remove old conent
		this.destroyEvents();

		// add new content
		for (uid in newEvents) {
			this.events[uid] = new UB.Event(newEvents[uid]);
			this.events[uid].inject(this.element, 'bottom', this.options.addColorWrapper);
		}
		
		// continue pager life cicle
		this.pager.callChain();
	},
	
	destroyEvents: function () {
		if (this.events) {
			this.events.each(function (event) {
				event.destroy();
			});
		}
		this.events = $H({});
	}
	
});
// static
UB.EventsGrid.extend({
	
});


