var $objects = new Object();

	if (!("console" in window)) {
		window.console = {};
		window.console.log = function(str){
			return str;
		};
	}

//--------------------------------------------
// ready 
//--------------------------------------------
$(document).ready(function(){

	//--------------------------------------------
	// common 
	//--------------------------------------------
	//browser check -------------------------------------------- */
	if (!jQuery.support.fixedPosition) { }
	if (navigator.platform.indexOf("Win") != -1) {
		$objects.os = "win"; $('body').addClass("win");
		if (!jQuery.support.leadingWhitespace ) { $('body').addClass("win-ie"); }
		if(!jQuery.support.style){
			if (typeof document.documentElement.style.maxHeight != "undefined") {
				//IE7 or IE8
				$objects.browser = "IE7";
				$('body').addClass("win-ie7");
			} else {
				//IE6
				$objects.browser = "IE6";
				$('body').addClass("win-ie6");
				//$('.header').before('<div id="ie6">最新のブラウザにアップグレードしてください。 / Please upgrade to a modern browser.</div>');
			}
		}
	}

	//scroll -------------------------------------------- */
	$('a.a-pagetop').click( function() { 
		$('html,body').animate({ scrollTop: 0 }, 600, 'quart');
		$(this).blur();
		return false;
	});

	//scroll - anchor  -------------------------------------------- */
	$('a.a-anchor').click( function() { 
		var target = $(this).attr('href');
		var scroll = $(target).offset();
		$('html,body').animate({ scrollTop: scroll.top }, 600, 'quart');
		$(this).blur();
		return false;
	});

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

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

	//mail -------------------------------------------- */
	$('.a-mail').each(function() {
		var e = $(this).html().split(' ').join('@');
		$(this).attr('href','mailto:'+ e).html(e);
	});

	//--------------------------------------------
	// script
	//--------------------------------------------
	//objects -------------------------------------------- */
	$objects = {
		html : $('html'),
		body : $('body'),
		header : $('#header'),
		nav : $('#nav'),
		news : $('#nav-news'),
		anchor : $('#anchor'),
		section : $('.section'),
		items : $('.items').children()
	};

	//init ------------------------------
	window_size();
	resize_section();

	$('#preload').show();
	$objects.html.height($objects.h).css('overflow-y','scroll');
	$objects.body.height($objects.h).css('overflow','hidden');

	//news ticker ------------------------------ */
	$('#nav-news ul').innerfade({
		speed: 1000,
		timeout: 5000,
		type: 'sequence',
		containerheight: '3em'
	});
	

	//item ------------------------------ */
	$objects.items.each(function() {
		//hover ------------------------------ */
		$(this).find('a').hover(function() {
			var target = $(this).attr('rel');
			if ( jQuery.support.opacity ) {
				$('#'+target).siblings('dd').hide().end().fadeIn();
			} else {
				$('#'+target).siblings('dd').hide().end().show();
			}
		},function() {
			var target = $(this).attr('rel');
			if ( jQuery.support.opacity ) {
				$('#'+target).fadeOut();
			} else {
				$('#'+target).hide();
			}
		});
		
	});

});

//--------------------------------------------------------------------------
// load 
//--------------------------------------------------------------------------
$(window).load(function() {
	
	$('#preload').fadeOut(800,function() {
		$(this).siblings().not('#preload, #header').show();
		$objects.html.css('overflow-y','scroll');
		$objects.body.css('height','auto');
	})

	var scroll = $(window).scrollTop() || $(document).scrollTop();
	var scroll_p = Math.floor(scroll / $objects.h);
	anchor_point(scroll_p);
	
	$objects.scroll_shadow = 50 / $objects.h;
	
	//anchor ------------------------------ */
	$objects.anchor.delay(1000).animate({
			backgroundPositionX: '0px'
		}, 600, 'quart',function() {
			$(this).css('background-position','0px 0px');
			$(this).find('img').fadeIn(200).delay(400).fadeOut(200);
			$(this).delay(1000).animate({
				backgroundPositionX: '-200px'
			}, 600, 'quart',function() {
				$(this).css('background-position','-200px 0px');
			});
		}
	).hover(function() {
		$(this).find('img').show();
		$(this).stop(true, false).animate({
			backgroundPositionX: '0px'
		}, 600, 'quart',function() {
			$(this).css('background-position','0px 0px');
		});
	}, function() {
		$(this).find('img').hide();
		$(this).stop(true, false).animate({
			backgroundPositionX: '-200px'
		}, 600, 'quart', function() {
			$(this).css('background-position','-200px 0px');
		});
	}).find('a').hover(function() {
		$(this).stop(true, false).animate({
			paddingLeft: '25px'
		}, 200, 'quart');
	},function() {
		$(this).stop(true, false).animate({
			paddingLeft: '20px'
		}, 200, 'quart');
	});

});

