// Depndent on Jquery

function resultsController(divID, options){
	if(!divID) return;
	
	options = options || {};
	
	options.columns = options.columns || 3;
	options.title   = options.title || '<h3>Rezultate</h3>';
	
	this.data = options.data || [];

	this.options = options;
	
	this.divID = divID;
	this.jID = $("#"+divID);
	}
	
resultsController.prototype.update = function(array, rowFunction){
	this.clear();

	var len = array.length,i;
	if(!len){
		this.hide();
		return;
		}
		
	for(i=0;i<len;i++)
		this.data.push(rowFunction(array[i]));
	
	this.populate();
	this.show();
	}

resultsController.prototype.append = function(array, rowFunction){
	
	this.jID.html("");
	
	var i;
	var newlen = array.length;
	
	for(i=0;i<newlen;i++)
		this.data.push(rowFunction(array[i]));
		
	this.populate();
        this.show();
	}

resultsController.prototype.prepend = function(array, rowFunction){
	}


resultsController.prototype.populate = function(){
	// Shortcuts
	var title	 = this.options.title;
	var c		 = this.options.columns; //collumn count 
	var i,j,div,el;
	var cols = this.makeCols();

		
	var mainDiv = $('<div></div>')
		.addClass('p');
	
	if(title) 
		mainDiv.append(title);
		
	for(i=0;i<c;i++){
		if(typeof(cols[i]) != "undefined"){
			div = $('<div></div>').css({
					'float'	: 'left',
					width	: Math.floor(100/c)+"%",
					'line-height'	: '26px'
				});
		
			for(j=0;j<cols[i].length;j++){
				if(typeof(cols[i][j]) != "undefined"){
					el = $(cols[i][j]);
					div.append(el).append('<br/>');
					}
				}
			
			mainDiv.append(div);
			}
		}
	mainDiv.append("<div style='clear:both'></div>");
	
	

	this.jID.append(mainDiv);
	}
	
resultsController.prototype.makeCols = function(){
	// Shortcuts
	var data	 = this.data;
	var c		 = this.options.columns; //collumn count
	var i,j,item;
	var cols = [];
	
	for(j=0;j<data.length;j+=c)
		for(i=0;i<c;i++)
			if(typeof(data[j+i]) != "undefined"){
				item = data[j+i];
				if(typeof(cols[i]) == "undefined")
					cols[i] = [];
				
				if(typeof(item) == "object")
					cols[i].push(item);
				}
	return cols;
	}
resultsController.prototype.clear = function(){
	this.data = [];
	this.jID.html("");
	}
	
resultsController.prototype.hide = function(){
	this.jID.slideUp();
	}
	
resultsController.prototype.show = function(){
	this.jID.slideDown();
	}
	
resultsController.prototype.clearAndHide = function(){
	this.clear();
	this.hide();
	}
