/**
 * Klasse ImageUploader
 */

Images = function (type, formId, lightboxId, previewId, infoId, imageListId, addButtonId, iFrameId, progressImageSrc, modalId, inputId, popupId)
{

	this.type = type;
	this.form = document.getElementById(formId);
	this.info = infoId;
	this.input = document.getElementById(inputId);
	this.addButton = document.getElementById(addButtonId);
	this.loadImage = progressImageSrc;	
	this.imagesList = document.getElementById(imageListId);
	this.iFrameId = iFrameId;
	this.iFrame = document.getElementById(iFrameId);
	this.modal = document.getElementById(modalId);
	if(document.getElementById(lightboxId)){this.lightbox = document.getElementById(lightboxId);} 
	this.popupId = popupId;
	
	// Private members
	this.url = global.MEDIA_URL + "preview/";
	this.path = global.MEDIA_PATH + "preview/";
	this.removedImages = new Array();
	this.image_width = 100;
	this.orgWidth;
	this.orgHeight;
	this.currentPreview;
	this.currentWidth;
	this.currentHeight;
	this.laterCrop = false;	//ob ein bereits hochgelade Bild gecrpt wird oder ein previwBil

	// Merke das Defaultbild, wenn kein Bild ausgewählt ist
//	this.previewDefault = this.preview.src;
//	this.info.innerHTML = "Choose a picture with the file dialog above and add it to the upload list";
}



Images.prototype.startUpload = function (color){
	hideDiv('save-button2');
	this.laterCrop = false; //laterCrop immer auf false setzen, da davon ausgegangen werden muss dass nach crop ein bild hochgeladen wird. Heiko
	// Prüfen, ob das IFrame schon existiert. Das ist erst der Fall, 
	//	wenn die Seite komplett geladen wurde; und dies ist beim ersten Aufruf von afterUpload nicht gewährleistet.
//	if (! this.iFrame) {
//		this.init();
//	}
/*	
	this.addButton.disabled = true;
	this.preview.style.border = "0px";
	this.preview.style.width = "90px";
	this.preview.style.height = "90px";
	this.preview.src = this.loadImage;
	this.preview.style.display = "block";
*/	
	var imgObj = this;
	console.log("startUpload::::::::");
	if(this.imagesList.id.indexOf("InAddWizard") > -1)
	{
		this.imagesList.innerHTML = '<img src="'+global.IMG_URL_NONTEXT+'teaser/progress-blue.gif" alt="onload" />';
		//info ( 'progress: ' + global.IMG_URL_NONTEXT + 'teaser/progress-' + color + '.gif');
		/*Heiko 2009-11-03
		Setze das onclick Event bei dem save-button standardmäßig auf UploadTemporarySave, da die Möglichkeit sein soll nach dem cropen ein weiteres Bild hochzuladen
		*/
		if(document.getElementById("save-button2")) //Image Upload hat im Add Wizard keinen Save-Button
		{
			switch(imgObj.type)
			{
				case "ef":
					document.getElementById("save-button2").onclick = function ()
					{
						imgObj.uploadTemporySave(imgObj.type, get.ef_id);
					}
					break;
				case "us":
					document.getElementById("save-button2").onclick = function ()
					{
						imgObj.uploadTemporySave(imgObj.type, global.user_id);
					}
					break;
				case "ent":
					//todo
					break;
			}
		}
		
	}
	this.form.submit();
	console.log(" start upload ende ::::::::::::");
}



/**
 *	Ein Bild in die Bilder-Liste im Hochlade-Dialog hinzufügen
 *@param crop soll das crop tool angezeigt werden --> true/false
 */
