/**
 * @author noehmeier-marcel
 */

function Galerie(sid){
	this.session_id		= sid;
	
	this.contextMenu	= null;
	
	//Mouseover -> Erklärungsdiv
	//Onclick -> Show
	//On Doubleclick -> Menü
	
	this.showHelpMenu			= function(obj){
		//alert('HelperMenü');
	};
	
	this.ClickHandler			= function(img,comment,tags,bewertung){
		showUserImageBig(img,comment,tags,bewertung);
	};
	
	this.hideContextMenu		= function(obj){
		this.contextMenu.hide();
	};
	
	this.showContextMenu		= function(obj){
		
		var posLeft, posTop;
		
		
		this.contextMenu = new Contextmenu();
		this.contextMenu.createOverlay(obj);
		
		var _this = this;
		
		this.contextMenu.insertBox({
			name: 'Bild Löschen',
			action: function(){
				_this.deleteImage(obj);
			}
		});
		this.contextMenu.insertBox({
			name: 'Bild bearbeiten',
			action: function(){
				_this.editImage(obj);
			}
		});
		this.contextMenu.insertBox({
			name: 'Als Avatar setzen',
			action: function(){
				var option = {
					onComplete : function(){
						location.reload();
					}
				}
				
				var imgVars = Json.evaluate(obj.id);
				
				new Json.Remote('user.php',option).send({
						session_id:	SID,
						img_id: imgVars.id,
						action: 'setImageToAvatar'
				});
			}
		});
		
		this.contextMenu.display();
	};
	
	
	
	
	//createImageActionMenüButton
	this.createIAMB=function(innerHtml){
		var li		= document.createElement('li');
		li.style.cursor = 'pointer';
		li.onmouseover = function(){
			this.style.fontWeight = 'bold';
		}
		li.onmouseout = function(){
			this.style.fontWeight = 'normal';
		}
		li.innerHTML = innerHtml;		
		return li;
	};
	
	

	
	this.editImage				= function(obj){
		var imgVars = Json.evaluate(obj.id);
		
		//Showcase
		var div = document.createElement('div');
		div.className = 'edit_user_image';
		//Image
		var img = document.createElement('img');
		img.src = '../bilder/user/m/'+imgVars.name;
		//alert(img.src);
		//Textarea for the Comments
		var com = document.createElement('input');
		com.maxLength = '30';
		com.value = imgVars.comment;
		
		var tags = document.createElement('input');
		tags.maxLength = '255';
		tags.value = imgVars.img_tags;
		
		var header = document.createElement('div');
		
		var closeBtn = document.createElement('button');
		closeBtn.innerHTML = 'X';
		if(!document.all){
			closeBtn.type = 'button';
		}
		closeBtn.onclick = function(){
			div.style.display = 'none';
		}
		
		var sendBtn = document.createElement('button');
		if(!document.all){
			sendBtn.type = 'button';
		}
		//sendBtn.type = 'button';
		sendBtn.innerHTML = '&Uuml;bernehmen';
		//Function to submit updated Comments
		sendBtn.onclick = function(){
			var option = {
				onComplete : function(){
					div.innerHTML = 'gespeichert!';
					div.style.borderWidth = '5px';
					div.style.borderColor = '#691c23';
					hide = function(){
						div.style.display = 'none';
						location.reload();
					}
					setTimeout("hide()",1000);
				}
			}
			new Json.Remote('user.php',option).send({
					session_id:	SID,
					img_id: imgVars.id,
					action: 'updateImage',
					comment: com.value,
					img_tags: tags.value
				});
			
			
		}
		var form = document.createElement('form');
		var text = document.createElement('span');
		text.innerHTML = 'Beschreibung:';
		form.appendChild(text);
		
		form.appendChild(com);
		
		text = document.createElement('span');
		text.innerHTML = 'Tags:';
		form.appendChild(text);
		
		form.appendChild(tags);
		
		form.appendChild(sendBtn);
		//form.onsubmit = function(){}
		form.action = '#images';
		form.method = 'post';
		
		header.appendChild(closeBtn);
		div.appendChild(header);
		div.appendChild(img);
		//div.appendChild(com);
		//div.appendChild(sendBtn);
		div.appendChild(form);
		
		document.body.appendChild(div);		
	};
	
	this.deleteImage			= function(obj){
		if(confirm('Wollen Sie wirklich dieses schöne Bild löschen?')){
			var imgVars = Json.evaluate(obj.id);
			var imgObj = obj;
	
			var option = {
				onComplete : function(obj){
					if(obj.ret == true){
						var p = imgObj.parentNode.parentNode;
						p.removeChild(imgObj.parentNode);
						
						if(p.firstChild.firstChild!=null){
							var lnVars = Json.evaluate(p.firstChild.firstChild.id);
							showUserImageBig(lnVars.name,lnVars.comment,lnVars.img_tags,lnVars.img_vote);
						}else{
							showUserImageBig('nouserpic.jpg','No Pic');
						}
						/*
						var _nodes = document.getElementById('profile_gallery').childNodes;
						var lastNode = null;
						for(var i = 0; i<_nodes.length; ++i){
							//alert('test_: '+_nodes[i].id+' == '+imgObj.id);
							if( _nodes[i] == imgObj ){
								//alert('Stimmt!');
								if(lastNode!=null){
									var lnVars = Json.evaluate(lastNode.id);
									showUserImageBig(lnVars.name,lnVars.comment);
								}else{
									showUserImageBig('nouserpic.jpg','No Pic');
								}
								document.getElementById('profile_gallery').removeChild(_nodes[i]);
							}
							lastNode = _nodes[i];
						}
						*/
					}else{
						alert('Fehler: Konnte Bild nicht löschen!');
					}				
				}
			}
		}
		new Json.Remote('user.php',option).send({
			session_id:	SID,
			img_id: imgVars.id,
			action: 'deleteImage'
		});
	};
	
}
