// JavaScript Document
$(document).ready(function(){
   
    var added_completed = 'Đã thêm vào giỏ hàng';
    var added_failed = 'Sản phẩm đã có trong giỏ hàng.';
    var added_alert = 'Bạn vui lòng chọn màu';
    //check current bag
    var url = '/1/shopping-bag/add-item/?itemid=0';
    
    $('#gh').load(url, function(data){
        $(this).html('Giỏ hàng ('+data+')');
    });

    //init click to bag
//    $('#gh').hover(function(){
//        $(this).css('cursor','pointer');
//    });
//    $('#gh').click(function(){
//        window.location = '/1/shopping-bag/show-item/';
//    });
    
    $('.shoppingbag_add').parent().append('<div id="shoppingajax" style="display:none;"></div>');
//    $('.shoppingbag_add').parent().append('<div id="shopping_completed" style="display:none;"><p style="float:left">Sản phẩm bạn chọn đã thêm vào giỏ hàng.</p><a style="float:left; width:120px; height:20px; color:#fff; background:#912B36; text-align:center" href="#" onclick="self.parent.tb_remove()">Tiếp tục mua hàng</a><a style="float:left; width:120px; height:20px; color:#fff; background:#912B36; text-align:center; margin:0 0 0 10px;" href="/1/shopping-bag/">Xem giỏ hàng</a></div>');
    $('.shoppingbag_add').parent().append('<div id="shopping_completed" style="display:none;"><p style="float:left">Sản phẩm bạn chọn đã thêm vào giỏ hàng.</p><a style="float:left; width:120px; height:20px; color:#fff; background:#912B36; text-align:center; margin:0 0 0 10px;" href="/1/shopping-bag/">Xem giỏ hàng</a></div>');
    $('.shoppingbag_add').parent().append('<div id="shopping_failed" style="display:none;"><p>Sản phẩm đã có trong giỏ hàng</p></div>');
    $('.shoppingbag_add').parent().append('<div id="shopping_alert" style="display:none;"><p>Bạn vui lòng chọn màu của sản phẩm</p></div>');
    $('.shoppingbag_add').click(function(){

        var _src = $('#thumb').attr('src');
        var _moveimage = '<img id="movingimage" src="'+_src+'" style="position:absolute; z-index:999; opacity:0.7; left:0; top:0;" width="200" heigh="120" />';
        $('#thumboutline').append(_moveimage);
        $('#movingimage').animate({
            opacity: 0,
            left: '+=600',
            top: '-=250',
            width: '-=120',
            height: '-=120'
        }, 1000, function() {
            // Animation complete.
            $('#movingimage').remove();
            var itemid = $('#itemid').html();
            var colorid = $('#colorid').html();
            var sizeid = $('#sizeid').html();
            var weight = $('#weight').html();
            var price = $('#pprice').html();
            price = price.replace(" ", "");
            price = price.replace("đ", "");
            price = price.replace(",", "");
            //price = price.trim();
	    price = price.replace(/^\s+|\s+$/g,"");
            
            if (sizeid == ''){
                tb_show("", "#TB_inline?height=50&width=300&inlineId=shopping_alert");
                return false;
            }

            var url = '/1/shopping-bag/add-item/?itemid='+itemid+'&colorid='+colorid+'&sizeid='+sizeid+'&weight='+weight+'&price='+price+'&rand='+Math.random();

            $('#shoppingajax').load(url, function(data){
                if (data > 0){
                    //alert(added_completed);
                    tb_show("", "#TB_inline?height=70&width=300&inlineId=shopping_completed");
                    $('#gh').html('Giỏ hàng ('+data+')');
                }
                else{
                    tb_show("", "#TB_inline?height=50&width=300&inlineId=shopping_failed");
                    var data = data * -1;
                    $('#gh').html('Giỏ hàng ('+data+')');
                }
            });
        });
        
    });
    $('.shoppingbag_add').hover(function(){
        $(this).css('cursor','pointer');
    });
});



function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

