//Methods to get dealer specific topics from hostname START
function getTopicsFromHost(){
  var host = self.location.hostname;
  var path = self.location.pathname;
  var result = null;
  
  if (self.location.search.indexOf("debug") > -1) {
  //  alert(host);
    host = self.location.hostname;
    path = self.location.pathname;
    alert(path);
  //  alert(host);
  }
  // Location in showroom looks like this: 
  // http://ah-funke.bmw.de/sync/showroom/de/de/newvehicles/...
  // http://nl-muenchen.bmw.de/sync/showroom/de/de/newvehicles/...
  // http://www.goteborg.bmw.se/sync/showroom/se/sv/newvehicles
  // http://oslo.stenshagen.bmw.no/sync/showroom/no/no/newvehicles
  
  // domains germany nl_
  if(host.search(/nl-.*\.bmw\..*/) > -1){
    result = host.replace(/nl-(.*)\.bmw\..*/, "nl_$1"); 
  // domains germany ah_
  } else if(host.search(/ah-.*\.bmw\..*/) > -1){
    result = host.replace(/ah-(.*)\.bmw\..*/, "ah_$1"); 
  // domain japan tokyo only
  } else if(host.search(/www\.bmw-tokyo\..*\..*/) > -1){
    result = host.replace(/www\.bmw-tokyo\..*\..*/, "tokyo"); 
  // domain japan osaka only
  } else if(host.search(/www\.bmw-osaka\..*\..*/) > -1){
    result = host.replace(/www\.bmw-osaka\..*\..*/, "osaka"); 
  // domains japan standard
  } else if(host.search(/www\.(.*)\.bmw\..*\..*/) > -1){
    result = host.replace(/www\.(.*)\.bmw\..*\..*/, "$1"); 
  // domains standard
  } else if(host.search(/www\.(.*)\.bmw\..*/) > -1){
    result = host.replace(/www\.(.*)\.bmw\..*/, "$1"); 
  // domains norway
  } else if(host.search(/(.*)\.(.*)\.bmw\..*/) > -1){
    result = host.replace(/(.*)\.(.*)\.bmw\..*/, "$1"+"-"+"$2"); 
  // domains standard w/o www
  } else if(host.search(/(.*)\.bmw\..*/) > -1){
    result = host.replace(/(.*)\.bmw\..*/, "$1"); 
  // domains japan specific
  } else if(host.search(/www\.(.*)\.co\.jp/) > -1){
    result = host.replace(/www\.(.*)\.co\.jp/, "$1"); 
  // domains indian specific
  } else if(host.search(/www\.bmw-(.*)\.in/) > -1){
    result = host.replace(/www\.bmw-(.*)\.in/, "$1"); 
  // domains rest specific
  } else if(host.search(/www\.(.*)\..*/) > -1){
    result = host.replace(/www\.(.*)\..*/, "$1"); 
  }
  
  result = path.replace(/\/sync\/showroom_rebrush\/(.*?)\/(.*?)\/.*/,"$1/"+result+"/$2");
  if (self.location.hostname.indexOf("liintra")>-1) {
    result = "se/dealer_master/sv";
  }
  if (self.location.hostname.indexOf("li0412")>-1) {
    result = "de/ah_mustermann/de";
  }
  if (self.location.hostname.indexOf("ithonl")>-1) {
    result = "de/ah_mustermann/de";
  }
  return result.split('/');
}

function getCountryTopicFromHost(){
  var topics = getTopicsFromHost();
  return topics[0];
}

function getDealerTopicFromHost(){
  var topics = getTopicsFromHost();
  if (topics[1] == 'dealerorigin') {
    return 'dealer_master';
  } else {
    return topics[1];
  }
}

function getLanguageTopicFromHost(){
  var topics = getTopicsFromHost();
  return topics[2];
}

function getCountryDealerLanguageTopicFromHost(){
   var topics = getTopicsFromHost();
   return '/'+topics[0]+'/'+topics[1]+'/'+topics[2];
}

function getCountryNSCAdminLanguageTopicFromHost(){
   var topics = getTopicsFromHost();
   return '/'+topics[0]+'/_nsc_admin/'+topics[2];
}
// Methods to get dealer specific topics from hostname END

function download_sync(fileUrl) {
  // download sync should only be used for files in sync folders
  // Some of the passed fileUrls will be server relative, without sync-folders, correct these, based on own url
  if(self.location.pathname.indexOf('/sync/') != -1){
  	// we are in synced page
  	if(fileUrl != null && fileUrl.indexOf('/sync/') == -1 && fileUrl.indexOf('..') == -1){
  		// add sync paths (first two of current page)
  		var splittedPath = self.location.pathname.split('/');
  		fileUrl = '/' + splittedPath[1] + '/' +splittedPath[2]+ fileUrl
  	}
  }
  var downloadUrl=buildValidServerRelativeUrl(fileUrl) + "?download=true";
  self.location.href=downloadUrl;
}

$(document).ready(function() {
// delete empty buttons froms sales navigation
  $(".sales_navigation_layer").each(function() {
    var counter = 0;
    $(this).find("a").each(function() {
      counter++;
    });
    if(counter == 0) {
      $("#" + $(this).attr("id").replace("layer","button")).hide();
    }
  });
// delete headline for empty related links
  $("#relatedLinks").each(function() {
    var counter = 0;
    $(this).find("a").each(function() {
      counter++;
    });
    if(counter == 0) {
      $(this).hide();
    }
  });
});

