$.dec78 = {};

$.dec78.index = {};

$.dec78.index.index = {
	initMenu: function() {
		var current = 0;
		var loaded  = 0;
		for (var i = 1; i < 4; ++i) {
			$('<img />').load(function() {
				++loaded;
				if (loaded == 3) {
					$('#bg1, #bg2, #bg3').mouseover(function(e) {
						var $this = $(this);
						if ($this.parent().index() == current) {
							return;
						}
						//var item = e.target.id;
						var item = $this.get(0).id;
						if (item == 'bg1' || current == 2) {
							$('#menu .sub' + parseInt(current+1)).stop().animate({backgroundPosition: "(-325px 0)"}, 300, function() {
								$(this).find('li').hide();
							});
						} else {
							$('#menu .sub'+parseInt(current+1)).stop().animate({backgroundPosition: "(325px 0)"}, 300, function() {
								$(this).find('li').hide();
							});
						}
						if (item == 'bg1' || current == 2) {
							$('#menu > li').animate({backgroundPosition: "(-980px 0)"}, 0).removeClass('bg1 bg2 bg3').addClass(item);
							move(1,item);
						} else {
							$('#menu > li').animate({backgroundPosition: "(980px 0)"}, 0).removeClass('bg1 bg2 bg3').addClass(item);
							move(0,item);
						}
						if (current == 2 && item == 'bg1') {
							$('#menu .sub' + parseInt(current)).stop().animate({backgroundPosition: "(-325px 0)"}, 300);
						}
						if (current == 0 && item == 'bg3') {
							$('#menu .sub' + parseInt(current+2)).stop().animate({backgroundPosition: "(325px 0)"}, 300);
						}
						current = $this.parent().index();
						
						$('#menu .sub' + parseInt(current+1)).stop().animate({backgroundPosition: "(0 0)"}, 300, function() {
							$(this).find('li').fadeIn();
						});
					});
				}
			}).attr('src', '/common/ec/p/top/menu/' + i + '.jpg');
		}
	
		function move(dir,item) {
			if(dir) {
				$('#bg1').parent().stop().animate({backgroundPosition: "(0 0)"}, 200);
				$('#bg2').parent().stop().animate({backgroundPosition: "(-325px 0)"}, 300);
				$('#bg3').parent().stop().animate({backgroundPosition: "(-650px 0)"}, 400,function() {
					$('#menuWrapper').removeClass('bg1 bg2 bg3').addClass(item);
				});
			} else {
				$('#bg1').parent().stop().animate({backgroundPosition: "(0 0)"}, 400, function() {
					$('#menuWrapper').removeClass('bg1 bg2 bg3').addClass(item);
				});
				$('#bg2').parent().stop().animate({backgroundPosition: "(-325px 0)"}, 300);
				$('#bg3').parent().stop().animate({backgroundPosition: "(-650px 0)"}, 200);
			}
		}

		// トップメニュー業種
		$('#menu').find("a[id^='bg']").click(function() {
			location.href = '/p/product/list/bid/' + $(this).attr('val') + '/lc/20/pno/1/';
		});
		// トップメニュー業種1つ目:メーカー
		$('#menu').find("a[id^='mkr_bg1_']").click(function() {
			location.href = '/p/product/list/bid/' + $('#bg1').attr('val') + '/maker/' + $(this).attr('val') + '/lc/20/pno/1/';
		});
		// トップメニュー業種2つ目:メーカー
		$('#menu').find("a[id^='mkr_bg2_']").click(function() {
			location.href = '/p/product/list/bid/' + $('#bg2').attr('val') + '/maker/' + $(this).attr('val') + '/lc/20/pno/1/';
		});
		// トップメニュー業種3つ目:メーカー
		$('#menu').find("a[id^='mkr_bg3_']").click(function() {
			location.href = '/p/product/list/bid/' + $('#bg3').attr('val') + '/maker/' + $(this).attr('val') + '/lc/20/pno/1/';
		});
	},
	
	/**
	 * メニュー
	 */
	loadMenu: function() {
		var $tpl1 = $(
			'<li class="bg1" style="background-position:0px 0px;">' + // last 
				'<a id="bgN" val="XXX-1" href="javascript:void(0);"><img src="" alt="" /></a>' +
				'<ul class="subN" style="background-position:0px 0px;">' +
				//	'<li><a id="mkr_bgN_N" val="0000" href="javascript:void(0);"></a></li>' +
				'</ul>' +
			'</li>'
		);
		var $tpl2 = $('<li><a id="mkr_bgN_M" val="0000" href="javascript:void(0);"></a></li>');
		
		$.ajax({
			url: '/common/ec/p/top/menu/menu.json',
			success: function(data) {
				var data = eval(data);
				if (data.length > 0) {
					var html = [];
					
					for (var i in data) {
						var menuId = parseInt(i, 10) + 1;
						// サブメニュー
						var sub = [];
						for (var j in data[i].sub) {
							var subId = parseInt(j, 10) + 1;
							sub[sub.length] = $tpl2.clone()
								.find('a')
									.attr({
										id: 'mkr_bg' + menuId + '_' + subId,
										val: data[i].sub[j].val
									})
									.html(data[i].sub[j].title)
								.end()
							.get(0);
						}
						
						// メニュー
						var $tmp = $tpl1.clone()
							.css('backgroundPosition', (-325 * i) +'px 0px')
							.find('a')
								.attr({
									id: 'bg' + menuId,
									val: data[i].val
								})
								.find('img').attr({
									src: '/common/ec/p/top/menu/' + data[i].image,
									alt: data[i].title
								}).end()
								//.html(data[i].title)
							.end()
							.find('ul')
								.attr('class', 'sub' + menuId)
								.css('backgroundPosition', (menuId != 1 ? -325 : 0) +'px 0px')
								.html(sub)
							.end();
						
						// 最後のメニューに last クラスを付与
						if (i == data.length - 1) {
							$tmp.addClass('last');
						}
						
						html[html.length] = $tmp.get(0);
					}
					
					$('#menu').html(html);
					$('#menu').find('ul').each(function(){
						$(this).find('li:lt(6)').wrapAll('<div style="float: left; width: 140px"></div>');
						$(this).find('li:gt(5)').wrapAll('<div style="float: left; width: 140px"></div>');
					});
					
					// メニューを初期化
					$.dec78.index.index.initMenu();
				}
			}
		});
	},
	
	/**
	 * カルーセル（バナー）
	 */
	loadCarousel: function() {
		$("#carousel div.banners").jCarouselLite({
			btnNext: '.next',
			btnPrev: '.prev',
			visible : 4
		});
		/*
		var $tpl = $('<li><a href="#"><img src="#" alt="" width="230" height="82" /></a></li>');
		
		$.ajax({
			url: '/common/ec/p/top/banner/banner.json',
			success: function(data) {
				var data = eval(data);
				if (data.length > 0) {
					var html = [];
					for (var i in data) {
						html[html.length] = $tpl.clone()
							.find('a')
								.attr('href', data[i].url)
							.end()
							.find('img')
								.attr('src', data[i].img)
							.end()
						.get(0);
					}
					$('#carousel ul.gallery').html(html);
					
					$("#carousel div.banners").jCarouselLite({
						btnNext: '.next',
						btnPrev: '.prev',
						visible : 4,
					});
					
					// IE で再描画しないと、右端が欠ける
					$('#carousel').css('opacity', 0).css('opacity', 1);
				}
			}
		});
		*/
	}
};

