// abs_interface.js
// -----------------------------------------------------------------------------
// Javascript functions to run the tabbed menu and floating promobox
// This depends on dmb0_lib.js (at /dmx/core/b0/js/dmb0_lib.js)
//
// Release    > 1.2.1
// Project    > dmClubNewYear08:S01_C33
// Changedate > 06-12-07
// Copyright  > (C) Digital Mail Limited (2005 - 2007)
// Author     > Richard Knight (RAK) for DML
// -----------------------------------------------------------------------------

var sm; // Object modelling active sub-menu using Brand0 DHTML API

var menu_show;

var dally = new Object(); // Stores timing dependent events for menu mouseovers
dally.state;      // Set to true if the user dallies on a main menu tab (as opposed to skimming over it)
dally.tab;        // Holds the id of the last main menu tab the user dallied on
dally.time = 700; // Defines the minimum time that constitues 'to dally', in milliseconds
dally.wait = 200; // Defines the time before a mousover causes a submenu to change, in milliseconds
dally.timout_id;  // The id returned by the setTimeout call that might hide the dally

var abs_layer_store = new Array(); // Stores references to DHTML objects
var abs_layer_count = 0;

var abs_editable = new Array(); // Stores IDs of editable DIVS

function abs_layout_init() {
	var promo_popup = new dmb0_DHTML_obj();
}

function close_promo_popup() {
	if (abs_promo_popup == undefined) var abs_promo_popup = new dmb0_DHTML_obj('firstpage_promobox');
	abs_promo_popup.hide();
}

// -----------------------------------------------------------------------------

function sub_menu_show(id) {
	debug('sub_menu_show(\'' + id + '\')');
	if (dally.next != undefined) clearTimeout(dally.next);
	if (dally.timeout_id != undefined) clearTimeout(dally.timeout_id);
	if (id != 'sub_menu_content') {
		dally.next = setTimeout('dally.next_visible = true; do_sub_menu_show(\'' + id + '\'); ', dally.wait);
	} else {
		dally.next = setTimeout('dally.next_visible = true; sub_menu_hide(); ', dally.wait);
	}
	dally.state = false;
	dally.timeout_id = setTimeout('dally.state = true; dally.tab = \'' + id + '\'; debug(\'dally.tab = ' + id + '\');', dally.time);
}

function do_sub_menu_show(id) {
	if (sm != undefined && sm.visible) sm.hide();
	if (id != 'sub_menu_content') {
		sm = new dmb0_DHTML_obj(id);
		sm.show();
	}
	sm.visible = true;
	debug('do_sub_menu_show(\'' + id + '\')');
}

function sub_menu_hide() {
	if (sm == undefined || !sm.visible) return;
	debug('sub_menu_hide()');
	sm.hide();
	sm.visible = false;
}

function abs_do_they_dally() {
	// If the cursor enters the submenu bar just moments after showing a submenu, revert to the previously visible submenu
	// 'just moments' is defined by dally.time
	clearTimeout(dally.next);
	clearTimeout(dally.timeout_id);
	if (!dally.state && dally.next_visible) {
		debug('NO DALLY: ' + dally.tab);
		sub_menu_hide();
		if (dally.tab != undefined) do_sub_menu_show(dally.tab);
	}
	dally.next_visible = false;
	dally.state = true;
}

// -----------------------------------------------------------------------------

function debug(msg) {
	// Add a debug bessage to the debugging console if it is visible
	if (abs_layer_store['debug'] && abs_layer_store['debug'].css.visibility == 'visible') {
		var d = document.createElement('p');
		d.innerHTML = msg;
		db.appendChild(d);
	}
}

function debug_clear() {
	db.innerHTML = '';
}

// -----------------------------------------------------------------------------

