// JavaScript Document

(function($) {
	$(function(){
		/* BEGIN : Document is ready
		---------------------------------------- */

        setForm();

		/* END : Document is ready
		---------------------------------------- */
	});
})(jQuery);

function selectReplacement(obj, callback) {

  // Is select disabled ?
  var isDisabled = $(obj).attr('disabled');
  // append a class to the select
  obj.className += ' replaced';
  // create list for styling
  var ul = document.createElement('ul');
  ul.className = 'selectReplacement';
  if (isDisabled) $(ul).css({ opacity: 0.5 });
  var opts = obj.options;
  for (var i=0; i<opts.length; i++) {
	var selectedOpt;
	if (opts[i].selected) {
	  selectedOpt = i;
	  break;
	} else {
	  selectedOpt = 0;
	}
  }
  for (var i=0; i<opts.length; i++) {
	var li = document.createElement('li');
	var iconpath = $(opts[i]).attr('iconpath');
	var label = $(opts[i]).attr('label');
	var txt = (label != '') ?  label : $(opts[i]).html();

	if (isDisabled) $(li).css({ cursor: 'default' });
	$(li).append('<span>' + ((iconpath != undefined) ? '<img src="' + iconpath + '" alt="icon" title="icon" />' : '') + txt + '</span>');
	li.selIndex = opts[i].index;
	li.selectID = obj.id;
	li.onclick = function() {
	  if (!isDisabled) selectMe(this, callback);
	}
	if (i == 0) {
		li.className = 'first';
	}
	else if (i == (opts.length - 1)) {
		li.className = 'last';
	}
	if (i == selectedOpt) {
	  $(li).addClass('selected');
	  li.onclick = function() {
		if (!isDisabled) {
			this.parentNode.className += ' selectOpen';
			this.onclick = function() {
			  selectMe(this, callback);
			}
		}
	  }
	}
	if (window.attachEvent) {
	  li.onmouseover = function() {
		this.className += ' hover';
	  }
	  li.onmouseout = function() {
		this.className =
		  this.className.replace(new RegExp(" hover\\b"), '');
	  }
	}
	ul.appendChild(li);
  }
  // add the input and the ul
  obj.parentNode.appendChild(ul);
}
function selectMe(obj, callback) {
  var lis = obj.parentNode.getElementsByTagName('li');
  for (var i=0; i<lis.length; i++) {
	if (lis[i] != obj) { // not the selected list item
	  $(lis[i]).removeClass('selected');
	  lis[i].onclick = function() {
		selectMe(this, callback);
	  }
   } else {
	  setVal(obj.selectID, obj.selIndex, callback);
	  $(obj).addClass('selected');
	  obj.parentNode.className =
		obj.parentNode.className.replace(new RegExp(" selectOpen\\b"), '');
	  obj.onclick = function() {
		obj.parentNode.className += ' selectOpen';
		this.onclick = function() {
		  selectMe(this, callback);
		}
	  }
	}
  }
}
function setVal(objID, selIndex, callback) {
  var obj = document.getElementById(objID);
  var prevIndex = obj.selectedIndex;
  obj.selectedIndex = selIndex;
  if (obj.selectedIndex != prevIndex) $(obj).change();
  if (callback && typeof(callback) == 'function') callback();
}
function setForm() {
  var s = $('select.to-ul');
  for (var i=0; i<s.length; i++) {
	selectReplacement(s[i]);
  }
}
function closeSel(obj) {
  // close the ul
}

