var cur_domain = window.location.hostname;
var doc_referrer = document.referrer;
if(doc_referrer.indexOf('my.mos.ru') + 1) { doc_referrer = ''; }
var protocol = "https:";

function LoadFile(file) {
  var ext = file.match(/[^.]+$/); 
  if(ext == 'css') {
      var link = document.createElement("link");
      link.setAttribute("rel", "stylesheet");
      link.setAttribute("type", "text/css");
      link.setAttribute("href", file);
  }
  else if(ext == 'js') {
      var link = document.createElement("script");
      link.setAttribute("type","text/javascript");
      link.setAttribute("src", file);
  }
  else {
  	  var link = document.createElement("script");
      link.setAttribute("type","text/javascript");
      link.setAttribute("src", file);
  }
	document.getElementsByTagName("head")[0].appendChild(link);
} 

function parseUrlQuery() {
    var data = {};
    if(window.parent.location.search) {
        var pair = (window.parent.location.search.substr(1)).split('&');
        for(var i = 0; i < pair.length; i ++) {
            var param = pair[i].split('=');
            data[param[0]] = param[1];
        }
    }
    return data;
}
var url = parseUrlQuery();

var new_meta = document.createElement('meta');
    new_meta.setAttribute('http-equiv', 'X-UA-Compatible');
    new_meta.setAttribute('content', 'IE=edge,chrome=1');
    
var new_meta2 = document.createElement('meta');
    new_meta2.setAttribute('name', 'viewport');
    new_meta2.setAttribute('content', 'width=device-width, initial-scale=1.0');
    
var new_js = document.createElement('script');
    new_js.setAttribute('src', protocol+'//evp.mos.ru/bitrix/templates/.default/static/js/libs/html5shiv.js');

var main_head = document.getElementsByTagName('head')[0];
    
main_head.appendChild(new_meta);
//main_head.appendChild(new_meta2);
main_head.appendChild(new_js);

var randomVersion = Math.floor((Math.random() * 10000000) + 1);
   
LoadFile(protocol + '//evp.mos.ru/bitrix/templates/.default/static/js/libs/postmessage_and_container.js?v='+randomVersion);
LoadFile(protocol + '//evp.mos.ru/bitrix/templates/.default/static/css/container.css');

function isIE () {
  var myNav = navigator.userAgent.toLowerCase();
  return (myNav.indexOf('msie') != -1) ? parseInt(myNav.split('msie')[1]) : false;
}

if ( isIE () && isIE () < 9 ) {
	LoadFile(protocol + '//evp.mos.ru/bitrix/templates/.default/static/css/ie.css');
}

var add_url = "";
if(url.special == "Y" || url.special == "N") {
 add_url = "&special=" + url.special;
}

function load_header(){
	var header = document.getElementsByTagName('body')[0];
	s = document.createElement("div"),
	s.className = 'top-panel-frame-wrp';
	s.style.height = '55px';
    	s.innerHTML = '<iframe src="' + protocol + '//evp.mos.ru/frame/v2/header.php?site_id=2034&set_lang=ru&langs=ru.&show_blind=1&text=&cur_domain='+cur_domain+'&auth_redirect='+encodeURIComponent(document.location.href)+'&logout_redirect='+encodeURIComponent(document.location.href)+'&ref_parent_url='+encodeURIComponent(doc_referrer)+add_url+'" frameborder="0" width="100%" scrolling="no" id="header_frame" class="top-panel-frame"></iframe>';
	
	var bitrixPanel = document.getElementById('bx-panel');
	
	function insertAfter(elem, refElem) {
	    return refElem.parentNode.insertBefore(elem, refElem.nextSibling);
	}
	
	if ( bitrixPanel ) {
		insertAfter(s, bitrixPanel);
	}
	else {
		header.insertBefore(s, header.firstChild);
	}
	
};

window[ addEventListener ? 'addEventListener' : 'attachEvent' ]( addEventListener ? 'load' : 'onload', load_header );
	
	
function load_footer(){
  var footer = document.getElementsByTagName('body')[0];
  s = document.createElement("div"),
  s.className = 'footer-frame-wrp';
  s.innerHTML = '<iframe src="' + protocol + '//evp.mos.ru/frame/v2/footer.php?site_id=2034&set_lang=ru&hidesitemap=0&hidecounter=0&cur_domain='+cur_domain+'" id="footer_frame" frameborder="0" width="100%" scrolling="no"></iframe>';
  footer.insertBefore(s, null);
}

window[ addEventListener ? 'addEventListener' : 'attachEvent' ]( addEventListener ? 'load' : 'onload', load_footer );
