/*
common.js
REQUIRES: prototype.js, scriptaculous.js, jquery,js
Site URI: 
Description: 大阪YMCA サポートクラス
Since: 2009-12-07
Author: Tooru Ehara
*/

/*jQuery.noConflict();*/


// *************************************************
// 初期発動関数
// *************************************************

$(document).ready(function(){
	setPullDownMenu();
	setRollOver();
	NewMarkUp();
	setStripedTable();
});



// *************************************************
// 汎用関数
// *************************************************


// 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', 'easeOutQuart');
					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 setPullDownMenu(){
	$('#globalnav li.menu').bind('mouseover', jsddm_open);
	$('#globalnav li.menu ').bind('mouseout',  jsddm_timer);
	
	var timeout			= 500;
	var closetimer	= 0;
	var ddmenuitem	= 0;
	
	function jsddm_open(){
		jsddm_canceltimer();
		jsddm_close();
		ddmenuitem = $(this).find('ul').eq(0).css('display', 'block');
	}

	function jsddm_close(){
		if(ddmenuitem) ddmenuitem.css('display', 'none');
	}
		
	function jsddm_timer(){
		closetimer = window.setTimeout(jsddm_close, timeout);
	}
		
	function jsddm_canceltimer(){
		if(closetimer){
			window.clearTimeout(closetimer);
			closetimer = null;
		}
	}
	document.onclick = jsddm_close;
}


// *************************************************
// 画像拡大処理
// *************************************************

/*jQuery(document).ready(function() {
	jQuery('a.fancybox').fancybox();
});
*/

// *************************************************
// ナビゲーションアクティブ処理
// *************************************************

$(document).ready(function() {
	$('#localnav li').each(function(){
		var className = $(this).attr('class');
		if(className == $('#primary').attr('class')){
			$(this).addClass('active');
		}
	});
});


// *************************************************
// 最新記事アイコン表示
// *************************************************

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 = '';
							}
					}
			}
	}
}


// *************************************************
// テーブルストライプ化
// *************************************************

function setStripedTable(){
	$('.stripe tr:even').addClass('even');
	$('.stripe tr:odd').addClass('odd');
}



