/*
 * Copyright 2006 OST-SYSTEMS. All rights reserved.
 */

function createInnerWidget(widget, widgetId) {
  var template = ''
  + '<div id="Border$ID$" class="widgetBorder">'
  + '  <div id="TitleBar$ID$" class="widgetTitleBar">'
  + '    <div class="iconifyIcon">'
  + '      <img width="20" height="20" id="IconifyIcon$ID$" alt="Collapse" title="Collapse Widget" src=""/>'
  + '    </div>'
  + '    <div class="addIcon">'
  + '      <img width="16" height="16" id="AddIcon$ID$" alt="New Instance" src="images/AddWidgets.png"/>'
  + '    </div>'
  + '    <div class="widgetTitle" id="Title$ID$"></div>'
  + '    <div class="networkIcon" id="NetworkIcon$ID$" onclick="widgetNetworkToggle(\'$ID$\')" style="background-image: url(images//network.png);" title="Toggle Network Access"></div>'
  + '    <div class="closeIcon"><img id="CloseIcon$ID$" alt="Close" title="Close Widget" src="images/closeDisabled.png"/></div>'
  + '  </div>'
  + '  <img id="DefaultImage$ID$" class="widgetDefaultImage" src="" />'
  + '  <img id="LoadingImage$ID$" class="widgetLoading" src="images/loading.gif" />'
  + '  <div class="widgetIcon" id="Icon$ID$"></div>'
  + '  <div id="FrameContainer$ID$"></div>'
  + '</div>';
  widget.innerHTML = template.replace(/\$ID\$/g, widgetId);
}  
  
var ICON_WIDTH = 80;

function setupWindow() {
  var widgets = document.getElementById("Widgets");
  //widgets.onmouseout = widgetDragStop;  
}

function addCommand() {
    var select = document.getElementById("WidgetSelector");
    var value =  select.value;
    addNewWidget(value);
}    

function addError(error) {
  widgetContext.addError(error);
}

function clearErrors() {
  widgetContext.clearError();
}

function clearStorage() {
  keyStorage.setStorage(new Array());
}

