var curitype,curicolor;
var pic = new Array();
var wd = new Array();
var prc = new Array();

// Array object for all of the individual styles
function avail(count) {
  this.count = count;
  this.list = new Array(count);
  this.addAvail = avail_addAvail;
}
// Add a style to the list of available items
function avail_addAvail(num,itype,icolor,istyle,tag1,tag2,sizes,price,pers) {
  this.list[num] = new Object();
  this.list[num].itype = itype;
  this.list[num].icolor = icolor;
  this.list[num].istyle = istyle;
  this.list[num].tag1 = tag1;
  this.list[num].tag2 = tag2;
  this.list[num].sizes = sizes;
  this.list[num].price = price;
  this.list[num].pers = pers;
  this.list[num].choose = updatePage;
}


function activateLabels(elemid,prefix) {
  theol = document.getElementById(elemid);
  if(!theol) return;
  for(i=0; i<theol.childNodes.length; i++){
    thisnode = theol.childNodes[i];
    if(thisnode.nodeName=='LABEL') {
      thisid = thisnode.getAttribute('for');
      if(!thisid) {
        thisid = thisnode.htmlFor;
      }
      for(j=0; j<thisnode.childNodes.length; j++){
        if(thisid && thisnode.childNodes[j].nodeName=='SPAN') {
          thisid = thisid.substring(5);
          thisvalue  = '<img src="http://www.newlifeshopper.com/i/'+prefix+'-'+thisid+'.gif" alt="';
          thisvalue += thisnode.childNodes[j].innerHTML;
          thisvalue += '" onclick="updatePage(\''+elemid+'\',\''+thisid+'\',\''+thisnode.childNodes[j].innerHTML+'\');"';
          thisvalue += '>';
          thisnode.childNodes[j].innerHTML = thisvalue;
        }
      }
    }
  }
  return;
}


function imgSwitch() {
  document.getElementById('picture').src = pic[selected].src;
  document.getElementById('stylename').innerHTML = items.list[selected].tag1;
  document.getElementById('styledesc').innerHTML = items.list[selected].tag2;
  return;
}


function loadImages() {
  for(i=0;i<items.count;++i) {
    pic[i] = new Image();
    pic[i].src = "http://www.newlifeshopper.com/images/"+des+"-"+items.list[i].istyle+".jpg";
    wd[i] = items.list[i].tag1+'!'+items.list[i].tag2;
  }
  return;
}


function persChange(switchTo) {
  if(!document.lk.elements['perselect'].options) return;
  if(switchTo) {
    document.lk.elements['perselect'].disabled = false;
    document.lk.elements['perselect'].value='--Select One--';
  } else {
    document.lk.elements['perselect'].disabled = true;
    document.lk.elements['perselect'].value='0';
  }
  return;
}


function perscheck()  {
  var persel = document.getElementById('perselect');
  if(!persel) return true;
  if(persel.selectedIndex==0) {
    alert("Please select a personalization option.");
    persel.focus();
    return false;
  }
  return true;
}


function prcTbl(prcname) {
  prcStr = prc[items.list[selected].price];
  prcTable = document.getElementById('pricetbl');
  if(prcStr) {
    temp = prcStr.split('!');
    if(temp.length > 1) {
      prcStr = "1!"+prcStr;
      temp = prcStr.split('!');
      var rowone = "";
      var rowtwo = "";
      for(var i=0; i<temp.length; i=i+2) {
        if(temp[i+2]) {
          rowone += "<th>"+temp[i]+" - "+(temp[i+2] - 1)+"</th>";
          rowtwo += "<td class='prcup'>$"+temp[i+1]+"</td>";
        }
      }
      prcTable.innerHTML = "<table cellspacing=0 class='pricetbl'><tr>"+rowone+"</tr><tr>"+rowtwo+"</table>"
    } else {
      prcTable.innerHTML = "<div class='oneprc'>$"+prcStr+"</div>";
    }
  } else {
    prcTable.innerHTML = "<div class='oneprc'>Price Coming Soon</div>";
  }
  return;
}

function relatedThumbs() {
  theol = document.getElementById('related');
  if(!theol) return;
  for(i=0; i<theol.childNodes.length; i++){
    if(theol.childNodes[i].childNodes.length > 0) {
      thisnode = theol.childNodes[i].childNodes[0];
      if(thisnode.nodeName=='A') {
        thisnode.onmouseover = relatedShow;
      }
    }
  }
  return true;
}

function relatedShow() {
  var design = this.getAttribute('id');
  if(this.childNodes[0].innerHTML.length > 1) return;
  this.childNodes[0].innerHTML = '<img src="http://www.newlifeshopper.com/images/'+design+'-CU.jpg" alt="" onclick="parent.location=\'/christian-shirts/'+design+'.htm\';">';
  return true;
}


function updatePage(element,updated) {
  if(element=='colors') curicolor = updated;
  if(element=='itypes') {
    curitype = updated;
  }
  usecolors = new Array();
  fail = false;
  selected = null;
  firstof = false;
  // find new selected index
  for(i=0;i<items.count;++i) {
    if(items.list[i].itype==curitype && items.list[i].icolor==curicolor) {
      selected = i;
      document.getElementById('0-style').value = items.list[i].istyle;
    }
    //update list of colors for this type
    if(items.list[i].itype==curitype) {
      usecolors[items.list[i].icolor] = true;
      if(!firstof) firstof = i;
    }
  }
  if(allcolors.length>1) {
    for(i=0;i<allcolors.length;++i) {
      if(usecolors[allcolors[i]]!=true) {
        document.getElementById('cl'+allcolors[i]).style.display = 'none';
        if(allcolors[i]==curicolor) {
          fail = true;
        }
      } else {
        document.getElementById('cl'+allcolors[i]).style.display = 'inline';
      }
    }
  }
  if(fail) {
    selected = firstof;
    curicolor = items.list[selected].icolor;
    document.getElementById('color'+curicolor).checked = true;
    toggleLabelStyle('lk', document.getElementById('cl'+curicolor), 'radioOn', 'radioOff');
  }
  szchange(items.list[selected].sizes);
  imgSwitch();
  prcTbl();
  return;
}


function zoombox() {
  var descd = document.getElementById('descd').innerHTML;
  zoompop = window.open('/enlarge.php?lgdesign='+descd,'shpcu',config='height=450,width=450,toolbar=0,menubar=0,resizable=1,location=0');
  return false;
}


window.onload = function() {
  document.getElementById('zoomlink').onclick = zoombox;
  document.getElementById('lk').onsubmit = perscheck;
  loadImages();

  curitype = items.list[selected].itype;
  curicolor = items.list[selected].icolor;
  updatePage();

  // Find all the elements that will trigger switching
  activateLabels('itypes','thm');
  activateLabels('colors','color');
  initARC('lk','radioOn','radioOff','checkboxOn','checkboxOff');
	var box = new Tabbox("infobox", { header: "h3", autosize: true })
  relatedThumbs();

  return;
}