Images.prototype.uploadFormAddToList = function (){
	console.log("uploadFormAddToList:::::::::::::");
	if(this.imagesList.id.indexOf("InAddWizard")>-1){	
		
		if(this.orgWidth<(global.CROP_QUADRAT_DIM_02+5) || this.orgHeight<(global.CROP_QUADRAT_DIM_02+5)){
			alert(Languages.IMAGE_UPLOAD_THE_IMAGES_HAS_TO_BE_LARGER);
			this.imagesList.innerHTML = '';
			return;
		}
		
		// if(preg_match("/\." . global.UPLOAD_ALLOWED_TYPES . "$/i", $files["name"])){
			// alert(Languages.IMAGE_UPLOAD_INCORRECT_FILE_EXTENSION);
			// this.imagesList.innerHTML = '';
			// return;
		// }
		
		if( ( this.orgWidth > global.MAX_IMAGE_SIZE ) || ( this.orgHeight > global.MAX_IMAGE_SIZE ) ){
			alert(Languages.IMAGE_UPLOAD_THE_IMAGES_HAS_TO_BE_SMALLER);
			this.imagesList.innerHTML = '';
			document.getElementById("upload").value="";
			return;
		}else{
			if(getDiv("searchDiv")){hideDiv("searchDiv");}
			if(getDiv("upload")){document.getElementById("upload").value="";}
		}
	
		var newDIV = document.createElement("div");
		newDIV.id = this.currentPreview;
		newDIV.style.float= "left";
		newDIV.setAttribute("float", "left");
	
		var newIMG = document.createElement("img");
		newIMG.id = "imageToCrop";
		
		var newBR = document.createElement("br");

		newIMG.setAttribute("src", this.url + this.currentPreview );
//console.log("::::::::::::::::::::this.orgHeight::"+this.orgHeight);		
		var x = this.orgWidth;
		var y = parseInt(this.orgHeight); // damit y+20 als Zahlen behandelt wurde, falls y<=250
		if(global.addearthfaveoverlay) //Abfrage ob man sich im Add Wizard befindet.
		{
			if( y > 250 )
			{
				global.skalierung = 250/ this.orgHeight;
				y = 250;
				x = Math.round(this.orgWidth * global.skalierung);

			}
			if( x > 250 )
			{
				global.skalierung = 250 / this.orgWidth;    // bildbreite des vorschaubildes wird neu skaliert damit es an die größes des vorschaudivs angepasst ist - saskia
				x = 250;
				y = Math.round(this.orgHeight * global.skalierung);		
			}
			if(document.getElementById("full_wizard")){
					if (parseInt(document.getElementById("full_wizard").style.height) < (y+20)){
					document.getElementById("full_wizard").style.height=(y+20)+"px";
					}
			}
		}
		else
		{
			if( y > 380 )
			{
				global.skalierung = 380 / this.orgHeight;
				y = 380;
				x = Math.round(this.orgWidth * global.skalierung);
			}
			if( x > 415 )
			{
				global.skalierung = 415 / this.orgWidth;    // bildbreite des vorschaubildes wird neu skaliert damit es an die größes des vorschaudivs angepasst ist - saskia
				x = 415;
				y = Math.round(this.orgHeight * global.skalierung);		
			}
		}
		newIMG.setAttribute("width", x);
		newIMG.setAttribute("height", y);
		newIMG.style.border = "1px solid";
		newDIV.appendChild(newIMG);
		newDIV.appendChild(newBR); //Heiko Oli
		
		/*heiko 2009-10-22
		*/
		console.log("Image.js------global.crop-----------:"+ global.crop);
		console.log("Image.js------laterCrop-----------:"+ this.laterCrop);
		if(global.crop|| this.laterCrop)	//crop buttons
		{			
			var newINPUT;
			newINPUT = document.createElement("input");
			newINPUT.setAttribute("type", "hidden");
			newINPUT.setAttribute("size", "5");
			newINPUT.setAttribute("maxlength", "5");
			newINPUT.id = "inpQuadDim";
			newDIV.appendChild(newINPUT);		
	
			newIMG = document.createElement("img");
			newIMG.style.marginTop = "10px";
			newIMG.style.marginLeft = "5px";
			newIMG.src = global.IMG_URL_NONTEXT + "buttons/plus.gif";
			newIMG.onclick = function () { setCropPlus(); } //nach ext.js
			newIMG.onmouseover = function(event) { myTip('bigger');	}
			newIMG.onmouseout = function(event) { UnTip(); }	
			newDIV.appendChild(newIMG);
				
			newIMG = document.createElement("img");
			newIMG.style.marginTop = "10px";
			newIMG.style.marginLeft = "5px";
			newIMG.src = global.IMG_URL_NONTEXT + "buttons/minus.gif";
			newIMG.onclick = function () { setCropMinus(); }
			newIMG.onmouseover = function(event) { myTip('smaller');	}
			newIMG.onmouseout = function(event) { UnTip(); }	
			newDIV.appendChild(newIMG);
			
			newIMG = document.createElement("img");
			newIMG.style.marginTop = "10px";
			newIMG.style.marginLeft = "5px";
			newIMG.src = global.IMG_URL_NONTEXT + "buttons/max.gif";
			newIMG.onclick = function () { setCropMax(); }
			newIMG.onmouseover = function(event) { myTip('max');	}
			newIMG.onmouseout = function(event) { UnTip(); }	
			newDIV.appendChild(newIMG);
		}
	
		newIMG = document.createElement("img");
		newIMG.style.marginTop = "10px";
		newIMG.style.marginLeft = x/2-10 +"px";
		newIMG.src = global.IMG_URL_NONTEXT + "buttons/remove.gif";
		var t_this = this;
		newIMG.onclick = function() { t_this.removePreviewImage(newDIV);	}	
		
		newIMG.onmouseover = function(event) { myTip('clear');	}
		newIMG.onmouseout = function(event) { UnTip(); }	
		newDIV.appendChild(newIMG);
		
		this.imagesList.innerHTML = '';		
		this.imagesList.appendChild(newDIV);
		
		this.removedImages.pop();
		this.input.value = "";
		
		/*
		 * 2er parameter ist das global.cropBild
		 * die urls und pfade muessen richtig initialisiert sein!!!
		 */
		 //delay(5000);
		 //Heiko 2009-10-22 if abfrage um show crop damit diese Funktion nicht immer aufgerufen wird.
		if( global.crop || this.laterCrop){
			showCrop( this.url + this.currentPreview, this.path + this.currentPreview, this.orgWidth, this.orgHeight ); //ext.js
		}
	}
	else 
	{
		var newDIV = document.createElement("span");
		newDIV.id = this.currentPreview;
		newDIV.style.margin = "12px";
		newDIV.style.float= "left";
		newDIV.setAttribute("float", "left");

		var newIMG = document.createElement("img");
		newIMG.setAttribute("src", this.url + this.currentPreview);
		var y = 60;
		var x = Math.round(y / this.orgHeight * this.orgWidth);
		newIMG.setAttribute("width", x);
		newIMG.setAttribute("height", y);
		newIMG.style.border = "1px solid";
		newDIV.appendChild(newIMG);
		
		newIMG = document.createElement("img");
		newIMG.src = global.IMG_PATH_NONTEXT + "buttons/remove-inplace-icon.png";
		// Trick, weil im onclick-Handler kein this verwendet werden kann
		var t_this = this;
		newIMG.onclick = function () {t_this.uploadFormRemoveFromList(newDIV); }
		newIMG.onmouseover = function(event) { myTip('clear');	}
		newIMG.onmouseout = function(event) { UnTip(); }	
		newDIV.appendChild(newIMG);

		this.imagesList.appendChild(newDIV);

		this.removedImages.pop();
		
		// Das Feld mit dem Namen der zuletzt hochgeladenen Bilddatei zurücksetzen
		this.input.value = "";
		
		// Vorschaubild zurücksetzen
	//	this.preview.src = this.previewDefault;
	//	this.preview.style.display = "none";
	//	this.addButton.disabled = true;
	}
		console.log("addToList ende ::::::::::::::");
}