function addWidget(id, plist, widgetPath) {  
  var imagePath = getHectorPath() + "images/";
  var path = widgetsPath + widgetPath + "/";
  var widgets = document.getElementById("Widgets");
  var widget = document.createElement("div");
  var widgetId = widgetIdPrefix + id;
  widget.className = "widget";
  widget.setAttribute("id", widgetId);
  widget.onmouseover = function() {widgetMouseOver(this);};
  widget.onmouseout = function() {widgetMouseOut(this);};
  widget.onclick = function() {widgetToFront(this);};
  
  if (widgetsHelper.getWidgetValue(id, "top")) {
    widget.style.top = widgetsHelper.getWidgetValue(id, "top");
    widget.style.left = widgetsHelper.getWidgetValue(id, "left");
  }  
  widgets.appendChild(widget);
  
  createInnerWidget(widget, widgetId);
  
  var widgetBorder = document.getElementById("Border" + widgetId);

  var titleBar = document.getElementById("TitleBar" + widgetId);
  titleBar.widgetElement = widget
  titleBar.onmouseover = function() {widgetTitleMouseOver(this.widgetElement);};
  titleBar.onmouseout = function() {widgetTitleMouseOut(this.widgetElement);};
  titleBar.onmousedown = function(event) {
    if (!event) {
      event = window.event;
    }
    widgetDragStart(this.widgetElement, event);
  };

  if (myBrowser.isIFrameHidden) {
    widgetBorder.style.visibility = "visible";
    widgetBorder.style.borderColor = "transparent";
    titleBar.style.visibility = "hidden";
  }

  var iconifyIconImage = document.getElementById("IconifyIcon" + widgetId);
  iconifyIconImage.setAttribute("src", path + "Icon.png");
  iconifyIconImage.widgetElement = widget
  iconifyIconImage.onclick = function() { 
    widgetIconify(this.widgetElement);
  }    

  var addIconImage = document.getElementById("AddIcon" + widgetId);
  addIconImage.widgetPath = widgetPath;
  addIconImage.onclick = function() { 
    addWidgetInstance(this.widgetPath);
  }    

  var title = document.getElementById("Title" + widgetId);

  title.innerHTML = getPListName(plist);

  if (plist.AllowNetworkAccess || plist.AllowFullAccess) {
    widgetsHelper.storeWidgetKey(id, "HasNetworkAccess", true);
    var networkIcon = document.getElementById("NetworkIcon" + widgetId);
    //networkIcon.style.display = "block";
    networkIcon.setAttribute("onclick", 
      "widgetNetworkToggle('" + widgetId + "')");
    var access = widgetsHelper.getWidgetValue(id, "HectorNetworkAccess");
    if (access == null || access == "true") {
      networkIcon.style.backgroundImage = "url(" + imagePath + "/network.png)";
    }
    else {
      networkIcon.style.backgroundImage = "url(" + imagePath + "networkOff.png)";
    }  
  }

  var closeIconImage = document.getElementById("CloseIcon" + widgetId);
  closeIconImage.setAttribute("src", imagePath + "closeDisabled.png");
  closeIconImage.widgetId = widgetId;
  closeIconImage.onclick = function() {
    widgetClose(this.widgetId);
  }  
  closeIconImage.onmouseover = function() {
    this.setAttribute("src", imagePath + "close.png");
  };    
  closeIconImage.onmouseout = function() {
    this.setAttribute("src", imagePath + "closeDisabled.png");
  };    
  closeIconImage.onmousedown = function() {
    this.setAttribute("src", imagePath + "closePressed.png");
  };    
  closeIconImage.onmouseup = function() {
    this.setAttribute("src", imagePath + "close.png");
  };    

  var defaultImage = document.getElementById("DefaultImage" + widgetId);
  defaultImage.onload = function() {    
    addFrame(widget, plist, path, defaultImage, document.getElementById("FrameContainer" + widgetId));
  };
  defaultImage.setAttribute("src", path + "Default.png");
  var defImage = new Image();
  defImage.onerror = function() {
    addFrame(widget, plist, path, defaultImage, widgetBorder);    
  };
  defImage.src = path + "Default.png";
    
  var loadingImage = document.getElementById("LoadingImage" + widgetId);
    
  var icon = document.getElementById("Icon" + widgetId);

  var iconImage = document.createElement("img");
  iconImage.setAttribute("alt", "Restore");
  iconImage.setAttribute("title", "Restore " + getPListName(plist));
  iconImage.setAttribute("src", path + "Icon.png");
  iconImage.widgetId = widgetId;
  iconImage.onclick = function() { 
    widgetRestore(this.widgetId);
  }  
  icon.appendChild(iconImage);
  
  if (iconifyToDock && document.getElementById("Dock")) {
    var dockIcon = document.createElement("div");
    dockIcon.className = "dockIcon";
    dockIcon.setAttribute("id", "DockIcon" + widgetId);
    dockIcon.setAttribute("onmouseover", "dockMouseOver(this)");
    dockIcon.setAttribute("onmouseout", "dockMouseOut(this)");
    document.getElementById("Dock").appendChild(dockIcon);

    var dockIconImage = document.createElement("img");
    dockIconImage.setAttribute("alt", "Restore");
    dockIconImage.setAttribute("title", "Restore " + getPListName(plist));
    dockIconImage.setAttribute("src", path + "Icon.png");
    dockIconImage.widgetId = widgetId;
    dockIconImage.onclick = function() { 
      widgetRestore(this.widgetId);
    }  
    dockIconImage.setAttribute("width", ICON_WIDTH);        
    dockIconImage.setAttribute("height", ICON_WIDTH);        
    dockIcon.appendChild(dockIconImage);

    var dockCloseIconClass = "dockCloseIcon";
    if (myBrowser.isSafari) {
      dockCloseIconClass += " dockCloseIconSafari";
    }
    addCloseIcon(dockIcon, dockCloseIconClass, "DockCloseIcon" + widgetId, 
                 getPListName(plist), "widgetClose('" + widgetId + "')");
  }               
  var icoImage = new Image();
  icoImage.onerror = function() {
    iconifyIconImage.setAttribute("src", imagePath + "defaultIcon.png");
    iconImage.setAttribute("src", imagePath + "defaultIcon.png");
    if (dockIconImage) {
      dockIconImage.setAttribute("src", imagePath + "defaultIcon.png");
    }
  };
  icoImage.src = path + "Icon.png";

  widgetToFront(widget);
}

