var languageCode = 'en';

//

if (typeof(Function.prototype.bind) === 'undefined') {
    Function.prototype.bind = function(oThis) {
        if (typeof this !== "function") {
            throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
        }

        var aArgs = Array.prototype.slice.call(arguments, 1), 
            fToBind = this, 
            fNOP = function () {},
            fBound = function () {
                return fToBind.apply(this instanceof fNOP && oThis
                    ? this
                    : oThis,
                    aArgs.concat(Array.prototype.slice.call(arguments)));
            };

        fNOP.prototype = this.prototype;
        fBound.prototype = new fNOP();

        return fBound;
    };
}

if (typeof(Array.prototype.indexOf) === 'undefined') {
    Array.prototype.indexOf = function(elt /*, from*/)
    {
        var len = this.length >>> 0;
        
        var from = Number(arguments[1]) || 0;
        from = (from < 0)
             ? Math.ceil(from)
             : Math.floor(from);
        if (from < 0)
          from += len;
        
        for (; from < len; from++)
        {
          if (from in this &&
              this[from] === elt)
            return from;
        }
        return -1;
    };
}

if (typeof(String.prototype.trim) === 'undefined') {
    String.prototype.trim = function()
    {
        return this.replace(/^(\s+)?|(\s+)?$/g, '');
    };
}

//

window.icrc = window.icrc || {};

icrc.addClass = function(element, className)
{
    className = className.trim().replace(/([ ]+)/g, '-');
    
    var elementClassList = new Array();
    var elementClass = element.getAttribute('class');
    
    // Checks if the class name is already set
    if ((elementClass !== null) && (elementClass !== '')) {
        
        elementClassList = elementClass.trim().replace(/([ ]+)/g, ' ').split(' ');
        
        if (elementClassList.indexOf(className) >= 0) {
            return true;
        }
    }
    
    // Adds the class name to the class name list
    elementClassList.push(className);
    
    // Sets the class attribute
    if (navigator.appVersion.match(/MSIE 7./g) !== null) {
        element.setAttribute('className', elementClassList.join(' '));
    } else {
        element.setAttribute('class', elementClassList.join(' '));
    }
};

icrc.removeClass = function(element, className)
{
    className = className.trim().replace(/([ ]+)/g, '-');
    
    var elementClassList = new Array();
    var elementClass = element.getAttribute('class');
    
    // Checks if the class name is already set
    if ((elementClass === null) || (elementClass === '')) {
        return true;
    }
    
    elementClassList = elementClass.trim().replace(/([ ]+)/g, ' ').split(' ');
    var elementClassListIndex = elementClassList.indexOf(className);
    
    if (elementClassListIndex < 0) {
        return true;
    }
    
    // Adds the class name to the class name list
    elementClassList.splice(elementClassListIndex, 1);
    
    // Sets the class attribute
    if (navigator.appVersion.match(/MSIE 7./g) !== null) {
        element.setAttribute('className', elementClassList.join(' '));
    } else {
        element.setAttribute('class', elementClassList.join(' '));
    }
};

icrc.getCookie = function(c_name)
{
    var i,x,y,ARRcookies=document.cookie.split(';');
    for (i=0; i < ARRcookies.length; i++) {
        x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
        y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
        x=x.replace(/^\s+|\s+$/g,"");
        if (x==c_name)
        {
            return unescape(y);
        }
    }
};

icrc.setCookie = function(c_name,value,exdays, path)
{
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value = escape(value) + ((exdays==null) ? "" : "; expires=" + exdate.toString()) + ((path==null) ? '; path=/' : "; path=" + path);
    document.cookie = c_name + "=" + c_value;
};

//

