
$(document).ready(function() {
	imgRollover();
	imgFade();
	objHeight();
    tabNavi();
	sidebarObj();
});

$(window).load(function() {
	objHeight();
});




/**********************************************************
* onload */

function addEvent(elm,listener,fn){
	try{
		elm.addEventListener(listener,fn,false);
	}catch(e){
		elm.attachEvent("on"+listener,fn);
	}
}




/**********************************************************
* imgRollover */

function imgRollover() {
	$('img.rollover, input.rollover').not('[src*="_on."],[src*="_over."]').each(function(){
		var img = $(this);
		var src = img.attr('src');
		var src_on = src.substr(0, src.lastIndexOf('.')) + '_over' + src.substring(src.lastIndexOf('.'));
		$('<img>').attr('src', src_on);
		img.hover(function(){
			img.attr('src', src_on);
		},function(){
			img.attr('src', src);
		});
	});
}




/**********************************************************
* imgFade */

function imgFade() {
	$('img.fade').not('[src*="_on."],[src*="_over."]').each(function(){
		$(this).fadeTo(0,1.0);
		$(this).hover(function(){
			$(this).stop();
			$(this).fadeTo(200,0.7);
		},function(){
			$(this).stop();
			$(this).fadeTo(200,1.0);
		});
	});
}




/**********************************************************
* objHeight */

function objHeight() {
	var obj = 0;
	$('.objH').css('height', '');
	$('.objH').each(function(){
		var h = this.offsetHeight;
		if (h > obj) obj = h;
	});
	$('.objH').css('height', obj + 'px').css('display', 'block');
}




/**********************************************************
* tabNavi */

function tabNavi(){
	$('#tabNavi a').each(function(i){

		var img = $('img',this);
		var src = img.attr('src');
		var src_on = src.substr(0, src.lastIndexOf('.')) + '_on' + src.substring(src.lastIndexOf('.'));
		$('<img>').attr('src', src_on);
		var src_over = src.substr(0, src.lastIndexOf('.')) + '_over' + src.substring(src.lastIndexOf('.'));
		$('<img>').attr('src', src_over);

		var check;
		var hash = window.location.hash;
		var id = $(this).attr('href');
		if (hash) {
			if (hash.match(id)) {
				$(id).css('display','block');
			} else {
				$(id).css('display','none');
			}
		} else {
			if (i == 0) {
				$(id).css('display','block');
			} else {
				$(id).css('display','none');
			}
		}
		check = $($(this).attr('href')).css('display');
		if (check == 'block') img.attr('src', src_on);

		img.hover(function(){
			check = $($(this).parent().attr('href')).css('display');
			if (check == 'none') img.attr('src', src_over);
		},function(){
			check = $($(this).parent().attr('href')).css('display');
			if (check == 'none') img.attr('src', src);
		});
		img.click(function(){
			$('#tabNavi img').each(function(){
				var src = $(this).attr('src').replace(/_on/,'');
				$(this).attr('src', src);
			});
			img.attr('src', src_on);
		});

		$(this).click(function(){
			hash = window.location.hash;
			$('#tabNavi a').each(function(){
				id = $(this).attr('href');
				$(id).css('display','none');
			});
			id = $(this).attr('href');
			$(id).css('display','block');
			return false;
		});

		var srcBg = $('#tabNavi').css('backgroundImage').replace(/_over/,'');
		var srcBg_on = srcBg.replace(/.gif/,'_over.gif');
		$(this).hover(function(){
			if ($(this).attr('class') == 'bgOver') {
				$('#tabNavi').css('backgroundImage',srcBg_on);
			}
		},function(){
			if ($(this).attr('class') == 'bgOver') {
				if ($('#box7').css('display') == 'block' || $('#box8').css('display') == 'block') {
					$('#tabNavi').css('backgroundImage',srcBg_on);
				} else {
					$('#tabNavi').css('backgroundImage',srcBg);
				}
			}
		});
		$(this).click(function(){
			if ($(this).attr('class') != 'bgOver') {
				$('#tabNavi').css('backgroundImage',srcBg);
			}
		});

	});

	var srcBgCheck = false;
	$('#tabNavi1 a.bgOver').each(function(i){
		if ($('img',this).attr('src').match(/_over/)) srcBgCheck = true;
	});
	$('#tabNavi1 a.bgOver').each(function(i){
		var srcBg = $('#tabNavi1').css('backgroundImage').replace(/_over/,'');
		var srcBg_on = srcBg.replace(/.gif/,'_over.gif');
		if (srcBgCheck) {
			$('#tabNavi1').css('backgroundImage',srcBg_on);
		} else {
			$(this).hover(function(){
				$('#tabNavi1').css('backgroundImage',srcBg_on);
			},function(){
				$('#tabNavi1').css('backgroundImage',srcBg);
			});
		}
	});
}




/**********************************************************
* sidebarObj */

function sidebarObj() {
	var $sidebar   = $('#sidebar'),
		$window    = $(window),
		offset     = $sidebar.offset(),
		topPadding = 0;

	$window.scroll(function() {
		if ($window.scrollTop() > offset.top) {
			$sidebar.stop().animate({
				marginTop: $window.scrollTop() - offset.top + topPadding
		});
		} else {
			$sidebar.stop().animate({
				marginTop: 0
			});
		}
	});
}




/**********************************************************
* openWindow */

function openWindow(winURL,winName,winW,winH) {
	var winFeatures = 'toolbar=no,resizable=no,menubar=no,directories=no,scrollbars=yes,status=no,location=no,width=' + winW + ',height=' + winH + '';
	window.subwin = window.open(winURL,winName,winFeatures);
	window.subwin.focus();
}

