function Paginator() {
	var mEntities;
	var mPageSize;
	var mCurrentPage = 0;
	var mCurrentEntity = null;
	var mContextPath = "..";
	var mDetailURL = null;
	var mDetailURLAttr = null;
	var mEntityName = null;
	var mPaginatorName = null;
}

Paginator.prototype = {
		
	initialize :  function(entities, pageSize) {
	  this.mEntities = entities;
	  this.mPageSize = pageSize;
	  this.mCurrentPage = 0;
	  this.mCurrentEntity = null;
	  this.mContextPath = "..";
    },

    setContextPath : function(contextPath) {
    	this.mContextPath = contextPath;
    	
    },
    
    setEntityName : function(entityName) {
    	this.mEntityName = entityName;
    	
    },
    
    setPaginatorName : function(paginatorName) {
    	this.mPaginatorName = paginatorName;
    	
    },
    
    setDetailURL : function(detailURL,detailAttr) {
    	this.mDetailURL = detailURL;
    	this.mDetailURLAttr = detailAttr;
    },
    
    setEntities : function(entities) {
      this.mEntities = entities;
      this.mCurrentPage = 0;
      this.mCurrentEntity = null;
      this.next();
    },
    
    getRow : function() {
    	
    	var entity = null;
    	if (this.mEntityName == null) {
    		entity = "product";
    	}
    	else {
    		entity = this.mEntityName;
    	}
    	
    	removeListItems(entity+"ListT");

		var tableId = document.getElementById(entity+"ListT");
		
		var bodyElem = document.createElement("tbody");
		bodyElem.setAttribute("id", entity+"ListTB");

		tableId.appendChild(bodyElem);
		
		var rowElem = document.createElement("tr");
		rowElem.setAttribute("valign","middle");
		rowElem.setAttribute("vAlign","middle");
		bodyElem.appendChild(rowElem);
		
		return rowElem;
    },
    
    getImageTable : function(entity) {
    	var imageStyle = null;
    	var ta = "<td align='center'><table border='0' cellpadding='0' cellspacing='0' class='product-display-in' style='width:100%'><tr valign='bottom'><td align='center'>";
    	if (this.mDetailURL == null) {
            ta = ta + "<a href='javascript:void(0);' onclick='showProductDetails();product.view("+entity["id"]+");'>";
    	}
    	else {
		    

    		ta = ta + " <a href='"+this.mDetailURL+"?d="+entity[this.mDetailURLAttr]+"' target='_blank'>";
    		imageStyle = "style='max-width:150px'";
			if (this.isIE()) 
			  ta = ta + "<div style='table-layout:fixed; display:table; width:100%'>";
			  
    	}
            		
         ta = ta +"<img src='"+this.mContextPath+"/"+entity["image"]+"' class='product-display-in' "
         if (imageStyle != null) {
        	 ta = ta+imageStyle;
			 ta = ta + "/>";
		    if (this.isIE()) 	 
			 ta = ta + "</div>";
         }
		 else {
		   ta = ta + "/>";
		 }
		 
         ta = ta + "</a></td></tr></table></td>";
         return ta;   		
    },
    
    setViewingEntity : function(entity) {
    	
    	this.mCurrentEntity = entity;
    },
    
    getNextViewingEntityId : function () {
    	
    	if (this.mCurrentEntity == null) {
    		return null;
    	}
    	
    	for (i=0; i<this.mEntities.length; i++) {
    		if (this.mCurrentEntity == this.mEntities[i]['id']) {
    			if (i < this.mEntities.length -1) { 
    				return this.mEntities[i+1]['id'];
    			}
    		}
    	}
    	
    	return null;
    },
    
    getImageSpace : function() {
    	if (this.mDetailURL != null) {
    		return 5;
    	}
    	return 15;
    },
    next : function() {
    	
    	
    	if (this.mEntities != null && this.hasNext()) {
    		
    		var start = this.mCurrentPage*this.mPageSize;
    		
    		var count = 0;
    		
    		var rowElem = this.getRow();
    		
    		if (start < this.mEntities.length) {
        		  this.mCurrentPage = this.mCurrentPage + 1;
            }
    		
    		this.addLink(rowElem, true);
    		
    		var t = "<table class='product-display-in' border='0' cellspacing='0' cellpadding='"+this.getImageSpace()+"'><tr valign='bottom'>"
    		
    		var thisObject = this;
    		for (i=start; i<this.mEntities.length && count < this.mPageSize; i++) {
    			
  				t = t+ thisObject.getImageTable(thisObject.mEntities[i]);
  				
    			count = count + 1;
    		}
    		
    		t = t + "</tr></table>";
    		
    		
    		createElement(rowElem, "td", t);
    		
    		this.addLink(rowElem, false);
    		
    	}
    },
    
    previous : function() {
    	if (this.mEntities != null && this.hasPrevious()) {
    		
    		this.mCurrentPage = this.mCurrentPage - 1;
    		
    		
            var start = (this.mCurrentPage-1)*this.mPageSize;
    		
    		var count = 0;
    		
    		var rowElem = this.getRow();
    		
    		this.addLink(rowElem, true);

    		var t = "<table class='product-display-in' border='0' cellspacing='0' cellpadding='"+this.getImageSpace()+"'><tr valign='bottom'>"
    		
    		var thisObject = this;
    		
    		for (i=start; i<this.mEntities.length && count < this.mPageSize; i++) {
    			
    			t = t+ thisObject.getImageTable(thisObject.mEntities[i]);
    			
    			count = count + 1;
    			
    		}
    		t = t + "</tr></table>";
    		createElement(rowElem, "td", t);
    		
    		this.addLink(rowElem, false);
    	}
    },	
    
    addLink : function(row, isPrevious) {
    	
    	var paginatorName = null;
    	
    	if (this.mPaginatorName == null) {
    		paginatorName = "paginator";
    	}
    	else {
    		paginatorName = this.mPaginatorName;
    	}
    	if (isPrevious) {
    		if (this.hasPrevious()) {
    			var link = "<a href='javascript:void(0)' onclick='"+paginatorName+".previous()'><img src='"+this.mContextPath+"/img/product_lscroll.png'/></a>"
    			createElement(row, "td", link);
    		}
    	}
    	else {
    		
    		if (this.hasNext()) {
    			var link = "<a href='javascript:void(0)' onclick='"+paginatorName+".next()'><img src='"+this.mContextPath+"/img/product_rscroll.png'/></a>"
				var td = createElement(row, "td", link);
			}
    	}
    },
    
    hasNext : function() {
    	return (this.mEntities != null && this.mEntities.length > (this.mCurrentPage*this.mPageSize));
    },
    
    hasPrevious : function() {
    	return this.mCurrentPage > 1;
    },
	
	isIE : function() {
	 return !!(window.attachEvent &&
      navigator.userAgent.indexOf('Opera') === -1);
	}
}