$.dec78.product = {};

$.dec78.product.list = {
	init: function() {
		$('#js_price_order_1').change(function() {
			location.href = $.dec78.product.list.condition('_1');
		});
		$('#js_list_count_1').change(function() {
			location.href = $.dec78.product.list.condition('_1');
		});
		$('#js_price_order_2').change(function() {
			location.href = $.dec78.product.list.condition('_2');
		});
		$('#js_list_count_2').change(function() {
			location.href = $.dec78.product.list.condition('_2');
		});
	},
	condition: function(suffix) {
		return $('#js_url').val() +
				('lc/' + $('#js_list_count' + suffix).val() + '/') +
				(($('#js_price_order' + suffix).val().length != 0)? ('po/' + $('#js_price_order' + suffix).val() + '/') : '') + 
				'pno/1/';	
	}
};

$.dec78.entry = {};

$.dec78.entry.agreement = {
	init: function() {
		$('#js_chk_agree').bind('click', this.agree);
		this.agree();
	},
	agree: function() {
		var chk = $('#js_chk_agree').attr('checked');
		$('#js_sbt_agree').attr('disabled', !chk);
		$('#js_sbt_nagree').attr('disabled', !chk);
	}	
};

$.dec78.common = {};

$.dec78.common.customer = {
	info: function() {
		$.ajax({
			url: '/p/api/customer',
			cache: false,
			success: function(data) {
				if (data) {
					$('#name > p').html(data.name);
					$('#name2').html(data.name);
					$('#point01 > p').html(data.point || 0).parent().show();
					// ヘッダの制御
					$('#h_formarea td.login-true').show();
				} else {
					$('#name > p').html('ゲスト');
					// ヘッダの制御
					$('#h_formarea td.login-false').show();
				}
			}
		});
	}
};

