﻿
//
// JAVASCRIPT PAGER (V 1.0) - ELETMOD FORK
//

var pagerQueue = new Array();

var tJsProtoPager = Class.create();

tJsProtoPager.prototype = {

    initialize: function( pObjectName, pBaseClass, pContainer ) {
		this.objectName = pObjectName;

		//public
		this.normalClass = pBaseClass + 'Normal';
		this.activeClass = pBaseClass + 'Active';
		this.hideSwitcherOnBump = true;
		
		//protected
		this.scannedPages = new Array();
		this.targetContainer = $(pContainer);
		this.pageOffset = 0;

		//init
		this.scanPages();
		this.generateHTML( pContainer );
		this.autoSwitch();
		
		if (pagerQueue) { pagerQueue.push(this); }
	},
	

	
	/*switches to the specified page*/	
	switchTo: function ( targetPage ) {
		
		//show/hide large containers
		this.showHidePage(targetPage - 1);
		
		//reset button classes
		buttons = this.targetContainer.getElementsByTagName('*');
		for(var i = 0; i < buttons.length; i++)
		{
			if ( buttons[i].className.has('jsPSwitch') )
			{
				buttons[i].className = this.normalClass;
			}
		}
		
		//mark active button (single instance)
		$( this.targetContainer.id + '_jsPSwitch' + targetPage).className = this.activeClass;
		self.scrollTo(0, 0); 
		
		//rewrite hash (not nice)
		location.hash = '#p' + targetPage; 
		
		//hide prev/next buttons
		if (this.hideSwitcherOnBump == true)
		{
			if (targetPage == 1) 
			{ 
				Element.hide($(this.objectName + '_btnPrev')); 
			} else {
				Element.show($(this.objectName + '_btnPrev')); 
			}
			
			if (targetPage == this.scannedPages.length)
			{ 
				Element.hide($(this.objectName + '_btnNext')); 
			} else {
				Element.show($(this.objectName + '_btnNext')); 
			}
		}
		
		
	},
	
	//call switchto (multiple times if needed)
	switchAllTo: function ( targetPage ) {
		if (targetPage < 1) { targetPage = 1 }
		if (targetPage > this.scannedPages.length) { targetPage = this.scannedPages.length }
		
		if (pagerQueue) 
		{ 
			for(var i = 0; i < pagerQueue.length; i++)
			{
				pagerQueue[i].switchTo(targetPage);
			}
		} else {
			this.switchTo(targetPage);
		}
	},
	
	switchAllToPrev: function () { 
		this.switchAllTo(this.getCurrentPage()-1);
	},
	
	switchAllToNext: function () { 
		this.switchAllTo(this.getCurrentPage()+1);
	},
	
	/*reveals the selected page, hide the others*/
	showHidePage: function ( pActivePageNum ) {
		for(var i = 0; i < this.scannedPages.length; i++)
		{
			if (i != pActivePageNum) { Element.hide(this.scannedPages[i]); }
				else { Element.show(this.scannedPages[i]); }
		}
	},
	
	/*pushes relevant containers into the scannedPages array*/
	scanPages: function () {
		var htmlTags = document.getAll();
		for(var i = 0;i < htmlTags.length; i++)
		{
			if ( htmlTags[i].className.has('jsPage') )
			{
				this.scannedPages.push(htmlTags[i]);
			}
		}
	},
	
	/*inserts the generated pager into the div specified by targetDiv id*/
	generateHTML: function () {
	
		newCode  = '';
		newCode += '<div class="pager">';
		
		newCode += '<div id="' + this.objectName + '_btnPrev" class="pgLeft">';
		newCode += '	<a href="#" onclick="javascript:' + this.objectName + '.switchAllToPrev(); return false;">';
		newCode += '	Előző</a></div>'; //prev
			 
		newCode += '<div class="pgNumbers">';
			for(var i = 1; i < this.scannedPages.length+1; i++)
			{
				m = i + this.pageOffset;
				newCode += '<span id="' + this.targetContainer.id + '_jsPSwitch' + i + '" class="' + this.normalClass + '">';
				newCode += '    <a href="#p' + i + '" onClick="javascript:' + this.objectName + '.switchTo(' + i + ')">' + m + '</a>';
				if (i < this.scannedPages.length) { newCode += '<span class="pSplit">|</span>'; }
				newCode += '</span>';
			}
			newCode += '</div>';
		
		newCode += '<div id="' + this.objectName + '_btnNext" class="pgRight">';
		newCode += '	<a href="#" onclick="javascript:' + this.objectName + '.switchAllToNext(); return false;">';
		newCode += '	Következő</a></div>'; //next
		
		newCode += '</div>';
		
		this.targetContainer.innerHTML = this.targetContainer.innerHTML.replace( '<!--[PAGES]-->', newCode);
	},
	
	getCurrentPage: function() {
		locHash = location.hash;
		if ( !locHash.has('#p') ) 
		{ 
			return 1;
		} else {
			pageNum = Number(locHash.substr(2));
			if ( (pageNum <= this.scannedPages.length) && (pageNum > 0) )
			{ 
				return pageNum; 
			} else {
				return 1; 
			}
		}
	},
	
	
	/*uses page location.hashmark for storing selected page name*/
	autoSwitch: function () {
		this.switchTo(this.getCurrentPage());
	}

}

