// This code is Copyright (C)2008, 2009 Carl A. Rhoades. All rights reserved.
// all variables are in lower-case (ie: newvalue) unless is passed from cafepress or database or for sql queries, tag IDs are all lower-case with underscore between words (ie: add_to_cart)
// key = size, color, qty --must be set
// value = we want to set key to this --must be set
// modifier = used for various things, right now to pass the CafePress name of a color or size
// productnumber = used to ensure the url is formatted correctly when updating the product thumbnail, and in displaying larger versions
// eid = will eventually be used to pass the current element's id to the script for multi-item pages (add page to cart)

// Site variables, so the site where the script is running can be changed, of course!
// You must include final '/' after all directories and URI, or directories break!

var imgnumber = 0; // set to 0;
var serveruri = "http://repulsivegear.com/"; // URI to your site.. ie: http://www.yourdomainname.com/ or http://www.somedomain.com/~yoursite/
var imagedir = "siteworks/images/numbers/"; //Leave blank if images are not in a sub directory of the above 'serveruri', NOT recommended!
function rgLinkChange(key, value, changetype, modifier, productnumber) {
  //Set the anchor tag we want to change ***Set to add_to_cart for now*** ~This will be used when more than one product is on the same page... eventually
  var anchor = "add_to_cart";
  var design_zoom = "";
  var fulluri = String(document.getElementById(anchor).href);
  //Tell JavaScript to treat fulluri as a string and split using ?
  //http://www.cafepress.com/cp/addtocart.aspx?storeid=repulsivegear&s=4473206&size_212336027=8&qty_212336027=1&color_212336027=28
  var shorturi = String(fulluri).split('?')[0];    // [0] = http://www.cafepress.com/cp/addtocart.aspx
  var querystring = String(fulluri).split('?')[1]; // [1] = storeid=repulsivegear&s=4473206&size_212336027=8&qty_212336027=1&color_212336027=28
  var newuri = shorturi + "?"; //since ? was removed by split, we need to add it back on
  var parameters = querystring.split('&'); //split querystring into parameters
  for(var j = 0; j < parameters.length; j++) {
    //Check for key and change it's current value, or do nothing
    if(parameters[j].match(key)) {
      var name = parameters[j].split('=')[0]; //name
      var oldvalue = parseInt(parameters[j].split('=')[1]); //value
      //If key is qty, check it's value and add/subtract from it's value
      if(key == "qty") {
        if(value == "+") {
          var newvalue = oldvalue + 1;
          newuri = newuri + name + "=" + newvalue + "&";
        } else if((value == "-") && (oldvalue != 1)) { //if - is pressed and value is still 1, do not update
          var newvalue = oldvalue - 1;
          newuri = newuri + name + "=" + newvalue + "&";
        } else { //since value is - here, we can't use it to update the anchor
          var newvalue = 1;
          newuri = newuri + name + "=" + newvalue + "&";
        }
        if(newvalue >= 21) { //Since it would take forever to create an image for a large order, just default to a 20+ image.
          var imgnumber = "20p";
        } else {
          var imgnumber = newvalue;
        }
        document.getElementById('qty').textContent=newvalue; //update qty here, or returns undefined when other keys are changed
        document.getElementById('qty').innerText=newvalue; // For IE7
		document.getElementById('qtyi').src=serveruri+imagedir+imgnumber+".png";
        document.getElementById('qtyi').alt="Current Quantity: "+newvalue;
        document.getElementById('qtyi').title="Current Quantity: "+newvalue;
      } else { //outputs new value for parameter
        newuri = newuri + name + "=" + value + "&";
      }
    } else { //outputs original values for unchanged parameters
      newuri = newuri + parameters[j] + "&";
    }
  }
  //Remove extra &s from end of newuri or CafePress has a fit!
  while(newuri.charAt(newuri.length-1) == "&" ) {
    newuri = newuri.substring(0,newuri.length-1);
  }
  //Update the anchor tag
  document.getElementById('add_to_cart').target="_rgCafePress"; // ensure all clicks will load into the same browser window/tab
  document.getElementById('add_to_cart').href=newuri; // actually updates the anchor tag;
  //document.getElementById('add_to_cart').textContent=newuri; // actually updates the anchor tag;
  //update product thumbnail... only works for t-shirts right now!  (may have to use product-type# to block for other items)
  if(key == "color") {
    //document.getElementById('product_image_thumb').src="http://images.cafepress.com/product/"+productnumber+"v6_150x150_Front_Color-"+modifier+".jpg";
	document.getElementById('rg_current_color').textContent=modifier;
	document.getElementById('rg_current_color').innerText=modifier; // For IE7
	//document.getElementById('rg_view_lg_color').textContent=modifier;
	//document.getElementById('rg_view_lg_color').innerText=modifier; // For IE7
    //remove '/' and spaces from colors
    var modifier = modifier.replace("/", "");
    var modifier = modifier.replace(" ", "");
	document.getElementById('product_image_large').src="http://images.cafepress.com/product/"+productnumber+"v6_350x350_Front_Color-"+modifier+".jpg";  
	// Change "View Larger" link to match the currently selected color.
    document.getElementById('design_zoom').href="http://images1.cafepress.com/product_zoom/"+productnumber+"v4_400x400_Front_Color-"+modifier+".jpg";
  }
  //if key is size, change current_size
  if(key == "size") {
    document.getElementById('rg_current_size').textContent=modifier;
	document.getElementById('rg_current_size').innerText=modifier; // For IE7
  }

  //http://images.cafepress.com/image/13606469_125x125.jpg
  //will eventually highlight the color that was last selected.
  //document.getElementById(eid).style.class="color_swatch_selected";
}