var o;

$(function(){
	
	o = {
		$win:			$(window),
		$doc:			$(document),
		$body:		$('body'),
		$catList:	$('ul#cat li'),
		$en:			$('.en'),
		$jp:			$('.jp'),
		$post:		$('#content .post'),
		$thumb:		$('#content .post a img'),
		$spacer:	$('img.spacer'),
		$pubs:		$('#publications .post'),
		$single:	$('#single p'),
		$concept:	$('#single .concept'),
		$data:		$('#single .data'),
		$sNav:		$('#single ul#single_nav'),
		$sNavLi:	$('#single ul#single_nav li'),
		$caption:	$('#content .caption'),
		$topNewsImg:	$('#news a img'),
		$newsImgs:$('.news_img img')
	};


	/* browser check -------------------------------------------- */
	if (navigator.platform.indexOf("Win") != -1) { //Windows
		o.$body.addClass("win");
	} else { // Mac
	}


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




/* lang setting -------------------------------------------- */

	var newsLng;
	if($.cookie("lang", {path: '/'}) ){
		if($.cookie('lang') == 'jp'){
			o.$jp.fadeIn(0);
			o.$en.fadeOut(0);
		}
		else{
			o.$en.fadeIn(0);
			o.$jp.fadeOut(0);
		}
	}else{
		$.cookie("lang", 'jp', {path: '/'});
		o.$jp.fadeIn(0);
		o.$en.fadeOut(0);
	}
	newsLng = $.cookie("lang", {path: '/'})

	var pageName = window.location.pathname;	

	$('#japanese, ul#nav li.last a').click(function(){
		$.cookie('lang', 'jp', {path: '/'});
		newsLng = $.cookie("lang", {path: '/'}); //newsのテキストを日本語に
		o.$en.fadeOut(0);
		o.$jp.fadeIn(200);
		window.location.reload();
	});
	$('#english').click(function(){
		$.cookie('lang', 'en', {path: '/'});
		newsLng = $.cookie("lang", {path: '/'}); //newsのテキストを英語に
		o.$jp.fadeOut(0);
		o.$en.fadeIn(200);
		window.location.reload();
	});
	
	
/* single page -------------------------------------------- */
	o.$single.simplePhotoSlide();
	if(o.$single.children('iframe').length > 0){
		$('.next-bt').remove();
		$('#single_nav-jp li, #single_nav-en li').remove();
	}
	
	$('#single_inner p img').each(function(){
		if($(this).width() > 570 || $(this).height() > 570){
			$(this).width($(this).width()*0.79).height($(this).height()*0.79);
		}
	});
	
	if($('#single .concept-jp').text()) $('#single_nav-jp').append('<li class="cncpt jp"><span>▷ outline</span></li>');
	if($('#single .concept-en').text()) $('#single_nav-en').append('<li class="cncpt en"><span>▷ outline</span></li>');
	if($('#single .data-jp').text()) $('#single_nav-jp').append('<li class="dt jp"><span>▷ data</span></li>');
	if($('#single .data-en').text()) $('#single_nav-en').append('<li class="dt en"><span>▷ data</span></li>');
	if($('#single .concept-en').text()) console.log("hoge");
	
	//init - quart
	jQuery.easing.quart = function (x, t, b, c, d) { return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b; }
	
	$('.photo').click(function(){ $('#single_inner').animate({marginTop: '0px'}, 600, 'quart'); });
	$('.cncpt').click(function(){ $('#single_inner').animate({marginTop: '-570px'}, 600, 'quart'); });
	$('.dt').click(function(){ $('#single_inner').animate({marginTop: '-1180px'}, 600, 'quart'); });



/* top pagesetting ------------------------------------------ */
	var pos = new Array('left','right','top','bottom');
	
	o.$thumb.each(function(){
		var rnd = Math.floor(Math.random()* 2);
		var w = $(this).width();
		var h = $(this).height();
		var wh = w/h;
		var preW;
		var preH;
		var fixedPos;
		
		if(h > w) {
			preW = w - (20*wh);
			preH = h - 20;
			fixedPos = 'top';
		} else {
			rnd += 2;
			preW = w - 20;
			preH = h - (20*wh);
			fixedPos = 'left';
		}
		
		$(this).width(preW).height(preH).css(pos[rnd], '10px').css(fixedPos, '10px');

		$(this).hover(
			function(i){
				var i = o.$thumb.index(this)
				$(this).stop(true, false).css(pos[rnd], '0px').css(fixedPos, '0px').width(w).height(h);
				o.$caption.eq(i).fadeIn(200);
			},
			function(){
				$(this).stop(true, false).css(pos[rnd], '10px').css(fixedPos, '10px').width(preW).height(preH);
				o.$caption.fadeOut(0);
			}
		);
	});
	


/* category sort ------------------------------------------*/
	var hash = window.location.hash;
	var preHash = '';
	var timer;
	var selOb;

	$('.post:first').addClass('first');
	
	if(hash) t();
	else if(hash == '' && preHash == '') o.$thumb.fadeTo(500, 1, t);

	//watching hash
	function t(){
		timer = setInterval(function(){
			hash = window.location.hash;
			
			if(hash != preHash) { //change hash
				if(hash == ''){
					o.$thumb.stop(true, false).fadeTo(500, 1);
					o.$spacer.stop(true, false).fadeOut(0);
				}else{
					selOb = $('.' + hash.slice(1) + ' a img');
					o.$thumb.stop(true, false).not(selOb).fadeTo(100, 0.3, function(){
						selOb.stop(true, false).fadeTo(500, 1);
						o.$spacer.stop(true, false).fadeIn(0);
						$('.' + hash.slice(1)).children('img.spacer').fadeOut(0);
					});
				}
			}
			preHash = hash;
		}, 30);
	}	
	
	//colorbox
	$('#content .post a').attr('rel', 'iframe');

	var userAgent = window.navigator.userAgent.toLowerCase();
	var appVersion = window.navigator.appVersion.toLowerCase();	
	if (userAgent.indexOf("msie") > -1) {
		if (appVersion.indexOf("msie 7.0") > -1) {
			$('a[rel="iframe"]').colorbox({width:"640px", height:"670px", iframe:true, transition:"fade"});
		}else{
			$('a[rel="iframe"]').colorbox({width:"600px", height:"600px", iframe:true, transition:"fade"});
		}
	}
	else{
		$('a[rel="iframe"]').colorbox({width:"600px", height:"600px", iframe:true, transition:"fade"});
	}

	
	/*o.$post.click(function(){
		$(this).addClass('opend').fadeTo(100, 0);
	});*/
	o.$doc.bind('cbox_complete', function(){
		$('#colorbox').css({
			'-webkitBoxShadow': '0px 0px 10px 2px #777',
			'-mozBoxShadow': '0px 0px 10px 2px #777',
			'box-shadow': '0px 0px 10px 2px #777'
		});		
	});
	o.$doc.bind('cbox_closed', function(){
		$('#colorbox').css({
			'-webkitBoxShadow': '0px 0px 0px 0px #777',
			'-mozBoxShadow': '0px 0px 0px 0px #777',
			'box-shadow': '0px 0px 0px 0px #777'
		});		
	});



/* news ------------------------------------------*/
	
	o.$topNewsImg.removeAttr('height').width('153');
	
	o.$newsImgs.each(function(){
		if($(this).height() > $(this).width()){
			var sWidth = (322 / 342) * $(this).width();
			$(this).height(322).width(sWidth);
		}else{
			$(this).removeAttr('height').width(322);
		}
	});
	o.$newsImgs.fadeIn(500);

	$('#news a img, #news_meta a, #nav li.last a').hover(
		function(){
			$('#content, #pages_wrapper, #publications').stop(true, false).fadeTo(100, 0.1, function(){
				$('.news_txt_' + newsLng).fadeIn(100);
			});
		},
		function(){
			$('#content, #pages_wrapper, #publications').stop(true, false).fadeTo(0, 1, function(){
				$('.news_txt_' + newsLng).fadeOut(0);
			});
		}
	);


	
/* publications -------------------------------------------- */
	o.$pubs.fadeTo(500, 1);


/* map ------------------------------------------*/	
	if(window.location.pathname.indexOf('contact') != -1) {
		//o.$body.attr('onload', 'GUnload()');
		//initialize();
	}
});

function initialize() {
  if(GBrowserIsCompatible()) {
  	var lat = 35.6355318;
  	var lng = 139.6456928;
  	var latlng = new GLatLng(lat, lng);
    var map = new GMap2(document.getElementById("map_canvas"));
    map.setCenter(latlng, 13);
    map.addControl(new GSmallMapControl());
    map.enableScrollWheelZoom();//ホイールズーム有効化

		var tinyIcon = new GIcon();
		tinyIcon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
		tinyIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
		tinyIcon.iconSize = new GSize(12, 20);
		tinyIcon.shadowSize = new GSize(22, 20);
		tinyIcon.iconAnchor = new GPoint(6, 20);
		tinyIcon.infoWindowAnchor = new GPoint(5, 1);
		
		// Set up our GMarkerOptions object literal
		markerOptions = { icon:tinyIcon };

    // Add 10 markers to the map at random locations
		map.addOverlay(new GMarker(latlng, markerOptions));
  }
}


$(window).load(function(){
	//o.$post.fadeTo(500, 1);
	$('#single p').hover(
		function(){ $(this).css('cursor','pointer'); },
		function(){ $(this).css('cursor','default'); }
	);
});