function addWidgetOld(id, plist, widgetPath) {  
  var imagePath = getHectorPath() + "images/";
  var path = widgetsPath + widgetPath + "/";
  var widgets = document.getElementById("Widgets");
  var widget = document.createElement("div");
  var widgetId = widgetIdPrefix + id;
  widget.className = "widget";
  widget.setAttribute("id", widgetId);
  widget.onmouseover = function() {widgetMouseOver(this);};
  widget.onmouseout = function() {widgetMouseOut(this);};
  widget.onclick = function() {widgetToFront(this);};
  if (widgetsHelper.getWidgetValue(id, "top")) {
    widget.style.top = widgetsHelper.getWidgetValue(id, "top");
    widget.style.left = widgetsHelper.getWidgetValue(id, "left");
  }  
  widgets.appendChild(widget);
  
  var widgetBorder = document.createElement("div");
  widgetBorder.className = "widgetBorder";
  widgetBorder.setAttribute("id", "Border" + widgetId);
  widget.appendChild(widgetBorder);

  var titleBar = document.createElement("div");
  titleBar.className = "widgetTitleBar";
  titleBar.setAttribute("id", "TitleBar" + widgetId);
  titleBar.onmouseover = function() {widgetTitleMouseOver(this.parentNode.parentNode);};
  titleBar.onmouseout = function() {widgetTitleMouseOut(this.parentNode.parentNode);};
  titleBar.onmousedown = function(event) {
    if (!event) {
      event = window.event;
    }
    widgetDragStart(this.parentNode.parentNode, event);
  };
  if (myBrowser.isIE) {
    //titleBar.style.overflow = "visible";
  }
  widgetBorder.appendChild(titleBar);

  if (myBrowser.isIFrameHidden) {
    widgetBorder.style.visibility = "visible";
    widgetBorder.style.borderColor = "transparent";
    titleBar.style.visibility = "hidden";
  }

  var iconifyIcon = document.createElement("div");
  iconifyIcon.className = "iconifyIcon";
  titleBar.appendChild(iconifyIcon);

  var iconifyIconImage = document.createElement("img");
  iconifyIconImage.setAttribute("id", "IconifyIcon" + widgetId);
  iconifyIconImage.setAttribute("alt", "Collapse");
  iconifyIconImage.setAttribute("title", "Collapse Widget");
  iconifyIconImage.setAttribute("src", path + "Icon.png");
  iconifyIconImage.setAttribute("width", "20");
  iconifyIconImage.setAttribute("height", "20");
  iconifyIconImage.onclick = function() { 
    widgetIconify(this.parentNode.parentNode.parentNode.parentNode);
  }    
  iconifyIcon.appendChild(iconifyIconImage);

  var addIcon = document.createElement("div");
  addIcon.className = "addIcon";
  titleBar.appendChild(addIcon);

  var addIconImage = document.createElement("img");
  addIconImage.setAttribute("id", "AddIcon" + widgetId);
  addIconImage.setAttribute("alt", "New Instance");
  addIconImage.setAttribute("title", "Add New " + getPListName(plist));
  addIconImage.setAttribute("src", imagePath + "AddWidgets.png");
  addIconImage.setAttribute("width", "16");
  addIconImage.setAttribute("height", "16");
  addIconImage.widgetPath = widgetPath;
  addIconImage.onclick = function() { 
    addWidgetInstance(this.widgetPath);
  }    
  addIcon.appendChild(addIconImage);

  var title = document.createElement("div");
  title.className = "widgetTitle";
  title.setAttribute("id", "Title" + widgetId);
  titleBar.appendChild(title);

  var titleText = document.createTextNode(getPListName(plist));
  title.appendChild(titleText);

  if (plist.AllowNetworkAccess || plist.AllowFullAccess) {
    widgetsHelper.storeWidgetKey(id, "HasNetworkAccess", true);
    var networkIcon = document.createElement("div");
    networkIcon.className = "networkIcon";
    titleBar.appendChild(networkIcon);
    networkIcon.setAttribute("id", "NetworkIcon" + widgetId);
    networkIcon.setAttribute("onclick", 
      "widgetNetworkToggle('" + widgetId + "')");
    var access = widgetsHelper.getWidgetValue(id, "HectorNetworkAccess");
    if (access == null || access == "true") {
      networkIcon.style.backgroundImage = "url(" + imagePath + "/network.png)";
    }
    else {
      networkIcon.style.backgroundImage = "url(" + imagePath + "networkOff.png)";
    }  
    networkIcon.setAttribute("title", "Toggle Network Access");

    /*
    var networkIconImage = document.createElement("img");
    networkIconImage.setAttribute("id", "NetworkIcon" + widgetId);
    networkIconImage.setAttribute("alt", "Network");
    networkIconImage.setAttribute("title", "Network Access Allowed");
    var access = widgetsHelper.getWidgetValue(id, "HectorNetworkAccess");
    if (access == null || access == "true") {
      networkIconImage.setAttribute("src", "network.png");
    }
    else {
      networkIconImage.setAttribute("src", "networkOff.png");
    }  
    networkIconImage.setAttribute("onclick", 
    "widgetNetworkToggle('" + widgetId + "')");
    networkIcon.appendChild(networkIconImage);
    */
  }

  var closeIcon = document.createElement("div");
  closeIcon.className = "closeIcon";
  titleBar.appendChild(closeIcon);

  var closeIconImage = document.createElement("img");
  closeIconImage.setAttribute("id", "CloseIcon" + widgetId);
  closeIconImage.setAttribute("alt", "Close");
  closeIconImage.setAttribute("title", "Close Widget");
  closeIconImage.setAttribute("src", imagePath + "closeDisabled.png");
  closeIconImage.widgetId = widgetId;
  closeIconImage.onclick = function() {
    widgetClose(this.widgetId);
  }  
  closeIconImage.onmouseover = function() {
    this.setAttribute("src", imagePath + "close.png");
  };    
  closeIconImage.onmouseout = function() {
    this.setAttribute("src", imagePath + "closeDisabled.png");
  };    
  closeIconImage.onmousedown = function() {
    this.setAttribute("src", imagePath + "closePressed.png");
  };    
  closeIconImage.onmouseup = function() {
    this.setAttribute("src", imagePath + "close.png");
  };    
  //iconifyIcon.style.verticalAlign = "middle";
  closeIcon.appendChild(closeIconImage);

  if (window.netscape) {
    //netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
    //netscape.security.PrivilegeManager.enablePrivilege("UniversalFileRead");
  }
  var defaultImage = document.createElement("img");
  defaultImage.className = "widgetDefaultImage";
  defaultImage.setAttribute("id", "DefaultImage" + widgetId);
  defaultImage.onload = function() {
    addFrame(widget, plist, path, defaultImage, widgetBorder);
  };
  defaultImage.setAttribute("src", path + "Default.png");
  var defImage = new Image();
  defImage.onerror = function() {
    addFrame(widget, plist, path, defaultImage, widgetBorder);    
  };
  defImage.src = path + "Default.png";
  widgetBorder.appendChild(defaultImage);  
    
  var loadingImage = document.createElement("img");
  loadingImage.className = "widgetLoading";
  loadingImage.setAttribute("id", "LoadingImage" + widgetId);
  loadingImage.setAttribute("src", imagePath + "loading.gif");
  widgetBorder.appendChild(loadingImage);  
    
  var icon = document.createElement("div");
  icon.className = "widgetIcon";
  icon.setAttribute("id", "Icon" + widgetId);
  widgetBorder.appendChild(icon);

  var iconImage = document.createElement("img");
  iconImage.setAttribute("alt", "Restore");
  iconImage.setAttribute("title", "Restore " + getPListName(plist));
  iconImage.setAttribute("src", path + "Icon.png");
  iconImage.widgetId = widgetId;
  iconImage.onclick = function() { 
    widgetRestore(this.widgetId);
  }  
  icon.appendChild(iconImage);
  
  if (iconifyToDock && document.getElementById("Dock")) {
    var dockIcon = document.createElement("div");
    dockIcon.className = "dockIcon";
    dockIcon.setAttribute("id", "DockIcon" + widgetId);
    dockIcon.setAttribute("onmouseover", "dockMouseOver(this)");
    dockIcon.setAttribute("onmouseout", "dockMouseOut(this)");
    document.getElementById("Dock").appendChild(dockIcon);

    var dockIconImage = document.createElement("img");
    dockIconImage.setAttribute("alt", "Restore");
    dockIconImage.setAttribute("title", "Restore " + getPListName(plist));
    dockIconImage.setAttribute("src", path + "Icon.png");
    dockIconImage.widgetId = widgetId;
    dockIconImage.onclick = function() { 
      widgetRestore(this.widgetId);
    }  
    dockIconImage.setAttribute("width", ICON_WIDTH);        
    dockIconImage.setAttribute("height", ICON_WIDTH);        
    dockIcon.appendChild(dockIconImage);

    var dockCloseIconClass = "dockCloseIcon";
    if (myBrowser.isSafari) {
      dockCloseIconClass += " dockCloseIconSafari";
    }
    addCloseIcon(dockIcon, dockCloseIconClass, "DockCloseIcon" + widgetId, 
                 getPListName(plist), "widgetClose('" + widgetId + "')");
  }               
  var icoImage = new Image();
  icoImage.onerror = function() {
    iconifyIconImage.setAttribute("src", imagePath + "defaultIcon.png");
    iconImage.setAttribute("src", imagePath + "defaultIcon.png");
    if (dockIconImage) {
      dockIconImage.setAttribute("src", imagePath + "defaultIcon.png");
    }
  };
  icoImage.src = path + "Icon.png";

  widgetToFront(widget);
}

