/**
 * AjaxBrowser
 * enables browsing through lists via page buttons
 * requirements:
 * - loaded dojo framework
 * - 4 browse buttons/links: first/last/previous/next page
 * - element containing all data (can include buttons if every page file contains buttons itself)
 * - dataDirectory needs to include all page data files in format pageDDDD.dat (f.e. page0001.dat)
 */
function AjaxBrowser(dataDirectory, contentNode, curPage, maxPage) {
	// path to load data
	this.dataDirectory = dataDirectory;

	// content node
	this.contentNode = contentNode;

	// current and max page
	this.curPage  = curPage;
	this.maxPage  = maxPage;

	// button types
	this.BUTTON_PREVIOUS = "BUTTON_PREVIOUS";
	this.BUTTON_NEXT     = "BUTTON_NEXT";
	this.BUTTON_FIRST    = "BUTTON_FIRST";
	this.BUTTON_LAST     = "BUTTON_LAST";

	// array of connected list elements (links in list)
	this.connectedElements = new Array();

	this.runLater = function() {
		if (maxPage >= 0) {
			dojo.addOnLoad(this, "connectButtons");
		}
	}
	this.run = function() {
		if (maxPage >= 0) {
			this.connectButtons();
		}
	}

	this.registerCallback = function(cbObject, callback) {
		this.cbObject = cbObject;
		this.callback = callback;
	}

	this.registerButton = function(buttonType, buttonId, imagePathEnabled, imagePathDisabled) {
		switch(buttonType) {
			case this.BUTTON_PREVIOUS:
				this.buttonPrev = new Button(buttonId, imagePathEnabled, imagePathDisabled);
				break;
			case this.BUTTON_NEXT:
				this.buttonNext = new Button(buttonId, imagePathEnabled, imagePathDisabled);
				break;
			case this.BUTTON_FIRST:
				this.buttonFirst = new Button(buttonId, imagePathEnabled, imagePathDisabled);
				break;
			case this.BUTTON_LAST:
				this.buttonLast = new Button(buttonId, imagePathEnabled, imagePathDisabled);
				break;
		}
	}

	this.prevPage = function(e) {
		this.curPage--;
		this.showPage(e);
	}
	this.nextPage = function(e) {
		this.curPage++;
		this.showPage(e);
	}
	this.firstPage = function(e) {
		this.curPage = 0;
		this.showPage(e);
	}
	this.lastPage = function(e) {
		this.curPage = this.maxPage;
		this.showPage(e);
	}
	this.showPage = function(e){
		if (e) e.preventDefault();
		// remove previously connected buttons
		while (this.connectedElements.length > 0) {
			dojo.disconnect(this.connectedElements.pop());
		}
		// correct page number in case of double click at beginning/end
		if (this.curPage < 0) this.curPage = 0;
		else if (this.curPage > this.maxPage) this.curPage = this.maxPage;
		// format page number
		var target = this.curPage+"";
		var contentNode  = dojo.byId(this.contentNode);
		var callerObject = this;
		dojo.xhrGet({
			url: callerObject.dataDirectory.replace(/XXXX/, target),
			handleAs: "text",
			load: function(data,args){
				contentNode.innerHTML = data;
				// reconnect buttons
				callerObject.connectButtons();
				if (callerObject.callback) {
					eval("callerObject.cbObject."+callerObject.callback+"()");
				}
			},
			// if any error occurs, it goes here:
			error: function(error,args){
				// uid needed in case of having several browsers per page
				var uid = (Math.random()+"").substr(2);
				contentNode.innerHTML = "Fehler beim Laden der Daten.<br><a href='#' id='ajaxBrowserErrorRetry"+uid+"'>Erneut versuchen</a>";
				callerObject.connectedElements.push(dojo.connect(dojo.byId("ajaxBrowserErrorRetry"+uid),"onclick",callerObject,"showPage"));
			}
		});
	};
	this.connectButtons = function() {
		if (this.curPage > 0) {
			this.buttonPrev.setStateEnabled(true);
			this.connectedElements.push(dojo.connect(this.buttonPrev.getButtonLink(),"onclick",this,"prevPage"));
			this.buttonFirst.setStateEnabled(true);
			this.connectedElements.push(dojo.connect(this.buttonFirst.getButtonLink(),"onclick",this,"firstPage"));
		}
		else {
			this.buttonPrev.setStateEnabled(false);
			this.buttonFirst.setStateEnabled(false);
		}
		if (this.curPage < this.maxPage) {
			this.buttonNext.setStateEnabled(true);
			this.connectedElements.push(dojo.connect(this.buttonNext.getButtonLink(),"onclick",this,"nextPage"));
			this.buttonLast.setStateEnabled(true);
			this.connectedElements.push(dojo.connect(this.buttonLast.getButtonLink(),"onclick",this,"lastPage"));
		}
		else {
			this.buttonNext.setStateEnabled(false);
			this.buttonLast.setStateEnabled(false);
		}
	}
}

function Button(buttonId, buttonPathEnabled, buttonPathDisabled) {
	this.buttonId           = buttonId;
	this.buttonPathEnabled  = buttonPathEnabled;
	this.buttonPathDisabled = buttonPathDisabled;

	this.setStateEnabled = function(enabledFlag) {
		if (enabledFlag) {
			dojo.query("img", this.buttonId).map( function(item) { item.src = this.buttonPathEnabled; item.style.cursor = "pointer"; }, this );
		}
		else {
			dojo.query("img", this.buttonId).map( function(item) { item.src = this.buttonPathDisabled; item.style.cursor = "default"; }, this );
		}
	}

	this.getButtonLink = function() {
		return dojo.byId(this.buttonId);
	}
}