(function($) {

	$(function() {
		$('body')
			.addClass('js-enabled')
			.find('[href^="http"]').each(function() {
				if (this.hostname != window.location.hostname) {
					$(this).addClass('external');
				}
			}).end()
			.find('[href$=".pdf"]').addClass('pdf');
		$('.external, .pdf').openWindow();
		$('.links a').inlineBgFix();
		$('.dropdown').dropdown();
		$('.accordion').accordion();
		$('.collapse').collapse();
		$('.collapse').cycle({
			target: '#news-list',
			cycleBox: '<ul class="links cycle-box"></ul>'
		});
		$('#home')
			.find('#simulation').hSim().end()
			.find('#random').hRand();
		$('.toggle-bundle').toggleBundle();

	});

	$.fn.openWindow = function() {
		$(this).click(function() {
			window.open(this.href);
			return false;
		});
		return this;
	};

	$.fn.submitExternal = function() {};

	$.fn.inlineBgFix = function() {
		if (!$.browser.msie || $.browser.version > 7.0) {
			return this;
		}
		$(this)
			.addClass('inline-bg-fix')
			.prepend('<span class="bg-l"> </span>')
			.append('<span class="bg-r"> </span>');
		return this;
	};

	$.fn.collapse = function(options) {
		var o = $.extend({
			tab: '.collapse-tab a',
			panel: '.collapse-panel',
			collapsedClass: 'collapsed',
			expandedClass: 'expanded',
			showSpeed: 200,
			hideSpeed: 100
		}, options);
		$(this).each(function() {
			var container = $(this);
			var tab = $(this).find(o.tab);
			var panel = $(this).find(o.panel);
			$(panel).hide();
			$(this).addClass(o.collapsedClass);
			$(tab).click(function() {
				if ($(container).is('.' + o.collapsedClass)) {
					$(container).removeClass(o.collapsedClass).addClass(o.expandedClass);
					$(panel).slideDown(o.showSpeed);
				}
				else {
					$(panel).slideUp(o.hideSpeed, function() {
						$(container).removeClass(o.expandedClass).addClass(o.collapsedClass);
					});
				}
				return false;
			});
		});
		return this;
	};

	$.fn.cycle = function(options) {
		var o = $.extend({
			target: '',
			cycleBox: '',
			cycleBoxClass: 'cycle-box',
			elem: 'li',
			timeOut: 6000,
			showSpeed: 400,
			hideSpeed: 200
		}, options);
		$(this).each(function() {
			$(this).append(o.cycleBox);
			$('.' + o.cycleBoxClass).html($(this).find(o.target).html());
			var elem = $('.' + o.cycleBoxClass).find(o.elem);
			var len = $(elem).length;
			var index = 0;
			if (len < 2) {return;}
			$(elem)
				.hide()
				.eq(index).fadeIn(o.showSpeed).addClass('this');
			$().everyTime(o.timeOut, 'cycleTimer', function() {
				index = index + 1;
				if (index >= len) {
					index = 0;
				}
				$(elem).filter('.this').fadeOut(o.hideSpeed, function() {
					$(this).removeClass('this');
					$(elem).eq(index).fadeIn(o.showSpeed).addClass('this');
				});
					
			});
		});
		return this;
	};

	$.fn.dropdown = function(options) { // depends jQuery Timers
		var o = $.extend({
			suckerfish: '> ul',
			hoverTime: 100,
			showSpeed: 200,
			hideSpeed: 100
		}, options);
		$(this).each(function() {
			var suckerfish = $(this).find(o.suckerfish);
			//$(children).hide();
			$(this).hover(function() {
				$(this).addClass('hover').stopTime('mouseOut').oneTime(o.hoverTime, 'mouseOver', function() {
					$(suckerfish).slideDown(o.showSpeed);
				});
			}, function() {
				$(this).removeClass('hover').stopTime('mouseOver').oneTime(o.hoverTime, 'mouseOut', function() {
					$(suckerfish).slideUp(o.hideSpeed);
				});
			});
		});
		return this;
	};
/*
	$.fn.suckerfish = function(options) { // depends jQuery Timers
		var o = $.extend({
			children: 'ul',
			hoverTime: 100,
			showSpeed: 200,
			hideSpeed: 100
		}, options);
		$(this).each(function() {
			var children = $(this).children(o.children);
			//$(children).hide();
			$(this).hover(function() {
				$(this).addClass('hover').stopTime('mouseOut').oneTime(o.hoverTime, 'mouseOver', function() {
					$(children).slideDown(o.showSpeed);
				});
			}, function() {
				$(this).removeClass('hover').stopTime('mouseOver').oneTime(o.hoverTime, 'mouseOut', function() {
					$(children).slideUp(o.hideSpeed);
				});
			});
		});
		return this;
	};
*/

	$.fn.accordion = function(options) {
		var o = $.extend({
			collapsible: true,
			event: 'click', // 'mouseover'
			header: '> li:has(ul) > a',
			body: '> li > ul',
			active: ':has(.current)', // number, boolean
			headerClass: 'accordion-header',
			bodyClass: 'accordion-body',
			activeClass: 'selected',
			showSpeed: 200,
			hideSpeed: 100
		}, options);
		$(this).each(function() {
			var header = $(this).find(o.header).addClass(o.headerClass);
			var body = $(this).find(o.body).addClass(o.bodyClass);
			var active = (typeof o.active == 'number')? ':eq(' + o.active + ')': o.active;
			$(body)
				.hide()
				.filter(active).show()
					.prev(header).addClass(o.activeClass);
			$(header).bind(o.event, function() {
				if ($(this).hasClass(o.activeClass)) {
					if (o.collapsible) {
						$(this)
							.next(body).slideUp(o.hideSpeed).end()
							.removeClass(o.activeClass);
					}
				}
				else {
					$(header).filter('.' + o.activeClass)
						.next(body).slideUp(o.hideSpeed).end()
						.removeClass(o.activeClass);
					$(this)
						.next(body).slideDown(o.showSpeed).end()
						.addClass(o.activeClass);
				}
				return false;
			});
		});
		return this;
	};

	$.fn.hSim = function() {
		var z0 = $(this).find('#frame, .select');
		var z1 = $(this).find('.photo');
		$(z0).hide();
		$(z1).show();
		$(this).hover(function() {
			$(this).addClass('expanded');
			$(z0).show();
			$(z1).hide();
			$(this).stopTime('show');
		}, function() {
			$(this).oneTime(3000, 'show', function() {
				$(this).removeClass('expanded');
				$(z0).hide();
				$(z1).show();
			});
		});
		return this;
	};

	$.fn.hRand = function(options) {
		var o = $.extend({
			desc: '.random-element'
		}, options);
		$(this).each(function() {
			var desc = $(this).find(o.desc);
			var index = Math.floor(Math.random() * $(desc).length);
			$(desc)
				.hide()
				.eq(index).show();
		});
		return this;
	};

	$.fn.toggleBundle = function(options) {
		var o = $.extend({
			header: '> dl > dt',
			panel: '> dl > dt + dd',
			button: '> p',
			headerClass: 'toggle-header',
			panelClass: 'toggle-panel',
			buttonClass: 'toggle-button',
			activeClass: 'active',
			shownClass: 'shown',
			expandedClass: 'expanded',
			expandedText: 'すべて閉じる',
			speed: 100
		}, options);
		$(this).each(function() {
			var header = $(this).find(o.header).addClass(o.headerClass).wrapInner('<a href="#"></a>');
			var panel = $(this).find(o.panel).addClass(o.panelClass).hide();
			var button = $(this).find(o.button).addClass(o.buttonClass).wrapInner('<a href="#"></a>');
			var buttonText = $(button).text();
			$(header).click(function() {
				if ($(this).hasClass(o.activeClass)) {
					$(this).removeClass(o.activeClass).next(panel).hide(o.speed);
				} 
				else {
					$(this).addClass(o.activeClass).next(panel).show(o.speed);
				}
				return false;
			});
			$(button).toggle(function() {
				$(panel).show(o.speed).prev(header).addClass(o.activeClass);
				$(this).addClass(o.expandedClass).find('a').text(o.expandedText);
			}, function() {
				$(panel).hide(o.speed).prev(header).removeClass(o.activeClass);
				$(this).removeClass(o.expandedClass).find('a').text(buttonText);
			});
		});
		return false;
	};


})(jQuery);

