/*
 * Copyright 2006 OST-SYSTEMS. All rights reserved.
 */

var myBrowser = findBrowser();

function isSupportedEngine() {
  if (myBrowser) {
    if (myBrowser.isExperimental) {
      /*
      alert("Support for " + myBrowser.name + " browser is still experimental.\nPlease " +
          "report any problems to:\n\n" +
          "developer@widgetop.com");
      */ 
    }
    return myBrowser.supported;
  }
  return false;  
}

function findBrowser() {
  var userAgent = navigator.userAgent;
  userAgent = userAgent.toLowerCase();
  var index = userAgent.indexOf("nokia");
  var index2 = userAgent.indexOf("symbian");
  if (index >= 0 || index2 >= 0) {
    var browser = new SafariBrowser();
    browser.name = "Nokia";
    browser.loadLocale = false;
    browser.staticScripts = true;
    browser.supportsSearchBox = false;
    //browser.supported = false;
    return browser;
  }
  var index = userAgent.indexOf("safari");
  if (index >= 0) {
    var browser = new SafariBrowser();
    if (window.MobileWidget) {
      browser.supportsSearchBox = false;
      browser.supportsSearchBoxPhone = true;
      browser.noMouseOver = 
        (userAgent.toLowerCase().indexOf("iphone") >= 0) 
        || (userAgent.toLowerCase().indexOf("ipod") >= 0);
    }
    return browser;
    //return new UnsupportedBrowser();
  }
  var index = userAgent.indexOf("opera");
  if (index >= 0) {
    var browser = new OperaBrowser();
    if (window.MobileWidget) {
      browser.supported = true;
    }
    return browser;
  }
  index = userAgent.indexOf("firefox");
  if (index >= 0) {
    var version = navigator.userAgent.substring(index + 8, index + 11);
    if (version == null) {
      return new UnsupportedBrowser();
    }
    if (version[0] == '1') {
      return new FirefoxBrowser(version[2] > '4');
    }
    return new FirefoxBrowser(version[0] > '1');
  }
  index = userAgent.indexOf("gecko");
  if (index >= 0) {
    var version = navigator.userAgent.substring(index + 6, index + 14);
    version = parseInt(version);
    var browser = new FirefoxBrowser(version > 20051201);
    index = navigator.userAgent.toLowerCase().indexOf("camino");
    if (index >= 0) {
      browser.name = "Camino";
    }
    else {
      browser.name = "General Gecko";
    }  
    if (browser.supported) {
      browser.isExperimental = false;
    }  
    return browser;
  }
  index = userAgent.indexOf("msie");
  if (index >= 0) {
    var browser = new IeBrowser();
    browser.isIE6 = userAgent.indexOf("msie 6") >= 0;
    //browser.supported = !browser.isIE6;
    return browser;
  }
  return new UnsupportedBrowser();
}

function FirefoxBrowser(supported) {
  this.supported = supported;
  this.isIFrameHidden = false;
  this.needsXMLHttpRequestVariable = false;
  this.supportsSearchBox = false;
  this.supportsListColor = true;
  this.styleInlineBlock = "inline";
  this.loadLocale = true;
  this.isSafari = false;
  this.isMozilla = true;
  this.needsDockHiddenMethod = false;
  this.name = "Firefox";
  
  this.getXMLHttpRequest = function() {
    if (this.hasProxy) {
      return XMLHttpRequestSafari;
    }
    return XMLHttpRequestFirefox;
  }  

  this.getXMLHttpRequestName = function() {
    if (this.hasProxy) {
      return "XMLHttpRequestSafari";
    }
    return "XMLHttpRequestFirefox";
  }  

  this.setInnerText = function(element, text) {
    element.innerHTML = text;
  }

  this.makeCompatible = function(win, doc) {
    try {
      win.Node.prototype.__defineGetter__("innerText", function() {
        return this.innerHTML;
      });
      win.Node.prototype.__defineSetter__("innerText", function(text) {
        this.innerHTML = text;
      });
      Event.prototype.__defineGetter__("x", function() {
        return this.clientX;
      });      
      Event.prototype.__defineGetter__("y", function() {
        return this.clientY;
      });      
      win.Event.prototype.__defineGetter__("x", function() {
        return this.clientX;
      });      
      win.Event.prototype.__defineGetter__("y", function() {
        return this.clientY;
      });
      win.Event.prototype.__defineGetter__("srcElement", function() {
        return this.target;
      });      
      win.CanvasRenderingContext2D.prototype.setFillColor = function(r, g, b, alpha) {
        if (!alpha) {
          alpha = 1.0;
        }
        this.fillColor = "rgba(" + parseInt(r * 255) + "," + parseInt(g * 255) 
                         + "," + parseInt(b*255) + "," + alpha + ")";
      }      
      win.CanvasRenderingContext2D.prototype.setStrokeStyle = function(r, g, b, alpha) {
        if (!alpha) {
          alpha = 1.0;
        }
        this.strokeStyle = "rgba(" + parseInt(r * 255) + "," + parseInt(g * 255) 
                         + "," + parseInt(b*255) + "," + alpha + ")";
      }      
      win.CanvasRenderingContext2D.prototype.setStrokeColor = function(color, alpha) {
        if (!alpha) {
          alpha = 1.0;
        }
      }      
      win.CanvasRenderingContext2D.prototype.setLineWidth = function(width) {
        this.lineWidth = width;
      }      
      win.CanvasRenderingContext2D.prototype.setLineCap = function(cap) {
        this.lineCap = cap;
      }      
    }
    catch (e) {
      alert(e);
    }
  }
}

