var main;
var slideshow;
var ranking;
var searcher;
var cart;
var is_static = is_ie6 || is_smartphone;


$(init);

function init () {
	
	slideshow = new SlideShow("#image");
	ranking = new Ranking("#ranking");
	searcher = new Search("#search");
	cart = new Cart("#cart");
	
	$(searcher).bind("onsend", delegate(cart, cart.add));
	$(cart).bind("onadded", delegate(searcher, searcher.hideAllCart));
	
	slideshow.start();
	ranking.start();
	searcher.start();
	cart.start();
	
	onReady();
	
	if (is_static) $("#cart").css({position:"relative", bottom:0, marginBottom:-1});
}


/* SlideShow */
function SlideShow (target) {
	//console.log("SlideShow(" + [target] + ")");
	
	var p = this;
	this.target = $(target);
	this.list = $("ul", this.target);
	this.entries = $("li", this.list);
	
	this.interval;
	this.visible_items = 2;
}

SlideShow.prototype.start = function () {
	//console.log("SlideShow.start(" + [] + ")");
	
	this.update(0);
}

SlideShow.prototype.update = function (time) {
	//console.log("SlideShow.update(" + [time] + ")");
	
	this.entries = $("li", this.list);
	
	this.selected = -1;
	this.min = 0;
	this.max = Math.ceil(this.entries.length / this.visible_items) - 1;
	
	if (this.max > 0) this.select(0, time);
}

SlideShow.prototype.previous = function (time, loop) {
	//console.log("SlideShow.previous(" + [time, loop] + ")");
	
	this.select(this.selected - 1, time, loop);
}

SlideShow.prototype.next = function (time, loop) {
	//console.log("SlideShow.next(" + [time, loop] + ")");
	
	this.select(this.selected + 1, time, loop);
}

SlideShow.prototype.select = function (value, time, loop) {
	//console.log("SlideShow.select(" + [value, time, loop] + ")");
	
	value = this.getSelected(value, loop);
	
	if (value == this.selected) return;
	
	time = !isNaN(Number(time)) ? time : 700;
	
	this.clear();
	this.list.stop().animate({opacity:0.0}, {duration:time, easing:"easeInOutCubic", complete:delegateWithArgs(this, this.show, [value, time])});
	
	this.selected = value;
}

SlideShow.prototype.show = function (value, time) {
	//console.log("SlideShow.show(" + [value, time] + ")");
	
	var selected_item = $(this.entries[value * this.visible_items]);
	var position = selected_item.position().left;
	
	this.list.css({left:-position});
	this.list.animate({opacity:1.0}, {duration:time, easing:"easeInOutCubic", complete:delegate(this, this.onSelected)});
}

SlideShow.prototype.onSelected = function () {
	//console.log("SlideShow.onSelected(" + [] + ")");
	
	this.clear();
	this.interval = setInterval(delegateWithArgs(this, this.next, [1000, true]), 4000);
}

SlideShow.prototype.clear = function () {
	//console.log("SlideShow.clear (" + [] + ")");
	
	clearInterval(this.interval);
}

SlideShow.prototype.getSelected = function (value, loop) {
	//console.log("SlideShow.getSelected(" + [value, loop] + ")");
	
	if (loop) {
		if (value < this.min) value = this.max + (value - this.min) + 1;
		if (value > this.max) value = this.min + (value - this.max) - 1;
	}
	else {
		if (value < this.min) value = this.min;
		if (value > this.max) value = this.max;
	}
	
	return value;
}


/* Ranking */
function Ranking (target) {
	//console.log("Ranking(" + [target] + ")");
	
	var p = this;
	this.target = $(target);
	this.categories = $("menu", this.target);
	this.items = $(".items", this.target);
	this.list = $("ol", this.items);
	
	this.interval;
	
	this.btn_previous = $('<a class="previous" href="javascript:void(0);">PREV</a>');
	this.btn_previous.click(delegate(this, this.previous));
	
	this.btn_next = $('<a class="next" href="javascript:void(0);">PREV</a>');
	this.btn_next.click(delegate(this, this.next));
	
	this.target
		.append(this.btn_previous)
		.append(this.btn_next);
}

Ranking.prototype.start = function () {
	//console.log("Ranking.start(" + [] + ")");
	
	this.update(100);
}

