//TODO
// - Sortieren auch für Dates 

function slyTable(parentNode, configuration) {

	this.parentNode = parentNode;
	if (!configuration) return;
	this.configuration = configuration;
	if (!this.configuration.entity) { alert('Configuration of slyTable failed. Please provide an entity!'); return; }
	
	/***************************************************************************************************
	   ADD HERE
	 ***************************************************************************************************/
	if (this.configuration.entity == 'news') {
		this.structure = {properties:{id:{type:'Number'}, date:{type:'Date'}, title:{type:'Text'}, text:{type:'Text'}, picture:{type:'Binary'}, published:{type:'Bool'}}};
	} else if (this.configuration.entity == 'links') {
		this.structure = {properties:{id:{type:'Number'}, name:{type:'Text'}, url:{type:'Text'}, publishOrder:{type:'Number'}, published:{type:'Bool'}, page:{type:'Text'}}};
	} else if (this.configuration.entity == 'members') {
		this.structure = {properties:{id:{type:'Number'}, company:{type:'Text'}, companyAddon:{type:'Text'}, street:{type:'Text'}, addressAddon:{type:'Text'}, zip:{type:'Number'}, city:{type:'Text'}, region:{type:'Text'}, phone:{type:'Text'}, fax:{type:'Text'}, website:{type:'Text'}, picture:{type:'Binary'}, published:{type:'Bool'}}};
	} else if (this.configuration.entity == 'organisation') {
		this.structure = {properties:{id:{type:'Number'}, name:{type:'Text'}, area:{type:'Text'}, func:{type:'Text'}, funcSpec:{type:'Text'}, picture:{type:'Binary'}, publishOrder:{type:'Number'}, published:{type:'Bool'}}};
	} else if (this.configuration.entity == 'files') {
		this.structure = {properties:{id:{type:'Number'}, name:{type:'Text'}, data:{type:'Binary'}, url:{type:'Text'}, size:{type:'Number'}, date:{type:'Date'}, type:{type:'Text'}, publishOrder:{type:'Number'}, published:{type:'Bool'}, page:{type:'Text'}}};
	} else if (this.configuration.entity == 'users') {
		this.structure = {properties:{id:{type:'Number'}, name:{type:'Text'}, token:{type:'Text'}, password:{type:'Text'}, email:{type:'Text'}}};
	} else if (this.configuration.entity == 'headerimages') {
		this.structure = {properties:{id:{type:'Number'}, picture:{type:'Binary'}, published:{type:'Bool'}}};
	}
	/***************************************************************************************************/
	else {
		alert('Configuration of slyTable failed. Schema not defined for entity ' + this.configuration.entity + '!'); return; 
		return;
	}
	
	this.data = Array();
	this.filteredData = this.data;
	
	this.filterString = '';
	this.sortPropertyName = 'id';   //Each object must have an 'id' property
	this.sortOrder = 'ASC';         //ASC or DESC
	this.page = 1;
	this.entriesPerPage = 5;
	this.selectedObjectIds = Array();
	this.currentEditProperty = false;
	this.currentEditPropertyData = false;
	this.currentEditPropertyIdField = false;
	
	this.id = 'slyTable-' + this.configuration.entity + '-' + (new Date()).getTime();
	this.node = document.createElement('div');
	this.node.id = this.id;
	this.node.className = 'slyWidget slyTable';
	this.parentNode.appendChild(this.node);
	this.formNode = document.createElement('div');
	this.formNode.style.display = 'none';
	this.formNode.id = this.id + '-form';
	this.parentNode.appendChild(this.formNode);
	sly.widgets[this.id] = this;
	
	this.loadData();
	
	/***************************************************************************************************
	   ADD HERE
	 ***************************************************************************************************/
	if (this.configuration.entity == 'news') {
		this.form = new slyNewsForm(document.getElementById(this.id + '-form'), {entity:this.configuration.entity, rowIndex:0, overviewTable:this});
	} else if (this.configuration.entity == 'links') {
		this.form = new slyLinksForm(document.getElementById(this.id + '-form'), {entity:this.configuration.entity, rowIndex:0, overviewTable:this});
	} else if (this.configuration.entity == 'members') {
		this.form = new slyMembersForm(document.getElementById(this.id + '-form'), {entity:this.configuration.entity, rowIndex:0, overviewTable:this});
	} else if (this.configuration.entity == 'organisation') {
		this.form = new slyOrganisationForm(document.getElementById(this.id + '-form'), {entity:this.configuration.entity, rowIndex:0, overviewTable:this});
	} else if (this.configuration.entity == 'files') {
		this.form = new slyFileForm(document.getElementById(this.id + '-form'), {entity:this.configuration.entity, rowIndex:0, overviewTable:this});
	} else if (this.configuration.entity == 'users') {
		this.form = new slyUserForm(document.getElementById(this.id + '-form'), {entity:this.configuration.entity, rowIndex:0, overviewTable:this});
	} else if (this.configuration.entity == 'headerimages') {
		this.form = new slyHeaderImageForm(document.getElementById(this.id + '-form'), {entity:this.configuration.entity, rowIndex:0, overviewTable:this});
	}
	/***************************************************************************************************/
}

