(function($){
	$(function(){
		
		//news
		$load('news');
		
		//renew
		$load('renew');
		
		//main (speed,interval)
		$main(1000,5000);
		
	});
	
	$load = function(name){
		$.ajax({
			url: '/datas/' + name + '.xml',
			cache: false,
			async: false,
			dataType: 'xml',
			success: function(xmlData){
				var count = 0;
				var html = '<table cellpadding="0" cellspacing="0">';
				$(xmlData).find('entry').each(function(){
					if(count < 5){
						if($(this).find('date').text() != ''){
							html += '<tr>';
							html += '<th>' + $(this).find('date').text() + '</th>';
							if(name == 'news'){
								if($(this).find('url').eq(0).text() != '' && $(this).find('url').eq(1).text() == ''){
									html += '<td><a href="' + $(this).find('url').eq(0).text() + '" target="_blank">' + $(this).find('title').text() + '</a></td>';
								}
								else{
									html += '<td><a href="./etc/info.html#a' + (count + 1) + '">' + $(this).find('title').text() + '</a></td>';
								}
							}
							else{
								html += '<td>' + $(this).find('title').text() + '</td>';
							}
							html += '</tr>';
							count++;
						}
						else{
							return false;
						}
					}
					else{
						return false;
					}
				});
				html += '</table>';
				$('#content .info .' + name + ' dd').html(html);
				$('#content .info .' + name + ' dd tr:last-child').addClass('last');
			}
		});
	};
	
	$main = function(speed,interval){
		var count = 0;
		var css_zi = new Array(4,3,2,2,3);
		var css_top = new Array('30px','50px','90px','90px','50px');
		var css_left = new Array('-20px','136px','384px','-170px','-100px');
		var css_width = new Array('1000px','924px','746px','746px','924px');
		var css_height = new Array('380px','350px','280px','280px','350px');
		var base = new Array(css_zi,css_top,css_left,css_width,css_height);
		
		for(var i = 0;i < 5;i++){
			$('#content .main li').eq(i).css({
				'z-index': css_zi[i],
				'top': css_top[i],
				'left': css_left[i],
				'width': css_width[i],
				'height': css_height[i]
			});
		}
		
		$move = function(mode){
			if(!flagMove){
				flagMove = true;
				if(mode == 'right'){
					for(var i = 0;i < 5;i++){
						base[i].unshift(base[i][4]);
						base[i].splice(5,1);
					}
				}
				else{
					for(var i = 0;i < 5;i++){
						base[i][5] = base[i][0];
						base[i].shift();
					}
				}
				for(var i = 0;i < 5;i++){
					$zi(i);
					$('#content .main li').eq(i).animate({
						'top': css_top[i],
						'left': css_left[i],
						'width': css_width[i],
						'height': css_height[i]
					},speed,function(){flagMove = false;});
				}
				}
		}
		
		$zi = function(i){
			$('#content .main li img').eq(i).fadeTo(500,0.5,function(){
				$('#content .main li').eq(i).css({
					'z-index': css_zi[i]
				});
			}).fadeTo(500,1.0);
		}
		
		timerOuto();
		timerR();
		timerL();
		clearInterval(timer2);
		clearInterval(timer3);
		function timerOuto(){
			timer = setInterval("$move('right')",interval);
		}
		function timerR(){
			timer2 = setInterval("$move('right')",interval / 2);
		}
		function timerL(){
			timer3 = setInterval("$move('left')",interval / 2);
		}
		
		var flagMove = false;
		var flag = true;
		var flagOuto = false;
		$('html').mousemove(function(ev){
			X = ev.pageX - $('#content .main').offset().left;
			Y = ev.pageY - $('#content .main').offset().top;
			if(X > 960){
				if(flag){
					flagOuto = true
					flag = false;
					clearInterval(timer);
					clearInterval(timer3);
					$move('right');
					timerR();
				}
			}
			else if(X < 0){
				if(flag){
					flagOuto = true
					flag = false;
					clearInterval(timer);
					clearInterval(timer2);
					$move('left');
					timerL();
				}
			}
			else{
				flag = true;
				clearInterval(timer2);
				clearInterval(timer3);
				if(flagOuto){
					flagOuto = false;
					timerOuto();
				}
			}
		});
	}
	
})(jQuery);
