/**
 * 買い物かごウィンドウオブジェクト
 * CartWindow
 **/
function CartWindow(id) {
    this._id = id ? id :"cart_window";
     this.create();
}
CartWindow.prototype = {
    _timeout : 2000,
	// カートウィンドウ枠作成
	create : function() {
	    var THIS = this;
		var win = $('<div>').attr('id', this._id).addClass("cart_window")
		    .append($('<div>').addClass("title").text('商品を買い物かごに入れました。'))
		    .append($('<div>').addClass("name"))
		    .append($('<div>').addClass("product_no"))
		    .append($('<div>')
			    .append($('<span>').addClass("product_price"))
			    .append($('<span>').addClass("subtotal"))
			)
		    
// 		    .append($('<div>').addClass("link")
// 		        .append($('<button>').addClass("close").text('×閉じる').click(function() {THIS.close();}))
// 			)
// 		    .append($('<div>').addClass("alert").text("※　この表示は自動的に消えます"))
			.hide();
	    $('body').append(win);
	},
	
	// カートウィンドウを表示
	insert : function(productTr, e) {
	    var THIS = this;

	    // 買い物かごに商品を入れる処理

	    productTr.find('input.num').val(parseInt(productTr.find('input.num').val()));
	    var num = isNaN(productTr.find('input.num').val()) ? 1 : productTr.find('input.num').val();

	    $.post(productTr.find('form').attr("action"),
		{
	        "insert" : productTr.find('input.insert').val(),
	        "num" : num
	    },
	    function(res){
	        THIS.open(productTr, num, e);
	    });
	},
	
	open : function(productTr, num, e) {
	    var THIS = this;
	    var win = $('#'+this._id);
	    win.find('.product_no').text(productTr.find('.product_no').text());
	    win.find('.product_price').text(productTr.find('.product_price').text() + "円 × " + num + '　＝　');
	    win.find('.subtotal').text(((productTr.find('.product_price').text().replace(/([^0-9])/g, '')) * num).split3() + '　円');
	
	    $('#'+this._id).css({
// 		    "bottom" : $('html').attr('clientHeight') - e.pageY + 32 +'px',
// 		    "right": $('html').attr('clientWidth') - e.pageX + 32 +'px'

			"left": e.pageX - win.outerWidth() - 32 +'px',
			"top": e.pageY - win.outerHeight() - 32 +'px'
	    }).fadeIn();
		setTimeout(function(){ THIS.close(); }, this._timeout);
	},
	// カートウィンドウを閉じる
	close : function () {
	    $('#'+this._id).fadeOut();
	    return false;
	}
}

// 3桁ごとに区切る
Number.prototype.split3 = function() {
    var r = ""; 
    var t = this.toString().split('.');
    var s = t[0].split("").reverse();
    var a = t[1];
    for(var i = 0; i < s.length; i++) {
        if(i % 3 == 0 && i != 0 && s[i] != "-") {
            r = s[i] + "," + r 
        } else {
            r = s[i] + r;
        }
    }   
    return a ? r + '.' + a : r;
}
