//This script requires that ajax.js has be loaded.

function Sponsors(sInstanceName, sHolderId, sNavHolderId, sSponsorsInfoId) {
	this.debugOut = function(sOutput) {
		if (true && typeof(console) != 'undefined') {
			console.log(sOutput);
		}
	}
	var that = this;
	
	this.oaLists = {};
	
	this.sHolderId = sHolderId;
	this.eHolder = document.getElementById(sHolderId);
	
	this.sNavHolderId = sNavHolderId;
	this.eNavHolder = document.getElementById(sNavHolderId);
	
	this.sSponsorsInfoId = sSponsorsInfoId;
	this.eSponsorsInfo = document.getElementById(sSponsorsInfoId);
	
	this.sInstanceName = sInstanceName;
	
	this.sHolderId;
	
	this.iPerPage = 999;
	this.iLogoWidth = 100;
	this.iLogoHeight = 100;
	this.LogoClass = "sponsorsLogo";
	this.sNextText = "Next page &gt;";
	this.sPreviousText = "&lt; Previous page";
	this.sInfoLoadingHTML = "Loading...";
	
	this.addSponsor = function(sListName, sCompanyId, sImageFile, sCompanyName) {
		if (typeof(sCompanyId) == 'array') {
			that.oaLists[sListName].push(sCompanyId);
		} else {
			that.oaLists[sListName].push([sCompanyId, sImageFile, sCompanyName]);
		}
	}
	
	this.init = function(sListName) {
		that.iCurrentPage = 0;
		that.sCurrentList = sListName;
		that.debugOut("Initialising...");
		that.buildPage(0);
		that.loadInfo(that.sSponsorsInfoId, 0);
	}
	
	this.previousPage = function() {
		if (that.iCurrentPage > 0) {
			that.iCurrentPage -= 1;
			that.debugOut("Go to previous page: " + that.iCurrentPage);
			that.buildPage(that.iCurrentPage);
		}
	}
	
	this.nextPage = function() {
		that.debugOut("Next page: clicked");
		if (that.iCurrentPage < (that.oaLists[that.sCurrentList].length/this.iPerPage) -1) {
			that.iCurrentPage += 1;
			that.debugOut("Go to next page: " + that.iCurrentPage);
			that.buildPage(that.iCurrentPage);
		}
		this.navVisibility();
	}
	
	this.navVisibility = function() {
		if (that.iCurrentPage == 0) {
			that.ePrevLink.style.display = "none";
		} else {
			that.ePrevLink.style.display = "block";
		}
		
		if (that.iCurrentPage > (that.oaLists[that.sCurrentList].length/this.iPerPage) -1) {
			that.eNextLink.style.display = "none";
		} else {
			that.eNextLink.style.display = "block";
		}
	}
	
	this.buildPage = function(iPageNumber) {
		that.debugOut("building page");
		that.iCurrentPageNumber = iPageNumber;
		that.buildLogos();
		that.buildNav();
		this.navVisibility();
	}
	
	this.buildLogos = function() {
		that.debugOut("building logos: " + that.oaLists[that.sCurrentList].length);
		that.removeChildren(that.eHolder);
		
		var iStart = that.iCurrentPage * this.iPerPage;
		var iEnd = (that.iCurrentPage+1) * this.iPerPage;
		
		for (var i=iStart; i < that.oaLists[that.sCurrentList].length && i < iEnd; i++) {
			that.debugOut("Creating logo " + i + ": " + that.oaLists[that.sCurrentList][i][0] + " defined");
			that.addElement(that.sHolderId, 'img', [
				['id',that.oaLists[that.sCurrentList][i][0]],
				['src',that.oaLists[that.sCurrentList][i][1]],
				['alt',that.oaLists[that.sCurrentList][i][2]],
				['class',that.LogoClass],
				['alt',that.oaLists[that.sCurrentList][i][2]],
				['width',that.iLogoWidth],
				['height',that.iLogoHeight],
				['onclick', that.sInstanceName + '.loadInfo("' + that.sSponsorsInfoId + '", ' + i + ')']
			]);
		}
	}
	
	this.buildNav = function() {
		that.removeChildren(that.eNavHolder);
		
		that.debugOut("building navigation");
		that.ePrevLink = that.addElement(that.sNavHolderId, 'div', [
				['id',that.sInstanceName + "_prev"],
				['cursor','pointer'],
				['onclick',that.sInstanceName + '.previousPage();']
			]);
		that.ePrevLink.innerHTML = that.sPreviousText;
		
		that.eNextLink = that.addElement(that.sNavHolderId, 'div', [
				['id',that.sInstanceName + "_next"],
				['cursor','pointer'],
				['onclick',that.sInstanceName + '.nextPage();']
			]);
		
		that.eNextLink.innerHTML = that.sNextText;
		
	}
	
	this.removeChildren = function(eElement) {
		while (eElement.childNodes[0]) {
   			eElement.removeChild(eElement.childNodes[0]);
		}
	}
	
	this.addElement = function(sParentId, sType, aAttributes) {
		that.debugOut("adding element: " + aAttributes[0]);
		
		eParent = document.getElementById(sParentId);
		
		var newElement = document.createElement(sType);
		for (var i=0; i < aAttributes.length; i++) {
			if (aAttributes[i][0] == "onclick" || 
				aAttributes[i][0] == "onload" ||
				aAttributes[i][0] == "onmouseover") {
				
				that.debugOut("event: " + aAttributes[i][0] + " = " + aAttributes[i][1]);

				
				if (document.all) {
					//Attach event in the IE way
					function makeEventFunc(sEvents) {
						return function() {
							eval(sEvents);
						}
					}
					newElement.attachEvent(aAttributes[i][0], makeEventFunc(aAttributes[i][1]), true);
					
				} else {
					//Attach event in the Moz way
					newElement.setAttribute(aAttributes[i][0], aAttributes[i][1]);
				}
				
			} if (aAttributes[i][0] == "class") {
				newElement.className = aAttributes[i][1];
			} else {
				that.debugOut("attribute: " + aAttributes[i][0] + " = " + aAttributes[i][1]);
				newElement.setAttribute(aAttributes[i][0],aAttributes[i][1]);
			}
		}
		
		
		eParent.appendChild(newElement);
		
		return newElement;
	}
	
	this.loadInfo = function(sDestinationId, iItemIndex) {
		document.getElementById(sDestinationId).innerHTML = that.sInfoLoadingHTML;
		
		that.debugOut("Load info from: " + that.oaLists[that.sCurrentList][iItemIndex][3] + " into " + sDestinationId);
		
		oAjax = new AjaxPage();
		oAjax.request(that.oaLists[that.sCurrentList][iItemIndex][3], sDestinationId);
	}
	
}