/* ----------------------------------------------------*/
// xfader
/* ----------------------------------------------------*/
function slideshow(adjuster) {
	var target = new Array();
	target = $('ul[id^="slideshow"]');
	
	for (var i=0; i<target.length; i++) {
		setTimeout((function(i) {
			return function() {
				var e = target[i];
				xfader(e, adjuster);
			}
		})(i), i * adjuster)
	}
}

function xfader(e, adjuster) {
	var speed = 1000;
	var easing = 'swing';
	var lists = $(e).find('li');
	
	for(var i=0; i<lists.length; i++) {
		var idx = (lists.length) - i;
		$(lists).eq(i).css('zIndex', idx);
	}
	
	if(adjuster != 0) {
		$(lists).first().stop(true,true).animate({
			opacity: 0
		}, speed, easing, function() {
			$(this).css({
				'zIndex': 1,
				'opacity': 1
			});
			$(this).appendTo(e);
		});
	}
}

$(function() {
	if($('ul[id^="slideshow"]').length > 0) {
		$('ul[id^="slideshow"]').find('li').first().css('zIndex', 1);
		var delay = 6000;
		var adjuster = 500;
		
		slideshow(0);
		var interval = setInterval(function() { slideshow(adjuster) }, delay);
	}
});

/* ----------------------------------------------------*/
// crossFader
/* ----------------------------------------------------*/
$(function(){
   $('.ttl_top_creation_over').crossFader({
	timer: 1000,
	speed: 500,
	changeSpd: 500
  });
});
$(function(){
   $('.ttl_top_creation_over_2').crossFader({
	timer: 1000,
	speed: 500,
	changeSpd: 500
  });
});

/* ----------------------------------------------------*/
// masonry
/* ----------------------------------------------------*/
$(function(){
  $('#top').masonry({
    // options
    itemSelector : 'article',
    columnWidth : 240,
    isAnimated: true
  });
});
$(function(){
  $('#sitemap').masonry({
    // options
    itemSelector : 'li.box',
    columnWidth : 240,
    isAnimated: true
  });
});

$(function(){
    var hash_ = location.hash;
    var li = $("#top article");
    var top = $('#top');
    
    if(hash_ != "" && hash_ != "#sort_top")
    {
        searchTxt = hash_.slice(6);
        $(li).sort(function(a, b)
        {
            var nA = $(a).attr("class").lastIndexOf(searchTxt);
            var nB = $(b).attr("class").lastIndexOf(searchTxt);
            return nA < nB ? 1 : -1;
        });
        showHideCategory(searchTxt);
        $(top).masonry( 'reload' );
    }

  $("li.lnav a.sort, #page_footer li a.sort").click(function()
  {
      searchTxt = (this).getAttribute('href','2').slice(6);
      $(li).sort(function(a, b)
      {
          var nA = $(a).attr("class").lastIndexOf(searchTxt);
          var nB = $(b).attr("class").lastIndexOf(searchTxt);
          return nA < nB ? 1 : -1;
      });
      showHideCategory(searchTxt);
      $(top).masonry('reload');
  });

  $("a[href=#sort_top]").click(function()
  {
      $(li).removeClass("heightzero");
      $(li).show();
        searchTxt = (this).getAttribute('href','2').slice(6);
      $(li).sort(function(a, b)
      {
          var fullClassNameA = $(a).attr("class");
          var fullClassNameB = $(b).attr("class");
          var nA1 = fullClassNameA.indexOf(searchTxt) + 4;
          var nA  = fullClassNameA.substring(nA1, nA1+1); 
          var nB1 = fullClassNameB.indexOf(searchTxt) + 4;
          var nB  = fullClassNameB.substring(nB1, nB1+1); 
          
          return nA > nB ? 1 : -1;
      });
      $(top).masonry('reload');
  });
});

/* ----------------------------------------------------*/
// fadeIn
/* ----------------------------------------------------*/
$(document).ready(function(){
    $("#top article.bg_creation h1").hover(function() {
		$("#top article.bg_creation h1 .ttl_top_creation_over").fadeIn();
		$("#top article.bg_creation h1 .ttl_top_creation_over_2").fadeIn();
		$("#top article.bg_creation h1 div.default").fadeOut();
    },function() {
		$("#top article.bg_creation h1 div.default").fadeIn();
		$("#top article.bg_creation h1 .ttl_top_creation_over").fadeOut();
		$("#top article.bg_creation h1 .ttl_top_creation_over_2").fadeOut();
    });
});

/* ----------------------------------------------------*/
// nav
/* ----------------------------------------------------*/
$(function() {
	$('#menu').find('a.current_s').find('img').css('visibility', 'hidden');
	var target = $('#menu').find('a[href^="#sort"]');
	
	$(target).click(function() {
		$(target).find('img.parent').css('visibility', 'visible');
		$(target).removeClass('current_s');
		
		$(this).find('img.parent').css('visibility', 'hidden');
		$(this).addClass('current_s');
	});
	
	var url = window.location.href;
	if(url.match(/#sort/)) {
		var anc = url.substr(url.lastIndexOf('#'), url.length).slice(0);
		
		if(anc == '#sort_home') {
			$(target).find('img.parent').css('visibility', 'visible');
			$(target).removeClass('current_s');
			
			var current = $('#menu').find('a[href^="#sort_home"]')
			
			$(current).find('img.parent').css('visibility', 'hidden');
			$(current).addClass('current_s');
		}
		else if(anc == '#sort_about') {
			$(target).find('img.parent').css('visibility', 'visible');
			$(target).removeClass('current_s');
			
			var current = $('#menu').find('a[href^="#sort_about"]')
			
			$(current).find('img.parent').css('visibility', 'hidden');
			$(current).addClass('current_s');
		}
		else if(anc == '#sort_works') {
			$(target).find('img.parent').css('visibility', 'visible');
			$(target).removeClass('current_s');
			
			var current = $('#menu').find('a[href^="#sort_works"]')
			
			$(current).find('img.parent').css('visibility', 'hidden');
			$(current).addClass('current_s');
		}
	}
});
