/* 
	Available UI functions :
		- uiSlider(UI element id, default value, min value, max value, step increments, form field, formatting type['currency',null])
		- uiOptions(UI element container, default selected option id OR null, auto-form submit[true, false])
*/
function uiSlider(e,va,mn,mx,s,f,t,d){
	jQueryCM(function() {
		jQueryCM(e).slider({
			range: "min",
			value:va,
			min: mn,
			max: mx,
			step: s,
			slide: function(event, ui) {
				if(t=='USD'){jQueryCM(f).val('$' + addCommas(ui.value));}
				if(t=='MILES'){jQueryCM(f).val(addCommas(ui.value));}
				else{jQueryCM(f).val(ui.value);}
			}
		});
		/*
		// use this to allow the input field to update the slider 
		jQueryCM(f).change(function() { 			 
			jQueryCM(e).slider({ value: jQueryCM(f).val() });
			if(t=='USD'){jQueryCM(f).val('$' + addCommas(jQueryCM(f).val()));}
		});
		*/
		var frm_min = mn;
		var frm_max = mx;
		var frm_val = jQueryCM(e).slider("value")
		if(t=='USD'){
			frm_min = '$' + addCommas(frm_min);
			frm_max = '$' + addCommas(frm_max);
			if(!frm_val){frm_val=0;}
			frm_val = '$' + addCommas(frm_val);
		}
		if(t=='YEARS'){
			frm_min = frm_min + ' ' + d;
			frm_max = frm_max + ' ' + d;
			jQueryCM("#measure_val").text(d);
			if(!frm_val){frm_val=0;}
		}
		if(t=='MONTHS'){
			frm_min = frm_min + ' ' + d;
			frm_max = frm_max + ' ' + d;
			jQueryCM("#measure_val").text(d);
			if(!frm_val){frm_val=0;}
		}
		if(t=='PERCENT'){
			frm_min = frm_min + ' %';
			frm_max = frm_max + ' %';
			jQueryCM("#measure_val").text('%');
			if(!frm_val){frm_val=0;}
		}
		if(t=='MILES'){
			frm_min = frm_min + ' ' + d;
			frm_max = addCommas(frm_max) + ' ' + d;
			jQueryCM("#measure_val").text(d);
			if(!frm_val){frm_val=0;}
		}
		// update our displayed values
		jQueryCM(f).val(frm_val);
		jQueryCM("#leftvalue").text(frm_min);
		jQueryCM("#rightvalue").text(frm_max);

	});
};


function uiOptions(e,va,s){
//	alert('uiOptions('+e+', '+va+', '+s+')');
	jQueryCM(function() {
		if(typeof s == 'undefined' || s == false) {
			s = false;
		}
		else if(s == true){
			jQueryCM(".form-submit").hide();	
		}
		if(typeof va == 'undefined' || va == false) {
			va = false;
		}
		jQueryCM(e + " label").click(function() { 
			var j = jQueryCM(this).attr('for'); // our field key, label must have the same 'for' value as our option's 'id'
//			alert('onClick= '+j);
			jQueryCM("#" + j).attr('checked','checked');
			jQueryCM(e + " label").removeClass('clicked'); // hide previous clicks
			jQueryCM(this).addClass('clicked'); // set new click
			if(s){
				//jQueryCM(e + " form").submit();
				jQueryCM(e).submit(); // use this line if we pass 'e' as the form element, instead of it's parent
			}
		});
		// preset label to selected style
		if(va){
			jQueryCM("label[for=" + va + "]").addClass('clicked');
		}
	});
}


function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}


function showFullChart(id){
	$(id).jOverlay();
}