var popupWidth = 990;
var popupHeight = 735;
var popupName = "print_window";

function sharePage(url, title) {
  top.location = 'mailto:?subject=' + title + '&body=' + url;
}

function bookmarkPage(url, title) {
  if (window.sidebar) { // firefox
    window.sidebar.addPanel(title, url, "");
  } else if (window.external ) {
    if (window.ActiveXObject) { //ie
      window.external.AddFavorite(url, title);
    } else { //chrome
      alert('Press ctrl+D to bookmark (Command+D for macs) after you click OK');
    }
  } else if (window.opera && window.print) { // opera
    return true;
  } else { //safari
    alert('Press ctrl+D to bookmark (Command+D for macs) after you click OK');
  }
}

function printPage(url) {
  if (self.name == popupName) {
    window.print();
  } else {
    var popup = window.open(url,popupName,"status=no,toolbar=no,location=no,menubar=no,directories=no,resizable=yes,scrollbars=yes,height=" + popupHeight + ",width=" + popupWidth);
    popup.moveTo((screen.width - popupWidth) / 2, (screen.height - popupHeight) / 2);
    if (window.focus) {
      popup.focus();
    }
  }
}

function prepareForPrinting() {
  doPrepareForPrinting("../../../..");
}

function prepareForPrinting2() {
  doPrepareForPrinting("../../../../../..");
}

function prepareForPrinting3() {
  doPrepareForPrinting("../../../../../../..");
}

function prepareForPrinting4() {
  doPrepareForPrinting("../../..");
}

function prepareForPrinting5() {
  doPrepareForPrinting("../../../../..");
}

function doPrepareForPrinting(level) {
  if (self.name == popupName) {
    //add print preview stylesheet
    addCss(level + "/css/print_preview.css");

    //replace print icon tooltip
    var link = document.getElementById("print_link");
    if (link != null) {
      link.title = "Print";
    }
  }
}

function navigateTo(htmlLink, gwtPage) {
  if (isInStaticSite()) {
    window.location = htmlLink;
  } else {
    top.goToPage(gwtPage);
  }
}

function addCss(path) {
  var fileref = document.createElement("link");
  fileref.setAttribute("rel", "stylesheet");
  fileref.setAttribute("type", "text/css");
  fileref.setAttribute("href", path);
  if (typeof fileref != "undefined") {
    document.getElementsByTagName("head")[0].appendChild(fileref);
  }
}

function isPrintPreview() {
  if (self.name == popupName) {
    return true;
  }
  return false;
}

function isLocalSite() {
  if (self.document.location.href.startsWith('file:')
    || self.document.location.href.startsWith('http://localhost:8888'))
    return true;
  return false;
}

function isInStaticSite() {
  if (top.isStaticSite) {
    return true;
  }
  return false;
}

function isInGwtSite() {
  if (top.isGwtSite) {
    return true;
  }
  return false;
}

function redirectToGWTSite(url) {
  //var topLoc = top.location.href;
  //var selfLoc = self.document.location.href;
  if (isLocalSite() || isInGwtSite() || isPrintPreview())
    return;
  if (window.top) {
    doTopRedirect(url);
  } else {
    doWindowRedirect(url);
  }
}

function doTopRedirect(url) {
  if (top.location.replace) {
    top.location.replace(url);
  } else {
    top.location.href = url;
  }
}

function doWindowRedirect(url) {
  if (isLocalSite()) {
    url = url + getGwtArg();
  }
  if (window.location.replace) {
    window.location.replace(url);
  } else {
    window.location.href = url;
  }
}

function addGwtArg(link) {
  if (isLocalSite()) {
    link.href = link.href + getGwtArg();
  }
}

function showRedirection() {
  window.setTimeout('document.getElementById(\'redirection_text\').style.display=\'block\';', 5000);
}

function getGwtArg() {
  return "?gwt.codesvr=127.0.0.1:9997";
}

// JAVASCRIPT UTILS

String.prototype.trim = function() {
  return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""));
}

String.prototype.startsWith = function(str) {
  return (this.match("^" + str) == str);
}

String.prototype.endsWith = function(str) {
  return (this.match(str + "$") == str);
}