function abs_show_sidepage(id) {
	// Show the sidepage with the DOM ID id, after assigning it to abs_later_store as a Brand 0 DHTML object
	// Position the sidepage (depending on the vertical scroll position of the browser window) so it is onscreen
	if (abs_layer_store[id] == undefined) abs_layer_store[id] = new dmb0_DHTML_obj(id);
	var y = dmb0_browser.ns4 || dmb0_browser.ns6 ? window.pageYOffset : document.documentElement.scrollTop;
	abs_layer_store[id].css.top = y > 100 ? y-50 + 'px' : '100px';
	abs_layer_store[id].css.zIndex = 1000 + abs_layer_count;
	abs_layer_store[id].show();
	abs_layer_store[id].display_block();
	abs_layer_store[id].dragdrop();
	abs_layer_count++;
	return false;
}

// -----------------------------------------------------------------------------

function abs_interest_counter() { 
	// Initilise the object that counts interest in services / ranges
	// and trackes the latest service and range looked at
	this.service = new Object();
	this.service.connect = this.service.voice = this.service.fax = this.service.switchboard = 0;
	this.service.latest = '';
	this.range = new Object();
	this.range.r0800 = this.range.r0844 = this.range.r0845 = this.range.r0870 = this.range.r0871 = this.range.r0207 = this.range.r07_D = this.range.r07_K = 0;
	this.range.latest = '';
	return this;
}

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------

function abs_toggle_edit_buttons(url_of_editor_script) {
	abs_edit_buttons == 'on' ? abs_unhighlight_editable() : abs_highlight_editable(url_of_editor_script);
	// Store state, so whether edit buttons are on or off is persistent from page-to-page
	var exp = dmb1_get_exp_date(1,0,0); // Set a cookie expiry 1 day from now
	dir = dmb1_get_cookie('abs_editmode');
	dir = dir.substring(0, dir.lastIndexOf('_'));
	dir += '_' + abs_edit_buttons;
	dmb1_set_cookie('abs_editmode', dir, exp, '/');
}

// -----------------------------------------------------------------------------

function abs_highlight_editable(url_of_editor_script) {
	debug('FUNCTION: abs_highlight_editable()'); // DELETE
	for (var i=0; i<abs_editable.length; i++) {
		var div = get_DOM_ref(abs_editable[i]);
		var id = div.getAttribute('id');
		var panel_path = id.substring(0, id.lastIndexOf('/') + 1);
		var panel_name = id.substring(id.lastIndexOf('/') + 1);
		var eb = document.createElement('a');
		eb.setAttribute('href', url_of_editor_script + '?panel_path=' + id + '&return_url=' + document.URL);
		eb.setAttribute('class', 'editbutton');
		eb.setAttribute('onmouseover', 'dmb0_show_infotext(\'<b>' + panel_name + '<\/b><br>' + panel_path + '\', event);');
		eb.setAttribute('onmouseout', 'dmb0_hide_infotext();');
		var tx = document.createTextNode('EDIT');
		eb.appendChild(tx);
		switch(panel_name.substring(0,13)) {
			case 'fragment--sp_':
				debug('SIDEPAGE: ' + panel_name); // DELETE
				div = div.getElementsByTagName('div');
				div = div[0];
				div.className += ' editable_sp';
				break;
			default:
				div.className += ' editable';
		}
	div.insertBefore(eb, div.firstChild);
	}
	abs_edit_buttons = 'on';
}

// -----------------------------------------------------------------------------

function abs_unhighlight_editable() {
	for (var i=0; i<abs_editable.length; i++) {
		var div = get_DOM_ref(abs_editable[i]);
		var id = div.getAttribute('id');
		var panel_name = id.substring(id.lastIndexOf('/') + 1);
		switch(panel_name.substring(0,13)) {
			case 'fragment--sp_':
				div = div.getElementsByTagName('div');
				div = div[0];
				break;
			default:
		}
		div.removeChild(div.firstChild);
		div.className = div.className.substring(0, div.className.lastIndexOf(' editable'));
	}
	abs_edit_buttons = 'off';
}

// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------

function abs_show_promo_popup() {
	if (abs_promo_popup == undefined) var abs_promo_popup = new dmb0_DHTML_obj('firstpage_promobox');
	abs_promo_popup.show();
	abs_promo_popup.dragdrop();
}