$(function(){

tr_select=0;
sub_job_select=0;



$(".up_arrow").live("click",function(){

tr_select=$(this).attr("tr_id");
	//alert($(this).attr("priority"));
	//alert($(".table_tr[tr_id="+tr_select+"]").prev(".table_tr").attr("priority"));
	//alert(tr_select);
	
if((
$(this).attr("priority")=="1" && 
$(".table_tr[tr_id="+tr_select+"]").prev(".table_tr").attr("priority")=="1"
)||(
$(this).attr("priority")=="0" && 
$(".table_tr[tr_id="+tr_select+"]").prev(".table_tr").attr("priority")=="0"
)


){

//alert("xx");

$("#save_tr").show();
move_up=$(".table_tr[tr_id="+tr_select+"]").prev(".table_tr").attr("tr_id");
sort_up=$(".table_tr[tr_id="+tr_select+"]").prev(".table_tr").attr("arrang");
sort_me=$(".table_tr[tr_id="+tr_select+"]").attr("arrang");

$(".table_tr[tr_id="+tr_select+"]").prev(".table_tr").attr("arrang",sort_me);
$(".table_tr[tr_id="+tr_select+"]").attr("arrang",sort_up);

$(".table_tr[tr_id="+tr_select+"]").insertBefore(".table_tr[tr_id="+move_up+"]");	


}



});


$(".down_arrow").live("click",function(){

tr_select=$(this).attr("tr_id");

if((
$(this).attr("priority")=="1" && 
$(".table_tr[tr_id="+tr_select+"]").next(".table_tr").attr("priority")=="1"
)||(
$(this).attr("priority")=="0" && 
$(".table_tr[tr_id="+tr_select+"]").next(".table_tr").attr("priority")=="0"
)


){


$("#save_tr").show();
move_down=$(".table_tr[tr_id="+tr_select+"]").next(".table_tr").attr("tr_id");
sort_down=$(".table_tr[tr_id="+tr_select+"]").next(".table_tr").attr("arrang");
sort_me=$(".table_tr[tr_id="+tr_select+"]").attr("arrang");

$(".table_tr[tr_id="+tr_select+"]").next(".table_tr").attr("arrang",sort_me);
$(".table_tr[tr_id="+tr_select+"]").attr("arrang",sort_down);

$(".table_tr[tr_id="+tr_select+"]").insertAfter(".table_tr[tr_id="+move_down+"]");	



}

});


$("#save_tr").click(function(){
$(".over_loading").show();
var arr = $('.table_tr').map(function(){
return " set `sort`="+$(this).attr("arrang") + " where `id`= " + $(this).attr("tr_id") ;
}).get();

query=arr+"";
//alert(arr);

table=$("#table_name").text();
$.post("ajax_save_tr_sort.php",{"query":query,"table":table},function(data){
$(".over_loading").hide();
$("#save_tr").hide();
//alert(data);

if(data=="done"){
alert("تم الحفظ")	;
}else{
alert("لم يتم الحفظ")	;
//alert(data);
}


});


});






});
           

