// JavaScript Document



function makeEditable(pic_id){
	
	if (document.getElementById("updatedescription") == null){ // Verhindert mehrfaches Ausführen
	
		var inputfield = document.createElement("input");
		//alert((document.getElementById("p_description").firstChild.nodeValue));
		// (document.getElementById("p_description").firstChild.nodeValue == ",") { // Wenn Description Object tagbeschreibung enthält
		try{
		inputfield.setAttribute("value",document.getElementById("description").firstChild.nodeValue );
		document.getElementById("p_description").removeChild(document.getElementById("description"));
		}
		catch (e){
			inputfield.setAttribute("value","" );
		}
		
		inputfield.setAttribute("type","text" );
		inputfield.setAttribute("id","updatedescription" );
		var action = "saveComment("+pic_id+", this.value)";
		inputfield.setAttribute("onblur", action);
		
		
		document.getElementById("p_description").appendChild(inputfield);

	
	}
}

function makeNormalDescription(pic_id, value){
	
	var des = document.createElement("span");

	des.appendChild(document.createTextNode(value));
	
	des.setAttribute("id","description" );
	
	var onclick = "makeEditable("+pic_id+")";
	
	des.setAttribute("onclick", onclick);
	
	document.getElementById("p_description").appendChild(des);
	document.getElementById("p_description").removeChild(document.getElementById("updatedescription"));
	
	
	}







function saveComment(pic_id, pic_description)
{
  var xmlObj = null;
  
  if(window.XMLHttpRequest)
  {
      xmlObj = new XMLHttpRequest();
  } 
  else if(window.ActiveXObject)
  {
      xmlObj = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else 
  {
      return;
  }
  xmlObj.onreadystatechange = function()
  {
    if(xmlObj.readyState == 4) // Wenn fertig
	{	
		
		makeNormalDescription(pic_id,xmlObj.responseText);
       
	}
  }	
  	var url = "../includes/update_description.php?pic_id="+pic_id+"&pic_description="+pic_description ;
	
    xmlObj.open('GET',url, true);
	
    xmlObj.send('');
}

