$(function() {


    //    $(".editButton").show();
  

    $(".deleted").hide();
    $("#hideDeleted").hide();

  
    $(".editButton").live('click',function() {
	
	var id = $(this).parents('tr').attr('id');
	getEditable(id);

	return false;
    });


    $(".cancelButton").live('click',function() {
	
	var id = $(this).parents('tr').attr('id');
	getViewable(id);
	
	return false;
    });


    $("#showDeleted").click(function() {
	$(".deleted").show();
	$(this).hide();
	$("#hideDeleted").show();
    });


    $("#hideDeleted").click(function() {
	$(".deleted").hide();
	$(this).hide();
	$("#showDeleted").show();
    });

    $(".saveButton").live('click',function() {
	var id = $(this).parents('tr').attr('id');
	var thisJobInputs = $('#'+id).find('input,select');

	/*var selectedShipmentCategories = [];
	$('#'+id+'_shipment_categories option').each(function(i) {
	    if (this.selected == true) {
		selectedShipmentCategories.push(this.value);
	    }
	});

	alert(selectedShipmentCategories);
	 */
	

	$.post('/jobs/ajax_edit', thisJobInputs, function(data) {
	    getViewable(id);
	});

	/*$.ajax({
	    type:"post",
	    url:"/ajax_edit",
	    data: {
		'job': thisJobInputs,
		'shipmentCategory': selectedShipmentCategories
	    }
	}); */


	return false;

    });


    $(".deleteButton").live('click', function() {
	var id = $(this).parents('tr').attr('id');
	markDeleted(id);

    });

    $(".undeleteButton").live('click', function() {
	var id = $(this).parents('tr').attr('id');
	markUndeleted(id);
    })

//console.profileEnd();

});


function getEditable(id) {
    $.get('/jobs/getEditableRow/'+id+'/editedRowCount', false, function(data) {

	$("#"+id).replaceWith(data);
	$("#"+id).find("button").button();
	var tempHeader = $("#originalHeader").clone();
	tempHeader.attr('class', 'tempHeader');
	$("#"+id).before(tempHeader);

    } );
}


function getViewable(id) {
    $.get('/jobs/getViewableRow/'+id, false, function(data) {

	$("#"+id).replaceWith(data);
	$("#"+id).find("button").button();
	$('.tempHeader').remove();
    } );
}

function cancelEditable(id) {
    $("#"+id).find('input').attr("readonly","readonly");
    $("#"+id).find('select').attr("disabled","disabled");
    $(".editButton").show();
}


function markDeleted(id) {
    $.get('/jobs/markDeleted/'+id,false, function(data) {
	alert(id);
	getViewable(id);
    });
}

function markUndeleted(id) {
    $.get('/jobs/markUndeleted/'+id,false, function(data) {
	getViewable(id);
    });
}


function getParent() {
    
}
