var box_main;

$(document).ready(function(){
	initTop();
	
});

function initTop() {
	stop_time = 3000;
	box_main = $("#box_main");
	img_w = box_main.width();
	img_h = box_main.height();
	box_bt = $("div[class='bt_change']");
	org_bg = box_main.css("backgroundColor");
	random = $("#top_images").attr("random");
	images = $("a[class='top_image']");
	moveMode = [];

	box_bt.hide();
	tempWork = $("#top_images").clone();
	tempWork.find("a").removeClass("top_image");
	tempWork.find("a").html("");
	box_bt.html(tempWork.html());
	buttons = $("div[class='bt_change'] a");
	
	var i = 1;
	images.each(function(){
		moveMode[i] = $(this).attr("mode");
		i++;
	});
	
	i = 1;
	buttons.each(function(){
		$(this).attr("num",i);
		$(this).hover(
			function() {
				setNum($(this).attr("num"));
				clearInterval(TID);
				img_area.css("opacity", 100);
			},
			function(){
				TID = setInterval("moveImage()", stop_time);
			}
		);
		i++;
	});
	
	box_bt.show();
	img_area = $("#top_images");
	img_cnt = images.length;
	img_area.css("width", img_w * img_cnt);
	img_area.css("height", img_h * img_cnt);
	box_main.css("backgroundColor", "");
	setTopImages(random);
	img_area.hover(
		function() {
			box_main.css("backgroundColor", org_bg);
			clearInterval(TID);
		},
		function() {
			box_main.css("backgroundColor", "");
			TID = setInterval("moveImage()", stop_time);
		}
	);
}

// トップページ情報取得
function getTopData(_target, _url) {
	var target = $(_target);
	if(target.length > 0) {
		$.ajax({
			type: 'get',
			async: true,
			url: _url,
			dataType: 'text',
			success: function(msg){
				if (msg != "NG") {
					target.html(msg);
				} else {
					return false;
				}
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				return false;
			}
		});
	}
}

// トップページイメージ
function setTopImages(_rand) {
	if (_rand == "on") {
		num = Math.ceil( Math.random() * img_cnt );
		setNum(num);
	} else {
		img_now = 1;
		setButton();
	}
	
	TID = setInterval("moveImage()", stop_time);
}
function moveImage() {
	img_now++;
	
	switch (moveMode[img_now]) {
		case "x":
			//横スクロールパターン
			images.css("float", "left");
			img_area.css("top", 0);
			img_area.css("left", img_w * (img_now - 2) * -1);
			if(img_now == 1) {
				img_area.animate({left: 0},{duration: 300, easing: "swing"});
			} else {
				next = img_w * (img_now - 1) * -1;
				img_area.animate({left: next},{duration: 800, easing: "swing", complete: nextWork()});
			}
			break;
		
		case "y":
			//縦スクロールパターン
			images.css("float", "none");
			img_area.css("left", 0);
			img_area.css("top", img_h * (img_now - 2) * -1);
			if(img_now == 1) {
				img_area.animate({top: 0},{duration: 300, easing: "swing"});
			} else {
				next = img_h * (img_now - 1) * -1;
				img_area.animate({top: next},{duration: 800, easing: "swing", complete: nextWork()});
			}
			break;
		
		default:
			//フェードパターン
			images.css("float", "left");
			img_area.css("top", 0);
			next = img_w * (img_now - 1) * -1;
			img_area.animate({opacity: 0},{duration: 500, easing: "easeOutExpo", complete: function(){img_area.css("left", next);} });
			img_area.animate({opacity: 100},{duration: 2000, easing: "easeInCubic", complete: nextWork()});
			break;
	}
}
//↑トランジション種類　linear swing jswing easeInQuad easeOutQuad easeInOutQuad easeInCubic easeOutCubic easeInOutCubic easeInQuart easeOutQuart 
//easeInOutQuart easeInQuint easeOutQuint easeInOutQuint easeInSine easeOutSine easeInOutSine easeInExpo easeOutExpo easeInOutExpo easeInCirc
//easeOutCirc easeInOutCirc easeInElastic easeOutElastic easeInOutElastic easeInBack easeOutBack easeInOutBack easeInBounce easeOutBounce easeInOutBounce

function nextWork() {
	setButton();
	if (img_now >= img_cnt) {
		img_now = 0;
	}
}
function setButton() {
	var i = 1;
	buttons.each(function(){
		if(i == img_now) {
			$(this).addClass("on");
		} else {
			$(this).removeClass("on");
		}
		i++;
	});
}
function setNum(_now) {
	img_now = _now;
	setButton();
	img_area.stop();
	images.css("float", "left");
	img_area.css("top", 0);
	img_area.css("left", (img_w * (img_now - 1) * -1));
	nextWork();
}