slyTable.prototype.loadData = function() {
	var __this = this;
	sly.call('find', {entity:this.configuration.entity, filterRule:(this.configuration.entity == 'files' || this.configuration.entity == 'links' ? "page = '" + this.configuration.pageForFiles + "'" : "")}, function(data) {
		__this.data = data.res;
		__this.filteredData = data.res;
		__this.filter(__this.filterString);
		__this.refreshView();
		sly.dialog.arrangeElements();
	});
};

slyTable.prototype.refreshView = function() {
	var html = '<div id="' + this.id + '-navDiv" class="slyTable-NavPanelContainer">';	
	html += '<div class="slyTable-NavPanel">';
	html += 'Seite: ';
	html += '<a id="' + this.id + '-navFirstPage" class="iconButton iconButtonFirstPage" href="" onclick="sly.widgets[\'' + this.id + '\'].firstPage(); return false">First page</a> ';
	html += '<a id="' + this.id + '-navPrevPage" class="iconButton iconButtonPrevPage" href="" onclick="sly.widgets[\'' + this.id + '\'].prevPage(); return false">Previous page</a> ';
	html += '<select id="' + this.id + '-navPage" onchange="sly.widgets[\'' + this.id + '\'].setPage(this.value)"></select> ';
	html += '<a id="' + this.id + '-navNextPage" class="iconButton iconButtonNextPage" href="" onclick="sly.widgets[\'' + this.id + '\'].nextPage(); return false">Next page</a> ';
	html += '<a id="' + this.id + '-navLastPage" class="iconButton iconButtonLastPage" href="" onclick="sly.widgets[\'' + this.id + '\'].lastPage(); return false">Last page</a> ';
	html += '</div>';
	html += '<div class="slyTable-NavPanel">';
	html += 'Anzahl Einträge pro Seite: ';
	html += '<select id="' + this.id + '-navEntriesPerPage" onchange="sly.widgets[\'' + this.id + '\'].setEntriesPerPage(this.value)">';
	html += '<option value="5"' + (this.entriesPerPage == 5 ? ' selected="selected"' : '') + '>5</option>';
	html += '<option value="10"' + (this.entriesPerPage == 10 ? ' selected="selected"' : '') + '>10</option>';
	html += '<option value="20"' + (this.entriesPerPage == 20 ? ' selected="selected"' : '') + '>20</option>';
	html += '<option value="50"' + (this.entriesPerPage == 50 ? ' selected="selected"' : '') + '>50</option>';
	html += '<option value="100"' + (this.entriesPerPage == 100 ? ' selected="selected"' : '') + '>100</option>';
	html += '</select> ';
	html += '</div>';
	html += '<div class="slyTable-NavPanel">';
	html += 'Suche: ';
	html += '<input id="' + this.id + '-NavFilter" type="text" class="slyTable-NavFilter" onkeyup="if (event.keyCode == 16 || event.keyCode >= 37 && event.keyCode <= 40) return; sly.widgets[\'' + this.id + '\'].filter(this.value)" value="' + (this.filterString ? this.filterString : '') + '" />';
	html += '</div>';
	html += '</div>';
	html += '<div style="clear:both"></div>';
	html += '<div id="' + this.id + '-table"></div>';
	html += '<div style="float:left; width:300px"><select id="' + this.id + '-action" onchange="sly.widgets[\'' + this.id + '\'].doAction(this.value)"><option value="">Aktion auswählen...</option><option value="selectall">Alle Elemente selektieren</option><option value="deselectall">Auswahl aufheben</option><option value="unpublish">Selektierte Elemente archivieren</option><option value="publish">Selektierte Elemente publizieren</option><option value="delete">Selektierte Elemente löschen</option><option value="exportcsv">Selektierte Elemente exportieren (CSV)</option></select></div>';
	html += '<div style="text-align:right"><input type="button" value="Neues Element hinzufügen" onclick="createObject(\'' + this.id + '\')" /></div>';
	this.node.innerHTML = html;
	this.refreshTable();
	document.getElementById(this.id + '-NavFilter').focus();
};

