// common.js

(function($) {

	$(function() {
		$('body').addClass('js-enabled');
		$('a, area').linkType();
		$('.external').openWindow();
		$('.pdf').openWindow().inlineBgFix();
		$('.rollover').rollover();
		$('.items').cols(4);
		$('.gallery').cols(9);
		$('.event dd a').inlineBgFix();
		//$('.gallery a, .lightbox a').lightBox({
		$('.gallery, .lightbox').each(function() {
			$(this).find('a').lightBox({
				imageLoading: '/img/common/lightbox-ico-loading.gif',
				imageBtnPrev: '/img/common/lightbox-btn-prev.gif',
				imageBtnNext: '/img/common/lightbox-btn-next.gif',
				imageBtnClose: '/img/common/lightbox-btn-close.gif',
				imageBlank: '/img/common/lightbox-blank.gif'
			});
		});
		$('.backToTop a, .section-backToTop a').backToTop();
		$('.toc a').animateScroll();
		//$('.localnav a:has(.ja)').swapText({target: '.ja'});
		//$('.items li').blockLink();
	});

	$.fn.linkType = function(options) {
		var o = $.extend({
			externalClass: 'external',
			pdfClass: 'pdf',
			zipClass: 'zip'
		}, options);
		$(this)
			.filter('[href^="http"]').each(function() {
				if (this.hostname != window.location.hostname) {
					$(this).addClass(o.externalClass);
				}
			}).end()
			.filter('[href$=".pdf"]').addClass(o.pdfClass).end()
			.filter('[href$=".zip"]').addClass(o.zipClass);
		return this;
	};

	$.fn.openWindow = function() {
		$(this).click(function() {
			window.open(this.href);
			return false;
		});
		return this;
	};

	$.fn.rollover = function(options) {
    var o = $.extend({
      postfix: '-ro'
    }, options);
		$(this).each(function() {
			this.originalSrc = $(this).attr('src');
			this.rolloverSrc = this.originalSrc.replace(/(\.gif|\.jpeg|\.jpg|\.png)/i, o.postfix + '$1');
			this.rolloverImg = new Image;
			this.rolloverImg.src = this.rolloverSrc;
		}).hover(function() {
			$(this).attr('src', this.rolloverSrc);
		}, function() {
			$(this).attr('src', this.originalSrc);
		});
		return this;
	};

  $.fn.cols = function(c, options) {
    var o = $.extend({
      col: '> li',
			colClass: 'col',
			firstInRowClass: 'first-in-row',
			lastInRowClass: 'last-in-row',
			lastRowClass: 'last-row',
			remainderClass: 'remainder',
			lastNthClassPrefix: 'last-'
    }, options)
    $(this).each(function() {
      var col = $(this).find(o.col).addClass(o.colClass);
      var r = col.length % c;
      col
        .filter(':nth-child(' + c + 'n+1)').addClass(o.firstInRowClass).end()
        .filter(':nth-child(' + c + 'n)').addClass(o.lastInRowClass);
      if (r == 0) {
        col.filter(':nth-child(n+' + (col.length - c + 1) + ')').addClass(o.lastRowClass);
      } else {
        for (var i = 0; i < r; i ++) {
          col.filter(':nth-child(' + (col.length - i) + ')').addClass(o.lastRowClass + ' ' + o.remainderClass + ' ' + o.lastNthClassPrefix + (i + 1));
        }
      }
    });
    return this;
  };

	$.fn.inlineBgFix = function() {
		if ($.browser.msie && $.browser.version < 8.0) {
			$(this)
				.addClass('inline-bg-fix')
				.prepend('<span class="bg-l">&nbsp;</span>')
				.append('<span class="bg-r">&nbsp;</span>');
		}
		return this;
	}

	$.fn.swapText = function(options) {
		var o = $.extend({
			target: 'em'
		}, options);
		$(this).each(function() {
			this.originalText = $(this).html();
			this.rolloverText = $(this).find(o.target).html();
		}).hover(function() {
			$(this).html(this.rolloverText);
		}, function() {
			$(this).html(this.originalText);
		});
		return this;
	};

	$.fn.blockLink = function(options) {
		var o = $.extend({
			hoverClass: 'hover'
		}, options);
		$(this).hover(function() {
			$(this).addClass(o.hoverClass);
		}, function() {
			$(this).removeClass(o.hoverClass);
		}).click(function() {
			location.href = $(this).find('a').href;
		});
		return this;
	};

	$.fn.backToTop = function() {
		var elem = $.browser.opera? 'html': 'html, body';
		$(this).click(function() {
			$(elem).animate({scrollTop: 0}, 800, 'easeOutExpo');
			return false;
		});
		return this;
	};

	$.fn.animateScroll = function() {
		var elem = $.browser.opera? 'html': 'html, body';
    $(this).click(function() {
			if ($.browser.msie && $.browser.version == 7.0) return this;
      var targetOffset = $(this.hash).offset().top;
      var documentHeight = $(document).height();
      var windowHeight = $(window).height();
			if ((documentHeight - targetOffset) < windowHeight) {
				targetOffset = (documentHeight - windowHeight);
			}
      $(elem).animate({scrollTop: targetOffset}, 800, 'easeOutExpo');
    });
		return this;
	}

})(jQuery);

/*
 * jQuery Easing v1.3 - http://gsgd.co.uk/sandbox/jquery/easing/
 *
 * Uses the built in easing capabilities added In jQuery 1.1
 * to offer multiple easing options
 *
 * TERMS OF USE - jQuery Easing
 * 
 * Open source under the BSD License. 
 * 
 * Copyright c 2008 George McGinley Smith
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of 
 * conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list 
 * of conditions and the following disclaimer in the documentation and/or other materials 
 * provided with the distribution.
 * 
 * Neither the name of the author nor the names of contributors may be used to endorse 
 * or promote products derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 *  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE. 
 *
*/

// t: current time, b: begInnIng value, c: change In value, d: duration
jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend( jQuery.easing,
{
	def: 'easeOutExpo',
	swing: function (x, t, b, c, d) {
		//alert(jQuery.easing.default);
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	}
});

/*
 *
 * TERMS OF USE - EASING EQUATIONS
 * 
 * Open source under the BSD License. 
 * 
 * Copyright c 2001 Robert Penner
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 * 
 * Redistributions of source code must retain the above copyright notice, this list of 
 * conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice, this list 
 * of conditions and the following disclaimer in the documentation and/or other materials 
 * provided with the distribution.
 * 
 * Neither the name of the author nor the names of contributors may be used to endorse 
 * or promote products derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 *  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE. 
 *
 */

/*
$(function () {
    if (! $.browser.safari) {
        $('.backToTop a').click(function () {
            $(this).blur();

            $('html,body').animate({ scrollTop: 0 }, 'slow');

            return false;
        });
    }
});
*/
