/* 
 * Select Improvement 1.5
 * David Gouch
 * 12 March 2008
 * 
 * Requires jQuery: http://jquery.com
*/

$(document).ready(function(){

var select_improvement_debug = false;

function selectImprovementStart() {
	$('form.improve.listable select').each(function(){
		var select_name = $(this).attr('name');
		createListableImprovedSelect($(this), select_name);
		$(this).remove();
	});

	$('form.improve.clickable select').each(function(){
		var select_name = $(this).attr('name');
		createClickableImprovedSelect($(this), select_name);
		$(this).remove();
	});
}

function debugStatus() {
	if (select_improvement_debug == true) { 
		return "text"; 
	}
	return "hidden"; 
}

function checkForOptionValues(option) {
	if (option.attr('value')) { 
		return 'rel="' + option.attr('value') + '"';
	}
	return "";
}

function createListableImprovedSelect(select, select_name) {
	select.after('\n<div rev="'+ select_name +'" id="'+ select_name +'" class="improved-selection">\n</div>');
	$('.improved-selection[@rev='+ select_name +']').append('<strong>'+ findDefaultOptionText(select) +'</strong>\n');
	$('.improved-selection[@rev='+ select_name +']').append('<ul class="improved-container">\n</ul>\n');
	select.after('<input id="'+ select_name +'" name="'+ select_name +'" value="'+ findDefaultOptionValue(select) +'" type="' + debugStatus() + '" />');
	select.children().each(function(){
		$('.improved-selection[@rev='+ select_name +'] ul').append('<li><a ' + checkForOptionValues($(this)) + ' href="#">' + $(this).text() + '</a></li>\n'); 
	});
}

function createClickableImprovedSelect(select, select_name) {
	select.after('\n<div rev="'+ select_name +'" id="'+ select_name +'" class="improved-selection">\n</div>');
	$('.improved-selection[@rev='+ select_name +']').append('<strong>'+ findDefaultOptionText(select) +'</strong>\n');
	$('.improved-selection[@rev='+ select_name +']').append('<ul class="improved-container clickable">\n</ul>\n');
	select.after('<input id="'+ select_name +'" name="'+ select_name +'" value="'+ findDefaultOptionValue(select) +'" type="' + debugStatus() + '" />');
	select.children().each(function(){
		$('.improved-selection[@rev='+ select_name +'] ul').append('<li><a ' + checkForOptionValues($(this)) + ' href="#">' + $(this).text() + '</a></li>\n'); 
	});
}

function findDefaultOptionValue(select) {
	if (select.attr('value')) { 
		return select.children('option[@selected]').attr('value'); 
	}
	return findDefaultOptionText(select);
}

function findDefaultOptionText(select) {
	return select.children('option[@selected]').text(); 
}

selectImprovementStart();

function getOptionRel(a) {
	if (a.attr('rel')) { 
		return a.attr('rel');
	}
	return getOptionText(a);
}

function getOptionText(a) {
	return a.text();
}

$('.improved-container a').click(function(){
	var selection_rev = $(this).parents('.improved-selection').attr('rev');
	$('#' + selection_rev).val(getOptionRel($(this)));
	$('.improved-selection[@rev='+ selection_rev +'] strong').text(getOptionText($(this)));
	return false;
});

$('.improved-container.clickable  a').click(function(){
	var selection_rev = $(this).parents('.improved-selection').attr('rev');
	$('#' + selection_rev).val(getOptionRel($(this)));
	$('.improved-selection[@rev='+ selection_rev +'] strong').text(getOptionText($(this)));
	window.location = $(this).attr('rel');
});

$('.improved-selection *').click(function(){
	var selection_rev = $(this).parents('.improved-selection').attr('rev');
	$('.improved-selection[@rev='+ selection_rev +']').toggleClass('selected')
	$('.improved-selection[@rev='+ selection_rev +'] .improved-container').toggleClass('opened')
	//$(this).parent().css('z-index', $(this).css('z-index') + 1);
});

});
