/**
 * @param: extName name of extension
 * @param: extIcon icon for installation manager
 * @param: extUrl url to download extension
 **/

function ExtensionInstaller (extName, extIcon, extUrl) {
    this.element = null;
    this.enabled = false;

    this.opacity = 0;
    this.animateTimer = null;

    this.extensionName = extName;
    this.extensionIcon = extIcon;
    this.extensionUrl = extUrl;
}

ExtensionInstaller.prototype.init = function () {
    this.element = this.createElement();
    document.body.appendChild(this.element);
    this.installElement();
}

ExtensionInstaller.prototype.toggle = function () {
    if (!this.enabled) {
        this.element.style.display = 'block';
        this.fade();
        this.enabled = true;
    } else {
        this.unfade();
        this.enabled = false;
    }
}

ExtensionInstaller.prototype.fade = function () {
    if (this.checkBrowser()) {
        this.animate('fadeIn');
    }
}

ExtensionInstaller.prototype.unfade = function () {
    if (this.checkBrowser()) {
        this.animate('fadeOut');
    }
}

ExtensionInstaller.prototype.animate = function (func) {
    var _self = this;
    this.animateTimer = setInterval(function () {
                _self[func](_self);
            }, 5);
}

ExtensionInstaller.prototype.fadeIn = function (inst) {
    inst.opacity += 10;

    if (inst.opacity < 100) {
        inst.element.style.opacity = inst.opacity / 100;
    } else {
        inst.element.style.opacity = null;
        inst.opacity = 100;
        clearInterval(inst.animateTimer);
    }
}

ExtensionInstaller.prototype.fadeOut = function (inst) {
    inst.opacity -= 10;

    if (inst.opacity > 0) {
        inst.element.style.opacity = inst.opacity / 100;
    } else {
        clearInterval(inst.animateTimer);
        inst.element.style.display = 'none';
        inst.opacity = 0;
    }
}

ExtensionInstaller.prototype.checkBrowser = function () {
    var br = navigator.userAgent;
    return /Firefox/.test(br);
}

ExtensionInstaller.prototype.createElement = function() {

    var bro = $.browser;
    var ver = parseFloat(bro.version);
    
    var _self = this;

    var el = document.createElement('div');
    el.setAttribute('id', 'faded-area');

    var bg = document.createElement('div');
    bg.setAttribute('id', 'faded-area-bg');

    with (el.style) {
        opacity = this.opacity;
    }

    with (bg.style) {
        width = document.body.clientWidth;
        height = document.body.clientHeight;
        opacity = .74;
    }

    var wrapper = document.createElement('div');

    //FF3.x browser
    if (ver < 2)
        wrapper.setAttribute('id', 'firefox-helper-area-wrapper');
    else
        wrapper.setAttribute('id', 'firefox-helper-area-wrapper_FF4');

    var infoEl = document.createElement('div');
    infoEl.setAttribute('id', 'firefox-helper-area');

    infoEl.innerHTML += '<h2>Installing is easy!<a href="#" class="helper-close-button" id="helper-close-button">Close</a></h2>';
    infoEl.innerHTML += '<ol><li>Click Allow</li><li>Click Install Now</li><li>After installing, Restart Firefox</li></ol>';

    el.appendChild(bg);
    wrapper.appendChild(infoEl);
    el.appendChild(wrapper);
    return el;
}

ExtensionInstaller.prototype.install = function () {
    var params = new Object();
    params[this.extensionName] =
        {
            URL: this.extensionUrl,
            IconURL: this.extensionIcon,
            toString: function () { return this.URL; }
        };

    var res = InstallTrigger.install(params);
    if (!res) {
        this.toggle();
    }

    return false; 
}

ExtensionInstaller.prototype.installElement = function () {
    var _self = this;
    var el = document.getElementById('helper-close-button');
    el.addEventListener('click', function () { _self.toggle(); }, false);
    var ell = document.getElementById('download-plugin');
    ell.addEventListener('click', function (evt) { return _self.install(); }, false);
}