Ranking.prototype.update = function (time) {
	//console.log("Ranking.update(" + [time] + ")");
	
	this.entries = $("li", this.list);
	
	this.selected = -1;
	this.min = 0;
	this.max = this.entries.length - 1;
	
	this.select(0, time);
}

Ranking.prototype.selectCategory = function (value) {
	//console.log("Ranking.selectCategory(" + [value] + ")");
	
	var selected_category_item = $("> li." + value, this.categories);
	$("li a", this.categories).removeClass("selected");
	$("a", selected_category_item).addClass("selected");
	
	this.target.removeClass();
	this.target.addClass(value);
}

Ranking.prototype.previous = function (time, loop) {
	//console.log("Ranking.previous(" + [time, loop] + ")");
	
	this.select(this.selected - 1, time, loop);
}

Ranking.prototype.next = function (time, loop) {
	//console.log("Ranking.next(" + [time, loop] + ")");
	
	this.select(this.selected + 1, time, loop);
}

Ranking.prototype.select = function (value, time, loop) {
	//console.log("Ranking.select(" + [value, time, loop] + ")");
	
	value = this.getSelected(value, loop);
	
	if (value == this.selected) return;
	
	time = !isNaN(Number(time)) ? time : 700;
	
	var selected_item = $(this.entries[value]);
	var position = selected_item.position().left;
	
	this.clear();
	this.list.stop().animate({left:-position}, {duration:time, easing:"easeOutCubic", complete:delegate(this, this.onSelected)});
	
	if (value == 0) this.btn_previous.css({backgroundPosition:"0 -45px"});
	else this.btn_previous.css({backgroundPosition:"0 0"});
	
	if (value == this.max) this.btn_next.css({backgroundPosition:"-20px -45px"});
	else this.btn_next.css({backgroundPosition:"-20px 0"});
	
	this.selected = value;
}

Ranking.prototype.onSelected = function () {
	//console.log("Ranking.onSelected(" + [] + ")");
	
	this.clear();
	this.interval = setInterval(delegateWithArgs(this, this.next, [1500, true]), 3000);
}

Ranking.prototype.clear = function () {
	//console.log("Ranking.clear (" + [] + ")");
	
	clearInterval(this.interval);
}

Ranking.prototype.getSelected = function (value, loop) {
	//console.log("Ranking.getSelected(" + [value, loop] + ")");
	
	if (loop) {
		if (value < this.min) value = this.max + (value - this.min) + 1;
		if (value > this.max) value = this.min + (value - this.max) - 1;
	}
	else {
		if (value < this.min) value = this.min;
		if (value > this.max) value = this.max;
	}
	
	return value;
}


/* Search */
function Search (target) {
	//console.log("Search(" + [target] + ")");
	
	var p = this;
	this.target = $(target);
	this.selected_main = "";
	this.selected_sub = "";
	
	//Header
	this.menu = $(".header menu", this.target);
	this.categories = $(".header ul", this.target);
	
	$("> li", this.menu).mouseover(function () {
		var name = $(this).attr("class");
		p.showCategories(name);
	});
	$("> li", this.menu).mouseout(function () {
		var name = $(this).attr("class");
		p.hideCategories(name);
	});
	$("> li", this.categories).mouseover(function () {
		var name = $(this).attr("class");
		p.showCategories(name);
	});
	$("> li", this.categories).mouseout(function () {
		var name = $(this).attr("class");
		p.hideCategories(name);
	});
	
	//Items
	this.items = $(".items", this.target);
	this.title = $("> h3", this.items);
	
	$(".submit", this.items).click(function () { p.send($(this).closest("td")); });
	
	var td = $(".items td", this.target);
	var td_inner = $('<div class="inner"></div>');
	td.wrapInner(td_inner);
	
	var btn_cart = $(".items a.cart", this.target);
	btn_cart.attr({href:"javascript:void(0);"});
	btn_cart.click(function () { p.showCart($(this).closest("td")); });
	
	var btn_close = $('<a class="close" href="#">閉じる</a>');
	btn_close.attr({href:"javascript:void(0);"});
	btn_close.click(function () { p.hideCart($(this).closest("td")); });
	
	var arrow = $('<div class="arrow"></div>');
	
	$(".items div.cart", this.target).append(btn_close);
	$(".items div.cart", this.target).append(arrow);
	$(".items div.cart", this.target).each(function () {
		var item = $(this).closest("td");
		var cart = $(this);
		var over = parseInt((item.position().top + cart.outerHeight() - 21) - (p.items.position().top + p.items.outerHeight() - 30));
		if (over > 0) {
			var top = parseInt(cart.css("top")) - over;
			cart.css({top:top});
			top = parseInt($(".arrow", cart).css("top")) + over;
			$(".arrow", cart).css({top:top});
		}
	});
}

