jQuery(function($) {
	/**
	 * jquery.scrollTo.js settings
	 */
	// smooth scroll
	$.easing.easeOutExpo = function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	}
	// anchor
	$('a[href^=#]').click(function(){
		var id = $(this).attr('href');
		var offset = Math.floor($(id).offset().top);
		var windowHeight = document.documentElement.clientHeight;
		var bodyHeight = $('body').height();
		var scroll = ( bodyHeight - offset > windowHeight ) ? offset : bodyHeight - windowHeight;
		$.scrollTo(scroll, 800, {easing:'easeOutExpo'});
		return false;
	});
	// page top
	$('#pageTop a', '.pageTop a').click(function(){
		$.scrollTo(0, 800, {easing:'easeOutExpo'});
		return false;
	});

	/**
	 * jquery.lightbox.js settings
	 */
	var lightBoxOptions = {
		imageLoading:	'/wp/wp-content/themes/wp-chiyoda/js/images/loading.gif',	// (string) Path and the name of the loading icon
		imageBtnPrev:	'/wp/wp-content/themes/wp-chiyoda/js/images/prevlabel.gif',	// (string) Path and the name of the prev button image
		imageBtnNext:	'/wp/wp-content/themes/wp-chiyoda/js/images/nextlabel.gif',	// (string) Path and the name of the next button image
		imageBtnClose:	'/wp/wp-content/themes/wp-chiyoda/js/images/closelabel.gif',	// (string) Path and the name of the close btn
		imageBlank:	'/wp/wp-content/themes/wp-chiyoda/js/images/lightbox-blank.gif'	// (string) Path and the name of a blank image (one pixel)
	};
	// article_content
	$('div.article_content').each(function() {
		$('a[href$=.jpg]', this).lightBox(lightBoxOptions);
		$('a[href$=.png]', this).lightBox(lightBoxOptions);
		$('a[href$=.gif]', this).lightBox(lightBoxOptions);
	});


	/**
	 * mainMenu
	 *
	 */
	$('#mainMenu > ul.mainMenu > li').each(function() {
		var child  = $('ul.menu_child', this);
		if( $(child).length > 0 ) {
			$(child).css({opacity:'0.72'});
			$(this).click(function() { $(child).hide() });

			var parent = $('.menu_parent', this);

			function slideDownMenu(){
				$(child).slideDown(500);
			}

			$(parent).mouseenter(slideDownMenu);
			$(this).mouseleave(function() {
				$(parent).unbind('mouseenter', slideDownMenu);
				$(child).slideUp(500, function(){$(parent).bind('mouseenter', slideDownMenu);});
			});
		}
	});


	/**
	 * home_works_list
	 *
	 */
	var homeWorksListList = $('#home_works_list_list'); 
	(function() {
		var last = $('li:last', homeWorksListList);
		$(homeWorksListList).prepend(last.clone());
		$(last).remove();
	})();
	worksListBind();

	function worksListBind() {
		$('#home_works_list_bull').bind('click', worksListBull);
		$('#home_works_list_bear').bind('click', worksListBear);
	}

	function worksListUnbind() {
		$('#home_works_list_bull').unbind('click', worksListBull);
		$('#home_works_list_bear').unbind('click', worksListBear);
	}

	function worksListBull() {
		worksListUnbind();
		var posTop = $(homeWorksListList).position().top;
		var posNex = posTop - 160;
		$(homeWorksListList).animate( {top: posNex}, 800, 'linear', function() {
			var first = $('li:first', this);
			$(this).append(first.clone());
			$(first).remove();
			$(homeWorksListList).css('top', posTop + 'px');
			worksListBind();
		});
	}

	function worksListBear() {
		worksListUnbind();
		var posTop = $(homeWorksListList).position().top;
		var posNex = posTop + 160;
		$(homeWorksListList).animate( {top: posNex}, 800, 'linear', function() {
			var last = $('li:last', this);
			$(this).prepend(last.clone());
			$(last).remove();
			$(homeWorksListList).css('top', posTop + 'px');
			worksListBind();
		});
	}

});

