// The order of the types in the following list determines the order of things on the doll
// Beginning of the list = bottom layer on doll
// Ending of the list = top layer on doll

var pieces=new Array("shape", "left_eye", "right_eye", "eye", "mouth", "eye_brows", "eye_glasses", "eye_access", "head_access")


var removals=new Array()

//To make a type remove other types when it is put on (as if it is made of those types)
//add a line with the following form
//removals["type_that_is_being_put_on"]=new Array("type_to_be_removed_1", "type_to_be_removed_2")

removals["eye"]=new Array("left_eye", "right_eye")
removals["left_eye"]=new Array("eye")
removals["right_eye"]=new Array("eye")


// The default color

var red=255
var green=255
var blue=0

// The default previous color

var old_red=255
var old_green=255
var old_blue=0

for (var count=0 ; count<pieces.length ; count++)
{
    pieces[pieces[count]]=new Array()
    pieces[pieces[count]]["current"]=new Array()
    pieces[pieces[count]]["current"]["visible"]=0
    pieces[pieces[count]]["current"]["val"]=0
    pieces[pieces[count]]["old"]=new Array()
    pieces[pieces[count]]["old"]["visible"]=0
    pieces[pieces[count]]["old"]["val"]=0
}


// Tell it to default to showing the circle
var selections="&shape=circle"
pieces["shape"]["current"]["val"]='circle'
pieces["shape"]["current"]["visible"]=1
pieces["shape"]["old"]["val"]='circle'
pieces["shape"]["old"]["visible"]=1


// Don't touch below here!

function change_color(new_red, new_green, new_blue)
{
  if ((new_red == red) && (new_green == green) && (new_blue == blue))
  {
    red=old_red
    green=old_green
    blue=old_blue

    old_red=new_red
    old_green=new_green
    old_blue=new_blue
  }
  else
  {
    old_red=red
    old_green=green
    old_blue=blue

    red=new_red
    green=new_green
    blue=new_blue
  }

  document.images["doll"].src="doll3.php?red="+red+"&green="+green+"&blue="+blue+selections
}

function descendent_swap(parent, touched)
{
  for (var descendent_count=0 ; descendent_count<removals[parent].length ; descendent_count++)
  {
    if (!(touched[removals[parent][descendent_count]]))
    {
      var temp_visible=pieces[removals[parent][descendent_count]]["current"]["visible"]
      pieces[removals[parent][descendent_count]]["current"]["visible"]=pieces[removals[parent][descendent_count]]["old"]["visible"]
      pieces[removals[parent][descendent_count]]["old"]["visible"]=temp_visible

      touched[removals[parent][descendent_count]]=1

      if (pieces[removals[parent][descendent_count]]["current"]["visible"] ^ pieces[removals[parent][descendent_count]]["old"]["visible"])
      {
        descendent_swap(removals[parent][descendent_count], touched)
      }
    }
  }
}

function change(piece, value)
{
  if (pieces[piece]["current"]["visible"])
  {
    if (value == pieces[piece]["current"]["val"])
    {
      pieces[piece]["current"]["val"]=pieces[piece]["old"]["val"]
      pieces[piece]["current"]["visible"]=pieces[piece]["old"]["visible"]

      pieces[piece]["old"]["val"]=value
      pieces[piece]["old"]["visible"]=1

      if ((pieces[piece]["current"]["visible"] != 1) && (window.removals[piece]))
      {
        var touched=new Array()
        touched[piece]=1
        descendent_swap(piece, touched)
      }
    }
    else
    {
      pieces[piece]["old"]["val"]=pieces[piece]["current"]["val"]
      pieces[piece]["old"]["visible"]=1

      pieces[piece]["current"]["val"]=value
    }
  }
  else
  {
    pieces[piece]["old"]["val"]=pieces[piece]["current"]["val"]
    pieces[piece]["old"]["visible"]=0

    pieces[piece]["current"]["val"]=value
    pieces[piece]["current"]["visible"]=1

    if (window.removals[piece])
    {
      var touched=new Array()
      touched[piece]=1

      for (var count=0 ; count<removals[piece].length ; count++)
      {
        touched[removals[piece][count]]=1
      }

      for (var count=0 ; count<removals[piece].length ; count++)
      {
        pieces[removals[piece][count]]["old"]["visible"]=pieces[removals[piece][count]]["current"]["visible"]
        pieces[removals[piece][count]]["current"]["visible"]=0

        if (pieces[removals[piece][count]]["current"]["visible"] ^ pieces[removals[piece][count]]["old"]["visible"])
        {
          descendent_swap(removals[piece][count], touched)
        }
      }
    }
  }

  selections=""

  for (count=0 ; count<pieces.length ; count++)
  {
    if ((pieces[pieces[count]]["current"]["val"] != 0) && pieces[pieces[count]]["current"]["visible"])
    {
      selections=selections+"&"+pieces[count]+"="+pieces[pieces[count]]["current"]["val"]
    }
  }

  document.images["doll"].src="doll3.php?red="+red+"&green="+green+"&blue="+blue+selections
}