function initRollovers() {
	if (!document.getElementById) return
	rollOverToTagName('img');
	rollOverToTagName('input');
}

function rollOverToTagName(tagName) {
 	var aPreLoad = new Array();
	var sTempSrc;
	var aImages = document.getElementsByTagName(tagName);
	
	for (var i = 0; i < aImages.length; i++) {
		if (aImages[i].className.toUpperCase() == 'IMGOVER') {
			var src   = aImages[i].getAttribute('src');
			var fname = src.substring( 0                    , src.lastIndexOf('.') );
			var ftype = src.substring( src.lastIndexOf('.') , src.length           );
			if ( fname.substring( fname.length - 3 ) != '_ov' ) {
				var hsrc  = src.replace( ftype , '_ov' + ftype );
				
				aImages[i].setAttribute( 'hsrc' , hsrc );
				
				aPreLoad[i] = new Image();
				aPreLoad[i].src = hsrc;
				
				aImages[i].onmouseover = function() {
					sTempSrc = this.getAttribute('src');
					this.setAttribute('src', this.getAttribute('hsrc'));
				}
				
				aImages[i].onmouseout = function() {
					if (!sTempSrc) sTempSrc = this.getAttribute( 'src' ).replace( '_ov' + ftype , ftype );
					this.setAttribute('src', sTempSrc);
				}
				
				aImages[i].onfocus = function() {
					sTempSrc = this.getAttribute('src');
					this.setAttribute('src', this.getAttribute('hsrc'));
				}
				
				aImages[i].onblur = function() {
					if (!sTempSrc) sTempSrc = this.getAttribute( 'src' ).replace( '_ov' + ftype , ftype );
					this.setAttribute('src', sTempSrc);
				}
			}
		}
	}
}
window.onload   = initRollovers;
window.onunload = initRollovers;
$(document).ready(function(){
	// ロールオーバーの切り替え
	if ($('#PageID').length == 1) {
	 	var id = '#' + $('#PageID').val();
	 	if ($(id).length == 1) {
			$(id).removeAttr('class');
			$(id).attr('src', $(id).attr('src').replace(/\.(gif|jpe?g|png)$/, '_ov.$1'));
		}
	}

	// show side form
	$('#contactable').toggle(function() {
		$('.error_message', '#contactForm').hide();
		$('#name', '#contactForm').val('');
		$('#comment', '#contactForm').val('');
		$('.message', '#contactForm').html('');
		$('.holder', '#contactForm').show();
		$('.success', '#contactForm').hide();

		$('#overlay').css({display: 'block'});
		$(this).animate({"marginLeft": "-=5px"}, "fast");
		$('#contactForm').animate({"marginLeft": "-=0px"}, "fast");
		$(this).animate({"marginLeft": "+=387px"}, "slow");
		$('#contactForm').animate({"marginLeft": "+=390px"}, "slow");
	},
	function() {
		$('#contactForm').animate({"marginLeft": "-=390px"}, "slow");
		$(this).animate({"marginLeft": "-=387px"}, "slow").animate({"marginLeft": "+=5px"}, "fast");
		$('#overlay').css({display: 'none'});
	});


	$('input.close', '#contactForm').click(function(){
		$('#contactable').click();
	});

	$('input.submit', '#contactForm').click(function(){
		var params = new Object();
		params['tweet_name'] = $('#name').val();
		params['tweet_text'] = $('#comment').val();
		$('.error_message', '#contactForm').hide();
		$('.holder', '#contactForm').hide();
		$('#loading').show();
		$.post('/system/inquiry/send.php', params, function(message){
			$('#loading').fadeOut('normal', function(){
				if (message.error == '0') {
					$('.holder', '#contactForm').hide();
					$('.success', '#contactForm').show();
				} else {
					$('.error_message', '#contactForm').show();
					$('.holder', '#contactForm').show();
					$('.error_message', '#contactForm').html(message.message);
				}
			});
		});
	});
});
