///////////////////////////////////////////////////////////////////////////////////////////
/* RMA *///////////////////////////////////////////////////////////////////////////////////

$(document).ready(function(){
	
	if($.os.name != 'iphone' && $.os.name != 'ipod' && $.os.name != 'ipad' && $.os.name != 'ipad' && navigator.userAgent.indexOf("Android")<0) {
		$("img", "#top, #project_text, #news_right, #column").lazyload({ 
		    placeholder : "/common/css/pixel.gif",
		    effect : "fadeIn",
		    threshold : 100
		});	
	}
	
	$("#project_text a img, #project #gallery_left nav ul li a img").mouseover(function(){
		$(this).fadeTo(150, 0.85);
	}).mouseout(function (){
		$(this).fadeTo(80, 1);
	});
	$("#top a img").mouseover(function(){
		$(this).fadeTo(120, 0.85);
	}).mouseout(function (){
		$(this).fadeTo(80, 1);
	});
	
	
	var url = location.href;
	if(url.charAt(url.length-1) == "/"){
		url = url.substr(0, url.length-1);
	}
	url = url.split("/tags/").join("/works/tags/");
	url = url + "/";
	$("a").each(function(){
		var href = $(this).attr("href");
		if(href && href.charAt && href.charAt(href.length-1) == "/"){
			href = href.substr(0, href.length-1);
		}
		if(href) href = href.split("/tags/").join("/works/tags/");
		href = href + "/";
		if( url.indexOf(href) > -1 && url.length == href.length + url.indexOf(href)){
			$(this).addClass("selected");
		}	
		if( url.indexOf(href) > -1){
			//$(this).addClass("selected");
		}
	});
	
	
	//publications
	if($("#publications").length > 0){
		publicationsView = new PublicationsView();
		
		$(".publications_entry").each(function(){
			var publicationsEntry = new PublicationsEntry(this);
			publicationsView.entries.push(publicationsEntry);
			//グループ分け
			var classNames = ["publications_2011-2010", "publications_2009-2007", "publications_2006-2004"];
			var classNamesLen = classNames.length;
			for(var i = 0; i < classNamesLen; i++){
				var className = classNames[i];
				if( $(this).hasClass(className) ){
					if(!publicationsView.entriesGroups[className]) publicationsView.entriesGroups[className] = [];
					publicationsView.entriesGroups[className].push(publicationsEntry);
				}
			}
			
		});
		publicationsView.entries[0].onRollOver();
		
		//hash changed
		$(window).bind( 'hashchange', function(e) {
		  
		  var url = $.param.fragment();
		  
		  if(url == ""){
		  	$('.publications_entry').show().fadeTo(0,0).fadeTo(300,1);
		  	publicationsView.entries[0].onRollOver();
		  	var imjJ = $(".article_publications_right .publications_img_container");
		  	imjJ.css({"margin-top": 0});
		  }else{
		  	$('.publications_entry').fadeTo(0,0).hide();
		  	$('.publications_'+url).show().fadeTo(300,1);
		  	publicationsView.entriesGroups['publications_'+url][0].onRollOver();
		  	var imjJ = $(".article_publications_right .publications_img_container");
		  	imjJ.css({"margin-top": 0});
		  }
		  
		  $("nav a", "#tags").each(function(){
			$(this).removeClass("selected");
		  });
		  $( 'nav a[href="#' + url + '"]' , "#tags").addClass("selected");
		})
		
		$(window).trigger('hashchange');
		
	}
	
	//profile
	if($("#profile").length > 0){
		profileView = new ProfileView();
	}
	
	//project
	if($("#project").length > 0){
		projectTextView = new ProjectTextView($("#project_text"));
		projectGalleryView = new ProjectGalleryView($("#project_gallery"));
		
		if(projectGalleryView.thumbnailAr.length > 0) projectGalleryView.firstImg();
		
		$(window).bind( 'hashchange', function(e) {
		  var url = $.param.fragment();
		  
		  if(url == "" || url == "text"){
			projectTextView.show();
		  	projectGalleryView.hide();
		  }else{
		  	projectTextView.hide();
		  	projectGalleryView.show();
		  }
		  
		  $("nav a", "#proj_header").each(function(){
			$(this).removeClass("selected");
		  });
		  $( 'nav a[href="#' + url + '"]' , "#proj_header").addClass("selected");
		  if(url == "") $( 'nav a[href="#text"]' , "#proj_header").addClass("selected");
		})
		
		$(window).trigger('hashchange');
		
	}
	
});


