var PII = function ()
{
  var _ie6 = false;

  return {

    init: function ()
    {
      PII.removeEmptyHeadings();
      PII.roundElements();
      PII.alignBoxes();
      PII.makeDivsLinkable();
      //PII.twitterize();
      //PII.adjustHeights();
      PII.equalizeChildrenHeights('.piirow');
      PII.columnify('ul.columnify');

      $$('a[target="_blank"]').invoke('writeAttribute', 'target', false); // remove target-blanks

      if (_ie6)
        PII.initNavigation();
    },

    /**
     *
     */
    twitterize: function ()
    {
      if (!$('twitterBox'))
        return;

      $('twitterBox').setStyle({ opacity: .9 });
      $$('.tweet a').each(function (link)
      {
        link.update(PII.shortUrl(link.firstChild.nodeValue, 22));
      });
      
      new Rounded($('twitterBoxHandle'), { corners: { tl: false, tr: true, bl: false, br: true }, radius: 5 });
      new Rounded($('tweets'), { corners: { tl: false, tr: false, bl: false, br: true }, radius: 5 });

      $('twitterBoxHandle').observe('click', function ()
      {
        var tb = $('twitterBox');
        var pos = tb.cumulativeOffset();
        var newX = pos.left < -100 ? -60 : -215;
        new Effect.Move(tb, { x: newX, y: pos.top, mode: 'absolute', duration: 1.0, transition: Effect.Transitions.spring });
      });

      $('twitterBox').show();
    },

    shortUrl: function (url, maxlength)
    {
      var partSize = Math.floor((maxlength - 3) / 2);
      return (url.length > maxlength ? url.slice(0, partSize) + '...' + url.slice(-partSize) : url);
    },

    /**
     *
     */
    roundElements: function ()
    {
      // boxes
      $$('div.box').each(function (item) { new Rounded(item); });
      $$('li.dlc-list-item').each(function (item) { new Rounded(item); });

      new Rounded($('dlc-item'));

      var body = $$('body').first();

      // content area
      if (!body.hasClassName('home') && !body.hasClassName('home2'))
        new Rounded($('subContainer'), { corners: { tl: true, tr: true, bl: true, br: true }});
    },

    /**
     *
     */
    alignBoxes: function ()
    {
      var lists = $$('div.groupBox ul', 'ul#boxNav', 'ul#overview-boxes');

      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' }); });
      });
    },

    columnify: function (selector)
    {
      var margin = arguments[1] || 7;

      $$(selector).each(function (parent)
      {      
        var children      = parent.childElements();
        var childCount    = children.size();
        var parentWidth   = parent.getWidth();
        var width         = ((parentWidth - (margin * (childCount - 1))) / childCount).floor();
        var totalWidth    = 0;
        
        children.each(function (item, index)
        {
          var colWidth = index < childCount - 1 ? width : (parentWidth - totalWidth);
          var content = item.innerHTML;
          var contentEl = new Element('div', { className: 'column-content' }).update(content);
          
          item.update(contentEl);
          item.setStyle({ float: 'left', width: colWidth + 'px', padding: '0 !important', margin: 0 });
        
          if (index < childCount - 1)
            item.setStyle({ marginRight: margin + 'px' });
            
          totalWidth += width + margin;
        });
        
        parent.setStyle({ overflow: 'hidden' });
        var parentHeight = parent.getHeight() - 30; // remove 2 * padding
        
        children.each(function (item)
        {
          item.down('.column-content').setStyle({ height: parentHeight + 'px' });
        })
      });
    }
  };
}();