function addWidgetInstance(path) {
  addNewWidget(path);
}

function widgetMouseOver(widget) {
  widgetBorderShow(widget);
}

function widgetBorderShow(widget) {
  var border = document.getElementById("Border" + widget.id);
  if (!myBrowser.isIFrameHidden) {
    border.style.visibility='visible';
  }
  else {
    document.getElementById('TitleBar' + widget.id).style.visibility='visible';
    if (border.style.removeProperty) {
      border.style.removeProperty("border-color");// = null;
    }
    else {
      border.style.borderColor = "";
    }  
    border.style.borderColor = "#B9B9B9";
    widgetTitleMouseOver(widget);
  }  
  /*
  if (window.frames["Frame" + widget.id].widget.onfocus) {
    window.frames["Frame" + widget.id].widget.onfocus();
  }
  */
}

function widgetMouseOut(widget) {
  widgetBorderHide(widget);
  //widgetDragStop(widget);
}

function widgetBorderHide(widget) {
  if (widget.id == dragwidget) {
    return;
  }
  widgetTitleMouseOut(widget);
  var border = document.getElementById("Border" + widget.id);//top.document.getElementById("Border" + widget.id);
  if (!myBrowser.isIFrameHidden) {
    border.style.visibility='hidden';
  }
  else {
    document.getElementById('TitleBar' + widget.id).style.visibility='hidden';
    border.style.borderColor = "transparent";
  }  
}

