/*remove left-nav-on-xs*/
if (($('#webEditContent.document').length > 0 && $('.ip-title').length > 0) || $('.books .book-head').length > 0) {
    $('.leftnav-sidebar-wrapper').addClass('hidden-xs')
}

function setDocument(mode){
    if (mode == 'small'){
        $('#document').find('td').removeAttr('width');
    }
}

$(function(){
    $('img').each(function(){
        var src = $(this).attr('src')
       // if (src)
       // $(this).attr('src', src.replace(/^\/media\/oecdorg\//, '//www.oecd.org/media/oecdorg'))
    });

    $('.document *').each(function(){
       $(this).css('background-color', $(this).attr('bgcolor'));
    });
	
});


$(".breadcrumb li a").each(function(index) {
    var brd = $(this).text();
    $(".header-menu li a").each(function(index) {
        if ($(this).text() == brd) {
            $(this).closest('li').addClass('selected-menu');
        } else {
            $(this).closest('li').removeClass('selected-menu');
        }
    });
});


$('.lang-btn').click(function() {
    if ($(this).text() == 'English') {
        window.location = $('#lang-url-en a').attr('href');
    } else {
        window.location = $('#lang-url-fr a').attr('href');
    }
});

/*fix bug if there is no list item dont show header*/
$(".also-avaliable-en").each(function(index) {
    if ($(this).find('>ul>li').length == 0) {
        $(this).hide()
    };
});
$(".also-avaliable-fr").each(function(index) {
    if ($(this).find('>ul>li').length == 0) {
        $(this).hide()
    };
});

/*BY TOPIC*/
$(function() {
	$('ul.topicsList li').sort(function(a,b) { return $(a).text().toLowerCase().localeCompare( $(b).text().toLowerCase() ); }).appendTo('ul.topicsList');
	if ( $('.breadcrumb a:last').text() == 'By Country' ) {
		$('ul.topicsList').addClass('ByCountry');
		$('ul.topicsList>li> a').each(function(idx, a) { 
			var $a = $(a);
			$a.before('<span class="flag"></span>');

			var $li = $a.parent();
			//$li.attr('class', '');

			var countryName = $a.text();
			var letter = countryName.charAt(0).toUpperCase();
			$li.addClass(letter); 
			
			countryName = countryName
				.replace(/\s+/g, '')
				.replace(',', '')
				.replace('(', '')
				.replace(')', '')
				.replace('\'', '').toLowerCase();

			$(a).parent().addClass(countryName.charAt(0).toUpperCase() + countryName.slice(1));

		});
		for(var i=65;i<=90;i++) {       
			var letter = (String.fromCharCode(i));
			$('ul.topicsList>li.'+letter+':first').before('<li class="split"><a name="#'+letter+'"><span>'+letter+'</span></a></li>');
		}
		$('ul.topicsList').columnlist({size:3, 'class':'col-md-4 col-sm-6 col-xs-12', incrementClass : 'li_col'});
	} else {
		$('ul.topicsList').addClass('ByTopic');
		$('ul.topicsList').columnlist({size:4, 'class':'col-md-3 col-sm-6 col-xs-12', incrementClass : 'li_col'});	
	}
	$('ul.topicsList').show();

});

/**DOWNLOAD TRACKING**/
$(function() {
  if ( typeof String.prototype.endsWith != 'function' ) {
    String.prototype.endsWith = function( str ) { 
      return this.substring( this.length - str.length, this.length ) === str; 
    }
  };

  $("div.section.container a[href]").each(function() {
    var linkValue = $(this).attr("href").replace(/[#]|\//g, '');
    if ( linkValue.endsWith(".pdf")
     || linkValue.endsWith(".doc")
     || linkValue.endsWith(".docx")
     || linkValue.endsWith(".ppt")
     || linkValue.endsWith(".pptx")
     || linkValue.endsWith(".xls")
     || linkValue.endsWith(".xlsx")) {

      $(this).attr("target", "_blank"); // to avoid race conditions
      $(this).attr("onMouseDown", "javascript:_gaq.push(['_trackPageview', '" + $(this).attr('href')+"']);");

    }

  });
  
  $('a#block-img-link').each(function(){
    if($(this).data("external-link")=="" && $(this).data("section-link")==""){
      $(this).addClass("nohover");
    }else{
      if($(this).data("external-link")!=""){
        $(this).attr('href', $(this).data("external-link"));
      }
      if($(this).data("section-link")!=""){
        $(this).attr('href', $(this).data("section-link"))
      }
    }
  });
  $('a#block-img-link').click(function(event){
    if(!$(event.target).parent().attr('href')){
    	return false;
    }
  });

});

/**POPUP 360**/

  
/**GTM**/
//search keyword event
$(document).on('submit','form.navbar-form',function(){
	var keyword = document.getElementById('search-input').value;
    dataLayer.push({
        "customCategory": "navigation",
        "customAction": "internalSearchTerm",
        "customLabel": keyword,
        "event": "customEvent"})
});

//slide title event
$('div.sp-slide a').mousedown(function() {
  var slideTitle = $(this).html().replace(/<(?:.|\n)*?>/gm, '').replace(/\ /g, '').replace(/(\r\n|\n|\r)/gm, '');
  dataLayer.push({
      "customCategory": "navigation",
      "customAction": "slider",
      "customLabel": slideTitle,
      "event": "customEvent"})
  
});

//download event
$('div.section.container a[href]').click(function() {
    var types = ['pdf', 'doc', 'docx', 'xlsx', 'xls', 'xlsm', 'ppt', 'pptx'];
    var href = $(this).attr("href");
    var hrefArray = href.split('/');
    var filename = hrefArray[hrefArray.length - 1];
    var typeArray = filename.split('.');
    var type = typeArray[typeArray.length - 1];
    if (types.indexOf(type) != -1 && typeArray.length == 2) {
        dataLayer.push({
        "customCategory": "engagement",
        "customAction": "downloadContent",
        "customLabel": type + ' | ' + typeArray[0],
        "event": "customEvent"})
  }
});

$(document).ready(function() {
  var siteName = $(location).attr("host");
  var idx = siteName.indexOf("www.");
  if (idx > -1) siteName = siteName.substring(idx+4);
  
  var path = $(location).attr("pathname");
  var event = "page_view";
  var payload = {
  "siteName" : siteName,
  "siteEnvironment" : "live",
  "is_global" : "yes",
  "is_directorates":"yes",
  "is_satellite":"no",
  "page_current_path" : path,
  "template_name" : "oecd.org",
  };
  var dataLayerPayload = Object.assign({}, payload, { event });
  window.dataLayer.push(dataLayerPayload);

});