slyTable.prototype.doAction = function(action) {
	if (action == 'selectall') {
		this.selectedObjectIds = Array();
		for(var i = 0; i < this.filteredData.length; i++) {
			this.selectedObjectIds.push(this.filteredData[i].id);
			var node = document.getElementById(this.id + '-checkbox-' + this.filteredData[i].id);
			if (node) {
				node.checked = true;
			}
		}
	}
	else if (action == 'deselectall') {
		for(var i = 0; i < this.selectedObjectIds.length; i++) {
			var node = document.getElementById(this.id + '-checkbox-' + this.selectedObjectIds[i]);
			if (node) node.checked = false;
		}
		this.selectedObjectIds = Array();
	}
	else if (action == 'exportcsv') {
		var s = '';
		for(var i = 0; i < this.selectedObjectIds.length; i++) {
			if (s != '') s += ',';
			s += this.selectedObjectIds[i];
		}
		window.open('data.csv?service=csv&entity=' + this.configuration.entity + '&ids=' + s,'_blank');
	}
	else {
		var _this = this;
		sly.call(action, {entity:this.configuration.entity, ids:this.selectedObjectIds}, function(data) {
			_this.updateNewsList();
			_this.loadData();
		});
	}
	document.getElementById(this.id + '-action').options[0].selected = true;
};