function widgetTitleMouseOver(widget) {
  document.getElementById('CloseIcon'+widget.id).style.visibility='visible';
  document.getElementById('IconifyIcon'+widget.id).style.visibility='visible';
  document.getElementById('AddIcon'+widget.id).style.visibility='visible';
  if (document.getElementById('NetworkIcon'+widget.id) != null) {
    document.getElementById('NetworkIcon'+widget.id).style.visibility='visible';
  }  
  var border = document.getElementById("Border" + widget.id);
  border.className = "widgetBorder widgetBorderHighlight";
  //border.style.background='#E6E6E6';
  //widgetDragInit();
}

function widgetTitleMouseOut(widget) {
  if (widget.id == dragwidget) {
    return;
  }
  document.getElementById('CloseIcon'+widget.id).style.visibility='hidden';
  document.getElementById('IconifyIcon'+widget.id).style.visibility='hidden';
  document.getElementById('AddIcon'+widget.id).style.visibility='hidden';
  if (document.getElementById('NetworkIcon'+widget.id) != null) {
    document.getElementById('NetworkIcon'+widget.id).style.visibility='hidden';
  }  
  var border = document.getElementById("Border" + widget.id);
  border.className = "widgetBorder widgetBorderNormal";
  //border.style.background='transparent';
  //widgetDragHalt();
}