/**
 *	Ein Bild aus dem Hochlade-Dialog entfernen 
 */
Images.prototype.uploadFormRemoveFromList = function (node)
{
	console.log("uploadFormRemoveFromList :::::::::::::");
	this.imagesList.removeChild(node);
	if(this.imagesList.id.indexOf("InAddWizard")>-1) {
		var doc = document.getElementById('overlayCrop');
		doc.style.width = "1px";
		doc.style.height = "1px";		
		doc.style.left = "0px";
		doc.style.top = "0px";
		document.onmousemove = null;
	}
	this.removedImages.push(node.id);
	if(document.getElementById('divInfotabs-1'))
	{
		document.getElementById('divInfotabs-1').innerHTML = ""; 
	}
	
	if(this.imagesList.id.indexOf("InAddWizard")>-1)
	{
		if(document.getElementById("searchDiv"))
		{
			document.getElementById("searchDiv").style.display="block";
		}
	}
	console.log("uploadFormRemove..ende :::::::::::::::");
}
/**
 * start upload and crop
 * @param $task -ef for earthfave <br>
 * 				-us for user <br>
 * 				-ent for entity <br>
 * @param $id -earthfave, user or entity id
 */
Images.prototype.uploadTemporySave = function( task, id ){// Todo richtig benennen. Heiko
	console.log("uploadTemporarySave:::::::::::::id: " + id);
	getDiv('modal-1').style.zIndex=99; 
	var e = getDiv(this.popupId + '-pop-up');
	if (e) { 		e.style.display = 'block';}
	var doc = getDiv('overlayCrop');
		doc.style.width = "1px";
		doc.style.height = "1px";		
		doc.style.left = "0px";
		doc.style.top = "0px";
	document.onmousemove = null;
	// Kopie von dem Objekt bingboom, der spaeter irgendwo irgendwann geloecht wird
	//blibblob = this;
	global.picture = true;
	
	if(!global.addearthfaveoverlay){	this.uploadFormSubmit( task, id );}
}

Images.prototype.uploadFormSubmit = function ( task, idAll ){
	console.log("Images.prototype.uploadFormSubmit:::::::::::::");
	if ( idAll == "" ){	alert("Upload darf nicht aufgerufen	werden (Images)");	return;	}
	
	// Nur wenn wir auf der Bilder-Seite sind, weitermachen
//	if (this.imagesList) return;
	var addedImages = "";
	var n = this.imagesList.childNodes.length;
	if(n == 0){	return;	}
//	while (this.imagesList.childNodes.length > 0)	// das klappt nicht in Opera
	for (var i = 0; i < n; i++)	{
		var child = this.imagesList.lastChild;
		if (child.nodeType == 1){	
			// Elementknoten
			addedImages += (addedImages == "" ? "" : ",") + child.id;

			// Elementknoten aus der Liste entFstfernen
			this.imagesList.removeChild(child);
			this.imagesList.innerHTML = '<img src="'+global.IMG_URL_NONTEXT+'teaser/progress-blue.gif" alt="onload" />'; //Heiko
		}
		else{}
	}

	// Embedded video
	var video = "";
	if (e = document.getElementById(global.IMAGEUPLOAD_ID_PREFIX + "video")) {	video = e.value;}
	// Vorschaubild zurücksetzen
	//	this.preview.src = this.previewDefault;
	//	this.info.innerHTML = "Choose a picture with the file dialog above and add it to the upload list";
	//	this.addButton.disabled = true;

	// Trick, um diese Image-Instanz an die Callback zu übergeben
	var t = this;
	var strCrop = 0;
	if (global.crop){	strCrop = 1;}
	Ajax.send(global.AJAX_URL + 'imagesUploadRequest.php', 'add=' + addedImages /*+ '&remove=' + this.removedImages.join(",")*/ + '&idAll=' + idAll
			+ '&task=' + this.type  + '&crop=' + strCrop + '&video=' + encodeForRequest(video) + '&dim=' + global.cropQuadDim/global.skalierung
			+ '&cropX=' + global.cropX/global.skalierung + '&cropY=' + global.cropY/global.skalierung,
							function(response) { t.uploadCallback(response); },
							function(response) { t.uploadCallbackOnError(response); },
							function(response) { t.uploadCallbackOnTimeout(response); }, 
							global.EARTHFAVES_TIMEOUT);
}

