// common.js

(function($) {

	$(function() {
		//$('body').addClass('js-enabled');
		//$('a, area').linkType();
		$('.external, .pdf').openWindow();
		$('.btn').rollover();
		//$('input').inputType();
	});

	$.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: '_on'
    }, 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.inputType = function() {
		$('input').each(function() {
			$(this).addClass($(this).attr('type'));
		});
		return this;
	};

	$.fn.nthChild = function(options) {
		var o = $.extend({
			firstChildClass: 'first-child',
			lastChildClass: 'last-child',
			onlyChildClass: 'only-child',
			oddClass: 'odd',
			evenClass: 'even',
			nthChildClass: 'nth-child-'
		}, options);
		$(this)
			.filter(':first-child').addClass(o.firstChildClass).end()
			.filter(':last-child').addClass(o.lastChildClass).end()
			.filter(':only-child').addClass(o.onlyChildClass).end()
			.filter(':nth-child(odd)').addClass(o.oddClass).end()
			.filter(':nth-child(even)').addClass(o.evenClass).end()
			.each(function(i) {
				$(this).addClass(o.nthChildClass + (i + 1));
			});
		return this;
	};

})(jQuery);
