// JavaScript Document
image_loaded = true;
cart_items = new Array();
cart_lock = false;
cart_empty = true;

function print_cart() {
    rebuild_cart();
    var total = cart_items.length;
    	
	output = '';
	for (var i = 0; i < cart_items.length; i++) {
	    output += ('<tr><td width="20" align="center"><a href="#" class="remove_from_cart" itemId="' + cart_items[i][0] + '" title="Remove">\\/</a>' + "</td><td><a href=\"/Detail/" + escape(cart_items[i][2].replace(new RegExp(' ', "g"), '-')) + "/" + cart_items[i][0] + "\">" + cart_items[i][2] + "</a></td></tr>");
    }
	if (total == 0) {
		cart_empty = true;
	}
	else {
		cart_empty = false;
	}
	total = Math.round(total*100)/100;
	total = total.toString();
	decimal = total.indexOf(".");
	if (decimal == '-1') {
		total = total;
    }
	if (cart_empty != true) {
		output += '<tr><td colspan="2" class="separator"></td></tr><tr><td colspan="2" align=\"right\"><strong>' + total + ' Selected</strong></td></tr>';
		output = "<h2>Selections</h2><table width=100%>" + output + "</table>";
		$("#cart").show();
	}
	else {
	    //output = "No items in cart.";
	    output = "";
	    $("#cart").hide();
	}

	$("#cart_items").empty().append(output);
}

function inArray(needle, haystack)
	{
		for (h in haystack) {
			if (haystack[h] == needle) {
			return true;
			// return h;	
		}
	}
	return false;
} 

function getSKU(needle, haystack) {
	for (var i = 0; i < haystack.length; i++) {
		if (haystack[i][0] == needle) {
			return i;
		}
	}
	return false;
}

function update_cart(method, id, description) {
    if (cart_lock == false) {
        var array_key = inArray(id, cart_items[id]);

        var key = getSKU(id, cart_items);

			if (method == 'add') {
				if (key === false) {
				    cart_items[cart_items.length] = Array(id, 1, description);
				    add_cart_cookie(id, description);
				}
			}
			
			if (key !== false) {
				if (method == 'remove') {
					new_total = cart_items[key][1] - 1;
				}
				cart_items[key] = Array(id, new_total);
				remove_cart_cookie(id);
			}	
		print_cart();
	}
}

/* Cookie vars */
var c_selectList = "selectList";

function add_cart_cookie(id,description) {
    if (!$.cookie) return;
    var val = $.cookie(c_selectList);
    if (val && val.indexOf(id) > -1) return;
    if (val && val.length > 0) val += ";" + id + "=" + description;
    else val = id + "=" + description;
    $.cookie(c_selectList, val);
}

function remove_cart_cookie(id) {
    if (!$.cookie) return;
    var val = $.cookie(c_selectList);
    if (!val || val.length == 0 || val.indexOf(id) < 0) return;
    var newVal = '';
    $.each(val.split(';'), function(splitId, cookie) {
        var c = $.trim(cookie), existingId = c.split('=')[0], description = c.split('=')[1];
        if (id != existingId) {
            if (newVal.length > 0)
                newVal += ";";
            newVal += existingId + "=" + description;
        }
    });
    $.cookie(c_selectList, newVal);
}

function get_cart_items() {
    var result = new Array();
    if (!$.cookie) return result;
    var val = $.cookie(c_selectList);
    if (!val || val.length == 0) return result;

    $.each(val.split(';'), function(splitId, cookie) {
        var c = $.trim(cookie), existingId = c.split('=')[0], description = c.split('=')[1];
        result[result.length] = Array(existingId, 1, description);
    });
    return result;
}


function rebuild_cart() {
	temp_cart = new Array();
	for (var i = 0; i < cart_items.length; i++) {
		if (cart_items[i][1] != 0) {
		    temp_cart[temp_cart.length] = Array(cart_items[i][0], cart_items[i][1], cart_items[i][2])
		}
	}
	cart_items = temp_cart;
}

$(document).ready(function() {
    cart_items = get_cart_items();
    print_cart();

    $('.add_to_cart')
    .livequery('click', function(event) {
        update_cart('add', $(this).attr("itemId"), $(this).attr("title"));
        return false;
    });

    $('.remove_from_cart')
    .livequery('click', function(event) {
        update_cart('remove', $(this).attr("itemId"), '');
        return false;
    });
});