var precaching = new Array();
var onloadFunction = new Array();
var loginDialogObj = null;
$(document).ready(function() {

  for(var i = 0; i < onloadFunction.length; i++){
    eval(onloadFunction[i] + "();");
  }
  //naviPic = $('#naviPic').attr("src");
  //checkScroll();
});

/*
function checkScroll(){
  var windowHeight = $(window).height();
  var pageHeight = $('.pageBorder').height() + 30;
  var heightDiff = windowHeight - pageHeight;

  if(heightDiff < 0){
    //alert("scrollbar");
    $('#submenu').get(0).style.position = "fixed";
    $('#submenu').get(0).style.bottom = "57px";
    $('#submenu').get(0).style.position = "absolute";
    $('#submenu').get(0).style.bottom = "5px";
  }
  else{
    //alert("keine scrollbar");
    $('#submenu').get(0).style.position = "absolute";
    $('#submenu').get(0).style.bottom = "5px";
  }
}
*/

function logoutDialog(){
  var dialogHtml = "Wollen Sie sich ausloggen?";
  var $dialog = $('<div></div>')
		.html(dialogHtml)
		.dialog({
			autoOpen: false,
			title: 'Administration',
			modal: true,
			hide: 'explode',
			buttons: {
						"Ja": function() {
							logout();
							$(this).dialog("close");
						},
						"Abbrechen": function() {
							$(this).dialog("close"); 
						}
					},
			resizable: false,
			show: 'fold',
			width: 480
	});
	
	$dialog.dialog("open");
}


function loginDialog(){
  var dialogHtml = "<table><tr><td colspan=\"2\" id=\"message\" style=\"color: red;\"></td></tr><tr><td>User: </td><td><input type=\"text\" name=\"user\" id=\"user\" style=\"border: 1px solid black;\"/></td></tr><tr><td style=\"padding-top: 5px;\">Passwort:&nbsp;</td><td style=\"padding-top: 5px;\"><input type=\"password\" name=\"password\" id=\"password\" style=\"border: 1px solid black;\"/></td></tr></table>";
  var $dialog = $('<div></div>')
		.html(dialogHtml)
		.dialog({
			autoOpen: false,
			title: 'Login',
			modal: true,
			hide: 'explode',
			buttons: {
						"Login": function() {
							login($(this));
						},
						"Abbrechen": function() {
							$(this).dialog("close"); 
						}
					},
			resizable: false,
			show: 'fold',
			width: 480
	});
	
	$dialog.dialog("open");
}

function login(obj){
  var user = $('#user').get(0).value;
  var password = $('#password').get(0).value;
  loginDialogObj = obj;
  
  $.ajax({
    type: "POST",
    url: "/admin/login.php",
    data: {
      user: user,
      password: password
    },
    success: loginResponse
  });
}

function loginResponse(response){
  if(response == "false"){
    $('#message').get(0).innerHTML = "User oder Passwort ist nicht korrekt.";
    $('#user').get(0).value = "";
    $('#password').get(0).value = "";
    $('#user').get(0).focus();
  }
  else{
    loginDialogObj.dialog("close");
    window.location.href = "index.php";
  }
}

function logout(){
  $.ajax({
    type: "POST",
    url: "/admin/logout.php",
    success: logoutResponse
  });
}

function logoutResponse(){
  window.location.href = "index.php";
}
