// --- Jostens Ringbuilder --------------------
// --- Rep Admin Common JS functions ----------
// --- Created by ideapark -- January 2007! ---


// --- colorMenu is for the color picker in step 5 on the admin page

function colorMenuInit(menuID) {
  if (document.getElementById(menuID)) {
    var menu = document.getElementById(menuID);
    var colorLIs = menu.getElementsByTagName('li');
    var defOrdinal = 0;
    
    var input = menuID.replace(/(.+)Selector/,'$1');
    input = document.getElementById(input);
    
    for (i=0; i<colorLIs.length; i++) {
      colorLIs[i].hex =  colorLIs[i].className.replace(/.+ (......)/,'$1');
      colorLIs[i].style.background = '#' + colorLIs[i].hex;
    
      if (i != 0) {
        colorLIs[i].style.display = 'none';
      }
      if (input.value != '' && input.value != null) {
        if (colorLIs[i].className.indexOf(input.value) != -1) {
          colorLIs[i].style.display = 'block';
          defOrdinal = i;
          if (i != 0) {
            colorLIs[0].style.display = 'none';
          }
        }
      }
      
      colorLIs[i].onclick = function() {
        for (var x=0; x<colorLIs.length; x++) {
          if (document.all) {
            document.getElementById('schoolMessage').style.visibility = 'hidden';
          }
          colorLIs[x].style.display = 'block';
          colorLIs[x].ordinal = x;
          colorLIs[x].onclick = function() {
            if (document.all) {
              document.getElementById('schoolMessage').style.visibility = 'visible';
            }
            colorMenu(menuID,this.ordinal);
          }
        }
      }
      colorLIs[i].onmouseover = function() {
        this.style.border = '4px solid #DCDCDC';
      }
      colorLIs[i].onmouseout = function() {
        this.style.border = '4px solid #FFF';
      }
    }   
    var indicator = menuID.replace(/(.+)Selector/,'$1Indicator');
    indicator = document.getElementById(indicator);
    indicator.style.background = '#' + colorLIs[defOrdinal].className.replace(/.+ (......)/,'$1');
  }
}

function colorMenu(menuID,ordinal) {
  var menu = document.getElementById(menuID);
  var colorLIs = menu.getElementsByTagName('li');
  
  var colorInput = menuID.replace(/(.+)Selector/,'$1');
  colorInput = document.getElementById(colorInput);
  
  var indicator = menuID.replace(/(.+)Selector/,'$1Indicator');
  indicator = document.getElementById(indicator);
  indicator.style.background = '#' + colorLIs[ordinal].className.replace(/.+ (......)/,'$1');
  
  colorInput.value = colorLIs[ordinal].className;
  for (i=0; i<colorLIs.length; i++) {
    if (i != ordinal) {
      colorLIs[i].style.display = 'none';
    }
    colorLIs[i].onclick = function() {
      for (var x=0; x<colorLIs.length; x++) {
        colorLIs[x].style.display = 'block';
        if (document.all) {
          document.getElementById('schoolMessage').style.visibility = 'hidden';
        }
        colorLIs[x].ordinal = x;
        colorLIs[x].onclick = function() {
          colorMenu(menuID,this.ordinal);
          if (document.all) {
            document.getElementById('schoolMessage').style.visibility = 'visible';
          }
        }
      }
    }
  }
}



// --- imagePicker switches the preview images in step 3 on the admin page

function imagePicker() {
  var inputs = document.getElementsByTagName('input');
  for (i=0; i<inputs.length; i++) {
    if (inputs[i].name.indexOf('image') != -1) {
      inputs[i].onclick = function() {
        var preview = document.getElementById(this.name + 'preview');
        preview.src = 'images/imageOptions/' + this.value;
      }
    }
  }
}



// --- messageEdit populates textareas with contents from a drop-down

function messageEdit() {
  var selects = document.getElementsByTagName('select');
  for (var i=0; i<selects.length; i++) {
    if (selects[i].className.indexOf('message') != -1) {
      selects[i].onchange = function() {
        //if (this.selectedIndex != 0) {
          var editBox = document.getElementById(this.id + 'Edit');
		  if(this.options[this.selectedIndex].innerHTML == 'none' || this.options[this.selectedIndex].innerHTML == 'Choose message'){
			  editBox.value = '';
    	      editBox.innerHTML = '';
		  } else {
	          editBox.value = this.options[this.selectedIndex].innerHTML;
    	      editBox.innerHTML = this.options[this.selectedIndex].innerHTML;
		  }
        //}
      }
    }
  }
}