slyTable.prototype.refreshTable = function() {
		
	//This method call only makes sense, if the table div element exists that's created by calling refreshView()
	if (!document.getElementById(this.id + '-table')) return;
	
	//If no data is available
	if (!this.filteredData || this.filteredData.length == 0) {
		document.getElementById(this.id + '-table').innerHTML = 'Keine Elemente gefunden.<p />';
		return;
	}
	
	var html = '<table class="slyTable-Table" cellspacing="0" cellpadding="0">';
	
	//table headers
	html += '<tr>';
	html += '<th class="slyTable-TableHeader"></th>'; //empty table header for checkboxes in cells
	for(var propertyName in this.filteredData[0]) {
				
		/***************************************************************************************************
		   ADD HERE
		 ***************************************************************************************************/
		if (propertyName == 'id') continue;
		if (propertyName == 'text') continue;
		if (propertyName == 'url') continue;
		if (propertyName == 'data') continue;
		if (propertyName == 'street') continue;
		if (propertyName == 'addressAddon') continue;
		if (propertyName == 'zip') continue;
		if (propertyName == 'phone') continue;
		if (propertyName == 'fax') continue;
		if (propertyName == 'website') continue;
		if (propertyName == 'funcSpec') continue;
		if (propertyName == 'page') continue;
		if (propertyName == 'password') continue;
		/***************************************************************************************************/
		
		html += '<th class="slyTable-TableHeader' + (this.structure.properties[propertyName].isSortable ? ' slyTable-SortableTableHeader' : '') + '"' + (propertyName == 'published' ? ' style="text-align:center"' : '') + '>';
		var n = propertyName;
		
		/***************************************************************************************************
		   ADD HERE
		 ***************************************************************************************************/
		if (n == 'date') n = 'Datum';
		else if (n == 'title') n = 'Titel';
		else if (n == 'email') n = 'E-Mail';
		else if (n == 'published') n = 'publiziert';
		else if (n == 'id') n = 'ID';
		else if (n == 'name') n = 'Name';
		else if (n == 'company') n = 'Firma';
		else if (n == 'companyAddon') n = 'Firmenzusatz';
		else if (n == 'city') n = 'Ort';
		else if (n == 'region') n = 'Region';
		else if (n == 'size') n = 'Dateigrösse';
		else if (n == 'type') n = 'Typ';
		else if (n == 'area') n = 'Tätigkeitsbereich';
		else if (n == 'func') n = 'Funktion';
		else if (n == 'publishOrder') n = 'Reihenfolge';
		else if (n == 'picture') n = 'Bild';
		else if (n == 'token') n = 'Token';
		/***************************************************************************************************/
		
		html += '<a href="" onclick="sly.widgets[\'' + this.id + '\'].sort(\'' + propertyName + '\', true); return false">' + n + '</a>';
		if (propertyName == this.sortPropertyName) {
			if (this.sortOrder == 'ASC') { //show ascending icon link
				html += ' <a href="" class="sortArrowIcon sortArrowAscIcon" onclick="sly.widgets[\'' + this.id + '\'].sort(\'' + propertyName + '\', true); return false">ASC</a>';
			} else { //show descending icon link
				html += ' <a href="" class="sortArrowIcon sortArrowDescIcon" onclick="sly.widgets[\'' + this.id + '\'].sort(\'' + propertyName + '\', true); return false">DESC</a>';
			}
		}
		html += '</th>';
	}
	html += '</tr>';
	
		
	//Iterate over the data depending on the current page (this.page) and the entries per page (this.entriesPerPage)
	var rowCountIndex = 0;
	var startIndex = (this.page - 1) * parseInt(this.entriesPerPage);
	for(var rowIndex = startIndex; rowIndex < startIndex + parseInt(this.entriesPerPage); rowIndex++) {

		//If end of data is reached, break the for loop
		if (!this.filteredData[rowIndex]) break;
		
		//Insert a new HTML table row
		html += '<tr class="' + (rowCountIndex % 2 == 0 ? 'slyTable-OddRow' : 'slyTable-EvenRow') + '" ondblclick="showObject(' + rowCountIndex + ', \'' + this.id + '\')" onmouseover="this.className = \'slyTable-HoverRow\'" onmouseout="this.className = \'' + (rowCountIndex % 2 == 0 ? 'slyTable-OddRow' : 'slyTable-EvenRow') + '\'">';
		
		//Insert as first table cell a checkbox, which makes is possible to select/unselect the data record
		html += '<td>';
		html += '<input type="checkbox" id="' + this.id + '-checkbox-' + this.filteredData[rowIndex].id + '" onclick="sly.widgets[\'' + this.id + '\'].toggleObjectSelection(\'' + this.filteredData[rowIndex].id + '\')"';
		if (this.selectedObjectIds.indexOf(this.filteredData[rowIndex].id) > -1) html += ' checked="checked"';
		html += ' />';
		html += '</td>';
		
		//Iterate over each property of the object
		for(var propertyName in this.structure.properties) {
			
			/***************************************************************************************************
			   ADD HERE
			 ***************************************************************************************************/
			if (propertyName == 'id') continue;
			if (propertyName == 'text') continue;
			if (propertyName == 'url') continue;
			if (propertyName == 'data') continue;
			if (propertyName == 'street') continue;
			if (propertyName == 'addressAddon') continue;
			if (propertyName == 'zip') continue;
			if (propertyName == 'phone') continue;
			if (propertyName == 'fax') continue;
			if (propertyName == 'website') continue;
			if (propertyName == 'funcSpec') continue;
			if (propertyName == 'page') continue;
			if (propertyName == 'password') continue;
			/***************************************************************************************************/
			
			var propertyStructure = this.structure.properties[propertyName];

			//Determine the displayed value
			var val = '';
						
			//If the value is not set, display a hyphen
			if (!this.filteredData[rowIndex][propertyName]) val = '-';
			else val = this.filteredData[rowIndex][propertyName];
			
			if (propertyStructure.type == 'Date') {
				val = val.substring(8,10) + '.' + val.substring(5,7) + '.' + val.substring(0,4);
			} else if (propertyStructure.type == 'Bool') {
				if (val == 1) val = 'ja';
				else val = 'nein';
			}
			

			if (propertyName == 'picture') {
				val = '<img src="slycms.php?service=image&entity=' + this.configuration.entity + '&id=' + this.filteredData[rowIndex].id + '&width=50&salt=' + (new Date()).getTime() + '" />'
			}
			else {
				//Mark the matched filtered characteres
				var unmarkedVal = val;
				if (this.filterString != '') {
					val = (val + '').replace(new RegExp(this.filterString, 'i'), '<span class="slyTable-FilterMatch">$&</span>');
				}
			}
						
			html += '<td' + (propertyName == 'published' ? ' style="text-align:center"' : '') + '>';
			html += '<div id="' + this.id + '-' + propertyName + '-' + rowIndex + '">';
			if (propertyName == 'published') html += '<img src="res/scripts/slycms/images/icons/' + (val == 'ja' ? 'published.png' : 'archived.gif') + '" />';
			else {
				html += val;
				if (propertyName == 'size' && val) html += ' KB';
			}
			html += '</div>';
			html += '</td>';
		}
		html += '</tr>';
		rowCountIndex++;
	}	
	html += '</table>';
	document.getElementById(this.id + '-table').innerHTML = html;	
	this.refreshNavigationBar();
};

