
function set_select_value(elem, value)
{
	var opts = elem.options;
	elem.selectedIndex = 0;
	for (var i = 0, l = opts.length; i < l; i++)
	{
		if (opts[i].value == value)
		{
			elem.selectedIndex = i;
		}
	}
}

function is_empty(el) {
	var val = trim(el.value);
	el.value = (val != '') ? val : 'all';
}
function trim(str) {
		return str.replace(/^\s*(.*\b)\s*$/, "$1");
}


var open_popup, open_modal = new Modalizer();;
show_popup = function (event)
{
	event.stop();

	var id = this.id.replace('_btn', ''), popup = $(id+'_popup');
	if (open_popup)
		hide_popup.bind(open_popup)();
	//open_modal.modalShow({modalStyle: {'opacity': .4}, hideOnClick: false});
	popup.setStyle('display', 'block').setPosition().fade(0, 1);
	$(id+'_close').addEvent('click', hide_popup.bind(popup));
	open_popup = popup
}
hide_popup = function (event)
{
	this.fade(0);
	//open_modal.modalHide();
}

window.addEvent('domready', function () {
	$$('a.btn').each(function(btn){
		var props = btn.getProperties('id', 'class', 'href');
		props.type = 'button';
		props.value = btn.get('text');
		btn = new Element('input', props).replaces(btn);
		btn.addEvent('click', function (e) {
			var href = this.getProperty('href');
			if (href)
			{
				location.href = href;
			}
		});
	});
});

// jobs timer

// *> make timer save time to cookie so it's not lost on page unload
// *> make start/stop functionality work without saving first
var timer_id = [];
var start_time = [];
var total_time = [];
function timer_tick(id)
{
	if ( ! start_time[id])
	{
		start_time[id]   = new Date();
	}

	var   now = new Date();
	var   diff = Math.floor((now.getTime() - start_time[id].getTime()) / 1000);

	hours = Math.floor(diff / 3600);
	minutes = Math.floor((diff % 3600) / 60);
	seconds = (diff % 3600) % 60;

	if (hours)
	{
		minutes = num_format(minutes);
	}

	total_time[id] = hours + ':' + minutes;
	var time = ((hours) ? hours + ':' : '') + minutes + ':' + num_format(seconds);

	var update = $('timer-' + id);
	update.innerHTML = time;
	update.addClass('timer');
	if (window.task_name)
	{
		document.title = time + ' ' + task_name[id];
	}
}

function num_format(num)
{
	return (num < 10) ? '0' + num : num;
}

function timer_start(id, elem)
{
	if ( ! timer_id[id])
	{
		if ( ! start_time[id])
		{
			start_time[id]   = new Date();
			timer_id[id]  = setInterval('timer_tick("' + id + '")', 1000);
			elem.className = 'stop';
			elem.title = 'Stop Timer';
			$('save-'+id).removeClass('hide');
		}
		else
		{
			alert('Please save your previous time first. \n(Start/stop without saving coming soon.)');
		}
	}
	else
	{
		timer_stop(id);
		elem.className = 'start';
		elem.title = 'Start Timer';
	}

	elem.blur();

	return false;
}

function timer_stop(id, elem)
{
	if (timer_id[id])
	{
		clearInterval(timer_id[id]);
		timer_id[id] = null;
	}

	if (elem)
	{
		elem.blur();
	}

	return false;
}

function timer_save(id, elem)
{
	timer_stop(id);

	notes = prompt('Please enter notes.', '');

	if (notes != null)
	{
		start_time[id] = null;

		var request = new Request.HTML({
				url: '/jobs/update_hours/' + id + '/' + total_time[id] + '/' + Math.random(),
				data: 'ajax=1&notes=' + notes,
				update:  $('timer-' + id)
			}).post();

		document.title = 'Jobs Manager'
	}

	if (elem)
	{
		elem.blur();
	}

	$('save-'+id).addClass('hide');
	$('timer-' + id).removeClass('timer');

	return false;
}



/*function position_controls()
{
	if ( ! window.controls_top)
	{
		controls_top = $('controls').offsetTop;
		$('controls_wrap').style.height = $('controls').offsetHeight+'px';
	}

	if (document.documentElement.scrollTop > controls_top || self.pageYOffset > controls_top)
	{
		$('controls').style.position = (window.XMLHttpRequest) ? 'fixed' : 'absolute';
		$('controls').style.top = (window.XMLHttpRequest) ? '0' : document.documentElement.scrollTop;
	}
	else if (document.documentElement.scrollTop < controls_top || self.pageYOffset < controls_top)
	{
		$('controls').style.position = '';
		$('controls').style.top = '';
	}
}*/
/*window.addEvent('domready', function() {
	if ($('controls'))
	{
		window.addEvent('scroll', position_controls);
	}
});*/
