// SeeCMS is a website content management system
// @author See Green <http://www.seegreen.uk>
// @license http://www.seecms.net/seecms-licence.txt GNU GPL v3.0 License
// @copyright 2015 See Green Media Ltd

var item;
var id;
var mode;
var selectedItem;
var selectedItemLabel;

var skipInitialMediaLoad = 0;
var onthemove = 0;

$(document).ready(function(){


  $( document ).tooltip();

    /*$(function() {
	    $('ul.draggable').nestedSortable({
	      handle: 'div',
	      items: 'li',
	      toleranceElement: '> div',
	      snap: "ul.draggable",
	      maxLevels: 10
	    });
  	});
  */
  	//$('ul.draggable li').has('ul').children().children('.name').addClass('expand');

    $(document).on("click", ".draggable div", function( e ){
      e.preventDefault();
      // if hasChildren or insert points showing
      if (($(this).parent().hasClass('hasChildren')) ) {
      
        if ($(this).parent().hasClass('open')){
          $(this).parent().children('ul').slideUp(200).fadeOut(200);
          $(this).parent().removeClass('open');
          $.ajax({ type: "POST", url: "../ajax/", data: { action: "page-adminTreeSession", id: $(this).parent().attr('id').replace('p',''), status: 0 } });
        } else {
          $(this).parent().children('ul').slideDown(200).fadeIn(200);
          $(this).parent().addClass('open');
          $.ajax({ type: "POST", url: "../ajax/", data: { action: "page-adminTreeSession", id: $(this).parent().attr('id').replace('p',''), status: 1 } });
        }
      }

    });
    
    $( "table.sortable tbody" ).sortable();
    
    $('.savewithtinymce').on( 'click', function() { 
    
      tinyMCE.triggerSave();
    });
      
    $(document).on("click", ".addnewroute", function(e){    
      e.preventDefault();
      
      if( nextroute == 1 ) {
        $( routeHTMLHead ).appendTo(".pageurlsinner");
      }
      
      $( routeHTML.replace(new RegExp('XXX', 'g'),nextroute) ).appendTo(".pageurlsinner");
      nextroute++;
    });
    
    $(document).on("click", ".name", function(e){
      e.stopPropagation();
    });
    
    //$(document).on("click", ".draggable .open div", function(){
    //});

  	var $this = $(this);

  	//$('.folders ul li').has('ul').addClass('child');

    // Media overlay windows
  	$('.medialistinner').on( 'mouseenter', '.thumb', function(){   
	  	$(this).children('.overlay').fadeIn(200);
  	});

		$('.medialistinner').on( 'mouseleave', '.thumb', function(){   
	  	$(this).children('.overlay').stop(true,true).fadeOut(200);
  	});

    $('.medialistinner').on( 'mouseleave', '.thumb', function(){   
      $(this).children('.overlay.onthemove').stop(true,true).fadeIn(0);
    });

    $(document).on("click", ".adf h3", function(){
      if( !$(this).hasClass('adfpopup') ) {
        $(this).parent().children('div').slideDown(200).fadeIn(200);
        $(this).addClass('open');
      }
    });
    
    $(document).on("click", "h3.open", function(){ 
      $(this).parent().children('div').slideUp(200).fadeOut(200);
      $(this).removeClass('open');
    });

    // set up datepicker
    $( ".datepicker" ).datepicker({dateFormat: "dd M yy"});
    $( ".datepicker.current" ).datepicker("setDate", new Date());

    // clears date from inputs
    $('.cleardate').click(function(){   
      $(this).parent().children('.datepicker').datepicker( "setDate" , null );
      $(this).parent().children('.time').val( '' );
    });

    // resets date to todays date
     $('.resetdate').click(function(){   
      $(this).parent().children('.current').datepicker( "setDate" , new Date() );
    });

    // default times if date entered
    $('#commencement').change(function(){
    if ($('#commencement').length) {
       $(this).parent().children('.time').val('00:00');
     } else {
       $(this).parent().children('.time').val('');
      }
    });
    
    $('#expiry').change(function(){
    if ($('#expiry').length) {
       $(this).parent().children('.time').val('23:59');
     } else {
       $(this).parent().children('.time').val('');
      }
    });

    // New page modal window
    $("#newpagetitle").dialog({
      resizable: true,
      autoOpen:false,
      modal: true,
      width:400,
      height:170,
      buttons: {
        'Create': function() {
        $(this).dialog('close');
        mode = 'create';
        $('.insertpoint').css( 'display', 'block' );
          if ( $('.insertpoint').is(':visible') ) {
            $('li .page').addClass('nohover');
          }
          else{
            $('li .page').removeClass('nohover');
          }
        }, 
        Cancel: function() {
        $(this).dialog('close');
        } 
      }
    }); 

    $("#newadminuser").dialog({
      resizable: true,
      autoOpen:false,
      modal: true,
      width:400,
      height:230,
      buttons: {
        'Create': function() {
        $(this).dialog('close');
        mode = 'create';
        $('.insertpoint').css( 'display', 'block' );
          if ( $('.insertpoint').is(':visible') ) {
            $('li .page').addClass('nohover');
          }
          else{
            $('li .page').removeClass('nohover');
          }
        }, 
        Cancel: function() {
        $(this).dialog('close');
        } 
      }
    });   

    // Delete page modal window
    $('.deletepage').click(function(){
      $('#deletepagepopup').dialog('open');
    });

    // Add new post title modal window
    $("#newposttitle").dialog({
      resizable: true,
      autoOpen:false,
      modal: true,
      width:400,
      height:240,
      buttons: {
        'Create': function() {
        
          var posttitle = $('#posttitle').val();
          var posttype = $('#posttype').val();
          $.ajax({
            type: "POST",
            url: "../ajax/",
            data: { action: "post-create", parentid: postfolder, title: posttitle, posttype: posttype }
          })
          .done(function( msg ) {
            if( msg ) {
              var ret = jQuery.parseJSON( msg );
              if( ret.done == 1 ) {
                $('.newslistinner').html( ret.data );
                return;
              }
            }
            alert( 'Error' );
          });
          $(this).dialog('close');
        }, 
        Cancel: function() {
          $(this).dialog('close');
        } 
      }
    });  

    // Add new folder title modal window
    $("#newfoldertitle").dialog({
      resizable: true,
      autoOpen:false,
      modal: true,
      width:400,
      height:170,
      buttons: {
        'Create': function() {
          var foldertitle = $('#foldertitle').val();
          $.ajax({
            type: "POST",
            url: "../ajax/",
            data: { action: "post-create", parentid: postfolder, title: foldertitle, isfolder: 1 }
          })
          .done(function( msg ) {
            if( msg ) {
              var ret = jQuery.parseJSON( msg );
              if( ret.done == 1 ) {
                $.ajax({
                  type: "POST",
                  url: "../ajax/",
                  data: { action: "post-folderTree" }
                })
                .done(function( msg ) {
                  $('.folders').html( msg );
                  return;
                });
              }
              return;
            }
            alert( 'Error' );
          });
          $(this).dialog('close');
        }, 
        Cancel: function() {
          $(this).dialog('close');
        } 
      }
    }); 

    $("#newgrouptitle").dialog({
      resizable: true,
      autoOpen:false,
      modal: true,
      width:400,
      height:170,
      buttons: {
        'Create': function() {
          var foldertitle = $('#foldertitle').val();
          $.ajax({
            type: "POST",
            url: "../ajax/",
            data: { action: "post-create", parentid: postfolder, title: foldertitle, isfolder: 1 }
          })
          .done(function( msg ) {
            if( msg ) {
              var ret = jQuery.parseJSON( msg );
              if( ret.done == 1 ) {
                $.ajax({
                  type: "POST",
                  url: "../ajax/",
                  data: { action: "post-folderTree" }
                })
                .done(function( msg ) {
                  $('.folders').html( msg );
                  return;
                });
              }
              return;
            }
            alert( 'Error' );
          });
          $(this).dialog('close');
        }, 
        Cancel: function() {
          $(this).dialog('close');
        } 
      }
    }); 

    // Add media folder modal window
    $("#newmediafoldertitle").dialog({
      resizable: true,
      autoOpen:false,
      modal: true,
      width:400,
      height:170,
      buttons: {
        'Create': function() {
          var foldertitle = $('#foldertitle').val();
          $.ajax({
            type: "POST",
            url: "../ajax/",
            data: { action: "media-create", parentid: mediafolder, title: foldertitle, isfolder: 1 }
          })
          .done(function( msg ) {
            if( msg ) {
              var ret = jQuery.parseJSON( msg );
              if( ret.done == 1 ) {
                $.ajax({
                  type: "POST",
                  url: "../ajax/",
                  data: { action: "media-folderTree" }
                })
                .done(function( msg ) {
                  $('.folders').html( msg );
                  return;
                });
              }
              return;
            }
            alert( 'Error' );
          });
          $(this).dialog('close');
        }, 
        Cancel: function() {
          $(this).dialog('close');
        } 
      }
    }); 

    // Add Downloads folder modal window
    $("#newdownloadfoldertitle").dialog({
      resizable: true,
      autoOpen:false,
      modal: true,
      width:400,
      height:170,
      buttons: {
        'Create': function() {
          var foldertitle = $('#foldertitle').val();
          $.ajax({
            type: "POST",
            url: "../ajax/",
            data: { action: "download-create", parentid: downloadfolder, title: foldertitle, isfolder: 1 }
          })
          .done(function( msg ) {
            if( msg ) {
              $('.folders').html( msg );
              return;
            }
            alert( 'Error' );
          });
          $(this).dialog('close');
        }, 
        Cancel: function() {
          $(this).dialog('close');
        } 
      }
    }); 

    // Add new folder title modal window
    $("#newcreategroup").dialog({
      resizable: true,
      autoOpen:false,
      modal: true,
      width:400,
      height:170,
      buttons: {
        'Create': function() {
          var foldertitle = $('#grouptitle').val();
          $.ajax({
            type: "POST",
            url: "../ajax/",
            data: { action: "group-create", parentid: postfolder, title: foldertitle, isfolder: 1 }
          })
          .done(function( msg ) {
            if( msg ) {
              var ret = jQuery.parseJSON( msg );
              if( ret.done == 1 ) {
                $.ajax({
                  type: "POST",
                  url: "../ajax/",
                  data: { action: "group-folderTree" }
                })
                .done(function( msg ) {
                  $('body').html( msg );
                  return;
                });
              }
              return;
            }
            alert( 'Error' );
          });
          $(this).dialog('close');
        }, 
        Cancel: function() {
          $(this).dialog('close');
        } 
      }
    }); 

    // Delete posts
    $('.newslist').on("click", '.deletepost', function(){
      
      item = $(this).attr('id').replace( 'deletepost-', '' );
      
      $('#deletepostpopup').html( '<p>Are you sure you want to delete this post?</p>' );
      $('#deletepostpopup').dialog('open');
    });

    $('.doclist').on("click", '.delete', function(){
    
      item = $(this).attr('id').replace( 'deletedoc-', '' );
    
      $('#deletedocpopup').html( '<p>Are you sure you want to delete this file?</p>' );
      $('#deletedocpopup').dialog('open');
    }); 

    // Delete media files
    $('.medialistinner').on("click", '.deletemedia', function(){
    
      item = $(this).attr('id').replace( 'deletemedia-', '' );
    
      $('#deletemediapopup').html( '<p>Are you sure you want to delete this file?</p>' );
      $('#deletemediapopup').dialog('open');      
    });

    // Delete folders
    $('.folders').on('click', 'span.delete', function(){
      $('#deletemediafolderpopup').dialog();
      $('.dialog').dialog('open');
      $('#deletemediafolderpopup').html( '<p>Are you sure you want to delete this folder?</p>' );
      $('#deletemediafolderpopup').dialog('open');
    });

    $('.folders').on('click', 'span.delete', function(){
      $('#deletepostfolderpopup').dialog();
      $('.dialog').dialog('open');
      $('#deletepostfolderpopup').html( '<p>Are you sure you want to delete this folder?</p>' );
      $('#deletepostfolderpopup').dialog('open');
    });
    
    // Delete users 
    $('table.users').on('click', 'a.delete', function(){
      selectedItem = $(this).attr( 'data-siteuserid' );
      $('#deleteuserpopup').dialog();
      $('.dialog').dialog('open');
      $('#deleteuserpopup').html( '<p>Are you sure you want to delete this user?</p>' );
      $('#deleteuserpopup').dialog('open');
    });
    
    $('table.sitegroups').on('click', 'a.delete', function(){
      selectedItem = $(this).attr( 'data-sitegroupid' );
      $('#deletesitegrouppopup').dialog();
      $('.dialog').dialog('open');
      $('#deletesitegrouppopup').html( '<p>Are you sure you want to delete this group?</p>' );
      $('#deletesitegrouppopup').dialog('open');
    });
    
    // Delete admin users
    $('table.adminusers').on('click', 'a.delete', function(){
      selectedItem = $(this).attr( 'data-adminuserid' );
      $('#deleteadminuserpopup').dialog();
      $('.dialog').dialog('open');
      $('#deleteadminuserpopup').html( '<p>Are you sure you want to delete this admin user?</p>' );
      $('#deleteadminuserpopup').dialog('open');
    });

    $('.folders').on('click', 'span.delete', function(){
      $('#deletedownloadfolderpopup').dialog();
      $('.dialog').dialog('open');
      $('#deletedownloadfolderpopup').html( '<p>Are you sure you want to delete this folder?</p>' );
      $('#deletedownloadfolderpopup').dialog('open');
    });
    
    $('.folders').on('click', 'span.viewedit', function(){
      $('#editmediafolderpopup').dialog();
      //$('.dialog').dialog('open');
      mediafolder = $(this).parent().attr('id').replace('folder', '');
      $('#foldertitle2').val( $(this).parent().children('.name').html() );
      $('.security').val( $(this).parent().children('.name').html() );
      $('#editmediafolderpopup').dialog('open');
    });
    
    $('.folders').on('click', 'span.viewedit', function(){
      $('#editdownloadfolderpopup').dialog();
      //$('.dialog').dialog('open');
      downloadfolder = $(this).parent().attr('id').replace('folder', '');
      $('#foldertitle2').val( $(this).parent().children('.name').html() );
      $('.security input:checkbox').prop('checked', false);
      if( $(this).attr('data-permission') ) {
        var p = $(this).attr('data-permission').split(",")
        for (i = 0; i < p.length; i++) {
          $('#security-group-'+p[i]).prop('checked', true);
        }
      } else {
        $('#security-allUserAccess').prop('checked', true);
      }
      $('#editdownloadfolderpopup').dialog('open');
    });

    $('.folders').on('click', 'span.viewedit', function(){
      $('#editpostfolderpopup').dialog();
      //$('.dialog').dialog('open');
      mediafolder = $(this).parent().attr('id').replace('folder', '');
      $('#foldertitle2').val( $(this).parent().children('.name').html() );
      $('#editpostfolderpopup').dialog('open');
    });

    $("#deletemediafolderpopup").dialog({
      resizable: true,
      autoOpen:false,
      modal: true,
      width:400,
      height:170,
      buttons: {
        Yes: function() {
          $.ajax({
            type: "POST",
            url: "../ajax/",
            data: { action: "media-delete", id: mediafolder }
          })
          .done(function( msg ) {
            if( msg ) {
              var ret = jQuery.parseJSON( msg );
              $('.medialistinner').html( ret.media );
              $('.folders').html( ret.folderTree );
              return;
            }
            alert( 'Error' );
          });
          $(this).dialog('close');
        }, 
        No: function() {
          $(this).dialog('close');
        } 
      }
    });

    $("#deletedownloadfolderpopup").dialog({
      resizable: true,
      autoOpen:false,
      modal: true,
      width:400,
      height:170,
      buttons: {
        Yes: function() {
          $.ajax({
            type: "POST",
            url: "../ajax/",
            data: { action: "download-delete", id: downloadfolder }
          })
          .done(function( msg ) {
            if( msg ) {
              var ret = jQuery.parseJSON( msg );
              $('.doclistinner').html( ret.downloads );
              $('.folders').html( ret.folderTree );
              return;
            }
            alert( 'Error' );
          });
          $(this).dialog('close');
        }, 
        No: function() {
          $(this).dialog('close');
        } 
      }
    });
    
    $("#deletepostfolderpopup").dialog({
      resizable: true,
      autoOpen:false,
      modal: true,
      width:400,
      height:170,
      buttons: {
        Yes: function() {
          $.ajax({
            type: "POST",
            url: "../ajax/",
            data: { action: "post-delete", id: postfolder }
          })
          .done(function( msg ) {
            if( msg ) {
              var ret = jQuery.parseJSON( msg );
              $('.newslistinner').html( ret.posts );
              $('.folders').html( ret.folderTree );
              return;
            }
            alert( 'Error' );
          });
          $(this).dialog('close');
        }, 
        No: function() {
          $(this).dialog('close');
        } 
      }
    });

    $("#deletesitegrouppopup").dialog({
      resizable: true,
      autoOpen:false,
      modal: true,
      width:400,
      height:170,
      buttons: {
        Yes: function() {
          $.ajax({
            type: "POST",
            url: "../../ajax/",
            data: { action: "websiteUser-deleteGroup", id: selectedItem }
          })
          .done(function( msg ) {
            if( msg ) {
              $('a[data-sitegroupid='+selectedItem+']').parent().parent().remove();
            } else {
              alert( 'Error' );
            }
          });
          $(this).dialog('close');
        }, 
        No: function() {
          $(this).dialog('close');
        } 
      }
    });

    $("#deleteuserpopup").dialog({
      resizable: true,
      autoOpen:false,
      modal: true,
      width:400,
      height:170,
      buttons: {
        Yes: function() {
          $.ajax({
            type: "POST",
            url: "../../ajax/",
            data: { action: "websiteUser-delete", id: selectedItem }
          })
          .done(function( msg ) {
            if( msg ) {
              $('a[data-siteuserid='+selectedItem+']').parent().parent().remove();
            } else {
              alert( 'Error' );
            }
          });
          $(this).dialog('close');
        }, 
        No: function() {
          $(this).dialog('close');
        } 
      }
    });

    $("#deleteadminuserpopup").dialog({
      resizable: true,
      autoOpen:false,
      modal: true,
      width:400,
      height:170,
      buttons: {
        Yes: function() {
          $.ajax({
            type: "POST",
            url: "../../ajax/",
            data: { action: "adminAuthentication-delete", id: selectedItem }
          })
          .done(function( msg ) {
            if( msg ) {
              $('a[data-adminuserid='+selectedItem+']').parent().parent().remove();
            } else {
              alert( 'Error' );
            }
          });
          $(this).dialog('close');
        }, 
        No: function() {
          $(this).dialog('close');
        } 
      }
    });
    
    $("#editmediafolderpopup").dialog({
      resizable: true,
      autoOpen:false,
      modal: true,
      width:400,
      height:170,
      buttons: {
        Save: function() {
          var foldertitle = $('#foldertitle2').val();
          $.ajax({
            type: "POST",
            url: "../ajax/",
            data: { action: "media-savefolder", title: foldertitle, id: mediafolder }
          })
          .done(function( msg ) {
            if( msg ) {
              $('.folders').html( msg );
              return;
            }
            alert( 'Error' );
          });
          $(this).dialog('close');
        }, 
        Cancel: function() {
          $(this).dialog('close');
        } 
      }
    });
    
    $("#editdownloadfolderpopup").dialog({
      resizable: true,
      autoOpen:false,
      modal: true,
      width:400,
      height:470,
      buttons: {
        Save: function() {
          var foldertitle = $('#foldertitle2').val();
          var forms = $( "#editdownloadfolderpopup form" ).serialize();
          $.ajax({
            type: "POST",
            url: "../ajax/",
            data: { action: "download-savefolder", title: foldertitle, id: downloadfolder, forms: forms }
          })
          .done(function( msg ) {
            if( msg ) {
              $('.folders').html( msg );
              return;
            }
            alert( 'Error' );
          });
          $(this).dialog('close');
        }, 
        Cancel: function() {
          $(this).dialog('close');
        } 
      }
    });
    
    $("#editpostfolderpopup").dialog({
      resizable: true,
      autoOpen:false,
      modal: true,
      width:400,
      height:170,
      buttons: {
        Save: function() {
          var foldertitle = $('#foldertitle2').val();
          $.ajax({
            type: "POST",
            url: "../ajax/",
            data: { action: "post-savefolder", title: foldertitle, id: mediafolder }
          })
          .done(function( msg ) {
            if( msg ) {
              $('.folders').html( msg );
              return;
            }
            alert( 'Error' );
          });
          $(this).dialog('close');
        }, 
        Cancel: function() {
          $(this).dialog('close');
        } 
      }
    });

    //sort tables by heading
    $('.order').tablesorter({
      widgets        : ['zebra', 'columns'],
      usNumberFormat : false,
      sortReset      : true,
      sortRestart    : true
    });

    //clear dates
    $('.cleardate').on( 'click', function( e ) { 
      e.preventDefault();
      $(this).parent().children('input').val('');
    });

    //character count on inputs
    $('input.count').keyup(function count(){
      counter = $("input.count").val().length;
      $("#count").html(counter);
    });

    $('.folders').on( 'click', 'span.move', function(e){
      e.stopPropagation();
      $('span.delete').fadeOut(200);
      $('span.viewedit').fadeOut(200);
      $('span.move').fadeOut(200);
      $(this).parent().addClass('onthemove');
      onthemove = $(this).parent().attr('id').replace('folder','');
      $('span.target').fadeIn(0);
      $('.onthemove').parent().find('span.target').fadeOut(0);
    });

     $('.medialistinner').on( 'click', '.thumb a.move', function(e){
      e.stopPropagation();
      if(onthemove) {
        $('.folders span.delete').fadeIn(200);
        $('.folders span.delete').fadeIn(200);
        $('.folders span.viewedit').fadeIn(200);
        $('.folders span.move').fadeIn(200);
        $(this).parent().removeClass('onthemove');
        onthemove = 0;
        $('.folders span.target').fadeOut(0);
        $('.folders .selected').children().children().next('span.target').fadeIn(0);
        $('.folders .selected').children().removeClass('exclude');
        $('.thumb a.delete').fadeIn(0);
        $('.thumb a.viewedit').fadeIn(0);
        $('.thumb a.move').fadeIn(0);
        
      } else {
        $('.folders span.delete').fadeOut(200);
        $('.folders span.viewedit').fadeOut(200);
        $('.folders span.move').fadeOut(200);
        $(this).parent().addClass('onthemove');
        onthemove = $(this).attr('id').replace('move','');
        $('.folders span.target').fadeIn(0);
        $('.folders .selected').children().children().next('span.target').fadeOut(0);
        $('.folders .selected').children().addClass('exclude');
        $('.thumb a.delete').fadeOut(0);
        $('.thumb a.viewedit').fadeOut(0);
        $('.thumb a.move').fadeOut(0);
        $(this).fadeIn(0);
      }
    });

    $('.newslist').on( 'click', '.page a.move', function(e){
      e.stopPropagation();
      if(onthemove) {
        $('.folders span.delete').fadeIn(200);
        $('.folders span.delete').fadeIn(200);
        $('.folders span.viewedit').fadeIn(200);
        $('.folders span.move').fadeIn(200);
        $(this).parent().removeClass('onthemove');
        onthemove = 0;
        $('.folders span.target').fadeOut(0);
        $('.folders .selected').children().children().next('span.target').fadeIn(0);
        $('.folders .selected').children().removeClass('exclude');
        $('.page a.delete').fadeIn(0);
        $('.page a.published').fadeIn(0);
        $('.page a.notpublished').fadeIn(0);
        $('.page a.viewedit').fadeIn(0);
        $('.page a.move').fadeIn(0);
        
      } else {
        $('.folders span.delete').fadeOut(200);
        $('.folders span.viewedit').fadeOut(200);
        $('.folders span.move').fadeOut(200);
        $(this).parent().addClass('onthemove');
        onthemove = $(this).attr('id').replace('move','');
        $('.folders span.target').fadeIn(0);
        $('.folders .selected').children().children().next('span.target').fadeOut(0);
        $('.folders .selected').children().addClass('exclude');
        $('.page a.delete').fadeOut(0);
        $('.page a.published').fadeOut(0);
        $('.page a.notpublished').fadeOut(0);
        $('.page a.viewedit').fadeOut(0);
        $('.page a.move').fadeOut(0);
        $(this).fadeIn(0);
      }
    });

    $('.doclist').on( 'click', '.page a.move', function(e){
      e.stopPropagation();
      if(onthemove) {
        $('.folders span.delete').fadeIn(200);
        $('.folders span.delete').fadeIn(200);
        $('.folders span.viewedit').fadeIn(200);
        $('.folders span.move').fadeIn(200);
        $(this).parent().removeClass('onthemove');
        onthemove = 0;
        $('.folders span.target').fadeOut(0);
        $('.folders .selected').children().children().next('span.target').fadeIn(0);
        $('.folders .selected').children().removeClass('exclude');
        $('.page a.delete').fadeIn(0);
        $('.page a.published').fadeIn(0);
        $('.page a.notpublished').fadeIn(0);
        $('.page a.viewedit').fadeIn(0);
        $('.page a.move').fadeIn(0);
        
      } else {
        $('.folders span.delete').fadeOut(200);
        $('.folders span.viewedit').fadeOut(200);
        $('.folders span.move').fadeOut(200);
        $(this).parent().addClass('onthemove');
        onthemove = $(this).attr('id').replace('move','');
        $('.folders span.target').fadeIn(0);
        $('.folders .selected').children().children().next('span.target').fadeOut(0);
        $('.folders .selected').children().addClass('exclude');
        $('.page a.delete').fadeOut(0);
        $('.page a.published').fadeOut(0);
        $('.page a.notpublished').fadeOut(0);
        $('.page a.viewedit').fadeOut(0);
        $('.page a.move').fadeOut(0);
        $(this).fadeIn(0);
      }
    });



    $('a.import').click(function(){
      $(this).next('input').trigger("click");
    });

});

