/*
 * Copyright 2006 OST-SYSTEMS. All rights reserved.
 */

var loginObj;
var started = false;
var StorageClass = KeyStorage;

function setup() {
  if (!isSupportedEngine()) {
    var search = location.search;
    if (!search || search.indexOf("ForceBrowser") < 0) {     
      window.open("unsupported.html", "_top");
      return;
    }  
  }
  widgetsHelper = new WidgetsHelper();
  widgetContext = new WidgetContext("dashboard");
  widgetContext.showErrorPage(false);  
  widgetsPath = getPath();
  sendServerTest(onServerTest);
}

function start() {
  widgetContext.restoreBackground();
  setupWidgets();
  reloadWidgets();
  $("Board").style.display = "block";
  $("StartupLoading").style.display = "none";
  Desktop.hideBusyGlassPane();
  started = true;
}

Desktop = function() {  
}

Desktop.getParameter = function (src, key, defaultValue) {
  if (src.indexOf("&amp;") >= 0) {
    src = src.replace(/\&amp\;/g, "&");
  }
  var index = src.indexOf("&" + key + "=");
  if (index < 0) {
    index = src.indexOf("?" + key + "=");
  }
  if (index >= 0) {
    var index2 = src.indexOf("&", index + 1);
    if (index2 >= 0) {
      return Desktop.decode(src.substring(index + key.length + 2, index2));
    }
    return Desktop.decode(src.substring(index + key.length + 2));
  }
  return defaultValue;   
}

Desktop.decode = function (value) {
  var result = decodeURIComponent(value);
  return result.replace(/(\+)/g, " ");
}

/*
 * Busy Glasspane functions
 */
 
Desktop.showBusyGlassPane = function(title, content, hideLoading) {
  if (hideLoading) {
    $("BusyLoading").style.display = "none";
  }
  else {
    $("BusyLoading").style.display = "block";    
  }
  $("BusyTitle").innerHTML = title;
  $("BusyContent").innerHTML = content;
  var pane = $("BusyGlassPane");
  if (!myBrowser.isIE) {
    pane.style.width = window.innerWidth + "px";
    pane.style.height = window.innerHeight + "px";
  }
  else {
    pane.style.width = document.body.clientWidth + "px";
    pane.style.height = document.body.clientHeight + "px";
  }
  var background = $("BusyBackground");
  var opacity = 0.1;
  background.style.opacity = "" + opacity;
  var darken = function() {
    opacity += 0.05;
    background.style.opacity = opacity + "";
    if (opacity < 0.6) {
      setTimeout(darken, 50);  
    }
  }
  setTimeout(darken, 50);
  pane.style.height="5000px";  
  pane.style.display = "block"; 
  window.scrollTo(0, 0); 
}

Desktop.hideBusyGlassPane = function() {
  $("BusyGlassPane").style.display = "none";
}

Desktop.reshowBusyGlassPane = function() {
  $("BusyGlassPane").style.display = "block";  
} 

function onServerTest(request) {
  keyStorage = new StorageClass();
  if (request.status == 200) {
    myBrowser.hasProxy = true;
    dir = new ServerDir("commands", $("ControlsLoading"));
    var page = Desktop.getParameter(location.search, "page");
    var password = Desktop.getParameter(location.search, "password");
    if (page) {
      Sharing.loadPage(pageLoaded, "widgets", page, password);
    }
    else {
      afterReload(null, false);
    }
  }
  else {
    Desktop.hideBusyGlassPane();
    Desktop.showBusyGlassPane("Error", "No connection to server", true);
  }
}

function pageLoaded(page, result, error) {
  if (page) {
    Desktop.page = page;
    document.title = "Widgetop Shared Desktop - " + Pages.escape(page.title);
    $("HeaderTitleText").innerHTML = document.title;          
  }
  if (result != null) {
    widgetsHelper.clearList();
    keyStorage = new StorageClass();
    for (var i = 0; i < result.length; i++) {
      keyStorage.store(result[i][0], result[i][1], result[i][2], result[i][3]);          
    }
    afterReload(error, true);      
  }
  else {  
    afterReload(error, false);      
  }    
}

function afterReload(error, success) {  
  widgetContext.restoreBackground();
  favourites = new CookieFavourites(dir, favouritesLoaded);
  if (error == null && keyStorage.get("widgets", "list", "all") == null) {
    if (!success) {
      addDefaultWidgets();
    }   
  }      
  Desktop.hideBusyGlassPane();
  if (!started) {
    start();
  }
  else {
    reloadWidgets();
  }
  if (error) {
    Desktop.showBusyGlassPane("Error", "<center>" + error + "</center>", true);
  }
  if (!error && !success) {
    showPages();    
  }  
}

function addDefaultWidgets() {
  if (startupWidgets) {
    for (var i = 0; i < startupWidgets.length; i++) {      
      var id = widgetsHelper.addWidget(startupWidgets[i].title, startupWidgets[i].path);
      widgetsHelper.storeWidgetKey(id, "top", startupWidgets[i].top + "px");  
      widgetsHelper.storeWidgetKey(id, "left", startupWidgets[i].left + "px");
      widgetsHelper.storeWidgetKey(id, "type", startupWidgets[i].type);
      if (startupWidgets[i].settings) {
        var settings = startupWidgets[i].settings;
        for (var property in settings) {
          var key = property;
          if (key.indexOf("#ID") >= 0) {
            key  = key.replace(/\#ID/, id);
          }
          keyStorage.store("widget", startupWidgets[i].type, key, settings[property]);
        }              	
      }
    }
  }
}

function showPageInfo() {
  if (!Desktop.page) {
    alert("No desktop loaded.");
    return;
  }
  var template = $("PageInfoTemplate").innerHTML;
  template = template.replace(/\_/g, "");
  Desktop.showBusyGlassPane(Pages.escape(Desktop.page.title), template, true);
  $("PageInfoDescription").innerHTML = Pages.escape(Desktop.page.description);  
}

function showPages() {
  var template = $("PagesTemplate").innerHTML;
  template = template.replace(/\_/g, "");
  Desktop.showBusyGlassPane("Public Desktops", template, false);
  Pages.currentPage = Desktop.page;
  Pages.loadPagesInfo(function(error) {
    Desktop.hideBusyGlassPane();
    alert(error);
  });
}

function replaceWithPage(page, done) {
  Sharing.loadPage(function(page, result, error) {
    pageLoaded(page, result, error);
    if (!error && !done) {
      Desktop.reshowBusyGlassPane();      
    }
  }, page.desktop, page.path, page.password);  
}

function backToDefault() {
  Pages.backToDefault();
  Desktop.hideBusyGlassPane();
}

function openDesktop() {
  window.open("desktop.html" + location.search, "_top");
}
    