icrc.Header = function(options)
{
    // Options
    
    if (typeof(options) !== 'undefined') {
        for (var i in this.settings) {
            if (typeof(options[i]) !== 'undefined') {
                this.settings[i] = options[i];
            }
        }
    }
    
    // Styles
    
    var styleElement = document.createElement('style');
    styleElement.setAttribute('type', 'text/css');
    
    var style = '#icrc-header, #icrc-header * { -moz-box-sizing: content-box; box-sizing: content-box; } #icrc-header { display: block; width: 100%; height: 60px; position: absolute; top: 0; left: 0; border-bottom: 3px solid #e32219; background-color: #252525; color: #ffffff; font-size: 11px; transition: all 0.5s ease; z-index: 9999; } #icrc-header[data-language-code="ar"], #icrc-header[data-language-code="fa"], #icrc-header[data-language-code="he"] { direction: rtl; } #icrc-header.type-push { position: relative; } #icrc-header.theme-light { background-color: #ffffff; color: #000000; } #icrc-header.open { top: 0; } #icrc-header.type-push.open { top: auto; margin-top: 0px; } #icrc-header.closed { top: -55px; } #icrc-header.type-push.closed { top: auto; margin-top: -55px; } #icrc-header.closed:hover { top: -50px; } #icrc-header.type-push.closed:hover { top: auto; margin-top: -50px; } #icrc-header #icrc-header-container { width: 960px; height: 60px; margin: 0 auto; position: relative; z-index: 10; border-bottom: 3px solid #e32219; background-color: #252525; } #icrc-header.theme-light #icrc-header-container { background-color: #ffffff; } /* logo */ #icrc-header #icrc-header-logo { display: block; width: 120px; height: 60px; position: absolute; top: 0; left: 30px; background: url(\'//app.icrc.org/api/js/header/sprite.png\') no-repeat; background-position: 0 0; cursor: pointer; } #icrc-header.position-reverse #icrc-header-logo, #icrc-header[data-language-code="ar"] #icrc-header-logo, #icrc-header[data-language-code="fa"] #icrc-header-logo, #icrc-header[data-language-code="he"] #icrc-header-logo { left: auto; right: 30px; } #icrc-header.position-reverse[data-language-code="ar"] #icrc-header-logo, #icrc-header.position-reverse[data-language-code="fa"] #icrc-header-logo, #icrc-header.position-reverse[data-language-code="he"] #icrc-header-logo { right: auto; left: 30px; } /* menu */ #icrc-header #icrc-header-menu { padding: 0; margin: 0; position: absolute; top: 15px; left: 180px; list-style: none; } #icrc-header.position-reverse #icrc-header-menu { left: 20px; } #icrc-header[data-language-code="ar"] #icrc-header-menu, #icrc-header[data-language-code="fa"] #icrc-header-menu, #icrc-header[data-language-code="he"] #icrc-header-menu { left: auto; right: 180px; } #icrc-header.position-reverse[data-language-code="ar"] #icrc-header-menu, #icrc-header.position-reverse[data-language-code="fa"] #icrc-header-menu, #icrc-header.position-reverse[data-language-code="he"] #icrc-header-menu { right: 20px; } #icrc-header #icrc-header-menu li { float: left; } #icrc-header[data-language-code="ar"] #icrc-header-menu li, #icrc-header[data-language-code="fa"] #icrc-header-menu li, #icrc-header[data-language-code="he"] #icrc-header-menu li { float: right; } #icrc-header #icrc-header-menu li a { display: block; height: 30px; padding: 0 20px 0 10px; margin: 0; border-left: 1px solid #e32219; color: #FFFFFF; font-family: \'Roboto\',\'Helvetica\',\'Arial\',sans-serif; font-size: 18px; font-weight: 300; line-height: 30px; text-decoration: none; transition: all 0.5s ease; } #icrc-header[data-language-code="ar"] #icrc-header-menu li a, #icrc-header[data-language-code="fa"] #icrc-header-menu li a, #icrc-header[data-language-code="he"] #icrc-header-menu li a { padding: 0 10px 0 20px; border-left: 0; border-right: 1px solid #e32219; } #icrc-header.theme-light #icrc-header-menu li a { color: #252525; } #icrc-header #icrc-header-menu li a:hover, #icrc-header #icrc-header-menu li a:focus { color: #e32219; } /* donation button */ #icrc-header #icrc-header-donation-button { position: absolute; top: 15px; right: 20px; } #icrc-header.position-reverse #icrc-header-donation-button { right: 180px; } #icrc-header[data-language-code="ar"] #icrc-header-donation-button, #icrc-header[data-language-code="fa"] #icrc-header-donation-button, #icrc-header[data-language-code="he"] #icrc-header-donation-button { right: auto; left: 0px; } #icrc-header.position-reverse[data-language-code="ar"] #icrc-header-donation-button, #icrc-header.position-reverse[data-language-code="fa"] #icrc-header-donation-button, #icrc-header.position-reverse[data-language-code="he"] #icrc-header-donation-button { left: 180px; } #icrc-header #icrc-header-donation-button a { display: block; height: 30px; padding: 0 20px; background-color: #e32219; color: #FFFFFF; font-family: \'Roboto\',\'Helvetica\',\'Arial\',sans-serif; font-size: 18px; font-weight: 300; text-decoration: none; line-height: 30px; transition: all 0.5s ease; } #icrc-header #icrc-header-donation-button a:hover, #icrc-header #icrc-header-donation-button a:focus { background-color: #c11d15; } /* tab */ #icrc-header #icrc-header-tab { display: block; width: 120px; height: 30px; position: absolute; z-index: 10; top: 60px; left: 30px; border-bottom: 3px solid #e32219; background: url(\'//app.icrc.org/api/js/header/sprite.png\') no-repeat; background-position: 0 -60px; background-color: #252525; cursor: pointer; transition: all 0.5s ease; } #icrc-header.theme-light #icrc-header-tab { background-color: #ffffff; background-position: -120px -60px; } #icrc-header[data-language-code="fr"] #icrc-header-tab, #icrc-header[data-language-code="es"] #icrc-header-tab, #icrc-header[data-language-code="it"] #icrc-header-tab { background-position: 0 -90px; } #icrc-header.theme-light[data-language-code="fr"] #icrc-header-tab, #icrc-header.theme-light[data-language-code="es"] #icrc-header-tab, #icrc-header.theme-light[data-language-code="it"] #icrc-header-tab { background-position: -120px -90px; } #icrc-header[data-language-code="pt"] #icrc-header-tab { background-position: 0 -120px; } #icrc-header.theme-light[data-language-code="pt"] #icrc-header-tab { background-position: -120px -120px; } #icrc-header[data-language-code="ru"] #icrc-header-tab { background-position: 0 -150px; } #icrc-header.theme-light[data-language-code="ru"] #icrc-header-tab { background-position: -120px -150px; } #icrc-header[data-language-code="de"] #icrc-header-tab { background-position: 0 -180px; } #icrc-header.theme-light[data-language-code="de"] #icrc-header-tab { background-position: -120px -180px; } #icrc-header.position-reverse #icrc-header-tab, #icrc-header[data-language-code="ar"] #icrc-header-tab, #icrc-header[data-language-code="fa"] #icrc-header-tab, #icrc-header[data-language-code="he"] #icrc-header-tab { left: auto; right: 30px; } #icrc-header.position-reverse[data-language-code="ar"] #icrc-header-tab, #icrc-header.position-reverse[data-language-code="fa"] #icrc-header-tab, #icrc-header.position-reverse[data-language-code="he"] #icrc-header-tab { right: auto; left: 30px; } /* alert */ #icrc-header #icrc-header-alert { display: block; width: 100%; height: 20px; position: absolute; z-index: 9; top: 60px; background-color: #e32219; transition: all 0.5s ease; } #icrc-header #icrc-header-alert.closed { opacity: 0; } #icrc-header #icrc-header-alert.hidden { display: none; } #icrc-header #icrc-header-alert #icrc-header-alert-container { width: 960px; margin: 0 auto; } #icrc-header #icrc-header-alert #icrc-header-alert-container #icrc-header-alert-wrap { width: 760px; margin-left: 170px; position: relative; } #icrc-header[data-language-code="ar"] #icrc-header-alert #icrc-header-alert-container #icrc-header-alert-wrap, #icrc-header[data-language-code="fa"] #icrc-header-alert #icrc-header-alert-container #icrc-header-alert-wrap, #icrc-header[data-language-code="he"] #icrc-header-alert #icrc-header-alert-container #icrc-header-alert-wrap, #icrc-header.position-reverse #icrc-header-alert #icrc-header-alert-container #icrc-header-alert-wrap { margin-left: 30px; margin-right: 170px; } #icrc-header[data-language-code="ar"].position-reverse #icrc-header-alert #icrc-header-alert-container #icrc-header-alert-wrap, #icrc-header[data-language-code="fa"].position-reverse #icrc-header-alert #icrc-header-alert-container #icrc-header-alert-wrap, #icrc-header[data-language-code="he"].position-reverse #icrc-header-alert #icrc-header-alert-container #icrc-header-alert-wrap { margin-left: 170px; margin-right: 30px; } #icrc-header #icrc-header-alert #icrc-header-alert-text { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; line-height: 20px; } #icrc-header #icrc-header-alert #icrc-header-alert-text a { color: #ffffff; text-decoration: none; transition: all 0.5s ease; } #icrc-header #icrc-header-alert #icrc-header-alert-text a:hover, #icrc-header #icrc-header-alert #icrc-header-alert-text a:focus { text-decoration: underline; } #icrc-header #icrc-header-alert #icrc-header-alert-close-button { display: block; width: 20px; height: 20px; position: absolute; top: 0; right: 0; background: url(\'//app.icrc.org/api/js/header/sprite.png\') no-repeat; background-position: -120px 0px; cursor: pointer; } #icrc-header[data-language-code="ar"] #icrc-header-alert #icrc-header-alert-close-button, #icrc-header[data-language-code="fa"] #icrc-header-alert #icrc-header-alert-close-button, #icrc-header[data-language-code="he"] #icrc-header-alert #icrc-header-alert-close-button { right: auto; left: 0; } /* rumble */ #icrc-header.rumble { animation: icrc-header-rumble 1s ease; -webkit-animation: icrc-header-rumble 1s ease; } #icrc-header.type-push.rumble { animation: none; -webkit-animation: none; top: auto; margin-top: -50px; } @keyframes icrc-header-rumble { 0% {transform: translate(0, 0);} 50% {transform: translate(0, 5px);} 100% {transform: translate(0, 0);} } @-webkit-keyframes icrc-header-rumble { 0% {-webkit-transform: translate(0, 0);} 50% {-webkit-transform: translate(0, 5px);} 100% {-webkit-transform: translate(0, 0);} } @media print { #icrc-header { display: none; } } @media (min-width: 1200px) { #icrc-header #icrc-header-container { width: 1200px; } } @media (min-width: 768px) and (max-width: 979px) { #icrc-header #icrc-header-container { width: 724px; } #icrc-header #icrc-header-menu > li { display: none; } #icrc-header #icrc-header-menu > li:first-child, #icrc-header #icrc-header-menu > li:nth-child(2) { display: block; } } @media (max-width: 767px) { #icrc-header #icrc-header-container { width: 100%; } #icrc-header #icrc-header-menu > li { display: none; } #icrc-header #icrc-header-menu > li:first-child { display: block; } } @media (max-width: 480px) { #icrc-header #icrc-header-donation-button { display: none; } }';
    if (styleElement.styleSheet) {
        // < IE8
        styleElement.styleSheet.cssText = style;
        document.getElementsByTagName('head')[0].insertBefore(styleElement, document.getElementsByTagName('head')[0].firstChild);
    } else {
        styleElement.innerHTML = style;
        document.head.insertBefore(styleElement, document.head.firstChild);
    }
    
    // Styles
    
    var styleElement = document.createElement('link');
    styleElement.setAttribute('type', 'text/css');
    styleElement.setAttribute('rel', 'stylesheet');
    styleElement.setAttribute('href', '//fonts.googleapis.com/css?family=Roboto:300');
    styleElement.setAttribute('media', 'all');
    
    document.getElementsByTagName('head')[0].insertBefore(styleElement, document.getElementsByTagName('head')[0].firstChild);
    
    // HTML
    
    this.headerElement = document.createElement('div');
    this.headerElement.setAttribute('id', 'icrc-header');
    icrc.addClass(this.headerElement, 'closed');   
    icrc.addClass(this.headerElement, this.settings.theme);   
    icrc.addClass(this.headerElement, this.settings.position);   
    icrc.addClass(this.headerElement, this.settings.type);
        this.headerElement.setAttribute('data-language-code', languageCode);
    this.headerElement.innerHTML = '<div id="icrc-header-container"> <div id="icrc-header-logo"></div> <ul id="icrc-header-menu"> <li> <a target="_blank" href="https://www.icrc.org/en">icrc.org</a> </li> <li> <a target="_blank" href="https://blogs.icrc.org/">Blogs</a> </li> </ul><div id="icrc-header-donation-button"> <a target="_blank" href="https://www.icrc.org/en/donate">Support us</a> </div> <div id="icrc-header-tab"></div> </div>';
    
    document.getElementsByTagName('body')[0].insertBefore(this.headerElement, document.getElementsByTagName('body')[0].firstChild);
    
    // Elements
    
    this.headerTabElement = document.getElementById('icrc-header-tab');
    this.headerLogoElement = document.getElementById('icrc-header-logo');
    this.alertElement = document.getElementById('icrc-header-alert');
    this.alertCloseButtonElement = document.getElementById('icrc-header-alert-close-button');
    
    // Events
    
    if (typeof(this.headerTabElement.addEventListener) !== 'undefined') {
        this.headerTabElement.addEventListener('click', this.onHeaderTabClick.bind(this));
        this.headerLogoElement.addEventListener('click', this.onHeaderTabClick.bind(this));
    } else {
        this.headerTabElement.attachEvent('onclick', this.onHeaderTabClick.bind(this));
        this.headerLogoElement.attachEvent('onclick', this.onHeaderTabClick.bind(this));
    }
    
    if (this.alertElement !== null) {
        if (typeof(this.headerTabElement.addEventListener) !== 'undefined') {
            this.alertCloseButtonElement.addEventListener('click', this.onHeaderAlertCloseButtonClick.bind(this));
        } else {
            this.alertCloseButtonElement.attachEvent('onclick', this.onHeaderAlertCloseButtonClick.bind(this));
        }
    }
    
    //
    
    var cookie = icrc.getCookie('icrc.header.alertClose');
    if ((this.alertElement !== null) && (cookie !== undefined)) {
        icrc.addClass(this.alertElement, 'hidden');
    }
    
    var cookie = icrc.getCookie('icrc.header.rumble');
    if (cookie === undefined) {
        icrc.setCookie('icrc.header.rumble', true);
        var t = setTimeout(this.rumble.bind(this), 100);
    }
};