//--------------------------------------------
// scroll 
//--------------------------------------------
$(window).scroll(function () {
	var scroll = $(window).scrollTop() || $(document).scrollTop();
	var scroll_p = scroll / ($objects.h / 1.05);
	var scroll_in = scroll - Math.floor(scroll / $objects.h) * $objects.h;
	//console.log('scroll:' + scroll + ' / scroll_p:' + scroll_p + ' / scroll_in:' + scroll_in);
	
	anchor_point(Math.floor(scroll_p));
	
	//section.background ------------------------------
/* css版
	if ( 0 < scroll_p && scroll_p < 2.1 ) {
		$('#concept .bg').css('bottom','-' + (scroll - $objects.h * 0.8) / 5 - 40 + 'px');
	}
	if ( 1.05 < scroll_p && scroll_p < 3.15 ) {
		$('#optimism .bg').css('bottom','-' + (scroll - $objects.h * 1.8) / 5 - 40 + 'px');
	}
	if ( 2.1 < scroll_p && scroll_p < 4.2 ) {
		$('#relaxation .bg').css('bottom','-' + (scroll - $objects.h * 2.8) / 5 - 40 + 'px');
	}
	if ( 3.15 < scroll_p && scroll_p < 5.25 ) {
		$('#specialcare .bg').css('bottom','-' + (scroll - $objects.h * 3.8) / 5 - 40 + 'px');
	}
*/
/* animate版
	if ( 0 < scroll_p && scroll_p < 2.1 ) {
		$('#concept .bg').stop(true, false).animate({bottom : '-' + Math.floor((scroll - $objects.h * 0.8) / 5) - 40 + 'px'},1,'quart');
	}
	if ( 1.05 < scroll_p && scroll_p < 3.15 ) {
		$('#optimism .bg').stop(true, false).animate({bottom : '-' + Math.floor((scroll - $objects.h * 1.8) / 5) - 40 + 'px'},1,'quart');
	}
	if ( 2.1 < scroll_p && scroll_p < 4.2 ) {
		$('#relaxation .bg').stop(true, false).animate({bottom : '-' + Math.floor((scroll - $objects.h * 2.8) / 5) - 40 + 'px'},1,'quart');
	}
	if ( 3.15 < scroll_p && scroll_p < 5.25 ) {
		$('#specialcare .bg').stop(true, false).animate({bottom : '-' + Math.floor((scroll - $objects.h * 3.8) / 5)  - 40 + 'px'},1,'quart');
	}
*/

	//item:line ------------------------------
	$('#optimism ul li.item01').css({ backgroundPositionY : $objects.h - scroll / 3  - 150 });
	$('#optimism ul li.item02').css({ backgroundPositionY : -1000 + scroll / 3 + 110});
	
	$('#relaxation ul li.item01').css({backgroundPositionY : -1000 + scroll / 3 + 110 - $objects.h / 2.8 });
	$('#relaxation ul li.item02').css({ backgroundPositionY : $objects.h * 1.4 - scroll / 3  - 190 });
	
	$('#specialcare ul li.item01').css({backgroundPositionY : $objects.h * 1.7 - scroll / 3  - 150});
	$('#specialcare ul li.item02').css({ backgroundPositionY : -1000 + scroll / 3 + 400 - $objects.h });

	//item:shadow ------------------------------
	if ( 1.5 < scroll_p && scroll_p < 2.5 ) {
		var p = $objects.scroll_shadow * (scroll - $objects.h * 1.5);
		if ( -50 <= p) { $('#optimism ul li a').css('background-position-x',25 - p ); }
	}
	if ( 2.5 < scroll_p && scroll_p < 3.5 ) {
		var p = $objects.scroll_shadow * (scroll - $objects.h * 2.5);
		if ( -50 <= p) { $('#relaxation ul li a').css('background-position-x',25 - p ); }
	}
	if ( 3.5 < scroll_p && scroll_p < 4.5 ) {
		var p = $objects.scroll_shadow * (scroll - $objects.h * 3.5);
		if ( -50 <= p) { $('#specialcare ul li a').css('background-position-x',25 - p ); }
	}



	//header & news ------------------------------
	if (scroll_p  < 1 ) { // top
		$objects.header.fadeOut();
		$objects.news.stop(true, false).animate({
			bottom : '40px'
		}, 500, 'quart');
	} else {
		$objects.news.stop(true, false).animate({
			bottom : '0px'
		}, 500, 'quart');
		if ( scroll_p < 2 ) { // concept
			$objects.header.fadeIn(1000);
			change_image($objects.header.find('img'),'.b.png','.w.png');
		} else {
			$objects.header.show();
			change_image($objects.header.find('img'),'.w.png','.b.png');
			if ( scroll_p >=2 && scroll_p < 3 ) { //optimism
			} else if ( scroll_p >=3 && scroll_p < 4 ) { //relaxation
			} else if ( scroll_p >=4 && scroll_p < 5 ) { //specialcare
			} else if ( scroll_p >=5 && scroll_p < 6 ) { //news
			}
		}
	}

});
//--------------------------------------------
// resize 
//--------------------------------------------
$(window).resize(function(){
		window_size();
		resize_section();
});
	