function SafariBrowser() {
  this.supported = true;
  this.isIFrameHidden = true;
  this.needsXMLHttpRequestVariable = true;
  this.supportsSearchBox = true;
  this.supportsListColor = false;
  this.loadLocale = true;
  this.isSafari = true;
  this.needsDockHiddenMethod = true;
  this.isExperimental = false;
  this.name = "Safari";

  this.getXMLHttpRequest = function() {
    return XMLHttpRequestSafari;
  } 

  this.getXMLHttpRequestName = function() {
    return "XMLHttpRequestSafari";
  }  

  this.setInnerText = function(element, text) {
    element.innerText = text;
  }
}

function OperaBrowser() {
  this.supported = true;
  this.isIFrameHidden = true;
  this.needsXMLHttpRequestVariable = false;
  this.supportsSearchBox = false;
  this.supportsListColor = true;
  this.needsWidgetBackgroundColor = false;
  this.needsAllowTransparency = true;
  this.strictElementsByName = true;
  this.needsAttachEvent = true;
  this.isExperimental = true;
  this.isSafari = false;
  this.isMozilla = false;
  this.isOpera = true;
  this.name = "Opera";

  this.getXMLHttpRequest = function() {
    return XMLHttpRequestSafari;
  } 

  this.getXMLHttpRequestName = function() {
    return "XMLHttpRequestSafari";
  }  

  this.setInnerText = function(element, text) {
    element.innerHTML = text;
  }
}

function IeBrowser() {
  this.supported = false;
  this.isIFrameHidden = false;
  this.needsXMLHttpRequestVariable = false;
  this.supportsSearchBox = false;
  this.supportsListColor = true;
  this.needsWidgetBackgroundColor = false;
  this.needsAllowTransparency = true;
  this.strictElementsByName = true;
  this.needsAttachEvent = true;
  this.styleInlineBlock = "inline";
  this.lateOnLoad = true;
  this.isSafari = false;
  this.isMozilla = false;
  this.isOpera = false;
  this.isIE = true;
  this.isIE6 = false;
  this.name = "Internet Explorer";

  this.getXMLHttpRequest = function() {
    return XMLHttpRequestSafari;
  } 

  this.getXMLHttpRequestName = function() {
    return "XMLHttpRequestSafari";
  }  

  this.setInnerText = function(element, text) {
    element.innerText = text;
  }
  
  this.makeCompatible = function(win, doc) {
    try {
      if (win.Array) {
        win.Array.prototype.push = function(element) {
          this[this.length] = element;
        }
      }  
      doc.defaultView = win;
      doc.defaultView.getComputedStyle = function(element, ignored) {
        return element.currentStyle;
      }
      win.addEventListener = function(signature, method, bubble) {
        win.attachEvent("on" + signature, method);
      }
      doc.addEventListener = function(signature, method, bubble) {
        doc.attachEvent("on" + signature, method);
      }
      win.removeEventListener = function(signature, method, bubble) {
        win.detachEvent("on" + signature, method);
      }
      doc.removeEventListener = function(signature, method, bubble) {
        doc.detachEvent("on" + signature, method);
      }
      win.innerWidth = doc.documentElement.clientWidth;
      win.innerHeight = doc.documentElement.clientHeight;
      for (var i = 0; i < doc.styleSheets.length; i++) {
      	doc.styleSheets[i].cssRules = doc.styleSheets[i].rules;
      }
    }
    catch (e) {
      alert("IE make Compatible: " + e);
    }
  }
}

function UnsupportedBrowser() {
  this.supported = true;
  this.isIFrameHidden = false;
  this.needsXMLHttpRequestVariable = false;
  this.styleInlineBlock = "inline";
  this.supportsSearchBox = false;
  this.isSafari = false;
  this.name = "Unknown";
  
  this.getXMLHttpRequest = function() {
    return XMLHttpRequestSafari;
  } 
  
  this.getXMLHttpRequestName = function() {
    return "XMLHttpRequestSafari";
  }  

  this.setInnerText = function(element, text) {
    element.innerHTML = text;
  }
}      