/**
 *	Wird der Upload abgebrochen, müssen alle bereits hochgeladenen Bilder gelöscht werden.
 *  Das sind die in der Liste "removedImages" ebenso wie die in "addedImages".
 *  
 *  id kann user, entity oder ef id sein
 */
Images.prototype.uploadFormCancel = function (){
	console.log("UploadFormCancel:::::::::::");
	//ich loesche den text in dem Div. Siese ext.js funktion showCrop();
	if(document.getElementById('divInfotabs-1')){document.getElementById('divInfotabs-1').innerHTML = ""; }
	if(this.imagesList.id.indexOf("InAddWizard")>-1){
		/*if(document.getElementById("searchDiv"))
		{
			document.getElementById("searchDiv").style.display="block";
		}*/
		if(document.getElementById("upload")){document.getElementById("upload").value="";}
	
		var doc = document.getElementById('overlayCrop');	//crop verstecken
	
		doc.style.width = "1px";
		doc.style.height = "1px";		
		doc.style.left = "0px";
		doc.style.top = "0px";
		
		document.onmousemove = null;
		global.picture = false;
		//global.crop = false;
	}
	
	var addedImages = this.removedImages.length == 0 ? "" : ",";
	var n = this.imagesList.childNodes.length;
	//info ( "addedImages: " + addedImages + ' n ' + n );

	for (var i = 0; i < n; i++){
		var child = this.imagesList.lastChild;
		if (child.nodeType == 1){	
			// Elementknoten aus der Liste entfernen
			this.imagesList.removeChild(child);

			// Elementknoten
			addedImages += (i == 0 ? "" : ",") + child.id;
		}
	}
	
	// Vorschaubild zurücksetzen
//	this.preview.src = this.previewDefault;
//	this.info.innerHTML = "Choose a picture with the file dialog above and add it to the upload list";
//	this.addButton.disabled = true;
	
	var t = this;
	var prev = "preview";	//besonderer Task um previeBild zu loeschen. Wenn currentPreview ein altes bild ist, dann wird nichts geloescjt
	Ajax.send(global.AJAX_URL + 'imagesUploadRequest.php', 'add=' + this.currentPreview + '&task=' + prev,
							function(response) { t.uploadCallback(response); },
							function(response) { t.uploadCallbackOnError(response); },
							function(response) { t.uploadCallbackOnTimeout(response); }, 
							global.EARTHFAVES_TIMEOUT);
}

/**
 *	Diese Funktion wird aufgerufen, nachdem neue Bilder hochgeladen wurden.
 *	Diese müssen in der Lightbox hinzugefügt werden.
 */
Images.prototype.uploadCallback = function (response){
	console.log("Images.prototype.uploadCallback :::::::::::::::" );
	if(!global.addearthfaveoverlay){
		hideImageUploader(); //Heiko ext.js
		self.location.href="#topPosition"; //Sprung auf die Ueberschrift
	}

	var r1 = response.split("<//>");
	var r = r1[0].split(",");
	var regE = new RegExp("ok"); // Im safari schein in result mehr zu stehen als "ok". Vielleicht ein leerzeichen ?
	var reg2 = new RegExp("unlink");
	if ( regE.test(r[0])){
		// rick
		if(global.addearthfaveoverlay)
		{
			document.getElementById('modal-1').style.zIndex=99; 
			var e = document.getElementById(this.popupId + '-pop-up');
			if (e)
			{ 
				e.style.display = 'none';
				document.onmousemove = null;
			}
		}
		else
		{	
			document.onmousemove = null;
			//hideForm(this.popupId);
		}
		//updatePaginationEFImageInfoBox(get.ef_id);//bilderInfoBox Pagination
		
		/* Heiko 2009-10-28
		Switch um ef, us, oder ent abzufragen
		*/
		if(!global.addearthfaveoverlay) //Abfrage ob earthfaveOverlay. Die Varibale wird false intialisert. Sie ist true wurde der Add Wizard aufgerufen.
		{
			switch (this.type)
			{
				case "ef":
					efHomeLeftNav(get.ef_id, get.page_id,'images');	
					this.imagesRequest( get.ef_id, 3); //alle eigene bilder zu dem ef anzeigen lassen in LightBox unterdrunter
					var param = 'EFbilder|0|0|4|1|' + get.ef_id + '|blue'; //Heiko blue statt green
					efImageInfoBox('EFbilder',param); //ajax.js
					break;
		
				case "us":
					console.log("Images.prototype.uploadCallback lade menu neu und status Leiste" );
					userHomeLeftNav(global.user_id, get.page_id,'images'); // ajax.js Heiko!!! 
					this.imagesRequest( global.user_id, 3); //alle eigene bilder des user anzeigen lassen in LightBox unterdrunter
					param = "userZahlen|" + global.user_id;
					statusLeiste(global.user_id);
					break;
				case "ent":
					//todo
					break;
			}
		}
		
		global.crop = false;

		//Komma als Seperator durch Pipe ersetzt!!!
	//	alert ("param " + param);		
		/*
		 * jetzt kommt der responce von imagesUploadRequest der nicht mehr verwendet wird
		 * es ist ziemlich viel code
		 * In response stehen alle wichtige eigenschaften des gerade hochgeladenen Bildes
		 */
			/* vorgesehen fuer mehrer images bei upload --> momentan ungenutzt
			//var newId = rp[0];
			//var paths = rp[1];
			//for (var i = 0; i < paths.length; i++)
			{*/
				//this.addToLightbox(r[1], r[2], r[3], r[4], r[5], r[6]);	
				//	>>> ACHTUNG <<< id aus XxXHasMedium und NICHT normale ID
			//}
			//this.changeMenu(paths.length);
			if(!global.addearthfaveoverlay){
				this.changeMenu( 1 ); //jetzt immer nur 1 bild kommt hinzu			
			}			
	}
	else if ( reg2.test(r[0]) ){
		//TODO was cooles nach loeschen
		console.log ( "previeBild geloescht");
	}
	else{
		alert("Line 408. Sorry, an error occurred while tranfering data to server. Please contact us with the following info: " + response);
	}
}



