function showViewAndDesc(image_name, button)
{
	showView(image_name, button);
	var image_name = image_name.split("/");
	image_name = image_name[image_name.length-1];
	if ($("div #press_image_"+image_name.replace(".", "_")))
	{
		$("#description_place").html($("div #press_image_"+image_name.replace(".", "_")).html());
	}
	else
	{
		$("#description_place").html("");
	}
}
function RedrawImages(data)
{
	//alert(data);
   currentView = 0;
   data = data.split("==================================");
   $("#descriptions_outer_block").empty();
   descriptions = data[1].split('"');
   images = data[0].split("\n");
   additionalViews = images;
   for (i = 0; i < descriptions.length; i++)
   {
		tmp_id = "press_image_"+images[i].split("/")[images[i].split("/").length-1].replace(".", "_");
		$("#descriptions_outer_block").append("<div id='"+tmp_id+"'>"+descriptions[i]+"</div>");
   }
   $("#my_images_table").empty();
  //$("#my_images_table").append();
   table_html = "<table width=\"400\" border=0><tr>"
   tmp_class = 'frameselected';
   if (images.length>2)
   {
	   for (i = 0; i < images.length-1; i++)
	   {
			table_html+='<td align="center" width="40">\
				<img id="buttonView_'+i+'" name="buttonView_'+i+'" class="'+tmp_class+'" style="cursor:pointer;" \
				onmouseup="showViewAndDesc(\''+images[i]+'\', \'buttonView_'+i+'\');" src="'+images[i]+'" height=76 >'+
				'<div style="border:1px solid #666;margin:2px"><a href="javascript:StartEditImage(\''+images[i]+'\')" style="font-family:Verdana;font-size:8px;" class="admin_link">edit</a></div><div style="border:1px solid #666;margin:5px">\
<a href="javascript:DeleteImage(\''+images[i]+'\')" style="font-family:Verdana;font-size:8px;" class="admin_link">delete</a></div>'
				+'</td>';
			tmp_class = 'frame';
	   }
   }
  table_html+="<td></td></tr></table>";
  
  $("#my_images_table").append(table_html);
  lastButton = document.getElementById('buttonView_0');
  $("#press_admin_form").fadeOut("fast");
  //alert(lastButton);
   if (images.length>2)
   {
		showViewAndDesc(images[0], 'buttonView_0');
   }
}
function StartEditImage(link)
{
	$("#press_admin_form").empty();
	$("#press_admin_form").fadeIn("fast");
	
	descr_id = "press_image_"+link.split("/")[link.split("/").length-1].replace(".", "_");
	
	$("#press_admin_form").append("<center><img src='"+link+"' height=76/><h1>Edit Image Description</h1><textarea id='press_edit_descr_textarea' style='height:230px;'>"+$("#"+descr_id).html()+"</textarea><button onclick='EditImageSubmit();'>Save</button><button onclick='javascript:$(\"#press_admin_form\").fadeOut(\"fast\")'>Cancel</button></center><input type='hidden' id='press_edit_descr_imglink' value='"+link.split("/")[link.split("/").length-1]+"'");
}
function EditImageSubmit()
{
	new_description = $("#press_edit_descr_textarea").val();
	img_link = $("#press_edit_descr_imglink").val();
	$.post("ajax_processor.php", {type:'edit', new_description:new_description, img_link:img_link, pressitemid:SelectpressItemId}, RedrawImages);
	$("#press_admin_form").empty();
	$("#press_admin_form").html("<center><h1>Processing...</h1></center>");
}
function DeleteImage(link)
{
	if (confirm("Do you really want to delete this image?"))
	{
		$("#press_admin_form").empty();
		$("#press_admin_form").fadeIn("fast");
		$("#press_admin_form").html("<center><h1>Processing...</h1></center>");
		$.post("ajax_processor.php", {type:'delete', img_link:link, pressitemid:SelectpressItemId}, RedrawImages);
	}
}


function AddNewPicture()
{
	$("#press_admin_form").empty();
	$("#press_admin_form").fadeIn("fast");
	$("#press_admin_form").html("<center><h1>Upload New Picture...</h1>"+
		"<input type='file' id='fileToUpload' name='fileToUpload'/><br/>"+
		"Description:<br/><textarea id='press_edit_descr_textarea' style='height:230px;'></textarea>"+
		"<input type='hidden' id='press_edit_descr_imglink' value=''>"+
		"<button onclick='javascript:ajaxFileUpload();'>Upload</button>"+
		"<button onclick='javascript:$(\"#press_admin_form\").fadeOut(\"fast\")'>Cancel</button><br/>"+
		"<div id='loading' style='display:none;'>loading...</div>"+
	"</center>");
}

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function ajaxFileUpload()
{
	$("#loading")
	.ajaxStart(function(){
		$(this).show();
	})
	.ajaxComplete(function(){
		$(this).hide();
	});
	$.ajaxFileUpload
		(
			{
				url:'ajax_file_upload.php?id='+SelectpressItemId, 
				secureuri:false,
				fileElementId:'fileToUpload',
				dataType: 'json',			
				success: function (data, status)
				{
					if(typeof(data.error) != 'undefined')
					{
						if(data.error != '')
						{
							alert(data.error);
						}else
						{
							$("#press_edit_descr_imglink").val(data.msg);
							EditImageSubmit();
							//$.post("ajax_processor.php", {type:'reload', pressitemid:SelectpressItemId}, RedrawImages);
						}
					}
				},
				error: function (data, status, e)
				{
					alert(e);
				}
			}
		)
		return false;
}
function pureCreate( name, attributes ) 
{
	  var el = document.createElement( name );
	  if ( typeof attributes == 'object' ) 
	  {
		for ( var i in attributes ) 
		{
		  el.setAttribute( i, attributes[i] );

		  if ( i.toLowerCase() == 'class' ) 
		  {
			el.className = attributes[i];  // for IE compatibility

		  } 
		  else if ( i.toLowerCase() == 'style' ) 
		  {
			el.style.cssText = attributes[i]; // for IE compatibility
		  }
		}
	  }
	  for ( var i = 2; i<arguments.length; i++ ) 
	  {
		var val = arguments[i];
		if ( typeof val == 'string' ) { val = document.createTextNode( val ) };
		el.appendChild( val );
	  }
	  return el;
}