slyTable.prototype.refreshNavigationBar = function() {
	document.getElementById(this.id + '-navPage').options.length = 0;
	for(var i = 1; i <= Math.ceil(this.filteredData.length / this.entriesPerPage); i++) {
		var option = document.createElement('option');
		option.value = '' + i;
		if (i == this.page) option.selected = 'selected';
		option.appendChild(document.createTextNode('' + i));
		document.getElementById(this.id + '-navPage').appendChild(option);
	}
	var lastPage = Math.ceil(this.filteredData.length / this.entriesPerPage);
	if (this.page == 1) {
		document.getElementById(this.id + '-navFirstPage').className = 'iconButton iconButtonFirstPage iconButtonFirstPageDisabled';
		document.getElementById(this.id + '-navPrevPage').className = 'iconButton iconButtonPrevPage iconButtonPrevPageDisabled';
	} else {
		document.getElementById(this.id + '-navFirstPage').className = 'iconButton iconButtonFirstPage';
		document.getElementById(this.id + '-navPrevPage').className = 'iconButton iconButtonPrevPage';
	}
	if (this.page < lastPage) {
		document.getElementById(this.id + '-navNextPage').className = 'iconButton iconButtonNextPage';
		document.getElementById(this.id + '-navLastPage').className = 'iconButton iconButtonLastPage';
	} else {
		document.getElementById(this.id + '-navNextPage').className = 'iconButton iconButtonNextPage iconButtonNextPageDisabled';
		document.getElementById(this.id + '-navLastPage').className = 'iconButton iconButtonLastPage iconButtonLastPageDisabled';
	}
};

slyTable.prototype.setPage = function(page) {
	this.page = page;
	this.refreshTable();
};

slyTable.prototype.firstPage = function() {
	this.page = 1;
	this.refreshTable();
};

slyTable.prototype.prevPage = function() {
	if (this.page > 1) this.page--;
	this.refreshTable();
};

slyTable.prototype.nextPage = function() {
	if (this.page < Math.ceil(this.filteredData.length / this.entriesPerPage)) this.page++;
	this.refreshTable();
};

slyTable.prototype.lastPage = function() {
	this.page = Math.ceil(this.filteredData.length / this.entriesPerPage);
	this.refreshTable();
};

slyTable.prototype.setEntriesPerPage = function(entriesPerPage) {
	this.entriesPerPage = entriesPerPage;
	this.page = 1;
	this.refreshTable();
};

slyTable.prototype.filter = function(filterString) {
	if (filterString || filterString == '') this.filterString = filterString;
	this.page = 1;
	if (this.filterString == '') {
		this.filteredData = this.data;
	} else {
		this.filteredData = Array();
		for(var i = 0; i < this.data.length; i++) {
			var useRecord = false;
			for(var propertyName in this.structure.properties) {
				
				/***************************************************************************************************
				   ADD HERE
				 ***************************************************************************************************/
				if (propertyName == 'id') continue;
				if (propertyName == 'text') continue;
				if (propertyName == 'url') continue;
				if (propertyName == 'data') continue;
				if (propertyName == 'picture') continue;
				if (propertyName == 'street') continue;
				if (propertyName == 'addressAddon') continue;
				if (propertyName == 'zip') continue;
				if (propertyName == 'phone') continue;
				if (propertyName == 'fax') continue;
				if (propertyName == 'website') continue;
				if (propertyName == 'funcSpec') continue;
				if (propertyName == 'page') continue;
				if (propertyName == 'password') continue;
				/***************************************************************************************************/
				
				if (this.structure.properties[propertyName].foreignEntity) {
					try {
						if (this.data[i][propertyName][this.structure.properties[propertyName].foreignEntityNameProperty] &&
							this.data[i][propertyName][this.structure.properties[propertyName].foreignEntityNameProperty].match &&
							this.data[i][propertyName][this.structure.properties[propertyName].foreignEntityNameProperty].match(new RegExp(this.filterString, 'i'))) {
							useRecord = true;
							break;
						}
					} catch (err) {}
				}
				else if (this.data[i][propertyName]) {
					if ((this.data[i][propertyName] + '').match(new RegExp(this.filterString, 'i'))) {
						useRecord = true;
						break;
					}
				}
			}
			if (useRecord) this.filteredData.push(this.data[i]);
		}
	}
	this.refreshTable();
};

