/**
 * まんが王汎用JavaScriptライブラリ
 *
 ***/

/**
 * jQuery共通セットアップ（いずれバラす予定）
 **/
$(function() {
	/** リコメンドウィンドウの初期化 */
	
	$('#recommend').jcarousel({
		wrap: 'last'
	});
	
	/** カートポップアップイベント登録 */
	
	$("#menu_cart").hover(
		function() {
			$("#cart_popup").load("/libs/cart/cart_popup.php")
											.slideDown(100)
											;
		},
		function() {
			$("#cart_popup").slideUp('fast');
		}
	);
	
	/** カートアイコンのロード */
	var preLoadImages = ["/img/icon/basket_add16.png", "/img/icon/basket_delete16.png"];
	$.each(preLoadImages, function() {
		$('<img>').attr('src',this);
	});

	var urlCartJson = '/libs/cart/cart_json.php';

	var preset_val = $('#preset_ajax_order').text();

	if (preset_val != '1') {
		$(".ajax_order:empty").each(function() {
			var id  = this.id;
			var state = this.getAttribute('value');
			$(this).append(renderOrderButton(id, state));
		});
	}
	function renderOrderButton(id, state){
		var html;
		if        (state == 1) {
			html = '<button id="add_' + id + '" class="btn_order"><img src="/img/icon/basket_add16.png"><div>カートに入れる</div></button>';
		} else if (state == 2) {
			html = '<button disabled="disabled" class="ajax_order_denied"><img src="/img/icon/star16.png" />購入済み</button>';
		} else if (state == 3) {
			html = '<button id="add_' + id + '" class="btn_order"><img src="/img/icon/basket_add16.png">カートに入れる<div class="warning">注文履歴あり</div></button>';
		} else if (state == 4) {
			html = '<button id="del_' + id + '" class="ajax_order_delete"><img src="/img/icon/basket_delete16.png">カートから出す</button>';
		} else {
			html = '<button disabled="disabled" class="ajax_order_denied"><img src="/img/icon/circle_denied16.png" />注文を停止中</button>';
		}
		return html;
	}
	/*
	$.getJSON(urlCartJson,
		function(data){
			updateOrderState(data);
			return;
	});
	*/

	function updateOrderState(cartInfo) {
			$('#qty_download').html(cartInfo.qty_download);
			$('#qty_normal').html(cartInfo.qty_normal);
			/*
			$.each(cartInfo.items, function(i,item){
				$('#' + item.id).find('button')
												.addClass('ajax_order_delete')
												.html('<img src="/img/icon/basket_delete16.png">カートから出す')
												.attr('id', 'del_'+item.id)
												;
			});
			*/
			return;
	}


	//updateOrderState(json_str);

	$('.btn_order').click(function() {
		var arParam = (this.id).split('_');
		var cmd = arParam[0];
		var id  = arParam[1];

		$.getJSON(urlCartJson, { "cmd":cmd, 'id':id }, function(data) {
			if (data.result == false) {
				alert(data.errorMessage);
			}
			
			updateOrderState(data);
			/*
			if (cmd == 'add') {
				$('#' + id).find('button')
									.attr('id', 'del_' + id)
									.html('<img src="/img/icon/basket_delete16.png">カートから出す')
									.addClass('ajax_order_delete')
								;
			
			} else if (cmd == 'del') {
				
				$(this).attr('id', 'add_' + id)
								.html('<img src="/img/icon/basket_add16.png">カートに入れる')
								.removeClass('ajax_order_delete');
				$(id).css('background-color', 'white');
				
			}
			*/
		});
		
		if (cmd == 'add') {
			$(this).attr('id', 'del_' + id)
							.html('<img src="/img/icon/basket_delete16.png">カートから出す')
							.addClass('ajax_order_delete')
							;
		
		} else if (cmd == 'del') {
			
			$(this).attr('id', 'add_' + id)
							.html('<img src="/img/icon/basket_add16.png">カートに入れる')
							.removeClass('ajax_order_delete');
			//$(id).css('background-color', 'white');
			
		}
		
		return false;
	});

});