Images.prototype.uploadCallbackOnError = function (response)
{
	alert("Line 416. Sorry, an error occurred while tranfering data to server. Please contact us with the following info: " + response);
}



Images.prototype.uploadCallbackOnTimeout = function (response)
{
	alert("Line 423. Sorry, an timeout occurred while tranfering data to server. Please try it again by clicking the 'Upload'-button.");
}



/**
 *	Diese Funktion wird aufgerufen, nachdem ein Bild per Form-Submit hochgeladen wurde. 
 *	Sie wird über die onload-Funktion des IFrames aufgerufen.
 */
Images.prototype.afterUpload = function (){
	var cLog = false;
	if(cLog){console.log("....Images.prototype.afterUpload:::::::::::::");}
//	this.preview.style.visibility = "hidden";
//	this.preview.style.display = "none";
//	this.preview.src = "";
	
	// Prüfen, ob das IFrame schon existiert. Das ist erst der Fall, 
	//	wenn die Seite komplett geladen wurde; und dies ist beim ersten Aufruf von afterUpload nicht gewährleistet.
	
	if (this.iFrame){
		result = trim(this.iFrame.contentWindow.document.body.innerHTML);	//von i-frame
		if (result != ""){
			if(getDiv('save-button2')){getDiv('save-button2').style.display="inline";}
			var rp = result.split("|");
			var regE = new RegExp("ok"); // Im safari schein in result mehr zu stehen als "ok". Vielleicht ein leerzeichen ?
			if ( regE.test(rp[0]) )
			{
				this.currentPreview = rp[1];
				this.orgWidth = rp[2];
				this.orgHeight = rp[3];

				this.currentHeight = (this.orgHeight > this.orgWidth) ? 100 : Math.round(this.orgHeight * this.image_width / this.orgWidth);
				this.currentWidth = Math.round(this.currentHeight / this.orgHeight * this.orgWidth);
/*
				this.preview.style.width = this.currentWidth + "px";
				this.preview.style.height = this.currentHeight + "px";
				this.preview.style.border = "1px solid";
				this.preview.src = this.url + this.currentPreview;
*/				
//				this.info.innerHTML = this.orgWidth + " x " + this.orgHeight + " Pixel";
//				this.preview.style.visibility = "visible";
//				this.preview.style.display = "block";

//				this.addButton.disabled = false;
				// Da wir nicht wissen, ob der Benutzer das hochgeladene Bild auch zur Liste hinzufügt, muß es erstmal in die Delete-Liste
				this.removedImages.push(this.currentPreview);
				this.url = global.MEDIA_URL + "preview/";	//falls zuvor mit einem bereits geladenen bild gearbeitet wurde
				this.path = global.MEDIA_PATH + "preview/";
				this.uploadFormAddToList();
			}
			else alert("FehlerHIER: >" + result + "<");
		}
	}
	if(cLog){console.log(".......Images.prototype.afterUpload ende:::::");}
}



/**
 * Fügt das Bild in leight-Box ein.
 * Das dritte Argument removable sagt, ob zu jedem Bild ein "Entfernen"-Button eingeblendet werden soll oder nicht
 * @param id -id aus XxXHasMedium Tabelle
 * @param path -datum z.B. 2009/09_23/
 * @param filename -dateiname
 * @param width -original breite
 * @param height -original hoehe
 * @param removeable -true wenn das DeleteButton angezeigt werden soll
 * 						-false wenn nicht anzeigen ( bei default Bildern )
 */
