
// jQuery_Auto 0.9
// Automatic functions for webpages (using the wonderful jQuery library)

// Copyright: (c) 2006, Michal Tatarynowicz (tatarynowicz@gmail.com)
// Licenced as Public Domain (http://creativecommons.org/licenses/publicdomain/)
// $Id: jquery_auto.js 426 2006-05-06 19:54:39Z Michał $


// Initialization

$.auto = {
	init: function() {
		for (module in $.auto) {
			if ($.auto[module].init)
				$.auto[module].init();
		}
	}
};

$(document).ready($.auto.init);


// Auto-hidden elements
// JS オフ時には .noScriptのものが出る。

$.auto.hide = {
	init: function() {
		$('.Hide').hide();
	}
};


// Mouse hover

$.auto.hover = {

	init: function() {
		$('img.RollOver, img.RollOver, .Nav img')
			.bind('mouseover', this.enter)
			.bind('mouseout', this.exit)
			.each(this.preload);

		$('.Nav img.Current')
		.bind('mouseover', this.current)
		.bind('mouseout', this.current)
		.each( function() {
			this.src = this.src.replace(/^(.+)(\.[a-z]+)$/, "$1_on$2");
		});


	},

	preload: function() {
		this.preloaded = new Image;
		this.preloaded.src = this.src.replace(/^(.+)(\.[a-z]+)$/, "$1_on$2");
	},

	enter: function() {
		if ( this.src.indexOf("_on.") == -1 ) {;
			this.src = this.src.replace(/^(.+)(\.[a-z]+)$/, "$1_on$2");
		}
	},

	exit: function() {
		this.src = this.src.replace(/^(.+)_on(\.[a-z]+)$/, "$1$2");
	},

	current: function(){
		if ( this.src.indexOf("_on.") == -1 ) {;
			this.src = this.src.replace(/^(.+)(\.[a-z]+)$/, "$1_on$2");
		}
	}
};



// Auto-submitting SELECTs
// ボタンを押さなくてもいいやつ

$.auto.submit = {
	init: function() {
		$('SELECT.Submit').bind('change', this.on_change);
	},

	on_change: function() {
		if (this.value) this.form.submit();
	}
};


// Auto-selected text in text fields after a label click

$.auto.select = {
	init: function() {
		$('LABEL.Select').each(this.label_action);
		$('INPUT.Select').bind('click', function(){ this.select(); });
	},

	label_action: function() {
		var field = $('#'+this.htmlFor).get(0);
		if (field && field.focus && field.select) {
			$(this).bind('click', function(){ field.focus(); field.select(); });
		}
	}
};


// Switches tabs on click
// 選択中のタブは.Active

$.auto.tabs = {

	init: function() {

		$('.Tabs').each(function(){
			var f = $.auto.tabs.click;

			var group = this;

			$('.TabMenu li a, .Tab', group).each(function(){
				this.group = group;
				$(this).click(f);
				$('#'+this.rel+'Area').hide();
//				alert(this.rel);
			}).filter(':first').trigger('click');
		});

	},

	click: function() {
		var tab = $('#'+this.rel+'Area').get(0);
		$('.TabMenu li a, .Tab', this.group).each(function(){
			$(this).removeClass('Active');
			$('#'+this.rel+'Area').hide();
		});

		$(this).addClass('Active');
		$(tab).show();
		this.blur();

		return false;
	}

};

$.auto.border = {
	init: function() {

		$("dl.ArchivesMontly dd + dt")
			.css("margin-top", "1em");
		$("#Content .Box .PageNav li + li")
			.css("border-left", "1px solid #000");
		$("#Content .Article .Image img + img")
			.css("margin-top", "10px");
	}
};

$.auto.year = {
	init: function () {
		$("#History table tbody").prepend('<tr><td colspan="5">&nbsp;</td></tr>');
		$("#History table tbody tr:odd").addClass("Odd");
		$("#History table tbody tr:even").addClass("Even");
	}
}