$.dec78.common.cart = {
	info: function() {
		$.ajax({
			url: '/p/api/cart',
			cache: false,
			success: function(data) {
				if (data) {
					$('#total_num').html(data.total_num + '個');
					$('#total_price').html(data.total_price + '円');
				} else {
					$('#total_num').html('0個');
					$('#total_price').html('0円');
				}
			}
		});
	}
};

$.dec78.common.product_history = {
	info: function(imgserver, shop) {
		$.ajax({
			url: '/p/api/producthistory',
			cache: false,
			success: function(data) {
				var $tpl = $(
					'<div class="case_t"><!-- rightmgnclear -->' +
						'<div class="case_b">' +
							'<div class="case_m" style="position: relative;">' +
								'<div class="case_inner">' +
									'<!--アイコン -->' +
									'<div class="icon clearfix">' +
										'<p class="icon_r"><img src="" /></p>' +
									'</div>' +
									'<!--画像 -->' +
									'<p class="thumb clear">' +
										'<a href="">' +
											'<img src="" />' +
										'</a>' +
									'</p>' +
									'<!--内容 -->' +
									'<div class="data">' +
										'<p class="item">' +
											'<a href=""><br /></a>' +
										'</p>' +
										'<p class="info">価格：円</p>' +
									'</div>' +
									'<!--ボタン -->' +
									'<div class="fvrit" style="position: absolute; bottom: 0pt; left:23px; >left:3px; padding-bottom: 5px;"><a href="#">お気に入りに追加</a></div>' +
								'</div>'+
							'</div>'+
						'</div>' +
					'</div>'
				);
				
				if (data) {
					var html = [];
					for (var i = 0, l = data.length; i < l; ++i) {
						
						// TODO: 表示は 4 つで決め打ち
						if (i == 4) {
							break;
						}
						
						var maker_name = data[i].maker_name || 'その他';
						var model_name = data[i].model_name || 'その他';
						var t = $tpl.clone()
									.find('p.icon_r > img')
										.attr('src', '/common/ec/p/img/' + shop + '/goods/icon/icon_rank_' + data[i].rank.toLowerCase() + '.gif')
									.end()
									.find('p.thumb > a')
										.attr('href', '/p/product/detail/sno/' + data[i].stock_no + '/cid/' + data[i].company_id + '/aid/' + data[i].area_id + '/sid/' + data[i].shop_id + '/')
										.find('img')
											.attr('src', imgserver + '/' + data[i].stock_no + '/S/1.jpg')
											.attr('alt', maker_name + ' ' + model_name)
										.end()
									.end()
									.find('p.item > a')
										.html(maker_name + '<br />' + model_name)
										.attr('href', '/p/product/detail/sno/' + data[i].stock_no + '/cid/' + data[i].company_id + '/aid/' + data[i].area_id + '/sid/' + data[i].shop_id + '/')
									.end()
									.find('p.info')
										.html('価格：' + data[i].sell_price + '円')
									.end();
						
						if (i % 4 == 3) {
							// 右マージンクリア
							t.addClass('rightmgnclear');
						}
						
						html[html.length] = t.get(0);
					}
					
					$('#product-history')
						.find('div.showcase')
							.html(html)
						.end()
						.show();

					$.dec78.common.list.equals($(".case_b > div"));
				}
			}
		});
	}
};

$.dec78.common.list = {
	equals: function(columns) {
		var tallestcolumn = 0;
		columns.each(
		function(){
			currentHeight = $(this).height();
			if(currentHeight > tallestcolumn){
				tallestcolumn = currentHeight;
			}
		});
		columns.height(tallestcolumn + 20);
	}
};

$(function() {
	$.dec78.common.customer.info();
});

