/* Copyright Ideakone Oy */

Kotisivukone = {
  popup: function(url, width, height) {
    var win = window.open(url, name, "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no, width=" + width + ", height=" + height)
    win.focus();
    return false;
  }
}

function init() {
  // Edit mode
  if (window.initEdit != null) {
    initEdit();
    Initialize.checkContentWidth("content_view"); // FIXME also frontpage's blog etc
  }
  else Initialize.checkContentWidth("content");

  if (window.stateFunction != null) stateFunction();
  // Clients script
  if (window.pageInit != null) pageInit();
}


Initialize = {
  // Scale some content
  checkContentWidth: function(elem) {
    if (document.getElementById && document.getElementById(elem)) {
      var element = document.getElementById(elem);
      var orig = element.innerHTML;

      if (window.disableWidthCheckIfScripts != null &&
          window.disableWidthCheckIfScripts != undefined &&
          window.disableWidthCheckIfScripts == true &&
          orig.indexOf("script") != -1) return;

      if (window.disableWidthCheck != null &&
          window.disableWidthCheck != undefined &&
          window.disableWidthCheck == true) return;

      // Padding, etc.
      var decreaseWidthBy = 40;

      if (window.decreaseWidthBy != null &&
          window.decreaseWidthBy != undefined &&
          window.decreaseWidthBy > 0)
          decreaseWidthBy = window.decreaseWidthBy;

      // Get max width for IE7&FF and IE6 etc
      var maxWidth = 0;
      // IE 7, mozilla, safari, opera 9
      if (window.XMLHttpRequest) {
        maxWidth = element.offsetWidth;
      }
      // IE6, older browsers
      else {
        element.innerHTML = "";
        var maxWidth = element.offsetWidth;
        element.innerHTML = orig;
      }
      // maxWidth is the absolute width of the element
      // elements inner width can be smaller (padding etc.)
      //maxWidth -= decreaseWidthBy;

      // Scale
      if (maxWidth > 100) {
        var elems = document.getElementById(elem).getElementsByTagName("img");
        for (var i=0; i<elems.length; i++)
          if (elems[i].offsetWidth > maxWidth)
            elems[i].style.width = (maxWidth - decreaseWidthBy) + "px";
        elems = document.getElementById(elem).getElementsByTagName("table");
        for (var i=0; i<elems.length; i++)
          if (elems[i].offsetWidth > maxWidth)
            elems[i].style.width = (maxWidth - decreaseWidthBy) + "px";
      }
    }
  }

}

menu = function() {
  if (document.getElementById && document.getElementById("navi")) {
    var items = document.getElementById("navi").getElementsByTagName("LI");
    for (var i=0; i<items.length; i++) {
      items[i].onmouseover = function() { this.className += " hover";  }
      items[i].onmouseout = function() { this.className = this.className.replace(new RegExp(" hover\\b"), ""); }
    }
  }
}
if (window.attachEvent) window.attachEvent("onload", menu);

// For sport clubs
function swapImage(id, src) {
  if (document.getElementById && document.getElementById(id)) {
    document.getElementById(id).src = src;
  }
}
function popUpImage(id, width, height) {
  if (document.getElementById && document.getElementById(id)) {
    var win = window.open(document.getElementById(id).src, "image","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no, width=" + width + ", height=" + height)
    win.focus();
    return false;
  }
}
function popup(url) {
  var win = window.open(url, "kotisivukone_popup","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhistory=no, width=250, height=300")
  win.focus();
  return false;
}

/* Changes date on calendar */
function changeCalendarDate(pageid, hash, action, year, month, page_root_url, boxID, subset) {
  new Ajax.Updater(boxID, page_root_url + '?ajax',
    {parameters:'resource=calendar_block_change_date&action=' + action + '&year=' + year + '&month=' + month +
                '&subset=' + subset + '&pageid=' + pageid + '&hash=' + hash + '&page_root_url=' + page_root_url,
     asynchronous:true, evalScripts:true
  });
}
