﻿// Thanks for doing your part to liber(IE)t the world from IE6!
// Here's to the "Support but Discourage" approach :)
// Original copy written by Jonathan Howard.
// jon@StaringIsPolite.com // twitter.com/staringispolite
//
// GNU LGPL License v3
// SevenUp 0.3 is released into the wild under a GNU LGPL v3
//
// Browser sniffing technique lovingly adapted from http://www.thefutureoftheweb.com/
// Simple CSS Lightbox technique adapted equally lovingly from http://www.emanueleferonato.com/
// Go read their blogs :)
//
// Tanslation & Random browser links added by Mark Creeten
// www.gigadesign.be & www.csssnippets.be

// Constructor technique advocated by Doug Crockford (of LSlint, JSON) in his recent Google tech talk.
var sevenUp = function () {
    // Define 'private vars' here.
    var userLang = (navigator.language) ? navigator.language : navigator.userLanguage;
    var userLang = userLang.toLowerCase();
    var osSupportsUpgrade = /(Windows NT 5.1|Windows NT 6.0|Windows NT 6.1|)/i.test(navigator.userAgent); // XP, Vista, Win7
    var locale = navigator.browserLanguage;
    var options = {  // Change these to fit your color scheme via the 'options' arg for test().
        enableClosing: false,
        enableQuitBuggingMe: false,
        overlayColor: "#000",
        lightboxColor: "#fff",
        borderColor: "#d02424",
        imageBorderColor: "#fff",
        borderWeight: "3px",

        url1: "http://www.microsoft.com/windows/Internet-explorer/default.aspx",
        url2: "http://www.mozilla.com/firefox/",
        url3: "http://www.apple.com/safari/download/",
        url4: "http://www.opera.com/download/",
        url5: "http://www.google.com/chrome",
        url6: "http://phoenixprods.com//phoenix-ie6.aspx",
        url1image: "/images/browsers/browser_ie.gif",
        url2image: "/images/browsers/browser_firefox.gif",
        url3image: "/images/browsers/browser_safari.gif",
        url4image: "/images/browsers/browser_opera.gif",
        url5image: "/images/browsers/browser_chrome.gif",

        url6image: "/masterpages/images/Phoenix/logo.gif",
        UpgradeTitle1EN: "Your Internet Explorer is out of date",

        UpgradeTitle2EN: "Why should I upgrade?",

        UpgradeMsgEN: "To get the best possible experience using our website we recommend that you upgrade to a newer version or other web browser. A list of the most popular web browsers can be found below. Just click on the icons to get to the download page  <br/> <b>or click on the All Parts eXpress icon to be taken to our page designed for older browsers</b>",

        WhyEN: "<ul style='font-size: 15px;margin-top:0;margin-left:25px;padding-left:0;'><li><b>Websites load faster</b>, often double the speed of this older version</li><li><b>Websites look better</b>, so you see sites they way they were intended</li><li><b>Tabs</b> let you view multiple sites in one window</li><li><b>Safer browsing</b> with phishing protection</li></ul>",

        CloseEN: "Close",
        CloseNL: "Sluiten",
        CloseFR: "Fermer",
        QuitEN: "Stop bugging me <em>(2 days)</em>",
        QuitNL: "Laat mij met rust <em>(2 dagen)</em>",
        QuitFR: "Laissez-moi tranquille <em>(2 jours)</em>",
        overrideLightbox: false,
        lightboxHTML: null,
        showToAllBrowsers: false,
        usePlugin: false
    };
    function mergeInOptions(newOptions) {
        if (newOptions) {
            for (var i in options) {
                if (newOptions[i] !== undefined) {
                    options[i] = newOptions[i];
                }
            }
        }
    }
    function browserDimension(t) {
        var viewportwidth = 0;
        var viewportheight = 0;
        if (typeof window.innerWidth != 'undefined') {
            viewportwidth = window.innerWidth;
            viewportheight = window.innerHeight;
        }
        else if (typeof document.documentElement != 'undefined'
     && typeof document.documentElement.clientWidth !=
     'undefined' && document.documentElement.clientWidth != 0) {
            viewportwidth = document.documentElement.clientWidth;
            viewportheight = document.documentElement.clientHeight;
        }
        if (t == 'h') {
            return viewportheight;
        }
        else {
            return viewportwidth;
        }
    }
    function writeMessage() {
        if (userLang.indexOf("nl") != -1) { return options.UpgradeMsgNL; }
        if (userLang.indexOf("en") != -1) { return options.UpgradeMsgEN; }
        if (userLang.indexOf("fr") != -1) { return options.UpgradeMsgFR; }
    }
    function writeTitle1() {
        if (userLang.indexOf("nl") != -1) { return options.UpgradeTitle1NL; }
        if (userLang.indexOf("en") != -1) { return options.UpgradeTitle1EN; }
        if (userLang.indexOf("fr") != -1) { return options.UpgradeTitle1FR; }
    }
    function writeTitle2() {
        if (userLang.indexOf("nl") != -1) { return options.UpgradeTitle2NL; }
        if (userLang.indexOf("en") != -1) { return options.UpgradeTitle2EN; }
        if (userLang.indexOf("fr") != -1) { return options.UpgradeTitle2FR; }
    }
    function writeWhy() {
        if (userLang.indexOf("nl") != -1) { return options.WhyNL; }
        if (userLang.indexOf("en") != -1) { return options.WhyEN; }
        if (userLang.indexOf("fr") != -1) { return options.WhyFR; }
    }
    function writeClose() {
        if (userLang.indexOf("nl") != -1) { return options.CloseNL; }
        if (userLang.indexOf("en") != -1) { return options.CloseEN; }
        if (userLang.indexOf("fr") != -1) { return options.CloseFR; }
    }
    function writeQuit() {
        if (userLang.indexOf("nl") != -1) { return options.QuitNL; }
        if (userLang.indexOf("en") != -1) { return options.QuitEN; }
        if (userLang.indexOf("fr") != -1) { return options.QuitFR; }
    }
    function browsersLinks() {
        var myLinks = new Array();
        myLinks[0] = "<a style='border: none;float:left;margin-right:15px;' title='Download Internet Explorer' href='" + options.url1 + "'><img alt='Download Internet Explorer' style='border: 1px solid " + options.imageBorderColor + ";' src='" + options.url1image + "' /></a>";
        myLinks[1] = "<a style='border:none;float:left;margin-right:15px;' title='Download FireFox' href='" + options.url2 + "'><img alt='Download FireFox' style='border: 1px solid " + options.imageBorderColor + ";' src='" + options.url2image + "' /></a>";
        myLinks[2] = "<a style='border:none;float:left;margin-right:15px;' title='Download Safari' href='" + options.url3 + "'><img alt='Download Safari' style='border: 1px solid " + options.imageBorderColor + ";' src='" + options.url3image + "' /></a>";
        myLinks[3] = "<a style='border:none;float:left;margin-right:15px;' title='Download Opera' href='" + options.url4 + "'><img alt='Download Opera' style='border: 1px solid " + options.imageBorderColor + ";' src='" + options.url4image + "' /></a>";
        myLinks[4] = "<a style='border:none;float:left;margin-right:15px;' title='Download Chrome' href='" + options.url5 + "'><img alt='Download Chrome' style='border: 1px solid " + options.imageBorderColor + ";' src='" + options.url5image + "' /></a>";
        myLinks[5] = "<a style='border:none;float:left;margin-right:15px; ' title='Ring Power' href='" + options.url6 + "'><img alt='IE Page' style='border: 1px solid " + options.imageBorderColor + ";' src='" + options.url6image + "' /></a>";

        return myLinks.join('');
    }

    function isCookieSet() {
        if (document.cookie.length > 0) {
            var i = document.cookie.indexOf("sevenup=");
            return (i != -1);
        }
        return false;
    }

    // Return object literal and public methods here.
    return {
        // Hate to define CSS this way, but trying to keep to one file.
        // I'll keep it as pretty as possible.
        overlayCSS: function () {
            return "display: block; position: absolute; top: 0; left: 0; bottom:0;" +
      "width:" + browserDimension() + "px; height: " + browserDimension('') + "px; background-color: " + options.overlayColor + "; " +
      "opacity: 0.8;filter: alpha(opacity = 80);z-index:1001;";
        },
        lightboxCSS: function () {
            return "display: block; position: absolute; top:5%; left: 25%; width: 50%; " +
      "padding: 16px; border: " + options.borderWeight + " solid " + options.borderColor + "; " +
      "background-color:" + options.lightboxColor + "; " +
      "z-index:1002; overflow: hidden;";
        },
        lightboxContents: function () {
            var html = options.lightboxHTML;
            if (!html) {
                html =
        "<div style='width: 100%; height: 95%;font-family: Arial,Verdana;line-height: 1.2;'>" +
          "<h2 style='margin-top:0;font-size: 24px;color:" + options.borderColor + ";'>" + writeTitle1() + "</h2>" +
          "<p class='upgrade_msg' style='font-size: 15px;margin-top:0;'>" + writeMessage() + "</p>" +
          "<p style='overflow:hidden;margin-top:0;margin-bottom:15px;height:1%;'>" +
          browsersLinks() +
          "</p>" +
          "<br style='clear:left;'>" +
          "<h3 style='font-size:16px;margin-top:0;color:" + options.borderColor + ";'>" + writeTitle2() + "</h3>" +
          writeWhy() +
        "</div>";
                if (options.enableClosing) {
                    html += "<p style='font-size: 15px; text-align: right;margin-top:0;margin-bottom: 8px;'>";
                    html += options.enableQuitBuggingMe ?
          ("<a href='#' onclick='sevenUp.quitBuggingMe();' " +
              "style='color:" + options.borderColor + "'>" +
              "<b>" + writeQuit() + "</b>" +
          "</a>") :
          ("<a href='#' title='" + writeClose() + "' onclick='sevenUp.close();' " +
              "style='color:" + options.borderColor + "'>" +
              "<b>" + writeClose() + "</b>" +
            "</a>");
                    html += "</p>";
                }
            }
            return html;
        },
        test: function (newOptions, callback) {
            mergeInOptions(newOptions);
            if (!isCookieSet()) {
                // Write layer into the document.
                var layerHTML = "<div id='sevenUpCallbackSignal'></div>";
                if (options.overrideLightbox) {
                    layerHTML += options.lightboxHTML;
                } else {
                    layerHTML += "<div id='sevenUpOverlay' style='" + this.overlayCSS() + "'>" +
  	        "</div>" +
            "<div id='sevenUpLightbox' style='" + this.lightboxCSS() + "'>" +
              this.lightboxContents() +
            "</div>";
                }
                if (options.showToAllBrowsers !== true) {
                    //layerHTML = "<!--[if lte IE 7]>" + layerHTML + "<![endif]-->"; // <= Only ie7 & ie6
                    layerHTML = "<!--[if lt IE 7]>" + layerHTML + "<![endif]-->"; // <= Uncomment if only for ie6
                }
                var layer = document.createElement('div');
                layer.innerHTML = layerHTML;
                document.body.appendChild(layer);
                // Fire callback.
                // I don't like this hack but IE6 seems to restrict dynamically created <script> tags to <head> only, 
                // and I don't see a way to add conditional comments around the script tag or its contents.
                // So for now, we write a 'signal' div inside the CC. If anyone has a better way please let me know.
                if (callback && document.getElementById('sevenUpCallbackSignal')) {
                    callback(options);
                }
            }
        },
        quitBuggingMe: function () {
            var exp = new Date();
            exp.setTime(exp.getTime() + (2 * 24 * 3600000)); // 2days
            document.cookie = "sevenup=dontbugme; expires=" + exp.toUTCString();
            this.close();
        },
        close: function () {
            var overlay = document.getElementById('sevenUpOverlay');
            var lightbox = document.getElementById('sevenUpLightbox');
            if (overlay) { overlay.style.display = 'none'; }
            if (lightbox) { lightbox.style.display = 'none'; }
        },
        plugin: {}
    };
} ();


