(function ($) {

  $(function () {
    adjuster();
    $('#gNaviList > li').dropdown();
    $('.number').number();
    $('a').linkType();
    $('.external, .pdf').openWindow();
    $('.teaserList').grid(2);
    $('.clubList').grid(4);
    //$('#facebookLikeButton').facebookLikeButton();
  });

  /*adjuster
  ------------------------------------------------------------------------*/
  function adjuster() {
    $("ul#gNaviList > li > ul > li:last-child, ul#bnrArea > li:last-child").addClass('lastChild');
    $("ul#footerInfo > li:first-child").addClass('firstChild');
  }

  $.fn.facebookLikeButton = function () {
    return this.each(function () {
      var container = $(this).empty(),
        url = encodeURIComponent('http://oracletech.jp/');
      $('<iframe/>', {
        src: 'http://www.facebook.com/plugins/like.php?href=' + url + '&amp;layout=button_count&amp;show_faces=false&amp;width=210&amp;action=like&amp;colorscheme=light&amp;height=21',
        scrolling: 'no',
        frameborder: '0',
        style: 'border:none; overflow:hidden; width:210px; height:210px;',
        allowTransparency: 'true'
      }).appendTo(container);
    });
  };

  $.fn.dropdown = function (options) {
    var o = $.extend({
      parentClass: 'naviStay',
      childClass: 'naviCurrent',
      showSpeed: 200,
      hideSpeed: 100,
      hideTimeout: 100
    }, options);
    return this.each(function () {
      var
        parent = $(this),
        child = $('ul:first', this);
      parent.hover(function () {
        $(this).addClass(o.parentClass).find('ul:first').slideDown(o.showSpeed);
      }, function () {
        window.setTimeout(function () {
          $('ul:first', parent).slideUp(o.hideSpeed, function () {
            parent.not(':has(.' + o.childClass + ')').removeClass(o.parentClass);
          });
        }, o.hideTimeout);
      });
    });
  };

  $.fn.linkType = function () {
    return this.each(function () {
      if (this.hostname !== window.location.hostname) {
        $(this).addClass('external');
      }
      $(this).filter('[href$=".pdf"]').addClass('pdf');
    });
  };

  $.fn.openWindow = function () {
    return this.each(function () {
      $(this).click(function () {
        window.open(this.href);
        return false;
      });
    });
  };

  $.fn.number = function () {
    return this.each(function () {
      $(this).addClass('numbered').children().each(function (i) {
        i += 1;
        $(this).addClass('n' + i).prepend($('<span/>').addClass('marker').append(i));
      });
    });
  };

  $.fn.grid = function (c, options) {
    var o = $.extend({
      cell: '> *',
      cellClass: 'cell',
      firstInRowClass: 'firstInRow',
      lastInRowClass: 'lastInRow',
      lastRowClass: 'lastRow',
      remainderClass: 'remainder',
      lastNthClassPrefix: 'last-'
    }, options);
    return this.each(function () {
      var
        cell = $(this).find(o.cell).addClass(o.cellClass),
        r = cell.length % c,
        i;
      cell.
        filter(':nth-child(' + c + 'n+1)').addClass(o.firstInRowClass).end().
        filter(':nth-child(' + c + 'n)').addClass(o.lastInRowClass);
      if (r === 0) {
        cell.filter(':nth-child(n+' + (cell.length - c + 1) + ')').addClass(o.lastRowClass);
      } else {
        for (i = 0; i < r; i += 1) {
          cell.filter(':nth-child(' + (cell.length - i) + ')').addClass(o.lastRowClass + ' ' + o.remainderClass + ' ' + o.lastNthClassPrefix + (i + 1));
        }
      }
    });
  };

})(jQuery);

// Function.method()
Function.prototype.method = function (name, func) {
  if (!this.prototype[name]) {
    this.prototype[name] = func;
    return this;
  }
};

// Date.format()
Date.method('format', function () {
  var
    dayNames = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
    dayNamesShort = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
    monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
    monthNamesShort = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
    zero = /%d|%i|%m|%H|%I|%M|%S/;
  return function (str) {
    var date = {
      '%a': dayNamesShort[this.getDay()],
      '%A': dayNames[this.getDay()],
      '%b': monthNamesShort[this.getMonth()],
      '%B': monthNames[this.getMonth()],
      '%C': this.getFullYear().toString().slice(0, 2),
      '%d': this.getDate(),
      '%D': this.getDate(),
      '%H': this.getHours(),
      '%I': (this.getHours() > 12)? this.getHours() - 12: this.getHours(),
      '%m': this.getMonth() + 1,
      '%M': this.getMinutes(),
      '%p': (this.getHours() > 12)? 'PM': 'AM',
      '%P': (this.getHours() > 12)? 'pm': 'am',
      '%s': this.getTime(),
      '%S': this.getSeconds(),
      '%u': this.getDay() + 1,
      '%w': this.getDay(),
      '%y': this.getYear(),
      '%Y': this.getFullYear(),
      '%%': '%%'
    };
    return str.replace(/(%[\w%])/g, function (c) {
      if (c.match(zero)) {
        return (date[c] < 10)? '0' + date[c]: date[c];
      } else {
        return date[c];
      }
    });
  };
}());

