// -*- mode: javascript; coding: utf-8 -*-
//
// Copyright 2011 Andrej A Antonov <polymorphm@gmail.com>.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

(function () {
    'use strict'
    
    var $ = jQuery
    
    var msie_detect_module = {}
    
    msie_detect_module.undefined_safe_new = function (factory) {
        if (factory !== undefined) {
            var obj = new factory()
            
            return obj
        }
    }
    
    msie_detect_module.event_test_msie = function () {
        if (window.addEventListener === undefined &&
                window.attachEvent !== undefined) {
            return true
        }
        
        return false
    }
    
    msie_detect_module.xhr_test_msie = function () {
        var xhr = msie_detect_module.undefined_safe_new(window.XMLHttpRequest)
        var xdr = msie_detect_module.undefined_safe_new(window.XDomainRequest)
        
        if ((xhr === undefined || xhr.responseType === undefined) &&
                xdr !== undefined) {
            return true
        }
        
        return false
    }
    
    msie_detect_module.msie_detect = function (before_version) {
        // детектирование 'Microsoft IE' по характерным несоответствиям стандартам
        //
        // 'before_version' -- это версия 'Microsoft IE' которая нас устраивает,
        //      тоесть которую (включительно и выше) мы тестировать уже не будем
        
        // детектирование версий: [6, 0], [7, 0], [9, 0]
        if (before_version[0] > 6 && msie_detect_module.event_test_msie()) {
            return true
        }
        
        // детектирование до версий: [9, 0], <..и..может..чуть..выше..>
        if (before_version[0] > 9 && msie_detect_module.xhr_test_msie()) {
            return true
        }
        
        // <ЗДЕСЬ> в будущем возможно будет детектирование других версий
        
        return false
    }
    
    window.msie_detect_module = msie_detect_module
})()