slyTable.prototype.sort = function(propertyName, toggle) {
	if (toggle) {
		if (this.sortPropertyName == propertyName && this.sortOrder == 'ASC') {
			this.sortOrder = 'DESC';
		} else this.sortOrder = 'ASC';
	}
	this.sortPropertyName = propertyName;
	this.page = 1;
	var __this = this;
	this.filteredData.sort(function(a, b) {
		if (!a[propertyName] && !b[propertyName]) return 0;
		if (!a[propertyName] && b[propertyName]) return 1;
		if (a[propertyName] && !b[propertyName]) return -1;
		
		if (__this.structure.properties[propertyName].foreignEntity) {
			//SORT: Foreign keys (Only Texts supported at the moment)
			if (__this.structure.properties[propertyName].foreignEntityNamePropertyType == 'Date') {
				//SORT: Foreign Entity Dates
				if (__this.sortOrder == 'ASC') return a[propertyName][__this.structure.properties[propertyName].foreignEntityNameProperty] - b[propertyName][__this.structure.properties[propertyName].foreignEntityNameProperty];
				else return b[propertyName][__this.structure.properties[propertyName].foreignEntityNameProperty] - a[propertyName][__this.structure.properties[propertyName].foreignEntityNameProperty];
			}
			var propA = a[propertyName][__this.structure.properties[propertyName].foreignEntityNameProperty];
			var propB = b[propertyName][__this.structure.properties[propertyName].foreignEntityNameProperty];
			for(var c = 0; c < Math.min(propA.length, propB.length); c++) {
				if (propA.toLowerCase().charCodeAt(c) == propB.toLowerCase().charCodeAt(c)) continue;
				if (__this.sortOrder == 'ASC') return propA.toLowerCase().charCodeAt(c) - propB.toLowerCase().charCodeAt(c);
				else return propB.toLowerCase().charCodeAt(c) - propA.toLowerCase().charCodeAt(c);
			}
			if (propA.length == propB.length) return 0;
			else if (propA.length < propB.length) return (__this.sortOrder == 'ASC' ? -1 : 1);
			else return (__this.sortOrder == 'ASC' ? 1 : -1);	                    
		}
		else if (__this.structure.properties[propertyName].type == 'Number' || __this.structure.properties[propertyName].type == 'Date') {
			//SORT: Numbers
			if (__this.sortOrder == 'ASC') return a[propertyName] - b[propertyName];
			else return b[propertyName] - a[propertyName];
		}
		else {
			//SORT: Texts
			for(var c = 0; c < Math.min(a[propertyName].length, b[propertyName].length); c++) {
				if (a[propertyName].toLowerCase().charCodeAt(c) == b[propertyName].toLowerCase().charCodeAt(c)) continue;
				if (__this.sortOrder == 'ASC') return a[propertyName].toLowerCase().charCodeAt(c) - b[propertyName].toLowerCase().charCodeAt(c);
				else return b[propertyName].toLowerCase().charCodeAt(c) - a[propertyName].toLowerCase().charCodeAt(c);
			}
			if (a[propertyName].length == b[propertyName].length) return 0;
			else if (a[propertyName].length < b[propertyName].length) return (__this.sortOrder == 'ASC' ? -1 : 1);
			else return (__this.sortOrder == 'ASC' ? 1 : -1);
		}
	});
	this.refreshTable();
};

slyTable.prototype.selectObject = function(objectId) {
	if (this.selectedObjectIds.indexOf(objectId) == -1) this.selectedObjectIds.push(objectId);
};

slyTable.prototype.deselectObject = function(objectId) {
	var pos = this.selectedObjectIds.indexOf(objectId); 
	if (pos > -1) this.selectedObjectIds.splice(pos, 1);
};

slyTable.prototype.toggleObjectSelection = function(objectId) {
	if (this.selectedObjectIds.indexOf(objectId) == -1) {
		this.selectObject(objectId);
	} else {
		this.deselectObject(objectId);
	}
};



