function itv_edit(image_id)
{
        // reject multiple calls to this function by checking for hidden input field created for image_id
        if ($('d_image_id')) return false;
        
	// grab current title
	var t = $('title123');
	var current_t = t.get('text');
	t.set('text', '');

	// grab current description
	var d = $('description123');
	var current_d = d.get('text');
	d.set('text', '');

	// create form
	var form = document.createElement("form"); // create a form
        form.setAttribute("id", "title_description_form");
	form.setAttribute("name", "edit"); // give form a name
	form.onsubmit = function() { //handle form submit with a message
		itv_save();
		return false;
	}

	// add label for Title
	var tlabel = document.createElement("strong");
	tlabeltext = document.createTextNode("Title:");
	tlabel.appendChild(tlabeltext);
	form.appendChild(tlabel);
	
	// add input box for Title

	var titleText = document.createElement("div");
	titleText.setAttribute("id", "title123");
	var input = document.createElement("input"); // create an input element
	with(input) {
		setAttribute("id", "title"); // give input a name
		value = current_t;
	}
	titleText.appendChild(input);
	form.appendChild(titleText);

	// add label for Description
	var dlabel = document.createElement("strong");
	dlabeltext = document.createTextNode("Description:");
	dlabel.appendChild(dlabeltext);
	form.appendChild(dlabel);

	// add textarea box for Description
	var descriptionText = document.createElement("div");
	descriptionText.setAttribute("id", "description123");
	var input = document.createElement("textarea"); // create an input element
	with(input) {
		setAttribute("id", "descript"); // give input a name
		setAttribute("cols", "35"); // make it a text input
		setAttribute("rows", "2"); // make it a text input
		value = current_d;
	}
	descriptionText.appendChild(input);
	form.appendChild(descriptionText);
	
	// create holder for form save and cancel buttons
	var buttonholder = document.createElement("div");
	buttonholder.setAttribute("id", "formbuttons");
	
	// create hidden input field for image_id
	var input = document.createElement("input"); // create an input element
	with(input) {
		setAttribute("value", image_id); // give input a value
		setAttribute("id", "d_image_id"); // give input a name
		setAttribute("type", "hidden"); // make it a submit button
	}
	buttonholder.appendChild(input); // append input to form

	// create save button
	var input = document.createElement("input"); // create an input element
	with(input) {
		setAttribute("type", "submit"); // make it a submit button
		setAttribute("value", "Save"); // give input a value
		setAttribute("style", "margin-top: 10px; margin-right: 10px;");
	}
	buttonholder.appendChild(input); // append input to form
    
	// create cancel button
	var input = document.createElement("input"); // create an input element
	with(input) {
		setAttribute("type", "button"); // make it a submit button
		setAttribute("value", "Cancel"); // give input a value
		onclick=function(){ itv_cancel(); };
	}
	buttonholder.appendChild(input); // append input to form

	// attach buttons to form
	form.appendChild(buttonholder);
	
	// add form to page
	$('title_description').set('text', '');
	$('title_description').appendChild(form); // append form to body element
}

function itv_cancel()
{
  var url = '/media_galleries/get_title_description/'+$('d_image_id').value;
  
  var myRequest = new Request.JSON(
	{
		url: url,
		onSuccess: function(transport)
	       {
		$('title123').set('text', transport['title']);
		$('description123').set('text', transport['description']);
		$('formbuttons').set('text', '');
		}
      }).send();

}

function itv_save()
{
  var url = '/media_galleries/set_title_description/';
  
  var myRequest = new Request(
	{
		method: 'post', 
		url: url,
		data: { 'data[MediaItem][id]' : $('d_image_id').value,
		'data[MediaItem][title]'      : $('title').value,
		'data[MediaItem][descript]'   : $('descript').value
		},
		onSuccess: function(transport)
		{
			$('title123').set('text', $('title').value);
			$('description123').set('text', $('descript').value);
		}
	
      }).send();
  
}

function profpic_showCommentsIndicator(elementID)
{
     var h = $(elementID).getStyle('height');
     $(elementID).setStyle('min-height',h);
     $(elementID).setStyle('text-align','center');
     $(elementID).innerHTML = '<img src="/img/ajax-loader-small.gif">';
     
     
}

function set_profile_pic($mediaID, $entity_id, $entity_type)
{
	////AAAARGH////
  //set entity
  if($entity_type=='user'){
  var url = '/users/set_profile_pic/';
  var myRequest = new Request(
      {method: 'post', 
      url: url,
      data: { 'data[User][id]' : $entity_id, 
      'data[User][media_item_id]' : $mediaID},
      onRequest: profpic_showCommentsIndicator('profile_pic_cell'),
        onSuccess: function(transport)
        {
          $('profile_pic_cell').innerHTML = 'Profile Photo Set';
          $('profile_pic_cell').setStyle('color',"#cc0000");
        }
        }).send();
	}else{
	  var url = '/groups/set_profile_pic/';
	  var myRequest = new Request(
	      {method: 'post', 
	      url: url,
	      data: { 'data[Group][id]' : $entity_id, 
	      'data[Group][media_item_id]' : $mediaID},
              onRequest: profpic_showCommentsIndicator('profile_pic_cell'),
	      onSuccess: function(transport)
	      {
	        $('profile_pic_cell').innerHTML = 'Profile Photo Set';
                $('profile_pic_cell').setStyle('color',"#cc0000");
	      }}).send();	
	}
}