/**
 * jQuery.timers - Timer abstractions for jQuery
 * Written by Blair Mitchelmore (blair DOT mitchelmore AT gmail DOT com)
 * Licensed under the WTFPL (http://sam.zoy.org/wtfpl/).
 * Date: 2009/02/08
 *
 * @author Blair Mitchelmore
 * @version 1.1.2
 *
 **/
jQuery.fn.extend({
	everyTime: function(interval, label, fn, times, belay) {
		return this.each(function() {
			jQuery.timer.add(this, interval, label, fn, times, belay);
		});
	},
	oneTime: function(interval, label, fn) {
		return this.each(function() {
			jQuery.timer.add(this, interval, label, fn, 1);
		});
	},
	stopTime: function(label, fn) {
		return this.each(function() {
			jQuery.timer.remove(this, label, fn);
		});
	}
});
jQuery.event.special
jQuery.extend({
	timer: {
		global: [],
		guid: 1,
		dataKey: "jQuery.timer",
		regex: /^([0-9]+(?:\.[0-9]*)?)\s*(.*s)?$/,
		powers: {
			// Yeah this is major overkill...
			'ms': 1,
			'cs': 10,
			'ds': 100,
			's': 1000,
			'das': 10000,
			'hs': 100000,
			'ks': 1000000
		},
		timeParse: function(value) {
			if (value == undefined || value == null)
				return null;
			var result = this.regex.exec(jQuery.trim(value.toString()));
			if (result[2]) {
				var num = parseFloat(result[1]);
				var mult = this.powers[result[2]] || 1;
				return num * mult;
			} else {
				return value;
			}
		},
		add: function(element, interval, label, fn, times, belay) {
			var counter = 0;
			if (jQuery.isFunction(label)) {
				if (!times) 
					times = fn;
				fn = label;
				label = interval;
			}
			interval = jQuery.timer.timeParse(interval);
			if (typeof interval != 'number' || isNaN(interval) || interval <= 0)
				return;
			if (times && times.constructor != Number) {
				belay = !!times;
				times = 0;
			}
			times = times || 0;
			belay = belay || false;
			var timers = jQuery.data(element, this.dataKey) || jQuery.data(element, this.dataKey, {});
			if (!timers[label])
				timers[label] = {};
			fn.timerID = fn.timerID || this.guid++;
			var handler = function() {
				if (belay && this.inProgress) 
					return;
				this.inProgress = true;
				if ((++counter > times && times !== 0) || fn.call(element, counter) === false)
					jQuery.timer.remove(element, label, fn);
				this.inProgress = false;
			};
			handler.timerID = fn.timerID;
			if (!timers[label][fn.timerID])
				timers[label][fn.timerID] = window.setInterval(handler,interval);
			this.global.push( element );
		},
		remove: function(element, label, fn) {
			var timers = jQuery.data(element, this.dataKey), ret;
			if ( timers ) {
				if (!label) {
					for ( label in timers )
						this.remove(element, label, fn);
				} else if ( timers[label] ) {
					if ( fn ) {
						if ( fn.timerID ) {
							window.clearInterval(timers[label][fn.timerID]);
							delete timers[label][fn.timerID];
						}
					} else {
						for ( var fn in timers[label] ) {
							window.clearInterval(timers[label][fn]);
							delete timers[label][fn];
						}
					}
					for ( ret in timers[label] ) break;
					if ( !ret ) {
						ret = null;
						delete timers[label];
					}
				}
				for ( ret in timers ) break;
				if ( !ret ) 
					jQuery.removeData(element, this.dataKey);
			}
		}
	}
});
jQuery(window).bind("unload", function() {
	jQuery.each(jQuery.timer.global, function(index, item) {
		jQuery.timer.remove(item);
	});
});
