// JavaScript Document.

jQuery.fn.textdropdown = function() {
  with ($(this)) {
    with (next('ul:first')) {
      find('li').click(function() {
		 //$(this).parent().find('#back_tread_colour').attr('value', $(this).html());
		 txt_value = $(this).find("span.txt_container").text();
        $(this).parent().prev('.textdropdown-outer').find('input:first').val(txt_value);
		$(this).parent("input").val(txt_value);

		parent_id = $(this).parent().prev('.textdropdown-outer').find('input:first').attr("id");
		
		if(parent_id == "frontFontField") {
			$("#backFontField").val(txt_value);
		}
		else if (parent_id == "front_tread_colour") {
			$("#back_tread_colour").val(txt_value);
		}
		 //$('#back_tread_colour').attr('value', $(this).html());
		
        $(this).parent().hide();
      });
      hide();
    } 

    keypress(function() { 
      $(this).parent().next('ul:first').hide();
    });

    change(function() { 
      $(this).parent().next('ul:first').hide();
    });

    wrap('<div class="textdropdown-outer" style="width: ' + width() + 'px; height: ' + (height() + 5) + 'px"></div>');
    var btn = parent().prepend('<div class="textdropdown-btn">&nbsp;</div>').find('.textdropdown-btn');
    width(width() - btn.width() - 5);
    css("border", "0");

    btn.click(function() {
      var p = parent();
      with (p.next('ul:first')) {
        css('position', 'absolute');
        css('width',    p.width());
        css('left',     p.position().left);
        css('top',      p.position().top + p.height() + 1);
        toggle();
      }
    });
	click(function() { btn.click(); });
  }
}
