var upload_tmp = 0;
var selected = 0;
var difficulty=0;
var needs_tmp = 0;
var cd

function resetNeedSearch() {
	$("#need_result").html("");
	$('#value').val("");
	$("#need_title").html("Search");
}

function createSortable() { 
	$("#steps").sortable({ 
		handle: '.step_handle',
		scroll: true,
		axis: 'y',
		stop: function(event, ui) {
			project.renumber_steps();
		}
	});
	$(".step_handle").disableSelection();
}

function imageUploaded(id,src,w,h,id,url) {
	i = $('#tmp_'+id+'_big img')
	//img_sizes['uploading_'+id] = {'width':w , 'height': h, 'id':id, 'url':url }
//	setupBump();
	i.attr('src', src);
}

function upload() {
	//$('#add_image').after('<li id="tmp_'+upload_tmp+'"><a class="img_thumb" href="#"><img class="img_thumb_img" src="/images/project/no_image.gif" width="60" height="45"></a></li>')
	//$('#add_image_form').after('<li id="tmp_'+upload_tmp+'_big"><img src="/images/project/uploading.jpg" width="300" height="225"></li>')
	$('#uploaded').append('<tr id="tmp_'+upload_tmp+'"><td><a href="#" class="bump"><img src="/images/project/uploading.jpg" width="200" height="140" id="uploading_'+upload_tmp+'"></a></td><td>Uploading...!</td><td>&nbsp;</td></tr>')
	img_sizes['uploading_'+upload_tmp] = {'width':300 , 'height': 225 }
	//console.log("Uploading id "+upload_tmp)
	setupBump();
	createUploadSlot();

	return true;
}



function createUploadSlot() {
	upload_tmp++
	var t = $('#uploads').append('<li><iframe id="project_upload_'+(upload_tmp)+'" name="project_upload_'+(upload_tmp)+'" src="/blank.html" width="1" height="1" frameborder="0" scrolling="no"></iframe></li>');
	$("#image_upload_form, #attachment_upload_form").attr('target', currentUploadSlot());
	return "project_upload_"+upload_tmp
}
function currentUploadSlot() {
	return "project_upload_"+upload_tmp
}


function imageDeleted(id) {
	$('#tr_img_'+id).slideUp()

}

function imageSetAsMain(id) {
	var targ = $('#img_thumb_tick_'+id);
	$('#project_images .tick').each(function(i, node){
	    if (node == targ) {
	      $(node).show();
	    } else {
	      $(node).hide();
	    };
	  });
}

step_temp = 0
var tmp2

function setstep(id, url) {
	if(url==null) {
		url="/images/project/upload_spinner.gif"
	}
	$('upload_' + id).html('<img id="upload_'+id+'_img" src="'+url+'" width="200" height="150" class="img">');
	closeUploadImage(id)
	return true;
}

function addToCraftopedia(loc, name) {
	$.ajax({
		url: loc,
		type: 'post',
		data: { name: name }
	})
}

function addImagePopup() {
	position = $("#img_upload").offset();
	$('#add_image').css('top', position.top+180);
	$('#add_image').css('left', position.left+130);
	$('#add_image').slideDown();
	
}

function addNeedPopup() {
	position = $("#add_need_btn").offset();
	$('#add_need').css('top', position.top+5);
	$('#add_need').css('left', position.left-5);
	
	$('#add_need').slideDown();
	$('#need_options').hide();
	$('#search_results').html('');
	$('#search_field').val('')

	
	return false;
}

function closeAddNeedPopup() {
	$('#add_need').fadeOut();
}


function saveBar(e) {
  el = $('#save_bar');
  if ($(this).scrollTop() > save_bar_pos ) { 
    	if (el.css('position') != 'fixed') {
			$('#save_bar').css({'position': 'fixed', 'top': '0px', 'left':0, 'width': "100%", 'border-bottom': '1px solid #999'}); 
			$('#save_bar_holder').show().height($("#save_bar").height())
		}
  } else {
	  	if (el.css('position') != 'relative') {
			$('#save_bar').css({'position': 'relative', 'width': 998, 'border-bottom': '0'});
			$('#save_bar_holder').hide()
		}
	}
}


function deleteCurrentImage() {
	setImages();
    i = images.eq(0);
	img = i.children('img').eq(0)
	d = imageSize($(img).attr('id'))
	i.parent().remove();
	setupBump();
	$.ajax({'url':d.url, 'type':'delete' })
}

