var dummy = '$Id: fb_static_bottom.js,v 1.1.4.5 2009-09-22 17:36:48 darmitag Exp $';
// Copyright (c) 2008 Orbis Technology Ltd. All Rights Reserved.
// PaddyPower Football Pages - Static Javascript Loaded *After* Content.
// Any functions needed to build the page should be in fb_static_top.js.

// ---------- Football Utils -------

// Add highlighting events to table cells with class 'fbhlt' in parent_el.
// If parent_el is not supplied, the entrire document will be used.
function fbm_add_mouseovers(parent_el) {
	if (parent_el == null) {
		parent_el = document;
	}
	var class_re = /(^| )fbhlt($| )/;
	var overF = new Function("fb_hlt_cell(this,true)");
	var outF  = new Function("fb_hlt_cell(this,false)");
	var cells = parent_el.getElementsByTagName('td');
	for (var i = 0; i < cells.length; i++) {
		var cell = cells[i];
		if ( cell.className.match(class_re) != null &&
		     cells[i].onmouseover == null ) {
			cell.onmouseover = overF;
			cell.onmouseout  = outF;
		}
	}
}

// Add the highlighting events now.
fbm_add_mouseovers();

// Add a soccer bet to the betslip.
function fbm_bet(ev_oc_id,lp_num,lp_den,hcap_value) {

	BS_set_leg('price_type','L');
	BS_set_leg('lp_num',lp_num);
	BS_set_leg('lp_den',lp_den);
	BS_set_leg('selections',ev_oc_id);
	BS_set_leg('hcap_value',hcap_value);

	BS_go_bet();

	return;
}


// Toggle whether some block element is shown or not.
function fbm_toggle(id) {

	var div_element = document.getElementById('div_' + id);
	var span_element = document.getElementById('span_' + id);
  
	if (div_element.style.display == 'none') {
		div_element.style.display = 'block';
		span_element.innerHTML = FB.xl_hide;
		span_element.className = 'remove';
	} else {
		div_element.style.display = 'none';
		span_element.innerHTML = FB.xl_show;
		span_element.className = 'show'; 
	}

	return;
}


// Save the user's My Matches preferences, and show the resulting page.
// Params:
//   None, but examines what's ticked in the My Matches form.
// Returns:
//   false if an error occurred, true otherwise (and the tab is reloaded).
//
function fb_save_my_matches_pref() {

	var f = document.getElementById('fb_my_matches_form');
	var type_ids = [];
	for (var i = 0; i < f.elements.length; i++) {
		var el = f.elements[i];
		if (el.type == 'checkbox' && el.checked) {
			type_ids.push(el.value);
		}
	}

	if (type_ids.length < 1) {
		alert(FB.xl_types_too_few);
		return false;
	}
	if (type_ids.length > FB.my_matches_pref_limit) {
		alert(FB.xl_types_too_many);
		return false;
	}

	fb_home_load_tab('fbcpnMY','&ev_type_ids=' + type_ids.join(','));

	return true;
}

// ---------- Football Coupons (and things that resemble coupons) -------

// Cleanup any JS data related to a coupon.
// Params:
//   fbcpn_id - id of coupon table. Silently fails if it doesn't exist.
// Returns:
//   Nothing, but data is free-ed (and the coupon may now be unusable).
// Notes:
//   It's the callers responsibility to destroy the HTML.
//
function fbcpn_cleanup_data(fbcpn_id) {
	if (FBCPN[fbcpn_id] != null) {
		var cpn = FBCPN[fbcpn_id];
		FBCPN[fbcpn_id] = null;
		for (var i = 0; i < cpn.row_ids.length; i++) {
			FBCPNROW[cpn.row_ids[i]] = null;
		}
		cpn = null;
	}
}