slyTable.prototype.updateNewsList = function() {
	
	/***************************************************************************************************
	   ADD HERE
	 ***************************************************************************************************/
	if (this.configuration.entity == 'news') {
		if (document.getElementById('newslist')) {
			sly.call('find', {entity:'news', filterRule:'published = 1', orderRule:'date DESC'}, function(data) {
				var html = '';
				for(var i = 0; i < data.res.length; i++) {
					var val = data.res[i].date;
					var dateStr = val.substring(8,10) + '.' + val.substring(5,7) + '.' + val.substring(0,4);
					html += '<li class="news-item" onclick="">';
					html += '<a href=""><img class="news-image" src="slycms.php?service=image&width=110&id=' + data.res[i].id + '&salt=' + (new Date()).getTime() + '" /></a>';
					html += '<div class="news-date">' + dateStr + '</div>';
					html += '<div class="news-title">' + data.res[i].title + '</div>';
					html += '</li>';
				}
			    document.getElementById('newslist').innerHTML = html;
			});
		}
	}
	else if (this.configuration.entity == 'links') {
		if (document.getElementById('linklist')) {
			sly.call('find', {entity:'links', filterRule:'published = 1 AND page LIKE \'%' + location.href.substring(location.href.lastIndexOf('/') + 1) + '\'', orderRule:'publishOrder, name'}, function(data) {
				var html = '';
				for(var i = 0; i < data.res.length; i++) {
					html += '<li class="link-item">';
					html += '<a href="' + data.res[i].url + '" target="_blank">';
					html += '<img class="link-image" src="res/images/link.png" />';
					html += '<div class="link-title">' + data.res[i].name + '</div>';
					html += '</a>';
					html += '</li>';
				}
				document.getElementById('linklist').innerHTML = html;
			});
		}
	}
	else if (this.configuration.entity == 'headerimages') {
		
	}
	else if (this.configuration.entity == 'users') {
	
	}
	else if (this.configuration.entity == 'members') {
		
	}
	else if (this.configuration.entity == 'organisation') {
		
	}
	else if (this.configuration.entity == 'files') {
		sly.call('find', {entity:'files', filterRule:'published = 1 AND page LIKE \'%' + location.href.substring(location.href.lastIndexOf('/') + 1) + '\'', orderRule:'publishOrder ASC, date DESC'}, function(data) {
			var html = '';
			for(var i = 0; i < data.res.length; i++) {
				var val = data.res[i].date;
				var dateStr = val.substring(8,10) + '.' + val.substring(5,7) + '.' + val.substring(0,4);
				html += '<li class="file-item">';
				html += '<a href="' + (data.res[i].url ? data.res[i].url : 'files/' + data.res[i].id + '-' + data.res[i].name + '.' + data.res[i].type + '?salt=' + (new Date()).getTime()) + '" target="_blank">';
	            html += '<img class="file-image" src="res/images/' + (data.res[i].type == 'pdf' ? 'pdf.gif' : data.res[i].type + '.png') + '" />';
	            html += '<div class="file-date">' + dateStr + '</div>';
				html += '<div class="file-title">' + data.res[i].name + '</div>';
				html += '<div class="file-size"> (' + data.res[i].size + ' KB)</div>';
				html += '</a>';
				html += '</li>';
			}
			document.getElementById('filelist').innerHTML = html;
		});
	}
	/***************************************************************************************************/

};

function showObject(rowIndex, id) { 
	
	var _this = sly.widgets[id];
	document.getElementById(id).style.display = 'none';
	_this.form.configuration.rowIndex = (_this.entriesPerPage * (_this.page - 1)) + rowIndex;
	_this.form.configuration.action = 'update';
	document.getElementById(_this.form.id + '-navDiv').style.display = 'block';
	_this.form.refresh();
	_this.formNode.style.display = 'block';
	document.getElementById(_this.form.id).style.display = 'block';
	if (sly.dialog) sly.dialog.arrangeElements();
	
	/***************************************************************************************************
	   ADD HERE
	 ***************************************************************************************************/
	if (_this.configuration.entity == 'news') document.getElementById('slyNewsForm_title').focus();
	else if (_this.configuration.entity == 'links') document.getElementById('slyNewsForm_name').focus();
	else if (_this.configuration.entity == 'members') document.getElementById('slyNewsForm_company').focus();
	else if (_this.configuration.entity == 'users') document.getElementById('slyNewsForm_name').focus();
	else if (_this.configuration.entity == 'organisation') document.getElementById('slyNewsForm_name').focus();
	else if (_this.configuration.entity == 'files') {
		if (document.getElementById('slyNewsForm_url').value != '')	{
			document.getElementById('slyNewsForm_remote').checked = true;
			document.getElementById('slyNewsForm_localFile').style.display = 'none';
			document.getElementById('slyNewsForm_remoteFile').style.display = 'block';
		} else {
			document.getElementById('slyNewsForm_local').checked = true;
			document.getElementById('slyNewsForm_localFile').style.display = 'block';
			document.getElementById('slyNewsForm_remoteFile').style.display = 'none';
		}
		document.getElementById('slyNewsForm_name').focus();
	}
	/***************************************************************************************************/
	
}