///////////////////////////////////////////////////////////////////////////////////////////
/* PROJECT *///////////////////////////////////////////////////////////////////////////////

//ProjectTextView
var ProjectTextView = function(){
     this.initialize.apply(this, arguments);
}
ProjectTextView.prototype = {
     initialize: function(j) {
		this.j = j;
     }
     
     ,show: function(){
     	this.j.show().fadeTo(300,1);
     }
     ,hide: function(){
     	this.j.fadeTo(0,0).hide();
     }
}

//ProjectGalleryView
var ProjectGalleryView = function(){
     this.initialize.apply(this, arguments);
}
ProjectGalleryView.prototype = {
     initialize: function(j) {
		this.j = j;

		this.mainImage = new ProjectGalleryMainImage($("#gallery_image", this.j));

		this.thumbnailAr = new Array();		
		var ref = this;
		var num = 0;
		$("nav li a", this.j).each(function(){
			ref.thumbnailAr.push( new ProjectGalleryThumbImage($(this), num) );
			num++;
		});
     }
     ,firstImg: function(){
     	this.thumbnailAr[0].onClick();
     }
     ,show: function(){
     	$('body').animate({"scrollTop": 0}, 250, "easeInQuart");
     	this.j.show().fadeTo(300,1);
     }
     ,hide: function(){
     	this.j.fadeTo(0,0).hide();
     }
     ,setImgId: function(id){
     	$("#gallery_count_current", this.j).empty().text(id+1);
     	
     	var len = this.thumbnailAr.length;
     	for(var i = 0; i < len; i++){
     		var thumb = this.thumbnailAr[i];
     		thumb.removeSelect();
     	}
     	this.thumbnailAr[id].addSelect();     	
     }
     ,setCredit: function(text){
     	$("#gallery_credit", this.j).empty().text(text);
     }
}

//Gallery Main
var ProjectGalleryMainImage = function(){
     this.initialize.apply(this, arguments);
}
ProjectGalleryMainImage.prototype = {
     initialize: function(j) {
		this.j = j;
     }
     ,setImg: function(file){
     	this.j.empty().addClass('loading').show();
     	var img = new Image();
     	this.$img = $(img);
     	var ref = this;
     	this.$img.load(function() {
     		if(ref.$img.attr('src') == $(this).attr('src')){
				$(this).hide();
				ref.j.removeClass('loading').append(this);
				$(this).fadeIn();
				if($(this).width() > 0 && $(this).height() > 0){
					$("#gallery_count").animate({"padding-left": (580-$(this).width())+"px"}, 0);
					$("#gallery_image").animate({"height": ($(this).height())+"px"}, 300, 'easeInQuart');
					$("#gallery_meta").css({visibility: "visible"});
				}else{
					var tim = setInterval(function(){
						if(ref.$img.width() > 0 && ref.$img.height() > 0){
							$("#gallery_count").animate({"padding-left": (580-ref.$img.width())+"px"}, 0);
							$("#gallery_image").animate({"height": (ref.$img.height())+"px"}, 300, 'easeInQuart');
							$("#gallery_meta").css({visibility: "visible"});
							clearInterval(tim);
						}
					}, 100);
				}
			}
		})
		.error(function() {
		})
		.attr('src', file);
     }
}

