$(document).ready(function(){
	$("#artistType div.jqTransformSelectWrapper ul li a").click(function(){
		var type_id = $('#input01 option:selected').val();
		$.ajax({
			url : '/booking/type/' + type_id,
			dataType : 'json',
			success: function (data) {
				$('#input02').children().remove();
				//$('#artistName').remove();
				//$('#artistType').after($('<dd>').attr('id', 'artistName').attr('class', 'grid_4 alpha omega').append($('<select>').attr('id', 'input02').attr('name', 'Artist')));
				$('#artistName .jqTransformSelectOpen span').html(data[0].title);
				$('#input02 option').eq(0).attr('selected', 'selected');
				//artist_li = $("#artistName div.jqTransformSelectWrapper ul").children().eq(0).clone();
				//$("#artistName div.jqTransformSelectWrapper ul").children().remove();
				//$("#artistName div.jqTransformSelectWrapper ul").append(artist_li.clone());
				//$("#artistName div.jqTransformSelectWrapper ul").append(artist_li.clone());
				Artists = $('#input02');
				$.each(data, function(id, item){
					Artists.append($('<option>').attr('value', item.id).html(item.title));
				})
				var $select = Artists;
				var $wrapper = $("#artistName div.jqTransformSelectWrapper");
				var oLabel  = $wrapper.children().eq(0);
				var $ul = $("#artistName div.jqTransformSelectWrapper ul");
				$ul.children().remove();
				$('option', Artists).each(function(i){
					var oLi = $('<li><a href="#" index="'+ i +'">'+ $(this).html() +'</a></li>');
					$ul.append(oLi);
				});
				$ul.find('a').click(function(){
					$('a.selected', $wrapper).removeClass('selected');
					$(this).addClass('selected');
					/* Fire the onchange event */
					if ($select[0].selectedIndex != $(this).attr('index') && $select[0].onchange) {
						$select[0].selectedIndex = $(this).attr('index');
						$select[0].onchange();
					}
					$select[0].selectedIndex = $(this).attr('index');
					$('span:eq(0)', $wrapper).html($(this).html());
					$ul.hide();
					return false;
				});
				$('a:eq('+ $select.selectedIndex +')', $ul).click();
				$('a:eq('+ $select.selectedIndex +')', $ul).click();
				//$('span:first', $wrapper).click(function(){$("a.jqTransformSelectOpen",$wrapper).trigger('click');});
				oLabel && oLabel.click(function(){
					$("a.jqTransformSelectOpen",$wrapper).trigger('click');
				});
				$select.oLabel = oLabel;
			}
		});
		return false;
	});
});