var iconifyToDock = true;
function widgetIconify(widget) {
  //widgetDragStop(widget);
  if (iconifyToDock) {
    if (myBrowser.needsDockHiddenMethod) {
      document.getElementById(widget.id).style.visibility="hidden";//display='none';
    }
    else {  
      document.getElementById(widget.id).style.display='none';
      document.getElementById('Frame' + widget.frameId).style.display='none';
    }  
    document.getElementById('DockIcon' + widget.id).style.display='inline';
  }
  else {
    document.getElementById('TitleBar' + widget.id).style.display='none';
    document.getElementById('Frame' + widget.frameId).style.display='none';
    document.getElementById('Icon' + widget.id).style.display='inline';
  }  
  var w = getWidget(widget);
  if (w && w.onhide) {
    w.onhide();
  }
  var id = getWidgetId(widget);
  widgetsHelper.storeWidgetKey(id, "status", "iconify");  
}

function widgetRestore(widgetId) {
  var widget = document.getElementById(widgetId);
  //widgetDragStop(widget);
  if (iconifyToDock) {
    document.getElementById('DockIcon' + widget.id).style.display='none';
    if (myBrowser.needsDockHiddenMethod) {
      document.getElementById(widget.id).style.visibility="visible";//display='block';
    }
    else {  
      document.getElementById(widget.id).style.display='block';
      document.getElementById('Frame' + widget.frameId).style.display='inline';
    }  
  }
  else {
    document.getElementById('Icon' + widget.id).style.display='none';
    document.getElementById('TitleBar' + widget.id).style.display='block';
    document.getElementById('Frame' + widget.frameId).style.display='inline';
  }
  var w = getWidget(widget);
  if (w.onshow) {
    w.onshow();
  }  
  widgetsHelper.storeWidgetKey(getWidgetId(widget), "status", "normal");  
}

function widgetClose(widgetId) {
  var widget = document.getElementById(widgetId);
  var w = getWidget(widget);
  if (w && w.onremove) {
    w.onremove();
  }
  widget.parentNode.removeChild(widget);
  if (iconifyToDock) {
    var dockIcon = document.getElementById("DockIcon" + widgetId);
    dockIcon.parentNode.removeChild(dockIcon);
  }  
  widgetsHelper.removeWidget(getWidgetId(widget));
}

