/*
common.js
REQUIRES: prototype.js, scriptaculous.js, jquery,js
Site URI: 
Description: ﾂ郤藝MCA ･ｦ･ｨ･・ﾍ･ｹ・
Since: 2009-12-07
Author: Tooru Ehara
*/

/*jQuery.noConflict();*/


// *************************************************
// 初期発動関数
// *************************************************

$(document).ready(function(){
	setRollOver();
	setStripedTable();
	NewMarkUp();
	$("a.fancybox").fancybox();
	if($('#programs')){
		$(function() {
				$('#programs').tabs({ fx: { opacity: 'toggle' } }).tabs();
		});
	}
});



// *************************************************
// ﾈﾆﾍﾑｴﾘｿ・
// *************************************************


// eventSetter
var isIE = isIE = (document.documentElement.getAttribute('style') == document.documentElement.style);

function eventSetter(obj,eventType,func){
	if(isIE) {
		obj.setAttribute(eventType,new Function(func));
	} else {
		obj.setAttribute(eventType,func);
	}
}


// open new window
function newWindow(uri,width,height){
	var myWindow = window.open(uri, 'newWindow', 'resizable=yes,scrollbars=yes,status=0,width='+width+',height='+height);
	if (myWindow.focus!=null) {
		myWindow.focus();
	}
}


// print window
function printWindow(){
	if(document.getElementById || document.layers){
		window.print();
	}
}


// Ajax recover UTF-8 (for Safari)
function recover_utf8(text){
	if(navigator.appVersion.indexOf('KHTML') > -1){
		var esc = escape(text);
		if(esc.indexOf('%u') < 0 && esc.indexOf('%') > -1){
			text = decodeURIComponent(esc);
		}
	}
	return text;
}


// input clear
function inputClear(id,txt){
	if ($(id).value == txt) $(id).value = '';
}

// scroll to top
$(function () {
	if (! $.browser.safari) {
			$('#pagetop').click(function () {
					$(this).blur();
					$('html,body').animate({ scrollTop: 0 }, 'slow');
					return false;
			});
	}
});



// *************************************************
// ｲ霖・ｽｼ･｡ｦｪ｡ｼ･ﾐ｡ｻ・
// *************************************************

function setRollOver(){
	var postfix = '_o';
	$('.swap').not('[src*="'+ postfix +'."]').each(function() {
		var img = $(this);
		var src = img.attr('src');
		var src_on = src.substr(0, src.lastIndexOf('.'))
		           + postfix
		           + src.substring(src.lastIndexOf('.'));
		$('<img>').attr('src', src_on);
		img.hover(
			function() {
				img.attr('src', src_on);
			},
			function() {
				img.attr('src', src);
			}
		);
	});
}



// *************************************************
// ･ﾆ｡ｼ･ﾖ･・･ｹ･ﾈ･鬣､･ﾗｲｽ・
// *************************************************

function setStripedTable(){
	$('.stripe tr:even').addClass('even');
	$('.stripe tr:odd').addClass('odd');
}


// *************************************************
// 最新記事アイコン表示
// *************************************************

function NewMarkUp(){
	// passage time
	var pass = 168;
	 
	// display content
	var content = '&nbsp;&nbsp;<img src="/img/icn_new.gif" alt="New!" />';
	 
	var currentDate = new Date(); 
	var spans = document.getElementsByTagName('span');
	for (i = 0; i < spans.length; i++) {
			if (spans[i].getAttribute('class') == 'new' ||
					spans[i].getAttribute('className') == 'new') {
					if (spans[i].childNodes[0]) {
							time = spans[i].childNodes[0].nodeValue.split(':');
							var entryDate = new Date(time[0], time[1]-1, time[2], time[3], time[4], time[5]); 
							var now = (entryDate.getTime() - currentDate.getTime())/(60*60*1000); 
							now = Math.ceil(now);
							if(-now <= pass){
									spans[i].innerHTML = content;
									spans[i].style.display = 'inline';
							}
							else{
									spans[i].innerHTML = '';
							}
					}
			}
	}
}

