$(document).ready(function()
{
  window.leftMenuTimer = 0;
  $('.leftmenu li:not(.on)').mouseover(function()
  {
    $(this).addClass('on');
  });
  $('.leftmenu li:not(.on)').mouseout(function()
  {
    $(this).removeClass('on');
  });
  $('.leftmenu > li').each(function()
  {
    var li = $(this);
    li.mouseover(function()
    {
      var thisLi = $(this);
      clearTimeout(thisLi.get(0).timer);
      thisLi.find('li:not(.top)').fadeIn(200); 
    });
    li.mouseout(function()
    {
      var thisLi = $(this);
      thisLi.get(0).timer = setTimeout(function()
      {
        thisLi.find('li').fadeOut(1);
      }, 200); 
    });
    li.find('ul').mouseover(function()
    {
      var thisLi = $(this).parent();
      clearTimeout(thisLi.get(0).timer);
    });
  });

  // REDIRECT TO HTTPS
  /*if(/^http:/.test(document.location.href))
    document.location.href = document.location.href.replace(/^http:/, 'https:');*/
    
  // ENABLE QUICKMENU VIEW
  if($('ul.quickmenu-data').length > 0)
    $('ul.quickmenu-data').QuickMenu();

  // HIDE NOT ALLOWED DEPARTMENTS FROM MAIN MENU
  $('li.notauthorized a').addClass('notauthorized');
	$('li.notauthorized').each(function()
	{
    var li = $(this);
    var a = li.children('a:first');
    var department = a.attr('href').replace(/^.*?\/([^\/]+)\/[^\/]*$/, '$1');
    var url = 'scripts/authorized2716.html?username=' + $.cookie('username') + '&department=' + department;
    $.ajax({
      url : url,
      username: $.cookie('username'),
      password: $.cookie('password'),
      success: function(data)
      {
        if(/^authorized/.test(data))
        {
          li.removeClass('notauthorized');
          li.addClass('authorized');
        }
      }
    });
  });

});

// LOGIN FUNCTION
function login(username, password)
{
  $.ajax({
    url: document.location.href.replace(/\/[^\/]*$/, '/access.txt?' + Math.random() * 10000),
    username: username,
    password: password,
    success: function(data)
    {
      if(/^authorized/.test(data))
      {
        $.cookie('username', username);
        $.cookie('password', password);
        if(window.PageTracker)
          window.PageTracker._trackPageview('/gebruiker/' + username);
        document.location.href = (/\.en\.html/.test(document.location.href) ? '/bestuur/algemeen/Home.en.html' : '/bestuur/algemeen/Home.html');
      }
    },
    error: function()
    {
      alert('Uw gebruikersnaam en/of wachtwoord is onjuist!');
    }
  });
}

function logout()
{
  $.cookie('username', 'no-user');
  $.cookie('password', 'no-pass');
  $.cookie('fullname', '');
  var fRedirect = function()
  {
    document.location.href = '/logout';
  };
  $.ajax({
    url: '/logout/access.txt?' + Math.random() * 10000,
    username: 'logoutuser',
    password: 'logoutuser',
    success: fRedirect,
    error: fRedirect
  });
}

function openFolder(id) {
	alert (id);
}

function showFile(currentfolder,name) {
	window.open('/dupontpensioenfonds/files/' + currentfolder + '/' + name);
}
function moveFile(path,type){
	$("#moveForm").show().find("input[name=sourcePath]").val(path).end().find("input[name=type]").val(type);
}
function cancelMove(){
	$("#moveForm").hide();
}
function renameFile(path,filename,type){
	$("#renameForm").show().find("input[name=sourcePath]").val(path).end().find("input[name=type]").val(type).end().find("input[name=fileName]").val(filename);
}
function cancelRename(){
	$("#renameForm").hide();
}
function copyFile(path,type){
	$("#copyForm").show().find("input[name=sourcePath]").val(path).end().find("input[name=type]").val(type);
}
function cancelCopy(){
	$("#copyForm").hide();
}

function remove(type,path){
	var answer = confirm("Weet u zeker dat u dit item wilt verwijderen?");
	
	if (answer){
		document.location.href="?"+type+"="+path;
	}else{
		return false;
	}
	
}

function showOptions(){
	if($("#route-menu li:first").hasClass("noArrow")){
		$("#route-menu li:first").removeClass("noArrow");
		$("#route-menu li:first ul").css("display","none");
	}else{
		$("#route-menu li:first").addClass("noArrow");
		$("#route-menu li:first ul").css("display","block");	
	}
}