/*
 * IE PNG Fix v1.4
 *
 * Copyright (c) 2006 Takashi Aida http://www.isella.com/aod2/
 *
 */

// IE5.5+ PNG Alpha Fix v1.0RC4
// (c) 2004-2005 Angus Turnbull http://www.twinhelix.com

// This is licensed under the CC-GNU LGPL, version 2.1 or later.
// For details, see: http://creativecommons.org/licenses/LGPL/2.1/

if (typeof IEPNGFIX == 'undefined') {
//--============================================================================

var IEPNGFIX = {
	blank:  '/common/images/blank.gif',
	filter: 'DXImageTransform.Microsoft.AlphaImageLoader',

	fixit: function (elem, src, method) {
		if (elem.filters[this.filter]) {
			var filter = elem.filters[this.filter];
			filter.enabled = true;
			filter.src = src;
			filter.sizingMethod = method;
		}
		else {
			elem.style.filter = 'progid:' + this.filter +
				'(src="' + src + '",sizingMethod="' + method + '")';
		}
	},

	fixwidth: function(elem) {
		if (elem.currentStyle.width == 'auto' &&
			elem.currentStyle.height == 'auto') {
			elem.style.width = elem.offsetWidth + 'px';
		}
	},

	fixchild: function(elem, recursive) {
		if (!/MSIE (5\.5|6\.|7\.)/.test(navigator.userAgent)) return;

		for (var i = 0, n = elem.childNodes.length; i < n; i++) {
			var childNode = elem.childNodes[i];
			if (childNode.style) {
				if (childNode.style.position) {
					childNode.style.position = childNode.style.position;
				}
				else {
					childNode.style.position = 'relative';
				}
			}
			if (recursive && childNode.hasChildNodes()) {
				this.fixchild(childNode, recursive);
			}
		}
	},

	fix: function(elem) {
		if (!/MSIE (5\.5|6\.|7\.)/.test(navigator.userAgent)) return;

		var bgImg =
			elem.currentStyle.backgroundImage || elem.style.backgroundImage;

		if (elem.tagName == 'IMG') {
			if ((/\.png$/i).test(elem.src)) {
//				this.fixwidth(elem);
				this.fixit(elem, elem.src, 'scale');
				elem.src = this.blank;
				elem.runtimeStyle.behavior = 'none';
			}
		}
		else if (bgImg && bgImg != 'none') {
			if (bgImg.match(/^url[("']+(.*\.png)[)"']+$/i)) {
				var s = RegExp.$1;
//				this.fixwidth(elem);
				elem.style.backgroundImage = 'none';
				this.fixit(elem, s, 'scale'); // crop | image | scale

				if (elem.tagName == 'A' && elem.style) {
					if (!elem.style.cursor) {
						elem.style.cursor = 'pointer';
					}
				}

				this.fixchild(elem);
				elem.runtimeStyle.behavior = 'none';
			}
		}
	},

	hover: function(elem, hvImg) {
		var bgImg = elem.style.backgroundImage;

		if (!bgImg && elem.currentStyle) bgImg = elem.currentStyle.backgroundImage;

		if (elem.tagName == 'IMG' && hvImg) {
			var image = elem.src;
			elem.onmouseover = function() {
				elem.src = hvImg;
				IEPNGFIX.fix(elem);
			};
			elem.onmouseout = function() {
				elem.src = image;
				IEPNGFIX.fix(elem);
			};
		}
		else if (bgImg && bgImg != 'none' && hvImg) {
			elem.onmouseover = function() {
				elem.style.backgroundImage = 'url(' + hvImg + ')';
				IEPNGFIX.fix(elem);
			};
			elem.onmouseout = function() {
				elem.style.backgroundImage = bgImg;
				IEPNGFIX.fix(elem);
			};
		}

		IEPNGFIX.fix(elem);
	}
};

//--============================================================================
} // end if (typeof IEPNGFIX == 'undefined')

