/*!
 * JavaScript for Pascal Metrics HTML Prototype
 * @author AChappelear <andrea@pixiscreative.com>
 * @author MRaichelson <mike@pixiscreative.com>
 */
/*jslint nomen:true, sloppy:true, maxerr:50, indent:2 */
/*global $:true, jQuery:true */

var Pascal = this.Pascal || {};

Pascal.config = {};

Pascal.f = {
  gridBox : function (domElement) {
    var $local;
        $local = $(domElement);
    $local.find('div.trigger a:first').addClass('active');
    $local.find('div.trigger a').each(function () {
      $(this).bind(
        'click',
        function () {
          var $target, $trigger;
              $target = $(this).attr('href');
              $trigger = $(this);
          $local.find('div.description:visible').hide();
          $local.find($target).show();
          
          $local.find('a.active').removeClass('active');
          $trigger.addClass('active');
          
          this.blur();
          return false;
        }
      );
    });
  },
  widgetPeopleControl : function (domElement) {
    var $local, $widget, type;
        $local = $(domElement);
        $widget = $local.parents('div.widget');
        type = $local.data('control');
    $local.bind({
      click : function () {
        $widget.find('li a img').each(function () {
          $(this).attr('src', $(this).data('bw'));
        });
        $widget.find('li.' + type + ' a img').each(function() {
          $(this).attr('src', $(this).data('color'));
        });
        return false;
      },
      focus : function () { this.blur(); }
    });
  },
  widgetPeoplePerson : function (domElement) {
    var $local, img_bw, img_color;
        $local = $(domElement);
        img_bw = $local.data('bw');
        img_color = $local.data('color');
    $local.hover(
      function () {
        $local.attr('src', img_color);
      },
      function () {
        $local.attr('src', img_bw);
      }
    );
  }
};

Pascal.init = function () {
  // apply tabbed behaviors to tab boxes.
  $('div.tabs').tabs();
  // apply grid box behavior
  $('div.gridBox').each(function () {
    Pascal.f.gridBox(this);
  });
  // apply about us people grid behaviors
  if ($('#widget-people').get(0)) {
    $('#widget-people ul.widget-control a').each(function () {
      Pascal.f.widgetPeopleControl(this);
    });
    $('#widget-people ul.widget-body a img').each(function () {
      Pascal.f.widgetPeoplePerson(this);
    });
  }
  // add IE behaviors if necessary.
  if ($.browser.msie) {
    Pascal.initIE();
  }
};

Pascal.initIE = function () {
  // special behaviors for IE
  $('ul,ol').each( function () {
    var $local;
        $local = $(this);
    $local.find('li:first').addClass('first');
    $local.find('li:last' ).addClass('last');
  });
};

// on DOC.READY trigger Pascal.init()
$(function () {
  Pascal.init();
});
