var currentSaved = { }; $(document).ready( function() { var cart_button = ''; update_cart_counter(); //console.log(currentSaved) $(document).on("click",".add-to-cart", function() { var link_id = $(this).data("linkid"); var type = $(this).data("type"); // nonvector or vector if (type == "vector" && currentSaved[link_id] == "nonvector") { alert("Sorry, you already have the non-vector version in your cart!") return false; } else if (type == "nonvector" && currentSaved[link_id] == "vector") { alert("Sorry, you already have the vector version in your cart!") return false; } if (currentSaved[link_id]) { // already exists...so lets remove it (saves having to use different classes) //localStorage.removeItem('cart_'+link_id); delete currentSaved[link_id]; // write back the current ones we now have writeCookie("GFCart",JSON.stringify(currentSaved),7); $("#cart_"+link_id+"_"+type).html(cart_button + " Add to Cart"); // change any that already have $(".add-to-cart").css("background", "#5cb85c"); } else { // update it so we start where the heart is... var offset=$(this).offset(); $('#flyingcart').css('left',offset.left+'px'); $('#flyingcart').css('top',offset.top+'px'); // now lets animate it, and then append it to the bar... $('#flyingcart').show().animate({ left: $('#cart_block').offset().left +"px", top: $('#cart_block').offset().top + "px" }, 1500, function() { $('#flyingcart').css('left','-30px'); }); currentSaved[link_id] = type; //console.log(currentSaved); writeCookie("GFCart",JSON.stringify(currentSaved),7); //localStorage.setItem('cart_'+link_id,1); $("#cart_"+link_id+"_"+type).html(cart_button + " Remove from Cart"); // change any that already have $("#cart_"+link_id+"_"+type).css("padding", "3px"); $("#cart_"+link_id+"_"+type).css("background", "#eee"); $("#cart_"+link_id+"_"+type).css("border-color", "#fff"); $("#cart_"+link_id+"_"+type).css("color", "#000"); } update_cart_counter(); }); function update_cart_counter() { var cart_contents = readCookie("GFCart"); var counter = 0; //console.log(cart_contents) if (cart_contents != undefined && cart_contents != null && cart_contents != "") { currentSaved = JSON.parse(cart_contents); for (var key in currentSaved) { //console.log("Adding into array: " + key) //console.log("#cart_"+key+currentSaved[key]) $("#cart_"+key+"_"+currentSaved[key]).html(cart_button + " Remove from Cart"); // change any that already have $("#cart_"+key+"_"+currentSaved[key]).css("padding", "3px"); $("#cart_"+key+"_"+currentSaved[key]).css("background", "#eee"); $("#cart_"+key+"_"+currentSaved[key]).css("border-color", "#fff"); $("#cart_"+key+"_"+currentSaved[key]).css("color", "#000"); } } $('#cart-counter').html(Object.keys(currentSaved).length); } $('#cart_block,.gotocart').click( function() { // var id_loop = []; // var vector_loop = []; // var nonvector_loop = []; // //var currentItems = readCookie("GFCart").replace("/;/g",","); // for (var key in currentSaved) { // id_loop.push(key); // if (currentSaved[key] == "vector") { // vector_loop.push(key) // } else { // nonvector_loop.push(key) // } // } window.location.href = "https://www.graphicsfactory.com/cgi-bin/art/cart.cgi?what=view_cart"; return false; }); }); function readCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for(var i=0;i