function hideAllWidgets() {
  var widgets = document.getElementById("Widgets");
  for (var i = 0; i < widgets.childNodes.length; i++) {
  	var w = getWidget(widgets.childNodes[i]);
  	if (w && w.onhide ) {
  	  w.onhide();
  	}
  }
}

function widgetNetworkToggle(widgetId) {
  var imagePath = getHectorPath() + "images/";
  var networkIcon = document.getElementById("NetworkIcon" + widgetId);
  var id = getWidgetId(networkIcon);
  var access = widgetsHelper.getWidgetValue(id, "HectorNetworkAccess");
  if (access == null || access == "true") {
    networkIcon.style.backgroundImage = "url(" + imagePath + "networkOff.png)";
    widgetsHelper.storeWidgetKey(id, "HectorNetworkAccess", "false");
  }
  else {
    networkIcon.style.backgroundImage = "url(" + imagePath + "network.png)";    
    widgetsHelper.storeWidgetKey(id, "HectorNetworkAccess", "true");
  }
}

function getWidgetId(element) {
  var index = element.id.indexOf(widgetIdPrefix);
  return element.id.substring(index + widgetIdPrefix.length);
}

var dockShown = true;
function dockToggle() {
  var dock = document.getElementById("Dock");
  var toggleDock = document.getElementById("ToggleDock");
  if (dockShown) {
    dock.className = "dockHidden";
    dockShown = false;
    toggleDock.className = "toggleDockShow";
    toggleDock.setAttribute("title", "Show Dock");
  }
  else {
    dock.className = "dockShown";
    dockShown = true;
    toggleDock.className = "toggleDockHide";
    toggleDock.setAttribute("title", "Hide Dock");
  }    
}

var favouritesDockShown = true;
function favouritesDockToggle() {
  /*
  var dock = document.getElementById("Dock");
  var toggle = document.getElementById("ToggleFavouritesDock");
  if (dockShown) {
    dock.style.height = "0px";
    dock.style.overflow = "hidden";
    favouritesDockShown = false;
    toggle.setAttribute("src", "images/show.png");
    toggle.setAttribute("title", "Show Dock");
  }
  else {
    dock.style.height = null;
    dock.style.overflow = null;
    favouritesDockShown = true;
    toggle.setAttribute("src", "images/hide.png");
    toggle.setAttribute("title", "Hide Dock");
  } 
  */    
  if (favouritesDockShown) {
    favouritesDockHide();
  }
  else {
    favouritesDockShow();    
  }
}

function favouritesDockShow() {
  var dock = document.getElementById("FavouritesDock");
  var toggle = document.getElementById("ToggleFavouritesDock");
  dock.className = "favouritesDock favouritesDockShown";
  toggle.className = "toggleFavouritesDock toggleFavouritesDockHide";
  favouritesDockShown = true;
  keyStorage.store("desktop", widgetContext.id, "showFavourites", "true");
}

function favouritesDockHide() {
  var dock = document.getElementById("FavouritesDock");
  var toggle = document.getElementById("ToggleFavouritesDock");
  dock.className = "favouritesDock favouritesDockHidden";
  toggle.className = "toggleFavouritesDock toggleFavouritesDockShow";
  favouritesDockShown = false;
  keyStorage.store("desktop", widgetContext.id, "showFavourites", "false");
}

function restoreFavouritesDock() {
  var hideFavourites = keyStorage.get("desktop", widgetContext.id, "showFavourites");
  if (hideFavourites == "false") {
    favouritesDockHide();
  }
  else {
    favouritesDockShow();
  }  
}

//Das Objekt, das gerade bewegt wird.
var dragwidget = null;

// Position, an der das Objekt angeklickt wurde.
var dragx = 0;
var dragy = 0;

