$(document).ready(function(){

	function removeItem() {
		$(this).remove();
		$("input[name='field_id_12']").remove();
		button.text('Upload an Image');
		button.css("display","inline");
	}
		
	function bindEvents() {
		$('#example1 li').bind('click', removeItem);
	}

	if ($('#button1').attr("class") == "button") {
	var button = $('#button1'), interval;
	new Ajax_upload(button,{

		action: '/includes/upload.php', 
		name: 'myfile',	
		autoSubmit: true,

		onSubmit : function(file , ext){
			if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext))){
				// extension is not allowed
				alert('Error: invalid file extension');
				// cancel upload
				return false;
			}

			// change button text, when user selects file			
			button.text('Uploading');
			
			// Uploding -> Uploading. -> Uploading...
			interval = window.setInterval(function(){
				var text = button.text();
				if (text.length < 13){
					button.text(text + '.');					
				} else {button.text('Uploading');}
			}, 200);
		},

		onComplete: function(file, response){
			if (response != "error") {
  				window.clearInterval(interval);
				button.css("display","none");
				$('<li></li>').appendTo('#example1 .files').html('<img alt="" src="/phpthumb/phpthumb.php?src=/images/uploads/' + response + '&w=280"/>');		
				$("#entryform").append('<input type="hidden" name="field_id_12" value="'+response+'" />');
				$("#example1 li").append('<span>' + response + '<span class="delete">Delete</span>');
				bindEvents();		
			}
			else {
				button.text('There was an error uploading your file. Click to try again.');
			}
		}
	});
	}

	var button2 = $('li.markItUpButton6'), interval;
	new Ajax_upload(button2,{
		action: '/includes/upload.php', 
		name: 'myfile2',	
		autoSubmit: true,
		onSubmit : function(file , ext){
			if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext))){
				// extension is not allowed
				alert('Error: invalid file extension');
				// cancel upload
				return false;
			}
		},

		onComplete: function(file, response){
			if (response != "error") {

				var text = $("#whatever").val();
			      $("#whatever").val(text + '<img alt="" src="/phpthumb/phpthumb.php?src=/images/uploads/' + response + '&w=375" />');


			//	$("#whatever").append('<img alt="" src="/phpthumb/phpthumb.php?src=/images/uploads/' + response + '&w=375" />');			
			}
			else {
				alert('There was an error uploading your file. Click to try again.');
			}
		}
	});
});



