
controls_bar = {};

controls_bar.show = function () {
	$('controls_content').style.display = 'block';
}

controls_bar.hide = function () {
	$('controls_content').style.display = 'none';
}

function showSettings() {
	controls_bar.hide();

	new Ajax.Updater('controls_content', '?s=settings_form&m=partial', {
		onComplete: function () { controls_bar.show() }
	});	
}

function addTimezone() {
	controls_bar.hide();

	new Ajax.Updater('controls_content', '?s=timezone_form&m=partial', {
		onComplete: function () { controls_bar.show() }
	});

}

timeZoneOnEdit = null;
function editTimezone(id) {
	controls_bar.hide();

	$$('#timezone_' + id + ' .timezone_body')[0].style.backgroundColor = '#ff7';	
	timeZoneOnEdit = id;

	new Ajax.Updater('controls_content', '?s=timezone_form&m=partial&id=' + id, {
		onComplete: function () { controls_bar.show() }
	});
}

function searchTimezones(obj) {
	
	$('timezone_select').options.length = 0;

	for (i in timezones) {
		if (timezones[i].toLowerCase().replace('_', ' ').indexOf($F('timezone_filter').toLowerCase()) != -1) {
			$('timezone_select').options[$('timezone_select').options.length] = new Option(timezones[i].replace('_', ' '), timezones[i]);
		}
	}
}

tzSearchTimer = null;

function filterTimezones(e) {

	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;

	if (code == Event.KEY_DOWN) {
		$('timezone_select').focus();
	}

	clearTimeout(tzSearchTimer);

	tzSearchTimer = setTimeout('searchTimezones()', 500);
}

function refreshTimes() {
	new Ajax.Updater('dashboard', '?s=index&m=partial', {
		onComplete: setup_page
	});

	setTimeout('refreshTimes()', 60000);

}

Event.observe(window, 'load', function () {

	setup_page();

	setTimeout('refreshTimes()', 60000);

});

function setup_page() {
	$$('.timezone .options').each(function (t) {
		t.style.visibility = 'hidden';
	});

	$$('.timezone_body').each(function (t) {
		if (t.offsetWidth < 170) {
			t.style.width = '170px';
		}
	});

	$$('.timezone').each(function (t) {
		t.onmouseover = function () {
			$$('#' + this.id + ' .options').each(function (opt) {
				opt.style.visibility = '';
			});
		}
		t.onmouseout = function () {
			$$('#' + this.id + ' .options').each(function (opt) {
				opt.style.visibility = 'hidden';
			});
		}
	});


}


function confirm_delete(id) {
	$$('#timezone_' + id + ' .timezone_body')[0].style.backgroundColor = '#e99';

	if (confirm('Sure?')) {
		return true;
	}
	else {
		$$('#timezone_' + id + ' .timezone_body')[0].style.backgroundColor = '#D9E6EF';
		return false;
	}
}

function cancel_controls() {
	$$('#timezone_' + id + ' .timezone_body')[0].style.backgroundColor = '#D9E6EF';	
}

function checkSearchOnFocus(obj) {
	if (obj.value == 'search...') {
		obj.value = '';
		obj.style.color = 'black';
	}
}

function checkSearchOnBlur(obj) {
	if (obj.value == '') {
		obj.value = 'search...';
		obj.style.color = '#888';
	}
}

function stopRKey(evt) {
   var evt = (evt) ? evt : ((event) ? event : null);
   var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
   if ((evt.keyCode == 13) && (node.type=="text")) {return false;}
}

document.onkeypress = stopRKey; 