Images.prototype.addToLightbox = function(id, path, filename, width, height, removable, cropBut, userArray ){
	var t = this;	//da nicht ueberall this verwendet werden kann
	var tbody = this.lightbox.getElementsByTagName("tbody")[0];
//	tbody.style.backgroundColor = "#666666";
	var tr_count = tbody.getElementsByTagName("tr").length;
	var tr = tbody.getElementsByTagName("tr")[tr_count - 1];
	var td_count = tr.getElementsByTagName("td").length;
	//info ("<<<<removable: " + removable + ">>>>"); Heiko
	//info ("<<<<cropBut: " + cropBut + ">>>>"); Heiko
	
	//info ( "type: " + this.type );
	
	var vorURL;
	if ( this.type == "ef"){	vorURL = global.MEDIA_URL_EARTHFAVES;}
	else if ( this.type == "us"){	vorURL = global.MEDIA_URL_USER;	}
	else if ( this.type == "ent"){	vorURL = global.MEDIA_URL_ENTITY;}
		
	

	// Falls die Zeile voll ist, erzeuge ein neues TR in die Tabelle ein
	if (td_count > 3) {
		tr = document.createElement("tr");
		tbody.appendChild(tr);
	}
	var newBR = document.createElement("br");
	var newTD = document.createElement("td");
	newTD.id = id;
//	newTD.setAttribute("align", "center");
	newTD.style.paddingRight = "5px";
	newTD.style.paddingLeft = "5px";
	newTD.style.verticalAlign = "bottom";
	newTD.style.textAlign = "center";
	newTD.style.paddingBottom = "10px";
	newTD.style.width = "100px";


	var newIMG = document.createElement("img");
	var bildPath = vorURL + path + "small/img90_" + filename;
	newIMG.setAttribute("src", bildPath);
	newIMG.style.border = "1px solid";
	newIMG.style.marginBottom = "0px";
	
	newIMG.onmouseover = function(event){	myTip('Click to zoom');	}
	newIMG.onmouseout = function(event){	UnTip();}
	newIMG.onmousedown = function(event) { 
		//document.getElementById("lightbox-image").src = global.MEDIA_URL + path;
		//showPopup(event, "lightbox");
		showLightBox( vorURL + path + "original/" + filename, width, height, this);
		}
	//newIMG.onmouseout = function() { hidePopup("lightbox"); }

	newTD.appendChild(newIMG);
	var newBR2 = document.createElement("br");
	newTD.appendChild(newBR2);
	
	var userName = userArray[1];
	if(userName.length > 10){userName = userName.substr(0,10) + '..';}
	var urlUser = createUserUrl(userArray[0],userArray[1]);
	var uploaderAnker = document.createElement("a");
	uploaderAnker.title = userArray[1];
	uploaderAnker.href = urlUser;
	uploaderAnker.style.fontSize = "9px";
	newTD.appendChild(newBR); 
  var space = '';
  if(width<height){space = '   ';}
	var uploaderText = document.createTextNode(space + 'by ' + userName);
	uploaderAnker.appendChild( uploaderText ); 

	newTD.appendChild(uploaderAnker);

	newTD.appendChild(newBR); // anna
	
	//crop button
	if ( cropBut )
	{
		newIMG = document.createElement("img");
		newIMG.src = global.IMG_URL_TEXTLANGUAGE + "buttons/blue_41_24_edit.png";
		newIMG.style.marginBottom = "5px";
		newIMG.onclick = function() { t.crop( id, filename, path, width, height )}; // <<< ACHTUNG >>> id ist XxXHasMedium id
		newIMG.onmouseover = function(event)
		{
			myTip('Crop Image');	
		}
		newIMG.onmouseout = function(event)
		{
			UnTip();
		}	
		newTD.appendChild(newBR);
		newTD.appendChild(newIMG);
	}

	// Nun die Option zum Loeschen des Bildes
	if (removable)
	{
		//info ( "erstelle deleteButton" );
		newIMG = document.createElement("img");
		newIMG.src = global.IMG_URL_TEXTLANGUAGE + "buttons/orange_51_24_del.png";
		newIMG.style.marginLeft = "4px";
		newIMG.style.marginBottom = "5px";

		newIMG.onclick = function() { t.remove(id); }
		newIMG.onmouseover = function(event)
		{
			myTip('Delete Image');	
		}
		newIMG.onmouseout = function(event)
		{
			UnTip();
		}	

		newTD.appendChild(newIMG);
	}

	tr.appendChild(newTD);
}



/**
 *	Entfernt den Bildeinrag mit der ID id aus der DB (Tabelle earthfaves_has_medium) und aus der Lichtbox
 */
Images.prototype.remove = function(id)
{
	// Trick, weil im onclick-Handler kein this verwendet werden kann
	var t = this;
	//info ( "uebergebe id: " + id);
	Ajax.send(global.AJAX_URL + "imagesDeleteRequest.php", this.type + '_id=' + id,
							function(response) { t.removeCallback(response); }, 
							null, null, global.EARTHFAVES_TIMEOUT);
}