//jQuery easing -------------------------------------------- *?
jQuery.easing.quart = function (x, t, b, c, d) { return -c * ((t=t/d-1)*t*t*t - 1) + b; };

//--------------------------------------------------------------------------
//functions 
//--------------------------------------------------------------------------
//window size -------------------------------------------- */
function window_size () {
	$objects.w = window.innerWidth || document.body.clientWidth || document.documentElement.clientWidth || document.body.clientWidth || $(window).width();
	$objects.h = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight || $(window).height();
	
/*
	if (navigator.userAgent.match(/iPhone/i)) {
		$objects.w = 640;
		$objects.h = 480;
	} else if (navigator.userAgent.match(/iPad/i)) {
		$objects.w = 640;
		$objects.h = 480;
	}
*/

	console.log('window size:' + $objects.w + "/" + $objects.h );
}

function change_image (target,rp1,pr2) {
	var onSrc = target.attr('src').replace(rp1, pr2);
	target.attr('src', onSrc);
}

function anchor_point(count) {
	$objects.anchor.find('a').each(function(e) {
		$(this).removeClass('current');
		if ( e == count) {
			$(this).addClass('current');
		}
	});
}

function resize_section() {
	//set height ------------------------------
	$objects.section.css({ height:$objects.h});
	
	//set background ------------------------------
	var ratio = $objects.w / $objects.h;
	if (ratio > 1.333333) {
		$objects.section.find('.bg').css({ width:$objects.w, height : 'auto' });
		var top_bg = Math.floor($objects.w / 1.33333);
		if (top_bg > $objects.h) {
			$('#top .bg').css('top', '-' + (top_bg - $objects.h) / 2  - 40 + 'px');
		}
	} else {
		$objects.section.find('.bg').css({ width:'auto', height : $objects.h });
	}

	//set items ------------------------------
	$objects.items.css({ height:$objects.h}).find('a').each(function() {
		var h = Math.floor(($objects.h - $(this).find('img').height()) / 2);
		$(this).css('margin-top','' + h + 'px');
		$(this).parent().parent().siblings('dl').css('margin-top', '' + h + 'px');
	});

	$('#optimism').each(function() {
		var offset = $(this).find('.item02 a').offset();
		$(this).find('dl').css('margin-left',offset.left + 220 + 'px');
	});

	$('#relaxation').each(function() {
		var offset = $(this).find('.item01 a').offset();
		$(this).find('dl').css('margin-left',offset.left -345 + 'px');
	});

	$('#specialcare').each(function() {
		var offset = $(this).find('.item02 a').offset();
		$(this).find('dl').css('margin-left',offset.left + 220 + 'px');
	});

	//set news ------------------------------
	$('#news div').each(function() {
		var height = $(this).height();
		$(this).css({
			marginTop : ($objects.h - height) / 2
		});
	});


}


