// global.js for TradeSheffield.co.uk

// script include order in HTML pages will be:
//   - all library js files in the order of any dependencies
//   - this (global.js) file
//   - any page-specific, external js files
//   - inline script blocks embedded in the HTML


// General, Anonymous Functions - - - - - - - - - - - - - -

function get(eid) { return document.getElementById(eid); }

function getElementsByTagAndClass(tName, cName, parent) {
	if ('undefined' == typeof parent) parent = document.getElementsByTagName('BODY')[0];
	if (!parent) return null;
	var children = parent.getElementsByTagName(tName);
	var list = new Array();
	var last = children.length;
	for (var i = 0; i < last; i++) {
		if (cName == children[i].className) list.push(children[i]);
	}
	return list;
}

function confirmSubmit(msg) {
	// msg...call-specific text to be appended to prompt for confirmation - 
	//       prompt will begin "Are you sure you wish to", and {msg}, if given,
	//       should be phrased so that confirmation ==> true
	//       optional with default of "continue"
	return confirm('Are you sure you wish to ' + ((!msg)?'continue?':msg) + '?');
}


// XMLHttpRequest Services - - - - - - - - - - - - - - - - 

// create an instance of the X(html)H(ttp)R(equest) Wrapper (xhr is default name)
// conditional, since not all pages need it and may not have included the definition
if ('undefined' != typeof XhrWrapper) var xhr = new XhrWrapper();


// Validation Services - - - - - - - - - - - - - - - - - - 

// create a page-global instance of the Validation Manager
// conditional, since not all pages need it and may not have included the definition
if ('undefined' != typeof aryooeye && 
		'undefined' != typeof aryooeye.ValidationManager) {
		var valMgr = new aryooeye.ValidationManager();
		// set the object's name to that actually used
		valMgr.setName('valMgr');
}

// call the naughty name service to get the current list and pass the response to
// the public mutator for the word filter of the Validation Manager instance
// conditional, since not all pages need it and may not have included the definitions
if ('undefined' != typeof xhr && 
		'undefined' != typeof valMgr) 
		xhr.doGetText('includes/validation_naughty_words.php', 'valMgr.setWordFilterList');

// do last ditch validation of all fields of the form and submit, ONLY if all
// pass validation tests
function chkSubmit(formId, fids, msgTargets) {
	var isValid = true;
	var last = fids.length;
	for (var i = 0; i < last; i++) {
		var f = document.getElementById(fids[i]);
		var val = valMgr.validateTextItem(f, false, msgTargets[i]);
		isValid = isValid && val;
	}
	if (isValid) document.getElementById(formId).submit();
	return isValid;
}


// AJAX Business Edit Services - - - - - - - - - - - - - - -

function bizEdit(bRef, type, tId) {
	var bVal = bRef.value;
	// if delete button pressed and user disconfirms, exit immediately
	if ('x' == bVal && !confirmSubmit('delete this item')) return;
	// implicit {else} - function exited for true, composite {if}
	var act = ('x' == bVal)?'delete':'edit';
	var url = 'business_editAJAX.php?off_id=' + tId + '&type=' + type + '&action=' + act;
	xhr.doGetText(url, 'handleBizEdit', ['"' + type + '"']);
}

function handleBizEdit(res, type) {
	// If server-side process failed, PHP service returns "bad" as the response.
	if ('bad' == res) {
		alert('could not comply with your ' + act + ' request at this time...sorry');
		return;
	}
	// implicit {else} - function exited for true, {if}
	// put response content into container div by id 
	get('ps'+ type).innerHTML = res;
}


// Navigation - - - - - - - - - - - - - - - - - - - - - - - -

function showNav(eRef, show) {
	if ('undefined' == typeof show) show = true;	// set default, if not given
	var submenu = eRef.getElementsByTagName('DIV')[0];
	// if no submneu for this main menu option, exit immediately - style sheet 
	// will manage color swapping for :hover pseudo-class on main option
	if ('undefined' == typeof submenu) return;
	var anchors = submenu.getElementsByTagName('A');
	var head = eRef.getElementsByTagName('H2')[0];
	var baseWidth = eRef.offsetWidth;
	var last = anchors.length;
	// leave a small amount for spacing between anchors when there are multiple
	// segments required to contain the many (more than 4) anchors
	var aWidth = (baseWidth - 10) + 'px';
	for (var i = 0; i < last; i++) {
		anchors[i].style.width = aWidth;
	}
	var segments = Math.ceil(last/4);	// max. of four anchors per "column" of display
	submenu.style.width = (2 + baseWidth*segments) + 'px';
	submenu.style.marginLeft = '-1px';
	var ha = head.getElementsByTagName('A')[0];
	ha.style.backgroundColor = show?'#538BB0':'#427191';
	var mainOptions = getElementsByTagAndClass('DIV', 'menu', get('navigation'));
	var moCnt = mainOptions.length;
	var thisPosition = parseInt(eRef.id.substring('menuOption'.length));
	var lastPosition = parseInt(mainOptions[moCnt - 1].id.substring('menuOption'.length));
	if (lastPosition < thisPosition + segments - 1) {
		submenu.style.marginLeft = (baseWidth*(1 + lastPosition - thisPosition - segments)) + 'px';
	}
	submenu.style.display = show?'block':'none';
}

function spreadMenuOptions() {
	// evenly spread the main menu options across the full breadth of the area
	// reserved for navigation controls
	// get references to the main menu options
	var mainOptions = getElementsByTagAndClass('DIV', 'menu', get('navigation'));
	if (!mainOptions) return;
	// how many options in all
	var moCnt = mainOptions.length;
	// per option width in CSS percentage format
	var w = (100/moCnt) + '%';
	for (var i = 0; i < moCnt; i++) {
		// set width for current option
		mainOptions[i].style.width = w;
	}
}

// set main menu options widths on-load
window.onload = init;
function init() {
spreadMenuOptions();
initFader();
}