function startImgLib() {
	if(typeof(imgLib) == 'undefined') {
		imgLib = new sitePEWin({resizable: true, minimize: true, setontop: false, enlarge: true});
		imgLib.setWinIcon(pic_path+"imgico.png");
		imgLib.setOnTop();
		imgLib.setHeight(400);
		imgLib.setWidth(800);
		imgLib.setTitle('Image Library');
		if(typeof(uploadFrame) == 'undefined') {
			uploadFrame = document.createElement('iframe');
			uploadFrame.style.display = 'none';
			uploadFrame.name = 'uploadFrame';
			bodyEle.appendChild(uploadFrame);
		}
		
		winLayout = '';
		
		winLayout += '<div class="imgLibUploadField">';
		winLayout += '<form id="imgUploadForm" target="uploadFrame" method="post" action="'+pre_path+'ajax.php?mode=sd_imglib&path='+imgLibPath+'" enctype="multipart/form-data" onsubmit="updloadFile();">';
		winLayout += 'Datei hochladen: <br /><input type="file" name="imgUpload" id="imgUpload" />';
		winLayout += '<input type="submit" Value="Hochladen" name="doImgUpload" id="doImgUpload" />';
		winLayout += '</form>';
		winLayout += '<div style="float: left; margin: 2px; cursor: pointer;" onmouseover="this.className=\'hover\';" onmouseout="this.className=\'\';" onclick="library.createFolder();"><img src="'+pic_path+'folder.png" style="width: 16px; height: 16px;" alt="" />Ordner erstellen</div>';
		winLayout += '<div id="librarydelbutton" style="visibility:hidden; float: left; margin: 2px; cursor: pointer;" onmouseover="this.className=\'hover\';" onmouseout="this.className=\'\';" onclick="library.deleteMarkedFile();"><img src="'+pic_path+'delete.png" style="width: 16px; height: 16px;" alt="" />Datei L&ouml;schen</div>';
		winLayout += '<br style="clear: both;" />';
		winLayout += '</div>';

		winLayout += '<div class="imgLibBrowser" id="imgLibBrowser">';
		winLayout += '</div>';
		imgLib.setContent(winLayout);
		if(typeof(library) == 'undefined') {
			library = new fileLibraryObject(imageLibrary);
		}
		
		library.loadLibrary();
	}

	imgLib.onResize = false;
	imgLib.openWin();
	imgLib.toTop();
}

function startFileLib() {
	if(typeof(fileLib) == 'undefined') {
		fileLib = new sitePEWin({resizable: true, minimize: true, setontop: false, enlarge: true});
		fileLib.setWinIcon(pic_path+"fileico.png");
		fileLib.setOnTop();

		fileLib.setHeight(400);
		fileLib.setWidth(800);
		fileLib.setTitle('File Library');
		
		winLayout = '';
		
		winLayout += '<div class="imgLibUploadField">';
		winLayout += '<form id="imgUploadForm" target="uploadFrame" method="post" action="'+pre_path+'ajax.php?mode=sd_filelib&path='+imgLibPath+'" enctype="multipart/form-data" onsubmit="updloadFile();">';
		winLayout += 'Datei hochladen: <br /><input type="file" name="fileUpload" id="fileUpload" />';
		winLayout += '<input type="submit" Value="Hochladen" name="doFileUpload" id="doFileUpload" />';
		winLayout += '</form>';
		winLayout += '<div style="float: left; margin: 2px; cursor: pointer;" onmouseover="this.className=\'hover\';" onmouseout="this.className=\'\';" onclick="library2.createFolder();"><img src="'+pic_path+'folder.png" style="width: 16px; height: 16px;" alt="" />Ordner erstellen</div>';
		winLayout += '<div id="library2delbutton" style="visibility:hidden; float: left; margin: 2px; cursor: pointer;" onmouseover="this.className=\'hover\';" onmouseout="this.className=\'\';" onclick="library2.deleteMarkedFile();"><img src="'+pic_path+'delete.png" style="width: 16px; height: 16px;" alt="" />Datei L&ouml;schen</div>';
		winLayout += '<br style="clear: both;" />';
		winLayout += '</div>';

		winLayout += '<div class="imgLibBrowser" id="fileLibBrowser">';
		winLayout += '</div>';

		if(typeof(uploadFrame) == 'undefined') {
			uploadFrame = document.createElement('iframe');
			uploadFrame.style.display = 'none';
			uploadFrame.name = 'uploadFrame';
			bodyEle.appendChild(uploadFrame);
		}
		fileLib.setContent(winLayout);
		if(typeof(library2) == 'undefined') {
			library2 = new fileLibraryObject(fileLibrary);
		}
		
		library2.loadLibrary();
	}

	fileLib.onResize = false;
	fileLib.openWin();
	fileLib.toTop();
}