icrc.Header.THEME_DARK = 'theme-dark';
icrc.Header.THEME_LIGHT = 'theme-light';
icrc.Header.POSITION_NORMAL = 'position-normal';
icrc.Header.POSITION_REVERSE = 'position-reverse';
icrc.Header.TYPE_OVER = 'type-over';
icrc.Header.TYPE_PUSH = 'type-push';

icrc.Header.prototype.settings = {
    theme: icrc.Header.THEME_DARK,
    position: icrc.Header.POSITION_NORMAL,
    type: icrc.Header.TYPE_OVER
};

icrc.Header.prototype.headerElement;
icrc.Header.prototype.headerTabElement;
icrc.Header.prototype.headerLogoElement;
icrc.Header.prototype.alertElement;
icrc.Header.prototype.alertCloseButtonElement;
icrc.Header.prototype.isClosed = true;


icrc.Header.prototype.onHeaderTabClick = function(event)
{
    if (this.isClosed === true) {
        this.open();
    } else {
        this.close();
    }
};

icrc.Header.prototype.onHeaderAlertCloseButtonClick = function(event)
{
    icrc.setCookie('icrc.header.alertClose', true);
    icrc.addClass(this.alertElement, 'closed');
    var t = setTimeout(this._onCloseAlertTimeout.bind(this), 500);
};

icrc.Header.prototype._onCloseAlertTimeout = function()
{
    icrc.addClass(this.alertElement, 'hidden');
};

icrc.Header.prototype.rumble = function()
{
    if (this.isClosed === true) {
        icrc.addClass(this.headerElement, 'rumble');
        var t = setTimeout(this._onRumbleTimeout.bind(this), 1000);
    }
};

icrc.Header.prototype._onRumbleTimeout = function()
{
    icrc.removeClass(this.headerElement, 'rumble');
};

icrc.Header.prototype.open = function()
{
    icrc.removeClass(this.headerElement, 'closed');
    icrc.addClass(this.headerElement, 'open');
    this.isClosed = false;
};

icrc.Header.prototype.close = function()
{
    icrc.removeClass(this.headerElement, 'open');
    icrc.addClass(this.headerElement, 'closed');
    this.isClosed = true;
};

window.onload = function() {
    new icrc.Header();
};
