        function prevent_dragdrop(recordcount) {
       //Prevent top and bottom rows from being dragged and dropped!
       $("table#cms_folder_content tr:nth-child(1)").addClass("nodrag"); 
       $("table#cms_folder_content tr:nth-child(1)").addClass("nodrop"); 
       $("table#cms_folder_content tr:nth-child(" + recordcount +  ")").addClass("nodrag");
       $("table#cms_folder_content tr:nth-child(" + recordcount +  ")").addClass("nodrop");
       }
   
       //Do not show up arrows for first record or bottom arrows for last record!
       function correct_arrows(updatemethod) {
       var recordcount = $("table#cms_folder_content tr").length;    
       if(updatemethod == 'pageload') {
            $("table#cms_folder_content tr:nth-child(2) img[src*=up_arrow]").hide();
            $("table#cms_folder_content tr:nth-child(" + (recordcount - 1) + ") img[src*=down_arrow]").hide();
       } else {
            var recordcount = $("table#cms_folder_content tr").length; 
            $("table#cms_folder_content tr:nth-child(2) img[src*=up_arrow]").hide();
            $("table#cms_folder_content tr:nth-child(3) img[src*=up_arrow]").show(); 
            $("table#cms_folder_content tr:nth-child(" + (recordcount - 1) + ") img[src*=down_arrow]").hide();    
            $("table#cms_folder_content tr:nth-child(" + (recordcount - 2) + ") img[src*=down_arrow]").show(); 
            }
       }
   
       //Produce string to reorder pages with AJAX, returns ids in new order in the form of url parameters
       function serialise_page_order() {
       var idarray = new Array();  
       $('table#cms_folder_content tr').each(function()
       {
       idarray.push($(this).find("td").html());
       });
       idarray.pop();
       idarray.shift();
   
       var result = '';
       for(var i in idarray) 
       {
       result += 'orderedpages[]='+idarray[i]+'&';
       }
       result = result.substring(0, result.length-1);
       return result;
       } 
  
       //Put table rows in new order once up/down arrows have been clicked
       function reorder () {
       var position = 1;
       $('table tbody tr').each(function () {
       // Change the text of the first TD element inside this TR
       //$('td:first', $(this)).text(position);
       //Now remove current row class and add the correct one
       $(this).removeClass('row1 row2').addClass( position % 2 ? 'row1' : 'row2');
       position += 1;
       fix_page_order(); 
       });
       }
       
       //Correct the numbers indicating page order
       function fix_page_order() {
       var i = 0;
       $('table#cms_folder_content tr td span.pageorder').each(function()
            {
            $(this).html(i);
            i++;
            });
       }
       
       function notify_message(message) {
                $("div#notification_area").stop();  
                $("div#notification_area").fadeTo('fast', 1);
                $("div#notification_area").text(message);   
                $("div#notification_area").fadeTo(2000, 0);
       }
        

$(function() {               
    	
        //name of current cms table
        var tablename = $('table#cms_folder_content').attr('class'); 
        //number of records in table
        var recordcount = $("table#cms_folder_content tr").length;
        
        //Remove arrows from top and bottom rows
        correct_arrows('pageload');
        //Prevent top and bottom rows from being draggable
        prevent_dragdrop(recordcount); 
        
        //Delete a page after confirmation         
        $("table#cms_folder_content > tbody > tr > td > a[href*=cms_delete]").click(function(event){  
        event.preventDefault();       
        var answer = confirm("Really Delete?")     
            if (answer){
                $(this).parents("tr").hide();
                notify_message('Page Deleted!');
                $.get(this.href);
            }
        });
        
        
   
   
        // Initialise the table for drag and drop
        $("#cms_folder_content").tableDnD({
            onDragClass: "dragrow",
            onDrop: function(table, row) {
            var params = 'method=dragpages&table=' + tablename + '&' + serialise_page_order();
                $.ajax({
                type: "POST",
                url: "./ajax/cmsajax.php",
                data: params,
                    success: function(msg){
                    notify_message('Reorder Successful!');
                    }
                });
                fix_page_order(); 
                correct_arrows();  
            },
            dragHandle: "dragHandle"
        });   
   
   
       $("table#cms_folder_content > tbody > tr > td > a[href*=cms_page_reorder]").click(function(event){ 
     
       event.preventDefault(); 
       var url = this.href;     
       var tr = $(this); // Start with this link and work upwards from there, this allows the TR element itself to be the control if desired
       var iterations = 0;
       while(tr.attr('tagName') != 'TR') {
            tr = tr.parent();
            iterations += 1;
            if (iterations == 100) {
                return false; // Bail out, surely something is wrong or there is lots and lots of html in there
            }
       }
       
       //If down arrow clicked
       if(url.search('-')==-1) {   
       tr.fadeTo('medium', 0.1, function () { 
       tr.insertBefore(tr.prev()).fadeTo('fast', 1);   
       reorder();
       correct_arrows(tr);  
       });    
       } else {       
       //If up arrow clicked 
       tr.fadeTo('fast', 0.1, function () {
       tr.insertAfter(tr.next()).fadeTo('fast', 1); 
       reorder();
       correct_arrows(tr); 
       });  
       } 
       $.get(this.href); 
       notify_message('Reorder Successful!');   
       }); 
       
       
       $("table#cms_folder_content > tbody > tr > td > img.approvedimage").click(function(event){ 
       var src = $(this).attr('src');
       var id = $('td:first', $(this).parents('tr')).text();

            if(src == 'gfx/file_icons/cross.gif') {      
                var params = 'method=approvepage&table=' + tablename + '&id=' + id;
                $.ajax({
                type: "POST",
                url: "./ajax/cmsajax.php",
                data: params,
                    success: function(msg){    
                    notify_message('Page Approved!'); 
                    }
                });
                $(this).attr('src', 'gfx/file_icons/tick.gif'); 
            }
       
            if(src == 'gfx/file_icons/tick.gif') {  
                var params = 'method=disapprovepage&table=' + tablename + '&id=' + id;
                $.ajax({
                type: "POST",
                url: "./ajax/cmsajax.php",
                data: params,
                    success: function(msg){       
                    notify_message('Page Unapproved!');  
                    }
                }); 
                $(this).attr('src', 'gfx/file_icons/cross.gif'); 
            }
       });
       
});
       
