// b0_interface.js
// -------------------------------------------------------------------------------------------------
// The 'Brand Zero' javascript library: Functions used to enhance the interactivity
// of B0 pages, show infotext
//
// Project    > dmABS
// Revision   > $Rev: 109 $
// Date		  > $Date: 2009-08-06 13:44:56 +0100 (Thu, 06 Aug 2009) $
// Author     > RAK for DML
// Copyright  > (C) Digital Mail Limited (2005-2009)
// -------------------------------------------------------------------------------------------------

// Add entry to library registration object, creating it if not present
if (typeof dmx_library != 'object') var dmx_library = new Object(); // Library registration object
if (typeof dmx_pg_init != 'object') var dmx_pg_init = new Object(); // Has page initialisation run?
dmx_library.b0_interface = true;
dmx_pg_init.b0_interface = false;

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

var b0_tips;
var b0_use_infotext = true;

function b0_interface_init()
{
	if (b0_use_infotext) b0_tips = new FancyTips($$('.infotext'));
}

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

var FancyTips = new Class({

	Implements: [Options],

	options : {
		tipcontents: 	'.tipcontents',
		className:		'infotext_bubble'
	},

	initialize: function (elements, options)
	{

		this.setOptions(options);

		// Set up the fancy tips by inserting the tip contents element
		// into the title attribute of the element that is to have the tooltip
		elements.each(function (element)
		{
			contents_element = element.getElement(this.options.tipcontents) ? element.getElement(this.options.tipcontents) : element.getNext(this.options.tipcontents);
			if (contents_element) element.set('title', contents_element.get('html'));
		},
		this);

		this.tips = new Tips(elements, this.options);
	},
	
	attach: function(el)
	{
		contents_element = el.getElement(this.options.tipcontents) ? el.getElement(this.options.tipcontents) : el.getNext(this.options.tipcontents);
		if (contents_element) el.set('title', contents_element.get('html'));
		this.tips.attach(el);
	}
	
});

// -------------------------------------------------------------------------------------------------
// FORMS

function b0_set_form_field(form_id, field_id, val)
{
	document.forms[form_id].elements[field_id].value = val;
}

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

function b0_preserve_selected(ref, val)
{
	// Make sure that the selected element in the drop down menu is preserved, from page hit to page hit
	// ref is a DOM reference to the <SELECT>, and val is the selected value
	for (i=0; i<ref.options.length; i++)
	{
		if (ref.options[i].value == val) ref.selectedIndex = i;
	}
}

