// JavaScript Document

$(function()
{
	$('div.a .m li a').click(function()
	{
		$('div.a .m iframe').attr('src',$(this).attr('href'));
		return false;
	});

	$('div.a a img, div.b a img, div.c a img').hover(
		function () { 	$(this).fadeTo(90, 0.6); },
		function () { 	$(this).fadeTo(90, 1); }
	);

	$.post('/news/index.php', function(data)
	{
		$('#news ul').html(data);
		news();
	});
	$.post('/kamishizu/news/index.php', function(data)
	{
		$('.r .news ul').html(data);
		$('.r div.news div:even').css('background-color','#FFF');
		cbox();
	});
	$.post('/kashiwa/news/index.php', function(data)
	{
		$('.l .news ul').html(data);
		$('.l div.news div:even').css('background-color','#FFF');
		cbox();
	});
	function cbox()
	{
		$('.cbox').colorbox(
		{
			iframe:true,
			innerWidth:700,
			innerHeight:'80%',
			onOpen:function(){	$('div.a iframe').hide();},
	　 		onClosed:function(){$('div.a iframe').show();}
		});
	}

	$('#kv a').click(function()
	{
		return false;
	});

	var timer;
	$('#kvb a').click(function()
	{
		clearInterval(timer);
		var index = $('#kvb a').index(this);
		var src = $(this).attr('rel');
		var href = $(this).attr('href');
		$('#kv img').fadeTo(600, 0, function()
		{
			$('#kv img').attr('src', src);
			$('#kv a').attr('href', href);
			$('#kv a').attr('rel', index);
			$('#kv img').fadeTo(1200, 1);

			timer = setInterval(function()
			{
				var rel = parseInt($('#kv a').attr('rel'));
				rel = (rel==2) ? 0 : rel+1;
				$('#kvb a:eq('+rel+')').trigger('click')
			},9000);

		});
		return false;
	});

	function news()
	{
		var i = 0;
		var l = $('#news ul li').length;
		if (l>2)
		{
			$('#news ul').css('width',790*l);
			setInterval(function()
			{
				$('#news ul').animate(
					{left:-790*i},
					{
						complete: function()
						{
							if (i==l-1)
							{
								$('#news ul').clone().css('left',0).insertAfter('#news ul');
								$('#news ul:eq(0)').remove();
								cbox();
								i=0;
							}
							i++;
						}
					}
				);

			},6000);
		}
	}

	function bnr(b)
	{
		var l = b.find('li').length;
		var x = 0;
		b.find('ul').css('width',230*l);
		b.find('.r img').click(function()
		{
			b.find('ul').animate({left:-230*++x});
			bnrP(b,l,x);
		});
		b.find('.l img').click(function()
		{
			b.find('ul').animate({left:-230*--x});
			bnrP(b,l,x);
		});
		bnrP(b,l,x);
	}
	bnr($('div.b'));
	bnr($('div.c'));
	function bnrP(b,l,x)
	{
		if (x<=0)
		{
			b.find('.l img').fadeTo('fast',0).removeClass('bnrP');
		} else {
			b.find('.l img').fadeTo('fast',1).addClass('bnrP');
		}
		if (x<l-4)
		{
			b.find('.r img').fadeTo('fast',1).addClass('bnrP');
		} else {
			b.find('.r img').fadeTo('fast',0).removeClass('bnrP');
		}
	}
	$('img.bnrP').live('mouseover', function()
	{
		$(this).fadeTo(90, 0.6);
	});
	$('img.bnrP').live('mouseout', function()
	{
		$(this).fadeTo(90, 1);
	});
});

