$(document).ready(function(){

	$('#bg ul').hide().fadeTo(2000,1).innerfade({
		speed: 3000,
		timeout: 5000,
		type: 'random_start', //type: Type of slideshow: 'sequence', 'random' or 'random_start' (Default: 'sequence'),
		containerheight: '100%'
	});

	//browser check -------------------------------------------- */
	if (navigator.platform.indexOf("Win") != -1) {
		$('body').addClass("win");
		if ( !jQuery.support.leadingWhitespace ) { $('body').addClass("win-ie"); }
	}

	//rollover -------------------------------------------- */
	$('a:has(img), input[type="image"]').live("mouseover", function(){
		var img = $(this).find('img');
		if (img.is('[src$=".0.png"]')) {
			hover_on(img);
		}
	}).live("mouseout",function() {
		var img = $(this).find('img');
		if (img.is('[src$=".1.png"]')) {
			hover_out(img);
		}
	});

	//rollovar - alpha --------------------------------------------
	$('a.alpha').hover(function() {
		$(this).stop(true, false).animate({ opacity: .5 }, { duration: 200});
		if ( !jQuery.support.leadingWhitespace ) { this.style.removeAttribute('filter'); }
	}, function () {
		$(this).stop(true, false).animate({ opacity: 1 }, { duration: 200});
		if ( !jQuery.support.leadingWhitespace ) { this.style.removeAttribute('filter'); }
	});

	//index-topics --------------------------------------------
	$('#index-topics dl').addClass('clearfix');
	//$('#index-topics dt').addClass('fm-crimson_hiraM3');
	var w = window_size();
	fit_index(w.x, w.y);
	fit_bg(w.x, w.y);
	window_scroll(w.x, w.y);
});

$(window).load(function() {
	$('#index div').not('#debug').show();
	$(window).resize(function(){
		var w = window_size();
		fit_bg(w.x, w.y);
		fit_index(w.x, w.y);
		window_scroll(w.x, w.y);
	//	$('body').css('height','auto');
	//	fitFooter();
	});

});



function window_scroll (x,y) {
	if ( x < 950 ) { 
		$('html').css('overflow-x','scroll'); } else { $('html').css('overflow-x','hidden');
	}
	if ( y < 600 ) { 
		$('html').css('overflow-y','scroll'); } else { $('html').css('overflow-y','hidden');
	}
}

function fit_index (x, y) {
	//width:950px height:194px;
	var mTop, mLeft;
	if ( x < 950 ) { 
		mLeft = 0;
	} else {
		mLeft = x /2 - 475;
	}
	if ( y < 600 ) { 
//		mTop = 208;
		mTop = y /2 - 92;
	} else {
		mTop = y /2 - 92;
	}
	$('#header').css({
		'paddingTop':mTop + 'px',
		'marginLeft':mLeft + 'px'
	});
}

function fit_bg (x,y) {
	debug("w",x,"w-width");
	debug("h",y,"w-height");

	if (x / y > 1.5) {
		$('#bg ul img').show().width(x).css('height','auto');
		//alert(x/y);
	}
	if (x / y <= 1.5) {
		$('#bg ul img').height(y).css('width','auto');
	}
}

//window size -------------------------------------------- */
function window_size () {
	var windowWidth = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth || $(window).width();
	var windowHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight || $(window).height();
	return { x:windowWidth, y:windowHeight };
}

function hover_on (target) {
	var onSrc = target.attr('src').replace('.0.png', '.1.png');
	target.attr('src', onSrc);
}

function hover_out (target) {
	var offSrc =target.attr('src').replace('.1.png', '.0.png');
	target.attr('src', offSrc);
}

function debug (name,value,target) {
	if ($('#debug').find('#' + target).text()) {
		$('#' + target).html(name + ' : ' + value);
	} else {
		$('#debug').prepend('<div id="' + target + '">'+ name + ' : ' + value + '</div>');
	}
}