function updloadFile() {
	winInfo = '<img src="'+pic_path+'upload_anim.gif" class="waitAnim" alt="" />';
	if(document.getElementById('imgUpload') && document.getElementById('imgUpload').value != '') {
		imgLib.closeWin();
		library.fileName = document.getElementById('imgUpload').value.replace(/(.*)\\/,''); // Should save Name only ...
	} else if(document.getElementById('fileUpload')) {
		fileLib.closeWin();
		library2.fileName = document.getElementById('fileUpload').value.replace(/(.*)\\/,''); // Should save Name only ...
	}

	setErrorWin(winInfo);
	errorWin.setTitle('Uploading');
	return true;
}

function setPicURL() {
	imgLib.closeWin();
	document.getElementById('picbox').value = root_path + imageLibrary + '/'+imgLibPath+library.fileName.toLowerCase();
	library.setFileName('');
	errorWin.closeWin();
	
	library.loadLibrary();
}

function setFileURL() {
	fileLib.closeWin();
	document.getElementById('filebox').value = root_path + fileLibrary + '/'+imgLibPath+library2.fileName.toLowerCase();
	library2.setFileName('');
	errorWin.closeWin();
	
	library2.loadLibrary();
}

function fileLibraryObject(lib_path) {
	var self = this;
	this.images;
	this.directories;
	this.lib_path = lib_path;
	this.printWin;

	this.fileName;

	this.delSecWin = new sitePEWin({resizable: false, setontop: false, minimize: false, enlarge: false});
	this.delSecWin.setTitle('');
	this.delSecWin.setWidth(250);
	this.delSecWin.setHeight(150);
	this.delSecWin.closeWin();
	this.createFolderWin = new sitePEWin({resizable: false, setontop: false, minimize: false, enlarge: false});
	this.createFolderWin.setTitle('Ordner erstellen');
	this.createFolderWin.setHeight(100);
	this.createFolderWin.closeWin();

	this.createFolder = function(){
		content = '<div style="margin: 5px; text-align: left">Name des neuen Ordners:';
		content += '<input type="text" id="'+(self.lib_path == imageLibrary ? 'library' : 'library2')+'cFolderName" value="" style="width: 100%" />';
		content += '<input type="button" value="Ordner erstellen" style="float: right" onclick="'+(self.lib_path == imageLibrary ? 'library' : 'library2')+'.cFolder();" /><br style="clear:both" />';
		content += '</div>';
		
		self.createFolderWin.setContent(content);
		self.createFolderWin.openWin();
		self.createFolderWin.toTop();
	}
	
	this.cFolder = function() {
		newFolderName = document.getElementById((self.lib_path == imageLibrary ? 'library' : 'library2')+'cFolderName').value;
		self.createFolderWin.closeWin();
		self.loadLibrary('&createFolder='+newFolderName);
	}

	this.deleteMarkedFile = function() {
		if(typeof(self.fileName) == 'undefined' || self.fileName == '')
			return false;
		
		content = '<div style="margin: 5px; text-align: center">Soll diese Datei wirklich gel&ouml;scht werden?<br /><br />';
		content += '<input type="button" style="float: left" value="L&ouml;schen" onclick="'+(self.lib_path == imageLibrary ? 'library' : 'library2')+'.delFile();" />';
		content += '<input type="button" style="float: right" value="Abbrechen" onclick="'+(self.lib_path == imageLibrary ? 'library' : 'library2')+'.delSecWin.closeWin();" /><br style="clear:both" /></div>';
		
		self.delSecWin.setContent(content);
		self.delSecWin.openWin();
		self.delSecWin.toTop();
	}
	
	this.delFile = function() {
		self.delSecWin.closeWin();
		self.loadLibrary('&deleteFile='+self.fileName);
	}

	this.parseImages = function(response){
		self.directories	= response.responseXML.getElementsByTagName('dir');
		self.images		= response.responseXML.getElementsByTagName('img');
		
		self.printfileLibrary();
	}
	
	this.setFileName = function(fN) {
		self.fileName = fN;
		if(fN == '') {
			if(self.lib_path == imageLibrary)
				if(document.getElementById('librarydelbutton'))
					document.getElementById('librarydelbutton').style.visibility = 'hidden';
			else
				if(document.getElementById('library2delbutton'))
					document.getElementById('library2delbutton').style.visibility = 'hidden';
		} else {
			if(self.lib_path == imageLibrary)
				document.getElementById('librarydelbutton').style.visibility = '';
			else
				document.getElementById('library2delbutton').style.visibility = '';
		}
	}
	
	this.printfileLibrary = function() {
		if(lib_path == imageLibrary)
			imgLib.onResize = self.printfileLibrary;
		else
			fileLib.onResize = self.printfileLibrary;
	
		if(typeof(self.printWin)=='undefined') {
			if(lib_path == imageLibrary)
				self.printWin = document.getElementById('imgLibBrowser');
			else
				self.printWin = document.getElementById('fileLibBrowser');
		}
		availableWidth = self.printWin.offsetWidth;
		
		colNum = Math.floor(availableWidth/250);
		
		m = 0;
		
		libcontent = '<table cellpadding="0" cellspacing="0">';
		c = 0;
		if(imgLibPath != '') {
			dblclick = ' ondblclick="'+(self.lib_path == imageLibrary ? 'library' : 'library2')+'.folderUp();"';
			libcontent += '<td '+dblclick+' onclick="'+(self.lib_path == imageLibrary ? 'library' : 'library2')+'.setFileName(\'\'); '+(self.lib_path == imageLibrary ? 'library' : 'library2')+'.marker('+m+')" ';
			libcontent +='	onmouseover="'+(self.lib_path == imageLibrary ? 'library' : 'library2')+'.marker('+m+',1)" onmouseout="'+(self.lib_path == imageLibrary ? 'library' : 'library2')+'.marker('+m+',2)"id="sym'+m+'" style="width: 50px; height: 50px;">';
			libcontent += '<img style="width: 50px; height: 50px;" src="'+pic_path+'folder.png" alt="" /></td><td '+dblclick+' onclick="'+(self.lib_path == imageLibrary ? 'library' : 'library2')+'.setFileName(\'\'); '+(self.lib_path == imageLibrary ? 'library' : 'library2')+'.marker('+m+')"';
			libcontent +='	onmouseover="'+(self.lib_path == imageLibrary ? 'library' : 'library2')+'.marker('+m+',1)" onmouseout="'+(self.lib_path == imageLibrary ? 'library' : 'library2')+'.marker('+m+',2)" id="nam'+m+'" style="width: 200px;">..</td>';
			c++;
			m++;
		}
		
		for(d = 0; d < self.directories.length; d++) {
			if(c == colNum) {
				libcontent += '</tr><tr>';
				c = 0;
			}
			folder = getNodeValue(self.directories[d],'name');

			dblclick = ' ondblclick="'+(self.lib_path == imageLibrary ? 'library' : 'library2')+'.appendFolder(\''+folder+'\');"';

			libcontent += '<td '+dblclick+' onclick="'+(self.lib_path == imageLibrary ? 'library' : 'library2')+'.setFileName(\''+folder+'\'); '+(self.lib_path == imageLibrary ? 'library' : 'library2')+'.marker('+m+')" onmouseover="'+(self.lib_path == imageLibrary ? 'library' : 'library2')+'.marker('+m+',1)" onmouseout="'+(self.lib_path == imageLibrary ? 'library' : 'library2')+'.marker('+m+',2)" id="sym'+m+'" style="width: 50px; height: 50px;"><img style="width: 50px; height: 50px;" src="'+pic_path+'folder.png" alt="" /></td>';
			libcontent += '<td '+dblclick+' onclick="'+(self.lib_path == imageLibrary ? 'library' : 'library2')+'.setFileName(\''+folder+'\'); '+(self.lib_path == imageLibrary ? 'library' : 'library2')+'.marker('+m+')" onmouseover="'+(self.lib_path == imageLibrary ? 'library' : 'library2')+'.marker('+m+',1)" onmouseout="'+(self.lib_path == imageLibrary ? 'library' : 'library2')+'.marker('+m+',2)" id="nam'+m+'" style="width: 200px;">'+folder+'</td>';
			c++;
			m++;
		}
		
		for(i=0; i < self.images.length; i++) {
			if(c == colNum) {
				libcontent += '</tr><tr>';
				c = 0;
			}
			
			time = new Date(getNodeValue(self.images[i],'date')*1000);
			
			if(self.lib_path == imageLibrary) {
				picture = pre_path + imageLibrary + '/'+imgLibPath + getNodeValue(self.images[i],'name');
				filename = getNodeValue(self.images[i],'name');
				dblclick = ' ondblclick="setPicURL();"';
				
				imgHeight = getNodeValue(self.images[i],'height')*1;
				imgWidth = getNodeValue(self.images[i],'width')*1;
				
				factor1 = imgHeight / imgWidth;
				factor2 = imgWidth / imgHeight;
				addInfo = '<br /><span>'+imgWidth+' x '+imgHeight+' px</span>';
				
				if(imgHeight > 75 || imgWidth > 75) {
					if(imgHeight > imgWidth) {
						showHeight = 75;
						showWidth = 75*factor2;
					} else {
						showHeight = 75*factor1;
						showWidth = 75;
					}
				} else {
					showHeight = imgHeight;
					showWidth = imgWidth;
				}
			} else {
				picture = pic_path + 'files.png';
				filename = getNodeValue(self.images[i],'name');
				dblclick = ' ondblclick="setFileURL();"';
				showHeight = 48;
				showWidth = 48;
				addInfo = '';
			}
			
			filesize = getNodeValue(self.images[i],'size')*1;
			sizeName = 'B';
			if(filesize > 1024) {
				filesize /= 1024;
				sizeName = 'KB';
				if(filesize > 1024) {
					filesize /= 1024;
					sizeName = 'MB';
				}
				
				filesize *= 100;
				filesize = Math.round(filesize);
				filesize /= 100;
			}
			
			libcontent += '<td '+dblclick+' onclick="'+(self.lib_path == imageLibrary ? 'library' : 'library2')+'.setFileName(\''+filename+'\'); '+(self.lib_path == imageLibrary ? 'library' : 'library2')+'.marker('+m+')" onmouseover="'+(self.lib_path == imageLibrary ? 'library' : 'library2')+'.marker('+m+',1)" onmouseout="'+(self.lib_path == imageLibrary ? 'library' : 'library2')+'.marker('+m+',2)" id="sym'+m+'" style="width: 75px; height: 100px; text-align: center; vertical-align: middle;"><img src="'+picture+'" style="width: '+showWidth+'px; height: '+showHeight+'px;" alt="" /></td>';
			libcontent += '<td '+dblclick+' onclick="'+(self.lib_path == imageLibrary ? 'library' : 'library2')+'.setFileName(\''+filename+'\'); '+(self.lib_path == imageLibrary ? 'library' : 'library2')+'.marker('+m+')" onmouseover="'+(self.lib_path == imageLibrary ? 'library' : 'library2')+'.marker('+m+',1)" onmouseout="'+(self.lib_path == imageLibrary ? 'library' : 'library2')+'.marker('+m+',2)" id="nam'+m+'" style="width: 200px;">'+filename;
			libcontent += '<br /><span>'+filesize+' '+sizeName+'</span>';
			libcontent += '<br /><span>'+time.getDate()+'.'+(time.getMonth()+1)+'.'+time.getFullYear().toString().substring(2)+' '+time.getHours()+':'+time.getMinutes()+':'+time.getSeconds()+'</span>';
			libcontent += addInfo;
			libcontent += '</td>';
			c++;
			m++;
		}
		
		if(c < colNum) {
			for(i = c; i < colNum; i++) {
				libcontent += '<td style="width: 50px;"></td><td style="width: 200px;"></td>';
			}
		}
		
		libcontent += '</table>';
		
		self.printWin.innerHTML = libcontent;
	}
	
	this.loadLibrary = function(addFileOptions){
		if(typeof(addFileOptions) == 'undefined')
			addFileOptions = '';

		self.setFileName('');

		if(self.lib_path == imageLibrary) {
			self.ajax			= new AJAXObj('mode=rd_imglib&path='+imgLibPath+addFileOptions,self.parseImages);
			self.ajax.doRequest();
			
			document.getElementById('imgUploadForm').action = pre_path+'ajax.php?mode=sd_imglib&path='+imgLibPath;
		} else {
			self.ajax			= new AJAXObj('mode=rd_filelib&path='+imgLibPath+addFileOptions,self.parseImages);
			self.ajax.doRequest();
			
			document.getElementById('imgUploadForm').action = pre_path+'ajax.php?mode=sd_filelib&path='+imgLibPath;
		}
	}
	
	this.appendFolder = function(folder) {
		imgLibPath += folder+'/';
		self.setFileName('');
		self.loadLibrary();
	}

	this.folderUp = function() {
		imgLibPath = imgLibPath.replace(/[^\/]+?\/$/,'');
		self.setFileName('');
		self.loadLibrary();
	}

	this.marker = function(m,mouseAction) {
		if(typeof(mouseAction) == 'undefined') {
			tablecells = self.printWin.getElementsByTagName('td');
			for(t = 0; t < tablecells.length; t++)
				tablecells[t].className = '';
				
			document.getElementById('sym'+m).className = 'marked';
			document.getElementById('nam'+m).className = 'marked';
		} else {
			if(mouseAction == 1) {
				document.getElementById('sym'+m).className += ' hover';
				document.getElementById('nam'+m).className += ' hover';
			} else {
				document.getElementById('sym'+m).className = document.getElementById('sym'+m).className.replace(/hover/g,'');
				document.getElementById('nam'+m).className = document.getElementById('nam'+m).className.replace(/hover/g,'');
			}
		}
	}
}