/*
 * Copyright 2006 OST-SYSTEMS. All rights reserved.
 */

  function Widget(id, type, parentWindow) {
    this.identifier = id;
    this.type = type;
    this.parentWindow = parentWindow;
    this.interval = null;
    this.opacity = 0;
    this.isFront = true;
    
    this.isHector = true;
    
    /*
    var showhandler;
    
    var onshowSetter = function(param) {
      showhandler = param;
      if (showhandler) {
        showhandler();
      }  
    }
    
    var onshowGetter = function() {
      return showhandler;
    }
    
    if (window.opera) {
      opera.defineMagicVariable("onshow", onshowGetter, onshowSetter);
    }
    else {
      if (this.__defineSetter__) {
        this.__defineSetter__("onshow", onshowSetter);
        this.__defineGetter__("onshow", onshowGetter);
      }
      else {
        addError("Widget: this is IE");
        this.onshow = null;
      }  
    }
    */ 
  }
  
  Widget.prototype.openApplication = function(bundleId) {
    addError("widget.openApplication is not supported");
  }
    
  Widget.prototype.openURL = function(url) {
    if (url) {
      top.window.open(url, "_blank");
    }
  }  
    
  Widget.prototype.preferenceForKey = function(key) {
    var value = keyStorage.get(Widget.prototype.group, this.type, key);
    addError("Retrieve " + key + ": " + value);
    return value;
  }
    
  Widget.prototype.prepareForTransition = function(transition) {
    this.isFront = ("" + transition).toLowerCase() != "toback";
    if (!myBrowser.isIE) {
      this.parentWindow.document.getElementsByTagName("body")[0].style.opacity = "0.0";
    }
    if (this.transitioned) {
      this.transitioned();  
    }  
  }
    
  Widget.prototype.performTransition = function() {
    if (myBrowser.isIE) {
      return;
    }
    var _self = this;
    this.opacity = 0;
    this.interval = this.parentWindow.setInterval(function() {
      _self.opacity += 0.2;
      if (_self.opacity > 1) {
        _self.opacity = 1;
      }
      _self.parentWindow.document.getElementsByTagName("body")[0].style.opacity = _self.opacity + "";
      if (_self.opacity >= 1) {
        _self.parentWindow.clearInterval(_self.interval);
        _self.parentWindow.document.getElementsByTagName("body")[0].style.opacity = "1";
      }
      //do nothing
    }, 50);  
      //do nothing
    }
    
  Widget.prototype.setCloseBoxOffset = function(x, y) {
    }
    
  Widget.prototype.setPreferenceForKey = function(preference, key) {
      addError("Store " + preference + " in " + key);
      keyStorage.store(Widget.prototype.group, this.type, key, preference);
  }
    
  Widget.prototype.system = function(command, endHandler) {
    addError("System is not supported: " + command);
    return new System();
  }
    
  function System() {
    this.outputString = "";
    this.errorString = "System is not supported";
    this.status = -1;
  }

  Widget.prototype.createMenu = function() {
    addError("Create Menu not officially supported.");
    var menu = new Object();
    menu.items = new Array();
    menu.addMenuItem = function(text) {
      addError("Add Menu Item: " + text);
      this.items.push(text);
    }
    menu.popup = function(x, y) {
      if (this.items.length > 0) {
        return 0;
      }
      return null;
    }
    menu.setMenuItemEnabledAtIndex = function(index, enabled) {
      
    }
    return menu;
  }
  
  Widget.prototype.group = "widget";

  Widget.prototype.resizeAndMoveTo = function(x, y, width, height) {
    this.parentWindow.resizeTo(width, height);
  }
