
var GghoPager = new Class ({

//id - identyfikator elementu który ma być stronicowany
//maxHeight - wysokosc tekstu po ktorej wlacza sie stronicowanie

	init : function (id, maxHeight) {
		this.text = $(id);
		this.text = $(this.text);
		
		//ustaw pager tylko kiedy tekst wyzszy niz kono
		if (this.text.offsetHeight <= maxHeight) {
			return false;
		}
		// ustaw wysokosc na potrzeby problemow w IE z
		// przesuwaniem stron z elementami float
		this.text.setStyle('height', this.text.offsetHeight);
		
		//przygotuj elementy pagera
		
		this.wind = new Element('div', {'class': 'ggho-pager-wind'});
		this.wind.setStyle('height', maxHeight);
		this.contener = new Element('div', {'class': 'ggho-pager-cont'});
		this.pager = new Element('div', {'class': 'ggho-pager-pager'});
		
		this.contener.injectBefore(this.text);
		
		//alert(wind);
		this.wind.adopt(this.text);
		this.contener.adopt(this.wind);
		this.pager.injectAfter(this.wind);
		
		this.initPager();
		
		this.crrPage = 1;
		this.updatePager();
		return true;

	},
	
	initPager : function () {
		var textHeight = this.text.offsetHeight;
		this.pagesCnt = Math.ceil(textHeight / this.wind.offsetHeight)
		this.pages = [];
		this.pagesNav = [];
		
		var navNext = new Element('img', {'src': 'assets/site/layout_images/nav_next.gif', 'id': 'ggho-pager-pg-n', 'class': 'ggho-pager-next'});
		navNext.addEvent('click', this.changePage.bind(this));
		this.pagesNav.push(navNext);
		
		var navPrev = new Element('img', {'src': 'assets/site/layout_images/nav_prev.gif', 'id': 'ggho-pager-pg-p', 'class': 'ggho-pager-prev'});
		navPrev.addEvent('click', this.changePage.bind(this));
		this.pagesNav.push(navPrev);
		navPrev.injectInside(this.pager);
		this.pager.appendText('  ');
		
		for (var i = 1; i < this.pagesCnt + 1; i++) {
			var pgEl = new Element('a', {href: '#', id: 'ggho-pager-pg-' + i})
			pgEl.appendText(i);
			pgEl.addEvent('click', this.changePage.bind(this));
			this.pages.push(pgEl);
			pgEl.injectInside(this.pager);
			this.pager.appendText('  ');
		}
		navNext.injectInside(this.pager);
	},
	
	changePage : function (e) {
		//alert('changePage');
		e = new Event(e);
		var el = e.target;
		el = $(el);
		var id = el.getProperty('id');
		
		
		var idArr = id.split('-');
		
		if (idArr[3] == 'p') {
			var pg = this.crrPage - 1;
		} else if (idArr[3] == 'n') {
			var pg = this.crrPage + 1;
		} else {
			var pg = idArr[3].toInt(); 
		}
	
		if (pg < 1) {
			pg = 1;
		} else if (pg > this.pagesCnt) {
			pg = this.pagesCnt;
		}
		
		var chPage = new Fx.Style(this.text.getProperty('id'), 'margin-top', {transition: Fx.Transitions.Quart.easeInOut})
		
		var crrMargin = this.text.getStyle('margin-top').toInt();
		//console.log(crrMargin);
		//alert(crrMargin);
		//alert(this.wind.offsetHeight * (pg - 1) * -1);
		chPage.start(crrMargin, this.wind.offsetHeight * (pg - 1) * -1);
		this.crrPage = pg;
		this.updatePager();
		
		//this.text.setStyle('margin-top', this.wind.clientHeight * (pg - 1) * -1)
		
	},
	updatePager : function() {

		this.pages.each(function(item, index) {			
				index = index + 1;
			if (index == this.crrPage) {
				item.addClass('ggho-pager-crrpg');
			} else {
				item.removeClass('ggho-pager-crrpg');
			}
		}.bind(this));
	}
}); 

