/*!
 * jQuery Upgrade Browsers Plugin v0.1
 *
 * Copyright (c) 2011 Eduardo Lingán, elingan@gmail.com, http://upgradebrowsers.com
 * 
 * Permission is hereby granted, free of charge, to any
 * person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the
 * Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the
 * Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice
 * shall be included in all copies or substantial portions of
 * the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
 * KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
 * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 * 
 */
(function($) {

    $.upgradebrowsers = function(user_options) {
        var upgradebrowses_html = '<div id="upgradebrowsers">' +
'<div>' +
'<p><span class="upgradebrowsers_message"></span></p>' +
'<a class="upgradebrowsers_chrome" href="http://www.google.com/chrome" target="_blank">chrome</a>' +
'<a class="upgradebrowsers_firefox" href="http://www.mozilla.org/firefox/" target="_blank">firefox</a>' +
'<a class="upgradebrowsers_opera" href="http://www.opera.com/browser/download/" target="_blank">opera</a>' +
'<a class="upgradebrowsers_safari" href="http://www.apple.com/safari/download/" target="_blank">safari</a>' +
'<a class="upgradebrowsers_iexplorer" href="http://windows.microsoft.com/" target="_blank">iexplorer</a>' + '<div class="upgradebrowsers_close"></div>' +
'</div>';

        var options = {
            versions: { chrome: 10, mozilla: 4, opera: 11, safari: 5, msie: 9 },
            messages: { en: "<strong>This is not the latest version of your browser!</strong><br/>Please update it and you can see this website better.", es: "<strong>¡Esta no es la última versión de su navegador!</strong><br/>Por favor, actualice y podrá ver mejor este sitio web.", de: "<strong>Das ist nicht die aktuellste Version Ihres Browsers!</strong><br/>Aktualisieren Sie ihn, um diese Webseite besser zu sehen." }
        };

        $.extend(true, options, user_options);
        var userAgent = navigator.userAgent.toLowerCase();
        $.browser_info = { version: (userAgent.match(/.+(?:ox|on|ie|me)[\/: ]([\d.]+)/) || [])[1], chrome: /chrome/.test(userAgent), mozilla: /mozilla/.test(userAgent) && !/(compatible|webkit)/.test(userAgent), opera: /opera/.test(userAgent), safari: /webkit/.test(userAgent) && !/chrome/.test(userAgent), msie: /msie/.test(userAgent) && !/opera/.test(userAgent) };
        var version = 0;
        $.each($.browser_info, function(key, value) {
            if (key == "version") {
                version = parseInt(value, 10);
            } else {
                if (value) {
                    if (version < options.versions[key]) {
                        $("body").prepend(upgradebrowses_html);
                        var language = (key == "msie") ? navigator.userLanguage.substring(0, 2) : navigator.language.substring(0, 2);
                        var message = (options.messages[language] == undefined) ? options.messages["en"] : options.messages[language];
                        $(".upgradebrowsers_message").html(message);
                        $("#upgradebrowsers").slideDown(150);
                    }
                }
            }
        });
        $(".upgradebrowsers_close").click(function() {
            $("#upgradebrowsers").slideUp(150);
        });
    }
})(jQuery);
