var PII = function ()
{
  var _ie6 = false;

  return {

    init: function ()
    {
      PII.removeEmptyHeadings();
      PII.roundElements();
      PII.alignBoxes();
      PII.makeDivsLinkable();
      //PII.adjustHeights();
      PII.equalizeChildrenHeights('.piirow');

      if (_ie6)
        PII.initNavigation();
    },

    roundElements: function ()
    {
      // boxes
      $$('div.box').each(function (item) { new Rounded(item); });

      var body = $$('body').first();

      // content area
      if (!body.hasClassName('alku') && !body.hasClassName('start'))
        new Rounded($('subContainer'), { corners: { tl: true, tr: true, bl: false, br: false }});
    },

    alignBoxes: function ()
    {
      var lists = $$('div.groupBox ul', 'ul#boxNav');

      lists.each(function (list)
      {
        var listItems     = list.immediateDescendants();
        var listItemCount = listItems.size();
        var parentWidth   = list.getWidth();
        var margin        = 7; // margin in px
        var listItemWidth = Math.floor((parentWidth - (listItemCount * (margin/* - 1*/))) / listItemCount);

        var style         = { width: listItemWidth + 'px' };

        listItems.each(function (el, index)
        {
          //style.margin = (index < listItemCount - 1) ? '0 ' + margin + 'px 0 0' : '0';
          el.setStyle(style);
        });

        listItems.first().addClassName('first');
        listItems.last().addClassName('last');
      });
    },

    removeEmptyHeadings: function ()
    {
      $$('h2').findAll(function (el) { return el.innerHTML == ''; }).invoke('remove');
    },

    setIE6: function (bool)
    {
      _ie6 = bool;
    },

    initNavigation: function ()
    {
      $('navigation').select('li').each(function (listItem)
      {
        listItem.observe('mouseover', function () { this.addClassName('over'); });
        listItem.observe('mouseout', function () { this.removeClassName('over'); });
      });
    },

    adjustHeights: function ()
    {
      var els = $A();

      $$('div.box').each(function (el)
      {
        if (!el.hasClassName('megaBox'))
        {
          var contentEl = el.down('div.rounded-content');

          if (contentEl)
            els.push(contentEl);
        }
      });

      if (els.size() > 1)
        new UniformHeightAdjuster({ elements: els });
    },

    loadGoogleMap: function ()
    {
      if (GBrowserIsCompatible())
      {
        var map = new GMap2(document.getElementById("map"));
        map.setCenter(new GLatLng(65.015648,25.480835), 2);

        // Add marker to the map
        var point = new GLatLng(65.011648,25.480835);
        var PiiMarker = new GMarker(point);
        var markerText = '<div style="width: 100px; height: 100px; color: black"><h3 style="color: black">Intopii<\/h3>Koulukatu 19 A 2 <br \/>90100 Oulu<br \/>Finland<\/div>';

        GEvent.addListener(PiiMarker, "click", function()
        {
          PiiMarker.openInfoWindowHtml(markerText);
        });

        map.addOverlay(PiiMarker);
        map.addControl(new GLargeMapControl());
      }
    },

    makeDivsLinkable: function ()
    {
      var paragraphs = $$('p.readMore');

      paragraphs.each(function (p)
      {
        var link = p.down('a');

        if (link)
          var url = link.readAttribute('href');

        var parent = p.up('.clickable');

        if (parent)
        {
          p.remove();
          parent.setStyle({ cursor: 'pointer' });
          parent.observe('click', function () { window.location.href = url; });
          parent.observe('mouseover', function () { this.addClassName('over'); });
          parent.observe('mouseout', function () { this.removeClassName('over'); });
        }
      });
    },

    equalizeChildrenHeights: function (parentSelector)
    {
      $$(parentSelector).each(function (item)
      {
        var parentHeight = item.getHeight() - 30; // remove 2*padding
        item.childElements().each(function (c) { c.setStyle({ height: parentHeight + 'px' }); });
      });
    }
  };
}();