//Gallery Thumb
var ProjectGalleryThumbImage = function(){
     this.initialize.apply(this, arguments);
}
ProjectGalleryThumbImage.prototype = {
     initialize: function(j, id) {
		this.j = j;
		this.id = id;
		this.mainImg = this.j.attr("href");
		this.credit = this.j.attr("credit");
		this.j.attr("href", "javascript:void(0);");
		this.j.removeAttr("credit");
		
		var ref = this;
		this.j.click(function(){
			ref.onClick();
		})
     }
     ,onClick: function(){
     	projectGalleryView.mainImage.setImg(this.mainImg)
     	projectGalleryView.setImgId(this.id);
     	projectGalleryView.setCredit(this.credit);
     }
     ,addSelect: function(){
     	this.j.addClass("selected");
     }
     ,removeSelect: function(){
     	this.j.removeClass("selected");
     }
}


///////////////////////////////////////////////////////////////////////////////////////////
/* PUBLICATIONS *//////////////////////////////////////////////////////////////////////////

//PublicationsEntry
var PublicationsEntry = function(a){
     this.a;
     this.imgName;
     this.initialize.apply(this, arguments);
}

PublicationsEntry.prototype = {
     initialize: function(div) {
		this.a = $('a', div);
		this.imgName = $(this.a).attr("img");
		//$(this.a).attr("imgName", this.imgName);
		//$(this.a).attr("href", "javascript:void(0);");
		
		var ref = this;
		$(this.a).mouseover(function(){
			ref.onRollOver();
		});
     },
     
     onRollOver: function() {
     	if(publicationsView.$img && publicationsView.$img.attr("src") == this.imgName) return;
     	publicationsView.setImg(this.imgName, $(this.a).offset().top - PublicationsView.firstAnchorY);
     }
}

//PublicationsView
var PublicationsView = function(){
     this.initialize.apply(this, arguments);
}
PublicationsView.prototype = {
     initialize: function() {
	    this.entries = new Array();
	    this.entriesGroups = {};
    	PublicationsView.firstAnchorY = $(".publications_entry a:first").offset().top;
     }
     , setImg: function(fileName, y) {
     
     	var rightJ = $(".article_publications_right .publications_img_container");
     	
     	rightJ.empty().addClass('loading').show();
     	var img = new Image();
     	publicationsView.$img = $(img);
     	var ref = this;
     	publicationsView.$img.load(function() {
     		if(publicationsView.$img.attr('src') == $(this).attr('src')){
	     		rightJ.removeClass('loading').append(this);
	     		$(this).fadeTo(0,0).fadeTo(200,1);
     		}
     	})
     	.error(function() {
     	})
     	.attr('src', fileName);
     	
     	rightJ.css({"margin-top": y});
     }
}

//ProfileView
var ProfileView = function(){
     this.initialize.apply(this, arguments);
}
ProfileView.prototype = {
     initialize: function() {
     	var ref = this;
     	$(window).bind( 'hashchange', function(e) {
     	  
     	  var url = $.param.fragment();
     	  
     	  $("nav a", "#tags").each(function(){
     	  	$(this).removeClass("selected");
     	  });
     	  
     	  if(url == "" || url == "biography"){
     	  	$('article#article_firm').fadeTo(0,0).hide();
     	  	$('article#article_biography').show().fadeTo(300,1);
     	  	$( 'nav a[href="#' + 'biography' + '"]' , "#tags").addClass("selected");
     	  }else{
	     	$('article#article_biography').fadeTo(0,0).hide();
     	  	$('article#article_firm').show().fadeTo(300,1);
  	  		$( 'nav a[href="#' + 'firm' + '"]' , "#tags").addClass("selected");
     	  }
     	  $("nav a", "#contents_header").each(function(){
     	  	$(this).removeClass("selected");
     	  });
     	  $( 'nav a[href="#' + url + '"]' , "#contents_header").addClass("selected");
     	})
     	
     	$(window).trigger('hashchange');
     }
}