Images.prototype.removeCallback = function (response)
{
	var r = response.split("|");
	var task = r[3];
	if (r[0] == "ok") 
	{
		this.removeFromLightbox(r[1]);
		/*Heiko 2009-11-05
		imageRequest auch durchführen bei löschen eines Bildes, damit die Buttons richtig gesetzt werden
		*/
		switch(task)
		{
			case "ef":
				efHomeLeftNav(get.ef_id, get.page_id);
				this.imagesRequest(get.ef_id, 3);
				var param = 'EFbilder|0|0|4|1|' + get.ef_id + '|blue'; //Heiko blue statt green
				efImageInfoBox('EFbilder',param); //ajax.js
				break;
			case "us":
				userHomeLeftNav(global.user_id, get.page_id);
				this.imagesRequest(global.user_id, 3);
				//daniel 2010.08.20 wegen fehler in statusLeiste
				//param = "userZahlen|" + global.user_id;
				//infoBox("userzahlen",param);
				break;
			case "ent":
				//todo
				break;
		}
		this.changeMenu(-1);
	}
	else {
		alert("Sorry, the image could not be removed [" + r[1] + "]");
	}
}

/**
 * startet crop fuer image mit der ID id aus der DB Tabelle earthfave_has_medium
 * @param id XxXHasMediumId und gleichzeitig id des objects in tabelle
 * @param name dateiname
 * @param oldDate datum im path, z.B. 2009/09_10/
 * @param wiedth breite des originalbildes
 * @param heigh hoehe des originalbildes
 */
Images.prototype.crop = function(id, name, oldDate, wiedth, heigh)
{
	console.log ( "crop aufgerufen id: " + id + " name " +  name + " oldDate " +  oldDate + " breite " +  wiedth + " height" +  heigh );
	this.laterCrop = true; //crop von einen bereits geladenen bild
	this.currentPreview = "original/" + name;	//bildnamen neu setzten
	
	/*
	 * setzte die pfade auf richtige werte
	 */

	if ( this.type == "ef")
	{
		this.url = global.MEDIA_URL_EARTHFAVES + oldDate;
		this.path = global.MEDIA_PATH_EARTHFAVES + oldDate;
		//info ( "neue url ist " + this.url + this.currentPreview );
		//info ( "neur path ist " + this.path + this.currentPreview );
	}
	else if ( this.type == "ent")
	{
		this.url = global.MEDIA_URL_ENTITY + oldDate;
		this.path = global.MEDIA_PATH_ENTITY + oldDate;
	}
	else if ( this.type == "us")
	{
		this.url = global.MEDIA_URL_USER + oldDate;
		this.path = global.MEDIA_PATH_USER + oldDate;
	}

	this.orgWidth = wiedth;
	this.orgHeight = heigh;

	this.currentHeight = (this.orgHeight > this.orgWidth) ? 100 : Math.round(this.orgHeight * this.image_width / this.orgWidth);
	this.currentWidth = Math.round(this.currentHeight / this.orgHeight * this.orgWidth);

	hideDiv("upload-button");
	showCropDivs();	//muss vor uploadFormAddToList() aufgerufen werden.
	//self.location.href = "#upload-anchor";
	this.uploadFormAddToList();
	var lCrop = this.laterCrop;
	
	
	//this sichern
	var t = this; 
	
	//document.getElementById('imageUploadInAddWizard-pop-up').style.display = "block"; //Heiko div war vorher "uploadDivAll"
	//showCropDivs(); // ext.js Heiko
	document.getElementById('save-button2').onclick = function () { cropImage(t.type, id, t.path + "original/" + name, lCrop); }
	/*	Heiko 2009-10-29 Funktion cropImage
	4ten Parameter hinzugefügt der laterCrop der Funktion mitliefert
	*/
	//	cropImage(this.type, id, this.path + "original/" + name );
	console.log (" image.js crop function ende ..");
}

/**
 *	Fordert die Bilder eines earthfaves oder eines Benutzer mit der ID id an
 *						----------------------------
 *	von Dima & Saskia am 10.08.09 aktualisiert
 */
Images.prototype.imagesRequest = function( id, cas){
	console.log ( "image type: "+ this.type + "_id " + " id " + id );
	if(getDiv('earthfaveImagesAni')){
		 showDiv('earthfaveImagesAni');
		 console.log('Images.prototype.imagesRequest  :: sichbar animation');
	}
	var t = this;
	console.log("huhu " + this.type);
  Ajax.send(global.AJAX_URL + "imagesRequest.php", t.type + '_id=' + id + '&se='+ cas, 
  						function(response) { t.imagesCallback(response); }, 
  						null, null, global.EARTHFAVES_TIMEOUT);
}



/**
 *	Diese Funktion wird mit den zurückgelieferten Bildern aufgerufen
 *	Diese müssen in der Lightbox hinzugefügt werden.
 */