function createObject(id) {
	var _this = sly.widgets[id];
	document.getElementById(_this.id).style.display = 'none';
	_this.formNode.style.display = 'block';
	_this.form.configuration.action = 'create';
	document.getElementById(_this.form.id + '-navDiv').style.display = 'none';
	
	/***************************************************************************************************
	   ADD HERE
	 ***************************************************************************************************/
	if (_this.configuration.entity == 'news') {
		var d = new Date();
		document.getElementById('slyNewsForm_date').value = (d.getDate() < 10 ? '0' + d.getDate() : d.getDate()) + '.' + ((d.getMonth() + 1) < 10 ? '0' + (d.getMonth() + 1) : (d.getMonth() + 1)) + '.' + d.getFullYear();
		document.getElementById('slyNewsForm_title').value = '';
		document.getElementById('slyNewsForm_published').checked = true;
		tinyMCE.activeEditor.setContent('');
		document.getElementById('slyNewsForm_picture').src = 'res/scripts/slycms/images/empty.png';
	}
	else if (_this.configuration.entity == 'links') {
		document.getElementById('slyNewsForm_name').value = '';
		document.getElementById('slyNewsForm_url').value = '';
		document.getElementById('slyNewsForm_published').checked = true;
	}
	else if (_this.configuration.entity == 'headerimages') {
		document.getElementById('slyNewsForm_picture').src = 'res/scripts/slycms/images/empty.png';
		document.getElementById('slyNewsForm_published').checked = true;
	}
	else if (_this.configuration.entity == 'files') {
		document.getElementById('slyNewsForm_name').value = '';
		document.getElementById('slyNewsForm_data').innerHTML = '';
		document.getElementById('slyNewsForm_url').value = '';
		document.getElementById('slyNewsForm_local').checked = true;
		document.getElementById('slyNewsForm_localFile').style.display = 'block';
		document.getElementById('slyNewsForm_remoteFile').style.display = 'none';
		document.getElementById('slyNewsForm_published').checked = true;
	}
	else if (_this.configuration.entity == 'members') {
		document.getElementById('slyNewsForm_company').value = '';
		document.getElementById('slyNewsForm_companyAddon').value = '';
		document.getElementById('slyNewsForm_street').value = '';
		document.getElementById('slyNewsForm_addressAddon').value = '';
		document.getElementById('slyNewsForm_zip').value = '';
		document.getElementById('slyNewsForm_city').value = '';
		document.getElementById('slyNewsForm_region').value = '';
		document.getElementById('slyNewsForm_phone').value = '';
		document.getElementById('slyNewsForm_fax').value = '';
		document.getElementById('slyNewsForm_website').value = '';
		document.getElementById('slyNewsForm_published').checked = true;
		document.getElementById('slyNewsForm_picture').src = 'res/scripts/slycms/images/empty.png';
	}
	else if (_this.configuration.entity == 'organisation') {
		document.getElementById('slyNewsForm_name').value = '';
		document.getElementById('slyNewsForm_area').value = '';
		document.getElementById('slyNewsForm_func').value = '';
		tinyMCE.activeEditor.setContent('');
		document.getElementById('slyNewsForm_published').checked = true;
		document.getElementById('slyNewsForm_picture').src = 'res/scripts/slycms/images/empty.png';
		document.getElementById('slyNewsForm_publishOrder').value = '';
	}
	else if (_this.configuration.entity == 'users') {
		document.getElementById('slyNewsForm_name').value = '';
		document.getElementById('slyNewsForm_token').value = '';
		document.getElementById('slyNewsForm_email').value = '';
		document.getElementById('slyNewsForm_password').value = '';
	}
	/***************************************************************************************************/
	
	
	if (sly.dialog) sly.dialog.arrangeElements();
	
	
	/***************************************************************************************************
	   ADD HERE
	 ***************************************************************************************************/
	if (_this.configuration.entity == 'news') document.getElementById('slyNewsForm_title').focus();
	else if (_this.configuration.entity == 'links') document.getElementById('slyNewsForm_name').focus();
	else if (_this.configuration.entity == 'members') document.getElementById('slyNewsForm_company').focus();
	else if (_this.configuration.entity == 'organisation') document.getElementById('slyNewsForm_name').focus();
	else if (_this.configuration.entity == 'files') document.getElementById('slyNewsForm_name').focus();
	else if (_this.configuration.entity == 'users') document.getElementById('slyNewsForm_name').focus();
	/***************************************************************************************************/
}


