/*
common.js
REQUIRES: prototype.js, scriptaculous.js, jquery,js
Site URI: 
Description: クラボウ
Since: 2011-5
Author: Yasuhiro Morita
*/

/*jQuery.noConflict();*/


// *************************************************
// 初期発動関数
// *************************************************


jQuery(function() {
	jQuery("ul.subdropDown").hide();
	jQuery("ul.subdropDown2").hide();
	jQuery("div#g_navi li").hover(function(){
		jQuery("ul.subdropDown:not(:animated)",this).slideDown("fast");
		},function() {
			jQuery("ul.subdropDown",this).hide();
		});
	jQuery("ul.subdropDown li.sub").hover(function(){
		jQuery("ul:not(:animated)",this).slideDown("fast");
		},function() {
			jQuery("ul",this).hide();
		});
});


jQuery(function(){
	jQuery("img.Over").hover(
		function(){jQuery(this).fadeTo(100, 0.7);},
		function(){jQuery(this).fadeTo(100, 1);}
	);
})


jQuery(function(){
	jQuery("img.Over2").hover(
		function(){jQuery(this).fadeTo(150, 0.3);},
		function(){jQuery(this).fadeTo(150, 1);}
	);
})

$(document).ready(function(){
	setPullDownMenu();
	setRollOver();
	setStripedTable();
});


/*-----------*/
jQuery(function(){
	addCss();
});
var addCss = (function(){
	$('li:last-child').addClass('last');
});

jQuery(function(){
	addCss();
});
var addCss = (function(){
	$('li:first-child').addClass('first');
});

// *************************************************
// 汎用関数
// *************************************************


// 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;
			});
	}
});



/* タブ */
/*
クリックしたときに点線が出るのを防ぐ（IE用）	
$(function(){
$("#tab_box li a").focus(function(){
	$(this).blur()
});
})

タブボタンの on off と タブの中身の切り替え　
$(function(){

初期設定
$("a.selected img").attr("src",$("a.selected img").attr("src").replace(/^(.+)(\.[a-z]+)$/,"$1_on$2"));
$("#tab_box div:not("+$("#tabs li a.selected").attr("href")+")").hide();
//クリックされた時の処理
$("#tabs li a").click(function(){
	var name1=$("#tabs li a.selected").attr("href");//今の#tabs li a.selectedのhrefの値を保存しておく
	$("a.selected img").attr("src",$("a.selected img").attr("src").replace(/^(.+)_on(\.[a-z]+)$/,"$1$2"));
	$("#tabs li a").removeClass("selected");
	$(this).addClass("selected");
	$("img",this).attr("src",$("img",this).attr("src").replace(/^(.+)_on(\.[a-z]+)$/,"$1$2"));
	$("img",this).attr("src",$("img",this).attr("src").replace(/^(.+)(\.[a-z]+)$/,"$1_on$2"));
	$(name1).hide();
	var name2=$("#tabs li a.selected").attr("href");//クリックされた#tabs li a.selectedのhrefの値を保持
	$(name2).show();
	return false;
	})
})

マウスオーバーの処理
$(function(){
	$("#tabs a").mouseover(function(){
	var className = $(this).attr('class');
	if(className!='selected'){
		$("img",this).attr("src",$("img",this).attr("src").replace(/^(.+)(\.[a-z]+)$/, "$1_on$2"))									
	}
}).mouseout(function(){
var className2 = $(this).attr('class');
if(className2!='selected'){
	$("img",this).attr("src",$("img",this).attr("src").replace(/^(.+)_on(\.[a-z]+)$/,"$1$2"))
}
})
})*/

// *************************************************
// 画像ロールオーバー
// *************************************************

function setRollOver(){
	var postfix = '_on';
	$('.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(){
	$('#gnavi li.menu').bind('mouseover', jsddm_open);
	$('#gnavi 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;
}

// *************************************************
// 最新記事アイコン表示
// *************************************************

function NewMarkUp(){
	// passage time
	var pass = 168;
	 
	// display content
	var content = '&nbsp;&nbsp;<img src="/img/icn_www.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');
}