Search.prototype.start = function () {
	//console.log("Search.start(" + [] + ")");
	
	
}

Search.prototype.send = function (item) {
	//console.log("Search.send(" + [item] + ")");
	
	var id = item.attr("id").split("item")[1];
	
	$(this).trigger("onsend", [id]);
}

Search.prototype.search = function (main, sub) {
	//console.log("Search.search(" + [main, sub] + ")");
	
	var selected_main_menu = $("> li." + main, this.menu);
	var selected_main_category = $("> li." + sub, this.categories);
	var title_string = main == "all" ? selected_main_menu.text() : selected_main_category.text();
	this.title.text(title_string);
	
	if (this.items.hasClass(this.selected_sub)) this.items.removeClass(this.selected_sub);
	this.items.addClass(sub);
	
	this.selectCategory(main, sub);
	
	$(this).trigger("onsearch", [main, sub]);
}

Search.prototype.selectCategory = function (main, sub) {
	//console.log("Search.selectCategory(" + [main, sub] + ")");
	
	var selected_main_category = $("> li." + main, this.categories);
	$("li a", this.categories).removeClass("selected");
	$("li." + sub + " a", selected_main_category).addClass("selected");
	
	this.showCategories(main);
	
	this.selected_main = main;
	this.selected_sub = sub;
}

Search.prototype.showCategories = function (main) {
	//console.log("Search.showCategories(" + [main] + ")");
	
	var selected_main_menu = $("> li." + main, this.menu);
	$("li a", this.menu).removeClass("selected");
	$("a", selected_main_menu).addClass("selected");
	
	$("> li", this.categories).css({display:"none"});
	$("> li." + main, this.categories).css({display:"block"});
	$("> li li", this.categories).css({display:"block"});
}

Search.prototype.hideCategories = function (main) {
	//console.log("Search.hideCategories(" + [main] + ")");
	
	var selected_main_menu = $("> li." + this.selected_main, this.menu);
	$("li a", this.menu).removeClass("selected");
	$("a", selected_main_menu).addClass("selected");
	
	$("> li", this.categories).css({display:"none"});
	$("> li." + this.selected_main, this.categories).css({display:"block"});
	$("> li li", this.categories).css({display:"block"});
}

Search.prototype.showCart = function (item) {
	//console.log("Search.showCart(" + [item] + ")");
	
	this.hideAllCart();
	
	var cart = $("div.cart", item);
	cart.css({display:"block"});
}

Search.prototype.hideCart = function (item) {
	//console.log("Search.hideCart(" + [item] + ")");
	
	var cart = $("div.cart", item);
	cart.css({display:"none"});
}

Search.prototype.hideAllCart = function () {
	//console.log("Search.hideAllCart(" + [] + ")");
	
	var p = this;
	$("div.cart", this.items).each(function () { p.hideCart($(this).closest("td")); });
}


/* Cart */
function Cart (target) {
	//console.log("Cart(" + [target] + ")");
	
	var p = this;
	this.target = $(target);
	this.items = $(".items", this.target);
	this.list = $("ul", this.items);
	
	this.is_open = false;
	//this.cart_height = this.target.height();
	this.cart_height = is_ie6 ? 173 : 163;
	this.container_bottom = $("#container").css("paddingBottom");
	
	$("h2 a", this.target).click(delegate(this, this.toggleOpen));
	$(".display a.open", this.target).click(delegate(this, this.toggleOpen));
	
	this.btn_previous = $('<a class="previous" href="javascript:void(0);">PREV</a>');
	this.btn_previous.click(delegate(this, this.previous));
	
	this.btn_next = $('<a class="next" href="javascript:void(0);">PREV</a>');
	this.btn_next.click(delegate(this, this.next));
	
	this.target
		.append(this.btn_previous)
		.append(this.btn_next);
	
	this.close(0);
}

