$(function(){
//alert("S");	
main_ques_select=0;
sub_ques_select=0;

$(".main_ques").live("click",function(){
main_ques_select=$(this).attr("id");
});
$(".sub_ques").live("click",function(){
sub_ques_select=$(this).attr("id");
});


$("#up_arrow_main_ques").live("click",function(){
if(main_ques_select!=0){
$("#save_main_ques").show();
move_up=$(".main_ques[id="+main_ques_select+"]").prev(".main_ques").attr("id");
sort_up=$(".main_ques[id="+main_ques_select+"]").prev(".main_ques").attr("code");
sort_me=$(".main_ques[id="+main_ques_select+"]").attr("code");

$(".main_ques[id="+main_ques_select+"]").prev(".main_ques").attr("code",sort_me);
$(".main_ques[id="+main_ques_select+"]").attr("code",sort_up);

$(".main_ques[id="+main_ques_select+"]").insertBefore(".main_ques[id="+move_up+"]");	

  
//alert(move_up);
//alert(move_down);
}

});


$("#down_arrow_main_ques").live("click",function(){
if(main_ques_select!=0){
$("#save_main_ques").show();
move_down=$(".main_ques[id="+main_ques_select+"]").next(".main_ques").attr("id");
sort_down=$(".main_ques[id="+main_ques_select+"]").next(".main_ques").attr("code");
sort_me=$(".main_ques[id="+main_ques_select+"]").attr("code");

$(".main_ques[id="+main_ques_select+"]").next(".main_ques").attr("code",sort_me);
$(".main_ques[id="+main_ques_select+"]").attr("code",sort_down);

$(".main_ques[id="+main_ques_select+"]").insertAfter(".main_ques[id="+move_down+"]");	


    
//alert(move_up);
//alert(move_down);
}

});


$("#save_main_ques").click(function(){
$(".over_loading").show();
var arr = $('#main_ques_list > option').map(function(){
return " set `code`="+$(this).attr("code") + " where `id`= " + this.id + 
", set `code`="+$(this).attr("code") + " where `sub`= " + this.id ;
}).get();

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

$.post("ajax_save_ques_sort.php",{"table":$(this).attr("table"),"query":query,"type":"main"},function(data){
$(".over_loading").hide();
$("#save_main_ques").hide();
//alert(data);
});


});





$("#up_arrow_sub_ques").live("click",function(){
if(sub_ques_select!=0){
$("#save_sub_ques").show();
move_up=$(".sub_ques[id="+sub_ques_select+"]").prev(".sub_ques").attr("id");
sort_up=$(".sub_ques[id="+sub_ques_select+"]").prev(".sub_ques").attr("sort");
sort_me=$(".sub_ques[id="+sub_ques_select+"]").attr("sort");

$(".sub_ques[id="+sub_ques_select+"]").prev(".sub_ques").attr("sort",sort_me);
$(".sub_ques[id="+sub_ques_select+"]").attr("sort",sort_up);

$(".sub_ques[id="+sub_ques_select+"]").insertBefore(".sub_ques[id="+move_up+"]");	

  
//alert(move_up);
//alert(move_down);
}

});


$("#down_arrow_sub_ques").live("click",function(){
if(sub_ques_select!=0){
$("#save_sub_ques").show();
move_down=$(".sub_ques[id="+sub_ques_select+"]").next(".sub_ques").attr("id");
sort_down=$(".sub_ques[id="+sub_ques_select+"]").next(".sub_ques").attr("sort");
sort_me=$(".sub_ques[id="+sub_ques_select+"]").attr("sort");

$(".sub_ques[id="+sub_ques_select+"]").next(".sub_ques").attr("sort",sort_me);
$(".sub_ques[id="+sub_ques_select+"]").attr("sort",sort_down);

$(".sub_ques[id="+sub_ques_select+"]").insertAfter(".sub_ques[id="+move_down+"]");	


    
//alert(move_up);
//alert(move_down);
}

});


$("#save_sub_ques").click(function(){
$(".over_loading").show();
var arr2 = $('#sub_ques_list > option').map(function(){
    return " set sort="+$(this).attr("sort") + " where id= " + this.id ;
}).get();

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

$.post("ajax_save_ques_sort.php",{"table":$(this).attr("table"),"query":query,"type":"sub"},function(data){
$(".over_loading").hide();
$("#save_sub_ques").hide();
alert(data);
});


});




});
           