// Called when the user requests that a coupon table be sorted by a column.
//
// Params:
//   fbcpn_id - id of coupon table.
//   sort_by  - either 'TIME', or one of the col_keys for the coupon.
//
// Returns:
//   Nothing, but re-arranges the tables and remembers the sort order.
//
function fbcpn_sort(fbcpn_id, sort_by) {

	var cpn = FBCPN[fbcpn_id];

	// Figure out which direction we should sort in,
	// updating the relevant coupon sort dir fields.

	// Toggle the current sort direction for this sort key, or
	// set to ascending if not currently sorted.
	cpn.sort_dirs[sort_by] = cpn.sort_dirs[sort_by] * -1;
	if (cpn.sort_dirs[sort_by] == 0) {
		cpn.sort_dirs[sort_by] = 1;
	}
	// Mark the other sort keys as now unsorted.
	for (var i = 0; i < cpn.col_keys.length; i++) {
		var col_key = cpn.col_keys[i];
		if (col_key != sort_by) {
			cpn.sort_dirs[col_key] = 0;
		}
	}
	if (sort_by != 'TIME') {
		// If we're not sorting by time, the time col is now unsorted.
		cpn.sort_dirs['TIME'] = 0;
	}

	var sort_dir      = cpn.sort_dirs[sort_by];

	// Foreach tbody (there's one per section (date or type) you see) ...

	for (var j = 0; j < cpn.tbodies.length; j++) {

		var tbody = cpn.tbodies[j];

		// Loop over the DOM rows in the body of the table, looking
		// up the corresponding cpnrow objects and construcing a
		// mapping from id to DOM row.
	
		var domrows_by_id = [];
		var cpnrows = [];
		for (var i = 0; i < tbody.rows.length; i++) {
			var domrow       = tbody.rows[i];
			var fbcpnrow_id  = domrow.id;
			var cpnrow       = FBCPNROW[fbcpnrow_id];
			if (cpnrow == null) {
				// Ignore non-coupon rows (e.g. section headers).
				// These will get removed and would need to be re-created.
				continue;
			} else {
				cpnrows.push(cpnrow);
				domrows_by_id[fbcpnrow_id] = domrow;
			}
		}
	
		// Sort the coupon rows (using a helper function/closure).
	
		cpnrows.sort(
			function(a,b) {
				return fbcpn_cmp_rows(sort_by,sort_dir,a,b);
			}
		);

		// Remove all the rows from the table body.
		// This sounds drastic, but it's the only way to reliably re-order rows
		// cross-browser.
	
		while (tbody.childNodes.length) {
			tbody.removeChild(tbody.childNodes[tbody.childNodes.length - 1]);
		}

		// Add the coupon rows back in, this time in the sorted order.

		cpn.prev_row_date = '';
		for (var i = 0; i < cpnrows.length; i++) {
			var cpnrow = cpnrows[i];
			var fbcpnrow_id = cpnrow.id;
			tbody.appendChild(domrows_by_id[fbcpnrow_id]);
		}

	}

	// The date sort direction needs to follow the time sort direction.
	// If the date sort direction has changed, we have to reverse the
	// order of the tables on the page.

	if (cpn.coupon_sort != "BN" && cpn.coupon_sort != "BL") {
		
		var time_sort_dir = cpn.sort_dirs['TIME'];
		if (time_sort_dir != 0 && time_sort_dir != cpn.date_sort_dir) {
			cpn.date_sort_dir = time_sort_dir;
			if (cpn.tbodies.length > 1) {
				var prev_table = cpn.tbodies[0].parentNode;
				var table_parent = prev_table.parentNode;
				for (var i = 1; i < cpn.tbodies.length; i++) {
					var table = cpn.tbodies[i].parentNode;
					table_parent.removeChild(table);
					table_parent.insertBefore(table,prev_table);
					prev_table = table;
				}
			}
			cpn.tbodies.reverse();
		}
	}

	// Update the sort indicators shown in the page.
	fbcpn_upd_sort_indicators(fbcpn_id);

}

// Turn the mouseover highlight on or off for a football odds cell.
// Params:
//   td_el - table cell element
//   on    - 0 = turn highlight off, 1 = turn highlight on
// Returns:
//   Nothing, but classes of the cell and it's contents change.
//
function fb_hlt_cell(td_el, on) {

	// If highlight on, give cell a bgGreen class if not already got one.
	// If highlight off, remove bgGreen class from cell if it has one.

	var classes = td_el.className;
	if (classes.match(/(^| )bgGreen($| )/) == null) {
		if (on)  { td_el.className += ' bgGreen'; }
	} else {
		if (!on) {
			td_el.className = classes.replace(/(^| )bgGreen($| )/,' ');
		}
	}

	// For each link inside the cell ...

	var links = td_el.getElementsByTagName('a');
	for (var i = 0; i < links.length; i++) {
		var link_el = links[i];

		// If highlight on, give link a white class if not already got one.
		// If highlight off, remove white class from link if it has one.

		var classes = link_el.className;
		if (classes.match(/(^| )white($| )/) == null) {
			if (on)  { link_el.className += ' white'; }
		} else {
			if (!on) {
				link_el.className = classes.replace(/(^| )white($| )/,' ');
			}
		}
	}

	// For each span inside the cell ...

	var spans = td_el.getElementsByTagName('span');
	for (var i = 0; i < spans.length; i++) {
		var span_el = spans[i];
		var classes = span_el.className;
		// If this span has a fbtip_outer class, make it appear/disappear.
		if (classes.match(/(^| )fbtip_outer($| )/) != null) {
			if (on) {
				span_el.style.display = 'block';
			} else {
				span_el.style.display = 'none';
			}
		}
	}

}


// Hide rows after the cutoff in a coupon's "Markets Offered" section.
// Params:
//   fbcpn_id - id of coupon table (cf. fb_got_CPN)
//   cutoff   - cutoff point (number of markets)
// Returns:
//   Nothing, but rows after the cutoff will be hidden.
//
function fb_hide_mkts_off(fbcpn_id, cutoff) {
	var table_el = document.getElementById(fbcpn_id + '_mkts_off');
	var rows = table_el.getElementsByTagName("tbody")[0].rows;
	var cols_per_row = 3;
	var start_at = Math.round((cutoff-1)/cols_per_row);
	for (var i = start_at; i < rows.length; i++) {
		// Hide the row.
		rows[i].style.display = 'none';
	}
	// Show the "show" link instead of the "hide" link
	document.getElementById(fbcpn_id + '_hide_mo').style.display = 'none';
	document.getElementById(fbcpn_id + '_show_mo').style.display = '';
}


// Show all rows in a coupon's "Markets Offered" section.
// Params:
//   fbcpn_id - id of coupon table (cf. fb_got_CPN)
// Returns:
//   Nothing, but hidden rows will appear.
//
function fb_show_mkts_off(fbcpn_id) {
	var table_el = document.getElementById(fbcpn_id + '_mkts_off');
	var rows = table_el.getElementsByTagName("tbody")[0].rows;
	for (var i = 0; i < rows.length; i++) {
		// Show the row (by resetting the display property).
		rows[i].style.display = '';
	}
	// Show the "hide" link instead of the "show" link
	document.getElementById(fbcpn_id + '_hide_mo').style.display = '';
	document.getElementById(fbcpn_id + '_show_mo').style.display = 'none';
}