Cart.prototype.start = function () {
	//console.log("Cart.start(" + [] + ")");
	
	this.update(0);
}

Cart.prototype.update = function (time) {
	//console.log("Cart.update(" + [time] + ")");
	
	this.entries = $("li", this.list);
	
	this.selected = -1;
	this.min = 0;
	this.max = this.entries.length - 1;
	
	this.select(0, time);
}

Cart.prototype.toggleOpen = function (time) {
	//console.log("Cart.toggleOpen(" + [time] + ")");
	
	if (this.is_open) this.close(time);
	else this.open(time);
}

Cart.prototype.open = function (time) {
	//console.log("Cart.open(" + [time] + ")");
	
	time = !isNaN(Number(time)) ? time : 500;
	
	this.target.stop().animate({height:this.cart_height}, {duration:time, easing:"easeInOutCubic"});
	if (!is_static) $("#container").stop().animate({paddingBottom:this.cart_height + 50}, {duration:time, easing:"easeInOutCubic"});
	
	$(".display a.open", this.target).css({backgroundPosition:"0 -76px"});
	
	this.is_open = true;
}

Cart.prototype.close = function (time) {
	//console.log("Cart.close(" + [time] + ")");
	
	time = !isNaN(Number(time)) ? time : 500;
	
	this.target.stop().animate({height:this.cart_height - 125}, {duration:time, easing:"easeInOutCubic"});
	if (!is_static) $("#container").stop().animate({paddingBottom:this.cart_height + 50 - 125}, {duration:time, easing:"easeInOutCubic"});
	
	$(".display a.open", this.target).css({backgroundPosition:"0 -48px"});
	
	this.is_open = false;
}

Cart.prototype.add = function (event, id) {
	//console.log("Cart.add(" + [event, id] + ")");
	
	var p = this;
	var item = $("#item" + id);
	var caution = $("div.cart em", item);
	var shoppingcart = $("#shoppingcart .body");
	var display = $("#cart .header .display");
	
	var goods_sub_no = $(".goods_sub_no", item).val();
	var shipment_date = $(".shipment_date", item).val();
	var params = $("form", item).serialize();
	
	if (goods_sub_no && shipment_date) {
		$.post("../scripts/getcart.php", {params:params}, function(data) {
			$('#cart .items ul').html(data);
			p.update();
			$.post("../scripts/getprice.php", {params:params}, function(data2) {
				data2 = $("<div>" + data2 + "</div>");
				var price = $("strong", data2).text();
				var quantity = $("em", data2).text();
				$("strong", shoppingcart).html(price);
				$("em", shoppingcart).html(quantity);
				$("strong", display).html(price);
				$("em", display).html(quantity);
			});
		});
		caution.html(' ');
		$(this).trigger("onadded", [id]);
	}
	else if (goods_sub_no){
		caution.html('商品発送日を選択してください');
	}
	else {
		caution.html('食数を選択してください');
	}
}

Cart.prototype.previous = function () {
	//console.log("Cart.previous(" + [] + ")");
	
	this.select(this.selected - 1);
}

Cart.prototype.next = function () {
	//console.log("Cart.next(" + [] + ")");
	
	this.select(this.selected + 1);
}

Cart.prototype.select = function (value, time) {
	//console.log("Cart.select(" + [value] + ")");
	
	value = this.getSelected(value);
	
	if (value == this.selected) return;
	
	time = !isNaN(Number(time)) ? time : 700;
	
	var selected_item = $(this.entries[value]);
	var position = selected_item.position().left;
	
	this.list.stop().animate({left:-position}, {duration:time, easing:"easeOutCubic"});
	
	if (value == 0) this.btn_previous.css({backgroundPosition:"-530px -98px"});
	else this.btn_previous.css({backgroundPosition:"-530px -48px"});
	
	if (value == this.max) this.btn_next.css({backgroundPosition:"-555px -98px"});
	else this.btn_next.css({backgroundPosition:"-555px -48px"});
	
	this.selected = value;
}

Cart.prototype.getSelected = function (value) {
	//console.log("Cart.getSelected(" + [value] + ")");
	
	if (value < this.min) value = this.min;
	if (value > this.max) value = this.max;
	
	return value;
}