Images.prototype.imagesCallback = function (response){
	console.log("Images.prototype.imagesCallback"); 
	if(getDiv('earthfaveImagesAni')){
		 hideDiv('earthfaveImagesAni');
		 console.log('Images.prototype.imagesRequest  :: unsichbar animation');
	}

	r = response.split("|");
	if (r[0] == 'ok'){
		// Bisherige Bilder löschen
		this.clearImageList();
		rp = r[1].split("<//>");
		for (var i = 0; i < rp.length; i++){
			rpp = rp[i].split(";");
			id = rpp[0];	// ID des Medium-Eintrags, **nicht** die ID des earthfaves selber!
			path = rpp[1];
			//daniel Aenderungen wegen DB aenderung !!!
			filename = rpp[2];
			width =  rpp[3];
			height =  rpp[4];
			removeBut = rpp[5];
			cropBut = rpp[6];
			var uploaderId = ''; if(rpp[7]){ uploaderId = rpp[7];}
			var uploaderName = ''; if(rpp[8]){ uploaderName = rpp[8];}
			var userArray = new Array(uploaderId,uploaderName);
			//info ("<<<<cropBut: " + cropBut + ">>>>");
			//info ("<<<<removeBut: " + removeBut + ">>>>");
			//alert("id: " + id + "; path: " + path + "; removable: " + removable + "; cropBut: " + cropBut);
			// Das dritte Argument sagt, ob zu jedem Bild ein "Entfernen"-Button eingeblendet werden soll oder nicht
			//this.addToLightbox(id, path, filename, width, height, discoverer == global.user_id);
			this.addToLightbox(id, path, filename, width, height, removeBut, cropBut, userArray );
		}
		
	}
	else{
		//Heiko; Wenn letztes Bild gelöscht wird global.crop auf true gesetzt.
		switch (this.type)
		{
			case "ef":
				//info ("hjfsafhkdfhkf...... " + global.crop);
				checkXxX_HasImagesRequest("ef", get.ef_id);
				//info ("global.crop imagesRequest ef...... " + global.crop);
				break;
			case "us": 
				checkXxX_HasImagesRequest("us", global.user_id);
				//info ("global.crop imagesRequest us...... " + global.crop);
				break;
			case "ent":
				//todo
				break;
		}
		
		return r;
	}
}



/**
 *	Private Funktionen
 *
 */



/**
 *	Entferne alle Bilder aus der Lightbox
 *	Editiert von Dima & Saskia am 12.08.09
 */
Images.prototype.clearImageList = function()
{	
	var tbody = this.lightbox.getElementsByTagName("tbody")[0];
	if(tbody){
    //_Images_clearImageList = function(lightbox)
	this.lightbox.removeChild(tbody);
	
	tbody = document.createElement("tbody");
	this.lightbox.appendChild(tbody);

	tr = document.createElement("tr");
	tbody.appendChild(tr);
	}
}



/**
 *	Entfernt den Bildeinrag mit der ID id aus der Lichtbox
 */
Images.prototype.removeFromLightbox = function(id)
{
	var td = document.getElementById(id);
	if (td) {
		td.parentNode.removeChild(td);
	}
	else {
		alert("Image with ID = " + id + " not found");
	}
}

/**
 *	Erhöhe im Images-Link ("Images (xy)") um den Betrag value (der auch negativ sein darf)
 */
Images.prototype.changeMenu = function (value)
{

	var link0 = document.getElementById("zahl0");	// images ef
	var link3 = document.getElementById("zahl3");	// my images ef
	var usLink = document.getElementById("userZahl"); //my images user
//	info ("usLink.innerHTML.........." + usLink.innerHTML);
	
	/* Heiko 2009-10-28
	Switch um ef, us, oder ent abzufragen
	*/
	switch(this.type)
	{
		case "ef":
			// Zur Sicherheit prüfen, ob es sich um einen Textknoten handelt
			if (link0  && link3 )
			{
				//info ( "links ok");
				var text0 = link0.firstChild.data; // zahl0 hat nur 1 element --> die anzahl der images
				var text3 = link3.firstChild.data;
				// Den alten Wert per RegExp auslesen und um value erhöhen
				//var r = text.match(/^.*\((.*)\).*$/);	//sucht nach ---\----\----
				//if (r[1])
				//{
				var newText0 = parseInt( text0 ) + value;
				var newText3 = parseInt( text3 ) + value;
					//info ( "change 0 von " + text0 + " auf " + newText0 );
					//info ( "change 3 von " + text3 + " auf " + newText3 );
				link0.innerHTML = newText0;
				link3.innerHTML = newText3;
					//var text = document.createTextNode("Images (" + (parseInt(r[1]) + value) + ")");
					//link.removeChild(link.firstChild);
					//link.appendChild(text);
				//}
			}
		//	else alert("link fehlt");
			break;
		
		case "us":
			// Zur Sicherheit prüfen, ob es sich um einen Textknoten handelt
			if(usLink){
				var usText = usLink.firstChild.data;
				var newUsText = parseInt( usText ) + value;
				usLink.innerHTML = newUsText;
			}
			else alert("link fehlt");
			break;
		case "ent":
			//todo
			break;
	}
}


/**
 *  Das previewBild auf dem server loechen, beim klicken auf das  Kreuz
 */
Images.prototype.removePreviewImage = function (newDIV) { 
	this.uploadFormRemoveFromList(newDIV); 
	UnTip(); 
	if( getDiv("full_wizard") ) {
		getDiv("full_wizard").style.height="160px";
	}
	var t = this;
	Ajax.send( global.AJAX_URL + 'removePreviewImageRequest.php', 'add=' + t.currentPreview,
				function(response) { t.removePreviewImageCallback(response); },
				function(response) { t.removePreviewImageOnError(response); },
				function(response) { t.removePreviewImageOnTimeout(response); }, 
				global.EARTHFAVES_TIMEOUT );
}
Images.prototype.removePreviewImageCallback = function (response){ return; }
Images.prototype.removePreviewImageOnError = function (response){ return; }
Images.prototype.removePreviewImageOnTimeout = function (response){ return; }