function widgetDragStart(widget, event) {
  if (myBrowser.isIE) {
    document.attachEvent ("onmousemove", widgetDrag, true);
    document.attachEvent("onmouseup", widgetDragStop, true);
    document.ondrag = function() {return false;};
    document.onselectstart = function() {return false;};
  }
  else {
    document.addEventListener("mousemove", widgetDrag, true);
    document.addEventListener("mouseup", widgetDragStop, true);
    document.addEventListener("mouseover", widgetDragCapture, true);
    document.addEventListener("mouseout", widgetDragCapture, true);
  }  
  showGlassPane();
  widgetToFront(widget);
  dragwidget = widget.id;
  //addErrorevent.pageY + " " + widget.offsetTop);
  if (event.pageX) {
    dragx = event.pageX - widget.offsetLeft;
    dragy = event.pageY - widget.offsetTop;
  }
  else {
    dragx = event.clientX - widget.offsetLeft;
    dragy = event.clientY - widget.offsetTop;
  }  
  var w = getWidget(widget);
  if (w && w.ondragstart) {
    w.ondragstart();
  }  
  if (event.stopPropagation) {
    event.stopPropagation();
    event.preventDefault();
  }
  if (window.getSelection && window.getSelection() && window.getSelection().removeAllRanges) {
    window.getSelection().removeAllRanges();
  }
  if (!widget.glassPane) {
    var glass = document.createElement("div");
    glass.className = "widgetGlassPane";
    widget.appendChild(glass);
    widget.glassPane = glass;
  }  
}

function widgetDragCapture(event) {
  event.stopPropagation();
  event.preventDefault();
}

function widgetDragStop(event) {
  //Wird aufgerufen, wenn ein Objekt nicht mehr bewegt werden soll.
  if (myBrowser.isIE) {
    if (dragwidget != null) {
      widget = document.getElementById(dragwidget);
      if (window.event.srcElement != widget.glassPane) {
        //return;
      }
    }      
    document.detachEvent("onmousemove", widgetDrag, true);
    document.detachEvent("onmouseup", widgetDragStop, true);
  }
  else {
    document.removeEventListener("mousemove", widgetDrag, true);
    document.removeEventListener("mouseup", widgetDragStop, true);
    document.removeEventListener("mouseover", widgetDragCapture, true);
    document.removeEventListener("mouseout", widgetDragCapture, true);
  }  
  hideGlassPane();
  if (dragwidget != null) {
    widget = document.getElementById(dragwidget);
    widget.removeChild(widget.glassPane);
    widget.glassPane = null;
    dragwidget=null;
    var w = getWidget(widget);
    if (w && w.ondragend) {
      w.ondragend();
    }
    widgetsHelper.storeWidgetKey(getWidgetId(widget), "top", widget.style.top);  
    widgetsHelper.storeWidgetKey(getWidgetId(widget), "left", widget.style.left);  
  }  
  if (myBrowser.isIE) {
  }
  else {  
    event.stopPropagation();
    event.preventDefault();
  }  
  if (window.getSelection && window.getSelection() && window.getSelection().removeAllRanges) {
    window.getSelection().removeAllRanges();
  }
}


function widgetDrag(event) {
  //Wird aufgerufen, wenn die Maus bewegt wird und bewegt bei Bedarf das Objekt.
  var posx = document.all ? window.event.clientX : event.pageX;
  var posy = document.all ? window.event.clientY : event.pageY;
  if(dragwidget != null) {
    widget = document.getElementById(dragwidget);
    if (posx - dragx >= 0) {
      widget.style.left = (posx - dragx) + "px";
    }  
    if (posy - dragy >= 0) {
      widget.style.top = (posy - dragy) + "px";
    }  
  }
  if (myBrowser.isIE) {
    //widgetBorderShow(widget);
    //widgetTitleMouseOver(widget)
  }
  else {  
    event.stopPropagation();
    event.preventDefault();
  }  
}

function widgetResized(frame) {
  //alert("Resized: " + frame.getAttribute("width") + " x " + frame.getAttribute("height"));
}  

/***
  Favourites Dock code
***/

function addFavourite(dockIcon) {
  addNewWidget(dockIcon.path);
}  