//<![CDATA[

/* global constants */
var consts = {
	IMAGE_DIR : '/images/'
};



/* global variables */
var preloaded = new Array();
var default_search_txt = 'Find a pub/bar...';



/* onload events */
Event.observe( window, 'load', blur_links );
//Event.observe( window, 'load', init_external_links );
Event.observe( window, 'load', init_search_box );
Event.observe( window, 'load', init_form_elements );
//Event.observe( window, 'load', focus_form );
//Event.observe( window, 'load', comment_system );


/*
function comment_system()
{
	// listen to window.location.hash to focus comment form when necessary
	var hashy = window.location.hash;
	if( hashy.substring( 1, hashy.length ) == 'add_comment' && $('add_comment') ) Form.element.focus( 'add_comment' );
	
	if( $('add_comment_link') ) Event.observe( $('add_comment_link'), 'click', function(){ $('add_comment').focus(); } );
} // cnuf
*/


function focus_form()
{
	var frms = $('content').getElementsByTagName( 'form' );
	if( frms.length > 0 )
	{
		// focus first element of content div
	Form.focusFirstElement( frms[0] );
	}
	/*
	else {
		// focus the search form
	Form.focusFirstElement( 'search_form' );
	} // fi
	*/
} // cnuf



function init_external_links()
{
  var hyperlinks = $A( document.getElementsByTagName( 'a' ) );
  hyperlinks.each( function( hyperlink ) {
    if( hyperlink.hasClassName( 'popup' ) )
    {
    	Event.observe( hyperlink, 'click', function( evt ) { var new_win = window.open( Event.element( evt ).href ); Event.stop( evt ); } );
    } // fi
  } );
} // cnuf


/*
* Stop a dotted box being drawn around links when focused / clicked
*/
function blur_links()
{
	var hyperlinks = $A( document.getElementsByTagName( 'a' ) );
	hyperlinks.each( function( hyperlink ) {
		Event.observe( hyperlink, 'focus', function( evt ) { Event.element( evt ).blur(); } );
	} );
} // cnuf

/*
* Preloads an image
*/
function preload_images() {
    for ( var i = 0; i < arguments.length; i++ )
    {
        preloaded[i] = document.createElement( 'img' );
        preloaded[i].setAttribute( 'src', arguments[i] );
    } // rof
} // cnuf


function init_form_elements()
{
	var fields = $A( document.getElementsByClassName( 'editable' ) );
	fields.each( function( field ) {
		//Event.observe( field, 'mouseover', function( evt ) { Event.element( evt ).addClassName( 'field_hover' ); } );
		//Event.observe( field, 'mouseout', function( evt ) { Event.element( evt ).removeClassName( 'field_hover' ); } );
		Event.observe( field, 'focus', function( evt ) { Event.element( evt ).addClassName( 'field_focus' ); } );
		Event.observe( field, 'blur', function( evt ) { Event.element( evt ).removeClassName( 'field_focus' ); } );
	} );
} // cnuf



function init_search_box()
{
var search_bx = $('search_txt');

if( search_bx.value == '' )
{
	search_bx.value = default_search_txt;
search_bx.addClassName( 'search_blurred' );
} // fi

Event.observe( search_bx, 'focus', clear_search );
Event.observe( search_bx, 'blur', unclear_search );

Event.observe( 'search_bttn', 'click', search_click );
} // cnuf


function clear_search( evt )
{
	var obj = Event.element( evt );
if( obj.value == default_search_txt )
{
obj.value = '';
obj.removeClassName( 'search_blurred' );
} // fi


var pos = Position.cumulativeOffset(obj);
var dims = Element.getDimensions(obj);
var props = { width: dims.width, height: dims.height, top: pos[1], left: pos[0] };
var div = document.createElement('div');
div.setAttribute('id', 'search_help');
div.appendChild( document.createTextNode('Hello world') );
document.body.appendChild( div );
div.innerHTML = "\
<p>Example searches:</p> \
<ul> \
<li>The Cricketers, Brighton</li> \
<li>The Cricketers in East Sussex</li> \
<li>The Cricketers</li> \
<li>Brighton</li> \
</ul> \
";
div.id = 'search_help';
div.style.left = ( props.left ) + 'px';
div.style.top = ( props.top + props.height ) + 'px';
//div.style.width = ( props.width ) + 'px';
Position.absolutize(div);
} // cnuf

function unclear_search( evt )
{
	var obj = Event.element( evt );
if( obj.value == '' )
{
obj.value = default_search_txt;
obj.addClassName( 'search_blurred' );
} // fi

var div = $('search_help');
if( div ) {
Element.remove(div);	
} // fi

} // cnuf



function search_click( evt )
{
	var obj = Event.element( evt );
var q = $('search_txt').value;
if( q == default_search_txt )
{
	Event.stop( evt );
	$('search_txt').focus();
	return false;
} // fi
} // cnuf


Event.observe( window, 'load', hide_facilities );
function hide_facilities() {
if( $('s_facilities') ) $('s_facilities').hide();
} // cnuf

function toggle_facilities() {
$('s_facilities').show();
$('show_hide_facilities').remove();
} // cnuf

//]]>