// jq_slct.js

//------------------------------------------------------------------------
$.fn.slct = function(o)
	{
	var slct = $(this).addClass("slct");
	slct.bind("change", o.func);
	if (o.reset) slct.bind("reset", function(){
		o.reset(slct,o);
	});
	if (o.id) slct.attr("id",o.id);
	slct.slctReset(o);
	return slct;
	}
//------------------------------------------------------------------------
$.fn.slctReset = function(o)
	{
	var slct = $(this).empty();
	if (o.first) $("<option value='-1'>"+o.first+"</option>").appendTo(slct);
	if (o.width) slct.css({width:o.width});
	if (o.recs)
		{
		var t = o.recs.length;
		if (t>0)
			{
			for (var i=0; i<t; ++i)
				{
				var r = o.recs[i];
				var opt = $("<option value='"+r[o.fldval]+"'>"+r[o.fldlbl]+"</option>").appendTo(slct);
				}	
			if (o.tourn_id) slct.slctVal(o.tourn_id);
			else if (t>1) slct.slctIdx(1);
			}
		}
	return $(this);
	}

//------------------------------------------------------------------------
$.fn.slctTxt = function(txt)
	{
	if (txt)
		{
		$(this).slctNone();
		var i = $(this).slctTxtToIdx(txt);
		if (i != -1)
			{
			$(this).children(":eq("+i+")").attr('selected', 'selected');
			}
		}
	else return $(this).children(":selected").text();
	}
//------------------------------------------------------------------------
$.fn.slctIdx = function(idx)
	{
	if (idx) 
		{
		$(this).slctNone();
		$(this).children(":eq(" + idx + ")").attr('selected', 'selected');
		}
	else 
		{
		$(this).children().each(function(i){
			if ($(this).attr("selected"))
				{
				idx = i;
				return false;
				}
		});
		return idx;
		}
	}

//------------------------------------------------------------------------
$.fn.slctVal = function(val)
	{
	if (val)
		{
		$(this).slctNone();
		var i = $(this).slctValToIdx(val);
		if (i != -1)
			{
			$(this).children(":eq("+i+")").attr('selected', 'selected');
			}
		}
	else return $(this).children(":selected").attr("value");
	}
//------------------------------------------------------------------------
$.fn.slctTotal = function()
	{
	return $(this).children().length;
	}

//------------------------------------------------------------------------
$.fn.slctNone = function()
	{
	$(this).children(":selected").attr('selected', '');
	}
//------------------------------------------------------------------------
$.fn.slctTxtToIdx = function(txt)
	{
	var idx = -1;
	$(this).children().each(function(i){
		if ($(this).text() == txt) {
			idx = i;
			return false;
		}
	});
	return idx;
	}
//------------------------------------------------------------------------
$.fn.slctValToIdx = function(val)
	{
	var idx = -1;
	$(this).children().each(function(i){
		if ($(this).attr("value") == val) {
			idx = i;
			return false;
		}
	});
	return idx;
	}
//------------------------------------------------------------------------
$.fn.slctIdxToVal = function(idx)
	{
	return $(this).children("option:eq("+idx+")").attr("value");
	}
//------------------------------------------------------------------------
$.fn.slctNextVal = function(val)
	{
	var idx = $(this).slctValToIdx(val);
	if (idx != -1)
		{
		var t = $(this).slctTotal();
		if (t>1)
			{
			if (idx < (t-1)) ++idx;
			else if (idx>1) --idx;
			else return -1;
			}
		else return -1;
		}
	return $(this).slctIdxToVal(idx);
	}
