﻿function JPager(records, size, index)
{
    this.pageCount = records / size;
    this.from = index * size;
    this.to = (records > this.from + size) ? this.from + size : records;
}

function JRepeater(id, template)
{
    // private members
    var _id = id;
    var _template = template;
    var _jevent = new JEvent(); 
    var _dataSource = null;
   
    // public members
    this.getId = function() { return _id; }
    this.getTemplate = function() { return _template; }
    this.getEvent =  function() { return _jevent; }
    this.pager = null;
    this.pageSize = 10;
    this.pageIndex = 0; 
    
    // public methods
    this.setDataSource = function(value)
    {
        this.pager = new JPager(value.length, this.pageSize, this.pageIndex);
        _dataSource = value;
    };
    
    this.getDataSource = function() { return _dataSource; }
    
    // events
    this.onSort = function(action) { _jevent.addListener(this,"onSort",action); }; 
    this.removeSort = function(action) { _jevent.removeListener(this,"onSort",action); };
    this.onItemDataBound = function(action) { _jevent.addListener(this,"onItemDataBound",action); }; 
    this.removeItemBind = function(action) { _jevent.removeListener(this,"onItemDataBound",action); };
    this.onItemDelete = function(action) { _jevent.addListener(this,"onItemDelete",action); }; 
    this.removeItemDelete = function(action) { _jevent.removeListener(this,"onItemDelete",action); };
}

JRepeater.prototype.sort = function()
{
    this.getEvent().fireEvent(null,this,'onSort');
}

JRepeater.prototype.dataBind = function()
{
    var html = "";
    if (this.getDataSource() == null ||
        this.getDataSource().length == 0)
    {   
        document.getElementById(this.getId()).innerHTML = html;
        return;
    }
    
    this.sort();
    
    for(var i = this.pager.from; i < this.pager.to; i++)
    {
        var row = this.getTemplate();
        var dataItem = this.getDataSource()[i];
    
        var dataArgs = [dataItem, row, i];
        this.getEvent().fireEvent(null,this,'onItemDataBound', dataArgs);
        dataItem = dataArgs[0];
        row = dataArgs[1];        
        
        for (var property in dataItem)
        {
			try {
			    row = row.replace( new RegExp("{\\$" + property + "}","g"), eval("dataItem." + property) );
			}catch(e){}
		}
		
		// set index
		row = row.replace( new RegExp("{\\@Index}","g"), i );
		
		html += row;
    }
    document.getElementById(this.getId()).innerHTML = html;
}

JRepeater.prototype.remove = function(index)
{
    if (!this.getEvent().fireEvent(null,this,'onItemDelete', index))
        return;
        
    var dataSource = this.getDataSource();  
    dataSource.splice(index, 1);
    this.setDataSource(dataSource);
    
    this.dataBind();
}

JRepeater.prototype.add = function(item)
{
    this.getEvent().fireEvent(null,this,'onItemAdd', item);
    var dataSource = this.getDataSource();
    dataSource[dataSource.length] = item;
    this.setDataSource(dataSource);
    this.dataBind();
}