$(document).ready(function(){

  $('a.createpage').on( 'click', function( e ) {
    e.preventDefault();
    $('#newpagetitle').dialog('open');
    $("a.createpage").hide();
    $("a.cancel").show();
  });

  $('#docreatepage').on( 'click', function( e ) {
    e.preventDefault();
    $(".insertpoint span.move").hide();
    $(".insertpoint span.create").show();
    $('.insertpoint').css( 'display', 'block' );
    $('#newpagetitle').css( 'display', 'none' );
    mode = 'create';
  });
  
  $(".pageTree").on("click", ".move", function( e ) {
    e.preventDefault();
    e.stopPropagation();
    
    if( mode != 'move' ) {
      $(this).parent().addClass("onthemove");
      $('.insertpoint').css( 'display', 'block' );
      $(this).parent().parent().prev().hide();
      $(this).parent().parent().next().hide();
      $(this).parent().parent().children('ul').children('li').find('.insertpoint').css( 'display', 'none' );
      $('.onthemove').parent().find('.insertpoint').hide();
      if ( $('.insertpoint').is(':visible') ) {
        $(".insertpoint span.move").show();
        $(".insertpoint span.create").hide();
        $('li .page').addClass('nohover');
        mode = 'move';
        id = $(this).attr('id').replace('movepage-','');
      }
    } else {
    
      newid = $(this).attr('id').replace('movepage-','');
      if( id == newid ) {
        $(this).parent().removeClass("onthemove");
        $('.insertpoint').css( 'display', 'none' );
        $('li .page').removeClass('nohover');
        mode = '';
        id = '';
      } else {
        $(this).parent().addClass("onthemove");
        $('#movepage-'+id).parent().removeClass("onthemove");
        $('.insertpoint').css( 'display', 'block' );
        $(this).parent().parent().prev().hide();
        $(this).parent().parent().next().hide();
        $(this).parent().parent().children('ul').children('li').find('.insertpoint').css( 'display', 'none' );
        id = newid;
      }
    }
  });

  $('.pageTree').on( 'click', '.deletepage', function( e ) {
    e.preventDefault();
    var title = $(this).parent().children('.name').html();
    id = $(this).attr('id').replace('deletepage-','');
    item = $(this).parent();
    
    //hidePageStuff();
    
    $('#deletepagepopup').html( '<p>Are you sure you want to delete \''+title+'\'?</p>' );

  });

  $("#deletepagepopup").dialog({
      resizable: true,
      autoOpen:false,
      modal: true,
      width:400,
      height:170,
      buttons: {
        'Yes': function() {
          $(this).dialog('close');
          if( item ) {
            $.ajax({
              type: "POST",
              url: "../ajax/",
              data: { action: "page-delete", id: id }
            })
            .done(function( msg ) {
              if( msg ) {
                window.location.href = './';
              }
            });
          }
        }, 
        No: function() {
        $(this).dialog('close');
        } 
      }
    });

  $("#deletepostpopup").dialog({
    resizable: true,
    autoOpen:false,
    modal: true,
    width:400,
    height:170,
    buttons: {
      'Yes': function() {
        $(this).dialog('close');
        if( item ) {
          $.ajax({
            type: "POST",
            url: "../ajax/",
            data: { action: "post-delete", id: item }
          })
          .done(function( msg ) {
            if( msg ) {
              var ret = jQuery.parseJSON( msg );
              $('.newslistinner').html( ret.posts );
            }
          });
        }
      }, 
      No: function() {
      $(this).dialog('close');
      } 
    }
  });

  $("#deletemediapopup").dialog({
    resizable: true,
    autoOpen:false,
    modal: true,
    width:400,
    height:170,
    buttons: {
      'Yes': function() {
        $(this).dialog('close');
        if( item ) {
          $.ajax({
            type: "POST",
            url: "../ajax/",
            data: { action: "media-delete", id: item }
          })
          .done(function( msg ) {
            if( msg ) {
              var ret = jQuery.parseJSON( msg );
              $('.medialistinner').html( ret.media );
            }
          });
        }
      }, 
      No: function() {
      $(this).dialog('close');
      } 
    }
  });
  
  $("#deletedocpopup").dialog({
    resizable: true,
    autoOpen:false,
    modal: true,
    width:400,
    height:170,
    buttons: {
      'Yes': function() {
        if( item ) {
          $.ajax({
            type: "POST",
            url: "../ajax/",
            data: { action: "download-delete", id: item }
          })
          .done(function( msg ) {
            if( msg ) {
              var ret = jQuery.parseJSON( msg );
              $('.doclistinner').html( ret.downloads );
            }
          });
          $(this).dialog('close');
        }
      }, 
      No: function() {
      $(this).dialog('close');
      } 
    }
  });

  $("#deletedownloadpopup").dialog({
    resizable: true,
    autoOpen:false,
    modal: true,
    width:400,
    height:170,
    buttons: {
      'Yes': function() {
        $(this).dialog('close');
        if( item ) {
          $.ajax({
            type: "POST",
            url: "../ajax/",
            data: { action: "download-delete", id: item }
          })
          .done(function( msg ) {
            if( msg ) {
              $('.doclistinner').html( msg );
            }
          });
        }
      }, 
      No: function() {
      $(this).dialog('close');
      } 
    }
  });

  $('.pageTree').on( 'click', '.insertpoint', function( e ) {
    e.preventDefault();
    if( mode == 'create' ) {
      $("a.createpage").show();
      $("a.cancel").hide();
      $.ajax({
        type: "POST",
        url: "../ajax/",
        data: { action: "page-create", title: $('#pagetitle').val(), at: $(this).attr('id') }
      })
      .done(function( msg ) {
        var ret = jQuery.parseJSON( msg );
        if( ret.done == 1 ) {
          $('.pageTree .draggable').html( ret.data );
          $('#pagetitle').val('');
          return;
        }
        alert('Error');
      });
    } else {
      mode = '';
      $.ajax({
        type: "POST",
        url: "../ajax/",
        data: { action: "page-move", id: id, at: $(this).attr('id') }
      })
      .done(function( msg ) {
        var ret = jQuery.parseJSON( msg );
        if( ret.done == 1 ) {
          $('.pageTree .draggable').html( ret.data );
          return;
        }
        alert('Error');
      });
    }
  });
  
  $('.pageTree').on( 'click', '.togglepagestatus', function( e ) {
    e.preventDefault();
    $.ajax({
        type: "POST",
        url: "../ajax/",
        data: { action: "page-status", id: $(this).attr('id').replace('statuspage-','') }
      })
      .done(function( msg ) {
        var ret = jQuery.parseJSON( msg );
        if( ret.done == 1 ) {
          $('.pageTree .draggable').html( ret.data );
          return;
        }
        alert('Error');
      });
  });
  
  // Posts
  $('.createpost').on( 'click', function( e ) {
    e.preventDefault();
    $('#newposttitle').dialog('open');
  });
  
  $('.createfolder').on( 'click', function( e ) {
    e.preventDefault();
    $('#newfoldertitle').dialog('open');
  });

  $('.creategroup').on( 'click', function( e ) {
    e.preventDefault();
    $('#newgrouptitle').dialog('open');
  });
  
  $('.folders').on( 'click', '.postfolder', function() {
  
    if( onthemove ) {
      postfolder = $(this).attr('id').replace('folder','');
      if( postfolder == onthemove ) {
        $('span.target').fadeOut(0);
        $('.onthemove').removeClass('onthemove');
        onthemove = 0;
        $('span.delete').fadeIn(200);
        $('span.viewedit').fadeIn(200);
        $('span.move').fadeIn(200);
      } else if( $( '#folder'+onthemove ).parent().has( '#'+$(this).attr('id') ).length >= 1 ) {
        alert('You can\'t move the item there');
      } else {
        $.ajax({
          type: "POST",
          url: "../ajax/",
          data: { action: "post-move", id: onthemove, at: postfolder }
        })
        .done(function( msg ) {
          onthemove = 0;
          if( msg ) {
            var ret = jQuery.parseJSON( msg );
            $('.folders').html( ret.folderTree );
            $('.newslistinner').html( ret.posts );
          } else {
            alert('Error');
          }
        });
      }
    } else {
      $('.folders a').parent().removeClass('selected');
      $(this).parent().addClass('selected');
      postfolder = $(this).attr('id').replace('folder','');
      $.ajax({
        type: "POST",
        url: "../ajax/",
        data: { action: "post-loadByFolder", id: postfolder }
      })
      .done(function( msg ) {
        $('.newslistinner').html( msg );
      });
    }
  });
  
  $('.newslist').on( 'click', '.togglepoststatus', function( e ) {
    e.preventDefault();
    $.ajax({
        type: "POST",
        url: "../ajax/",
        data: { action: "post-status", id: $(this).attr('id').replace('status','') }
      })
      .done(function( msg ) {
        var ret = jQuery.parseJSON( msg );
        if( ret.done == 1 ) {
          $('.newslistinner').html( ret.data );
          return;
        }
        alert('Error');
      });
  });
  
  // Media
  //$('.createpost').on( 'click', function( e ) {
  //  e.preventDefault();
  //  $('#newposttitle').dialog('open');
  //});
  
  $('.createmediafolder').on( 'click', function( e ) {
    e.preventDefault();
    $('#foldertitle').val( $(this).parent().children('.name').html() );
    $('#newmediafoldertitle').dialog('open');
  });
  
  $('.folders').on( 'click', '.mediafolder', function() {
  
    if( onthemove ) {
      mediafolder = $(this).attr('id').replace('folder','');
      $('#fallbackparentid').val( mediafolder );
      if( mediafolder == onthemove ) {
        $('span.target').fadeOut(0);
        $('.onthemove').removeClass('onthemove');
        onthemove = 0;
        $('span.delete').fadeIn(200);
        $('span.viewedit').fadeIn(200);
        $('span.move').fadeIn(200);
      } else if( $( '#folder'+onthemove ).parent().has( '#'+$(this).attr('id') ).length >= 1 ) {
        alert('You can\'t move the item there');
      } else {
        $.ajax({
          type: "POST",
          url: "../ajax/",
          data: { action: "media-move", id: onthemove, at: mediafolder }
        })
        .done(function( msg ) {
          onthemove = 0;
          if( msg ) {
            var ret = jQuery.parseJSON( msg );
            $('.folders').html( ret.folderTree );
            $('.medialistinner').html( ret.media );
          } else {
            alert('Error');
          }
        });
      }
    } else {
      $('.folders a').parent().removeClass('selected');
      $(this).parent().addClass('selected');
      mediafolder = $(this).attr('id').replace('folder','');
      $('#fallbackparentid').val( mediafolder );
      $('.dropzone .dz-success').remove();
      $('.dropzone .dz-error').remove();
      loadMediaByFolder();
    }
  });
  
  // Downloads
  $('.createdownloadfolder').on( 'click', function( e ) {
    e.preventDefault();
    $('#newdownloadfoldertitle').dialog('open');
  });
  
  $('.folders').on( 'click', '.downloadfolder', function() {
  
    if( onthemove ) {
      downloadfolder = $(this).attr('id').replace('folder','');
      $('#fallbackparentid').val( downloadfolder );
      if( downloadfolder == onthemove ) {
        $('span.target').fadeOut(0);
        $('.onthemove').removeClass('onthemove');
        onthemove = 0;
        $('span.delete').fadeIn(200);
        $('span.viewedit').fadeIn(200);
        $('span.move').fadeIn(200);
      } else if( $( '#folder'+onthemove ).parent().has( '#'+$(this).attr('id') ).length >= 1 ) {
        alert('You can\'t move the item there');
      } else {
        $.ajax({
          type: "POST",
          url: "../ajax/",
          data: { action: "download-move", id: onthemove, at: downloadfolder }
        })
        .done(function( msg ) {
          onthemove = 0;
          if( msg ) {
            var ret = jQuery.parseJSON( msg );
            $('.folders').html( ret.folderTree );
            $('.doclistinner').html( ret.downloads );
          } else {
            alert('Error');
          }
        });
      }
    } else {
      $('.folders a').parent().removeClass('selected');
      $(this).parent().addClass('selected');
      downloadfolder = $(this).attr('id').replace('folder','');
      $('#fallbackparentid').val( downloadfolder );
      $.ajax({
        type: "POST",
        url: "../ajax/",
        data: { action: "download-loadByFolder", id: downloadfolder }
      })
      .done(function( msg ) {
        $('.doclistinner').html( msg );
      });
    }
  });
  
  $('.doclist').on( 'click', '.toggledownloadstatus', function( e ) {
    e.preventDefault();
    $.ajax({
        type: "POST",
        url: "../ajax/",
        data: { action: "download-status", id: $(this).attr('id').replace('status','') }
      })
      .done(function( msg ) {
        var ret = jQuery.parseJSON( msg );
        if( ret.done == 1 ) {
          $('.doclistinner').html( ret.data );
          return;
        }
        alert('Error');
      });
  });
  
  $('body').keydown(function(e) {
    if( e.ctrlKey == false && e.altKey == false ) {
      var tag = e.target.tagName.toLowerCase();
      if ( (e.which !== 0) && (tag != 'input') && (tag != 'textarea') && (tag != 'div') && (e.which != 13) && (e.which != 27) && (e.which != 42) && (e.which != 44) && (e.which != 112) && (e.which != 113) && (e.which != 114) && (e.which != 115) && (e.which != 116) && (e.which != 117) && (e.which != 122) && (e.which != 123)) {    
        $('.searchme').fadeIn(200);
        $('.searchme input').focus();
      } else if (e.which == 27){
        $('.searchme').fadeOut(200);
        $('.searchme input').val('');
      }
    }
  });
  
  $('body').keyup(function(e) {
    var search = $('.searchme input').val();
    if( search.length >= 3 ) {
      $.ajax({
        type: "POST",
        url: cmsURL+"ajax/",
        data: { action: "search-adminSearch", search: search }
      })
      .done(function( msg ) {
        if( msg ) {
          $('.results1').html( msg );
        } else {
          $('.results1').html( '<p class="noresults">Sorry, we\'ve not found any content matching ' + search + '</p>' );
        }
      });
    } else if( search.length == 0 ) {
      $('.searchme').fadeOut(200);
      $('.searchme input').val('');
    } else {
      $('.results1').html( '<p class="tooshort">Please enter a longer search term</p>' );
    }
  });
  
  $('.searchme .close').on('click', function(e) {
    e.preventDefault();
    $('.searchme').fadeOut(200);
    $('.searchme input').val('');
  });

      $('input:checkbox').after("<span class='checkbox'></span>");

    $('input:checkbox:checked').each(function(){
      $(this).next('span').addClass("checked");
    });



    $(document).on('change', 'input:checkbox', function(){
      if($(this).is(":checked")) {
        $(this).next('span').addClass("checked");
      } else {
        
        $(this).next('span').removeClass("checked");
      }
    });

    $(document).on('focus', 'input:checkbox', function(){
      $(this).next('span').addClass("focussed");
    });
    $(document).on('blur', 'input:checkbox', function(){
        $(this).next('span').removeClass("focussed");
    });

    $('input#hidefromsitemap:checkbox:checked').each(function(){
      $('input#hidefromnavigation').attr('checked', 'checked');
    });

    $('input:checkbox').on('change', function(){
      if(($('#hidefromnavigation').is(":checked")) && ($('#hidefromsitemap').is(":checked"))) {
        $('input#hidefromnavigation').attr('checked', 'checked');
        $('input#hidefromnavigation').next('span').addClass('checked');
        $('input#hidefromsitemap').attr('checked', 'checked');
        $('input#hidefromsitemap').next('span').addClass('checked');
      }
      else if (($('#hidefromsitemap').is(":checked"))) {
        $('input#hidefromnavigation').attr('checked', 'checked');
        $('input#hidefromnavigation').next('span').addClass('checked');
        $('input#hidefromsitemap').attr('checked', 'checked');
        $('input#hidefromsitemap').next('span').addClass('checked');
      }
      else if ($('#hidefromnavigation').is(":checked")) {
        $('input#hidefromnavigation').attr('checked', 'checked');
        $('input#hidefromnavigation').next('span').addClass('checked');
      }
    });

    $('.security input:checkbox').on('click', function(){
    
      if( $(this).attr('id') == 'security-allUserAccess' ) {
      
        $('.security-group').prop('checked', false); 
      } else {

        $('#security-allUserAccess').prop('checked', !$('.security-group').filter(':checked').length);  
      }
      
    });
  
  $('#imageFolder, .adfimageFolder').on( 'change', function() {
    
    mediafolder = $(this).children(":selected").attr("id").replace('folder','');
    loadMediaByFolder( '', 'selectimage' );
  });
  
  $('#seecmsredirectlink').on("click", function( e ){
      
    e.preventDefault();
    $('#selectseecmsredirectlink').dialog('open');
    $('#selectseecmsredirectlink .adflinks').show();
  });
  
  $("#seecmsremoveredirect").on("click", function( e ){
      
    e.preventDefault();
    $('#redirect').val( '' );
    $('#seecmsredirectlink').html( "Select a link" );
    $('#seecmsremoveredirect').html( "" );
  });
  
  $(".selectseecmsredirectlink").dialog({
      resizable: true,
      autoOpen:false,
      modal: true,
      width:800,
      height:600,
      buttons: {
        'Done': function() {
            if( $('.adflinks .emails').is(":visible") ) {
              selectedItem = 'email-' + $('.adflinks #emaillink').val();
              selectedItemLabel = $('.adflinks #emaillink').val();
            }
          
            if( $('.adflinks .externals').is(":visible") ) {
              selectedItem = 'link-' + $('.adflinks #weblink').val();
              selectedItemLabel = $('.adflinks #weblink').val();
            }
            
            $('#redirect').val( selectedItem );
            $('#seecmsredirectlink').html( 'Redirect to: ' + selectedItemLabel );
            $('#seecmsremoveredirect').html( "Remove redirect" );
            $(this).dialog('close');
        }, 
        Cancel: function() {
          $(this).dialog('close');
        } 
      }
    });
  
  $( "body" ).on('change', ".adfselectLink select", function(){
    $( ".adfselectLink select option:selected").each(function(){
      if($(this).attr("value")=="page"){
        $(this).parent().parent().parent().siblings(".hidden").children("div").fadeOut(0);
        $(this).parent().parent().parent().siblings(".hidden").children(".pages").fadeIn(200);
      }
      else if($(this).attr("value")=="post"){
        $(this).parent().parent().parent().siblings(".hidden").children(".hidden div").fadeOut(0);
        $(this).parent().parent().parent().siblings(".hidden").children(".posts").fadeIn(200);
      }
      else if($(this).attr("value")=="email"){
        $(this).parent().parent().parent().siblings(".hidden").children(".hidden div").fadeOut(0);
        $(this).parent().parent().parent().siblings(".hidden").children(".emails").fadeIn(200);
      }
      else if($(this).attr("value")=="external"){
        $(this).parent().parent().parent().siblings(".hidden").children(".hidden div").fadeOut(0);
        $(this).parent().parent().parent().siblings(".hidden").children(".externals").fadeIn(200);
      }
      else if($(this).attr("value")=="download"){
        $(this).parent().parent().parent().siblings(".hidden").children(".hidden div").fadeOut(0);
        $(this).parent().parent().parent().siblings(".hidden").children(".downloads").fadeIn(200);
      }
  	});
  }).change();

  $(".adflinks").on( 'click', 'a',function(e){
  	e.preventDefault();
    selectedItem = $(this).attr('id');
    selectedItemLabel = $(this).html();
  	$(".finalstep").fadeIn(500);
  	$("html, body").animate({ scrollTop: $(document).height() }, 500);
  });

  $(".adflinks li a").on('click', function(e){
    e.preventDefault();
    if($(this, 'li a').parent().hasClass('folder')) {

    } else {
      $('.selected').removeClass('selected');
      $(this).parent().addClass('selected');
    }
  });
  
  $(".adflinks .pages li a").on('click', function(e){
    e.preventDefault();
    selectedItem = $(this).attr('id');
    selectedItemLabel = $(this).html();
    $('.selected').removeClass('selected');
    $(this).addClass('selected');
  });
  
  $(".adflinks li span.arrow").on('click', function(){
    if($(this).parent().hasClass('expand')) {
      $(this).parent().children('ul').fadeIn(0);
      $(this).parent().addClass('expanded');
      $(this).parent().removeClass('expand');
    }
    else if($(this).parent().hasClass('expanded')) {
      $(this).parent().children('ul').fadeOut(0); 
      $(this).parent().addClass('expand');
      $(this).parent().removeClass('expanded');
    }
    else if($(this).parent().hasClass('downloadexpand')) {
      $(this).parent().children('ul').fadeIn(0);
      $(this).parent().addClass('downloadexpanded');
      $(this).parent().removeClass('downloadexpand');
    }
    else if($(this).parent().hasClass('downloadexpanded')) {
      $(this).parent().children('ul').fadeOut(0); 
      $(this).parent().addClass('downloadexpand');
      $(this).parent().removeClass('downloadexpanded');
    }
  });
  
  $(".adflinks .downloads.folders li.folder").on('click', function(){

    $(this).children('ul').fadeIn(0);
    $(this).addClass('downloadexpanded');

    if($(this).hasClass('downexpanded')) {
      $(this).children('ul').fadeOut(0); 
      $(this).removeClass('downexpanded');
    }
  });
  
  if (typeof(loadMediaByFolder) == 'function') {
    if( !skipInitialMediaLoad ) {
      loadMediaByFolder( '', 'selectimage' );
      loadMediaFolders( '', 'option', 'imageFolder' );
    }
  }
  
  
  $('#seecmspostmedia').on("click", function( e ){
      
    e.preventDefault();
    $('#selectpostimage').dialog('open');
  });
  
  $('#seecmspostremovemedia').on("click", function( e ){
      
    e.preventDefault();
    $('#media_id').val( 0 );
    $('.postthumbnail').html( '' );
  });


  $(".selectpostimage").dialog({
      resizable: true,
      autoOpen:false,
      modal: true,
      width:800,
      height:600,
      buttons: {
        'Done': function() {
            $(this).dialog('close');
        }, 
        Cancel: function() {
          $(this).dialog('close');
        } 
      }
    });  

  $('body').on( 'click', '.selectpostimage .adfimages .image', function( e ) {
    e.preventDefault();
    id = $(this).attr('id').replace('i','');
    $('#media_id').val( id );
    $('.postthumbnail').html( $(this).html() );
    $(".selectpostimage").dialog('close');
  });
  
  $('table').on( 'click', 'a.deactivate', function( e ) {
    var cA = $(this);
    e.preventDefault();
    $.ajax({
        type: "POST",
        url: "../../ajax/",
        data: { action: "websiteUser-deactivate", id: $(this).attr('data-siteuserid') }
      })
      .done(function( msg ) {
        if( msg ) {
          cA.parent().addClass('activate').removeClass('deactivate');
          cA.addClass('activate').removeClass('deactivate');
          cA.attr('data-siteuseractivation', msg);
          cA.attr('title', "Activate user");
        }
      });
  });
  
  $('table').on( 'click', 'a.activate', function( e ) {
    var cA = $(this);
    e.preventDefault();
    $.ajax({
        type: "POST",
        url: "../../ajax/",
        data: { action: "websiteUser-activate", email: $(this).attr('data-siteuseremail'), activation: $(this).attr('data-siteuseractivation') }
      })
      .done(function( msg ) {
        if( msg ) {
          cA.parent().addClass('deactivate').removeClass('activate');
          cA.addClass('deactivate').removeClass('activate');
          cA.attr('data-siteuseractivation','');
          cA.attr('title', "Deactivate user");
        }
      });
  });
  
  $('.selected .postfolder').click();
  
});