// JavaScript Document

jQuery(document).ready(function(){
	jQuery.each(jQuery(".click"), function(){
		jQuery(this).css('cursor','pointer');
	});
	
});

function addToCart(inItemId){
	var requestUrl = "/lsc/cart/";
	var msec = (new Date()).getTime();
	/*
	var paramArray = ["m=add", "t=" + msec, "cd=true"];
	var loadingFunc = function(){}
	if(typeof inItemId != "undefined"){
		paramArray[paramArray.length] = "itemId=" + inItemId;
	}
	var param = paramArray.join("&");
	*/
	var param = { "m" : "add", "t" : msec, "cd" : true };
	if(typeof inItemId != "undefined"){
		param.itemId = inItemId;
	}
	cartRequest(requestUrl, param, refleshCart);
}

function addAndRequest(inItemId){
	var requestUrl = "/lsc/cart/";
	var msec = (new Date()).getTime();
	/*
	var paramArray = ["m=add", "t=" + msec];
	if(typeof inItemId != "undefined"){
		paramArray[paramArray.length] = "itemId=" + inItemId;
	}
	var param = paramArray.join("&");
	*/
	var param = { "m" : "add", "t" : msec, "itemId" : inItemId };
	cartRequest(requestUrl, param, function(inObjRes){ location.href = "/guestCommon/cart/"; });
}

function delFromCart(inItemId){
	if(typeof inItemId != "undefined"){
		var requestUrl = "/lsc/cart/";
		var msec = (new Date()).getTime();
		//var param = "m=del&itemId=" + inItemId + "&t=" + msec;
		var param = { "m" : del, "itemId" : inItemId, "t" : msec };
		cartRequest(requestUrl, param, refleshCart);
	}
}

function cartRequest(inRequestUrl, inParam, inCompleteFunc, inCreateFunc){
	if(typeof inRequestUrl == "undefined"){
		return false;
	}
	if(typeof inParam == "undefined"){
		inParam = "";
	}
	if(typeof inCompleteFunc != "function"){
		inCompleteFunc = function(inObjRes){}
	}
	if(typeof inCreateFunc != "function"){
		inCreateFunc = function(){}
	}
	/*
	new Ajax.Request(inRequestUrl, {
			"method" : "get"
			, "parameters" : inParam
			, "onComplete" : inCompleteFunc
			, "onCreate" : inCreateFunc
			, "asyncflag" : true
		});
	*/
	$.get(inRequestUrl,inParam, inCompleteFunc);
}

function drawCart(objRes){
	//var objRes = eval("(" + res.responseText + ")");
	$("#cartBox").innerHTML = "";
	var cartHTML = ['<img src="/goods/images/cartTitle.jpg" />'];
	/*

                        <ul>
                          <li>現在、買物カゴは空です</li>
                        </ul>
                      <p> 0点　合計　0円 </p>
                      <div class="shoppingBt"> <a>お会計手続きへ進む</a> 
	*/
	if(objRes.items.length>0){
		var i = 1;
		$.each(objRes, function(obj){
			cartHTML[cartHTML.length] = createCartHTML(i++,obj.id, obj.string, obj.count);
		});
		
		cartHTML[cartHTML.length] = '<p> '+objRes.counts+'点　合計　'+addFigure(objRes.sum)+'円 </p>';
		cartHTML[cartHTML.length] = '<div class="shoppingBt"> <a href="/cart/">お会計手続きへ進む</a> </div>';
	}else{
		cartHTML[cartHTML.length] = '<li>現在、買物カゴは空です</li>';
	}
	
	$("#cartBox").html('<ul>'+cartHTML.join("\n")+'</ul>');
}

// 20080229 Append the arg. inIndex (S.U)
function createCartHTML(inIndex,inItemId, inItemString, inItemCount){
	return '<li>'
			//+ inIndex 
			//+ '<span class="deleteBt" onclick="delFromCart(\'' + inItemId + '\')">'
			+ '<div class="itemString">' + decodeURIComponent(inItemString) + '</div>'
			+ '<div class="itemCount">' + decodeURIComponent(inItemCount) + '点</div>'
			+ '<div class="clear"></div>'
			//+ '</span>'
			+ '</li>';
}

function refleshCart(){
	/*
	new Ajax.Request("/cms/cart/detailAsJson.php", {
		"method": "get"
		, "parameters": "t="+(new Date()).getTime()
		, "onComplete": drawCart
		, "asyncflag" : true
	});
	*/
	$.getJSON("/lsc/cart/detailAsJson.php",
		  {
			 "t" : (new Date()).getTime()
		},
		drawCart
		);
}

function imageHilight(inObjRes){
	var itemIds = eval("(" + inObjRes.responseText + ")");
	
	var cartImages = $('img.addList');
	cartImages.each(function(img){
		var itemId = img.getAttribute("value");
		
		if(itemIds.indexOf(parseInt(itemId)) >= 0){
			img.src = 'images/b_list_f2.gif';;
		}else{
			img.src = 'images/b_list.gif';;
		}
	});
	/*
	itemIds.each(function(itemId){
		if(typeof $("pickupList_" + itemId) != "undefined" && $("pickupList_" + itemId) != null){
			Element.setStyle($("pickupList_" + itemId), { backgroundPosition:"0px -40px" });
		}
	});
	*/
	
}

function addFigure(str) {
　var num = new String(str).replace(/,/g, "");
　while(num != (num = num.replace(/^(-?\d+)(\d{3})/, "$1,$2")));
　return num;
}

function cartDebug(inObjRes){
	if(eval("(" + inObjRes.responseText + ")")){
		alert("成功");
	}else{
		alert("失敗");
	}
}

/*
var lsItemSelectForCart = Class.create();

lsItemSelectForCart.prototype = {
	
	initialize : function (targetElement, drawElement){
		this.target = $(targetElement);
		this.draw = $(drawElement);
		this.selector = [];
		this.selectorId = this.target.id + '_selector';
		
		if(arguments.length > 3){
			for(i=2; i<arguments.length; i++){
				this.selector.push(arguments[i]);
			}
			this.createDropdown();
		}
	},
	
	createDropdown : function(){
		var dropdown = [];
		
		dropdown.push('<select id="' + this.selectorId + '">');
		for(i=0; i<this.selector.length; i++){
			value = this.selector[i].value;
			label = this.selector[i].label;
			dropdown.push('<option value="' + value + '">' + label + '</option>');
		}
		dropdown.push('</select>');
		this.draw.innerHTML = dropdown.join("");
		
		Event.observe($(this.selectorId), "change", this.change.bind(this));
		
		this.target.onclick = function(){ addAndRequest($F(this.selectorId)); }.bind(this);
	},
	
	change : function(){
		selected = $F(this.selectorId);
		this.target.onclick = function(){ addAndRequest($F(this.selectorId)); }.bind(this);
	}
	
}
*/


$(function(){
	//addToCart();
});


