// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

function cleanUpForm(element_id) {
  var element = document.getElementById(element_id);
  if(element != null) {
    element.remove();
  }
}

// **************************************************
// LOADING LINKS
// **************************************************
function status_loading() {
	$('loading_status').innerHTML = '<img src="/images/loading.gif" />';
}

function status_complete() {
	$('loading_status').innerHTML = '&nbsp;';
}

function status_saving() {
	$('submit_cancel_buttons').hide();
	$('saving').show();
}

function restore_save_button(obj_id) {
  $('submit_cancel_buttons').show();
	$('saving').hide();
}


// -----------------------------------------------------------------------
// Default Horizontal Alpha Bar ******************************************
// -----------------------------------------------------------------------

/**
 * Used for the HorizontalAlphaBar.
 *
 * @param String elementID The name of the object.
 * @param String status If "IN" then it is a mouse over event.
 * If "OUT" then it is a mouse out event.
 * @param String colorToUse This is the background color to
 * use while the mouse is in the area. If status does not
 * equal "IN" then this parameter is ignored.
 */

var lastAlphaBarBackgroundImage = '';

function GBDevPhpLibs_alphaBar(elementID, status, colorToUse) {
    var tableCell = document.getElementById(elementID);
    if(status == "IN") {  // Save the background image.
		lastAlphaBarBackgroundImage = tableCell.getAttribute('background');
		tableCell.removeAttribute('background');
		tableCell.setAttribute('bgColor', colorToUse);
    } else if(status == "OUT") { // Restore the background image.
		tableCell.removeAttribute('bgColor');
		tableCell.setAttribute('background', lastAlphaBarBackgroundImage);
    }
}

// **************************************************
// Resource Search Stats
// **************************************************
function stat_attr_check() {
	var year = $('period_year');
	var month = $('period_month');
	var day = $('period_day');

	if (year == null) { year = now.getYear() + 1900; }
	else { year = year.options[year.selectedIndex].value; }

	if (month != null) { month = month.options[month.selectedIndex].value; }
	else { month = 'nil'; }

	if (day != null) { day = day.options[day.selectedIndex].value; }
	else { day = 'nil'; }

	var path = '/stats/update_stats_view/' + year + '/' + month + '/' + day
	new Ajax.Request(path, {
	   	method: 'get',
	   	onFailure:function(request){alert('HTTP Error ' + request.status + '!')}});

}

function update_searched_stats() {
	var day = $('period_day');
	var month = $('period_month');
	var year = $('period_year');

	if( day[day.selectedIndex].value == 'nil' ) { day = null; }
	if( month[month.selectedIndex].value == 'nil' ) { month = null; }
	if( year[year.selectedIndex].value == 'nil' ) { year = null; }

	if(year == null && month != null) {
		month.selectedIndex = 0; // All
		month = null;
		$('period_month').disabled = true;
	}
	else if (year != null) {
		$('period_month').disabled = false;
	}

	if(month == null && day != null) {
		day.selectedIndex = 0; // All
		day = null;
		$('period_day').disabled = true;
	}
	else if(month != null) {
		$('period_day').disabled = false;
		iYear = parseInt(year.options[year.selectedIndex].value, 10);
		iMonth = parseInt(month.options[month.selectedIndex].value, 10) - 1;
		iDay = $('period_day');

		// Update the number of days in the days-dropdown
		number_of_days_in_month = (32 - new Date(iYear, iMonth, 32).getDate());

		day_selected = '';
		if(day != null) {
 		  day_selected = iDay[iDay.selectedIndex].value
    }

		// Clean all options
		iDay.options.length = 0;
		iDay.options[0] = new Option('All','nil');
		for(i=1; i<=number_of_days_in_month; i++) {
			iDay.options[i] = new Option(i,i);
			if(i == day_selected) { iDay.selectedIndex = i; }
		}
	}

	if (year != null) { year = year.options[year.selectedIndex].value; }
	else { year = 'nil';}

	if (month != null) { month = month.options[month.selectedIndex].value; }
	else { month = 'nil';}

	if (day != null)	{	day = day.options[day.selectedIndex].value; }
	else { day = 'nil';}

	var path = '/stats/update_searched_stats_view/' + year + '/' + month + '/' + day
	new Ajax.Request(path, {
	   	method: 'get',
	   	onFailure:function(request){alert('HTTP Error ' + request.status + '!')}});
}

// **************************************************