/**
 * Used to handle the cart-inserting & cart-removing process
 */
 
function cartUpdate(url, code, remove, quantity)
{
    url = url+'/ajax_cart.php?code='+code;
    if (remove === true) {
        url += '&remove=1';
    }
    if (quantity !== null) {
        url += '&quantity='+quantity;
    }
    $.get(url, cartHandle);
}
function cartHandle(response, code)
{
    if (code == 'success') {
		var results = response.split("\r\n");
        if (results[0] == 'INSERT_OK' || results[0] == 'REMOVE_OK') {
            if (elm = $('#box-cart .total strong')[0]) {
                elm.innerHTML = results[2];
                fadeInit(elm);
                
                buttInsert = document.getElementById('butt-'+results[1]+'-insert');
                buttRemove = document.getElementById('butt-'+results[1]+'-remove');
                
                var row = $('#prod-'+results[1]);
                
                if (results[0] == 'INSERT_OK') {
                    row.addClass('incart');
                    if (buttInsert) buttInsert.style.display = 'none';
                    if (buttRemove) buttRemove.style.display = 'inline';
                }
                else {
                	row.removeClass('incart');
                    if (buttInsert) buttInsert.style.display = 'inline';
                    if (buttRemove) buttRemove.style.display = 'none';
                }
                if (results[2].split('&')[0].replace(',', '.') > 0) {
                	$('#box-cart p[class!=total]').hide();
                } else {
                	$('#box-cart p[class!=total]').show();
                }
            }
        }
        else {
            window.location.reload();
        }
        //opacity('caption-total', 100, 0, 1000);
    }
}

var fadeColor = new Array();
fadeColor[1] = "ff";
fadeColor[2] = "ee";
fadeColor[3] = "dd";
fadeColor[4] = "cc";
fadeColor[5] = "bb";
fadeColor[6] = "aa";
fadeColor[7] = "99";

function fadeInit(elm)
{
    setTimeout(function() { fadeIn(7, elm); }, 200);
}

function fadeIn(where, elm)
{
    if (where >= 1) {
        elm.style.backgroundColor = "#ffff" + fadeColor[where];
        if (where > 1) {
            where -= 1;
            setTimeout(function() { fadeIn(where, elm); }, 200);
        } 
        else {
            where -= 1;
            setTimeout(function() { fadeIn(where, elm); }, 200);
			elm.style.backgroundColor = "transparent";
        }
    }
}

