////////////////////////////////////////////////////////////////////////////////
//                                                                            //
//   This file is part of the autohausen software package.                    //
//   Author: Thomas Mlynarczyk <thomas.mlynarczyk@autohausen.de>              //
//   Copyright (C) 2011 autohausen                                            //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////



var ah = 
{
	NAME_QUERY: 'query',
	NAME_QUERIES: 'queries',
	NAME_PLACE: 'place',
	NAME_RADIUS: 'radius',
	NAME_FILTER: 'filter',
	NAME_SORT: 'sort',
	NAME_BUTTON: 'find',
	LABEL_PLACEHOLDER: 'Geben Sie hier Ihren Fahrzeugwunsch ein',
	LABEL_QUERIES: 'Suchbeispiele',
	RGX_EXTERN: /\bext\b/,
	
	/**
	 *  External links open in new window or tab.
	 */
	setupExternalLinks: function ()
	{
		var links = document.getElementsByTagName( 'A' );
		for ( var i = links.length; i--; )
		{
			if ( !links[i].className.match( ah.RGX_EXTERN ) ) continue;
			links[i].target = '_blank';
		}
	},
	
	/**
	 *  Automatically submit form when settings are changed.
	 */
	setupSettings: function ()
	{
		var button = ah.find( ah.NAME_BUTTON );
		var submit = function () { button.click(); };
		if ( !button ) return;
		ah.addHandler( ah.find( ah.NAME_PLACE ), 'change', submit );
		ah.addHandler( ah.find( ah.NAME_RADIUS ), 'change', submit );
		ah.addHandler( ah.find( ah.NAME_FILTER ), 'change', submit );
		ah.addHandler( ah.find( ah.NAME_SORT ), 'change', submit );
	},
	
	/**
	 *  Placeholder text in query field.
	 */
	setupQueryPlaceholder: function ()
	{
		var query = ah.find( ah.NAME_QUERY );
		var button = ah.find( ah.NAME_BUTTON );
		
		var showPlaceholder = function ()
		{
			if ( query.value != '' ) return;
			query.value = ah.LABEL_PLACEHOLDER;
		}
		
		var hidePlaceholder = function ()
		{
			if ( query.value != ah.LABEL_PLACEHOLDER ) return;
			query.value = '';
			query.className = '';
		}
		
		ah.addHandler( button, 'click', hidePlaceholder );
		ah.addHandler( query, 'focus', hidePlaceholder );
		ah.addHandler( query, 'blur', showPlaceholder );
		showPlaceholder();
		query.blur(); // IE6 seems to need this
	},
	
	/**
	 *  Dropdown list with queries.
	 */
	setupQueryList: function ()
	{
		var query = ah.find( ah.NAME_QUERY );
		if ( !query || !ah.queries ) return;
		var queries = document.createElement( 'DIV' );
		document.body.appendChild( queries );
		queries.id = ah.NAME_QUERIES;
		queries.style.display = 'none';
		var title = document.createElement( 'H3' );
		title.appendChild( document.createTextNode( ah.LABEL_QUERIES ) );
		queries.appendChild( title );
		
		// Ugly hack for IE<7 to prevent select box from showing through.
		if ( window.XMLHttpRequest == undefined && ActiveXObject != undefined )
		{
			queries.appendChild( document.createElement( 'IFRAME' ) );
		}
		
		for ( var q, i = 0; i < ah.queries.length; i += 1 )
		{
			q = document.createElement( 'A' );
			q.href = '';
			q.appendChild( document.createTextNode( ah.queries[i] ) );
			ah.addHandler( q, 'click', function ( e )
			{
				query.value = ( query.value &&
					query.value != ah.LABEL_PLACEHOLDER
					? query.value + ' ' : '' ) + 
					this.firstChild.nodeValue;
				this.parentNode.style.display = 'none';
				// Prevent link default action
				( e = e || window.event ) && ( e.preventDefault &&
					e.preventDefault() || ( e.returnValue = false ) );
			} );
			queries.appendChild( q );
		}

		ah.addHandler( query, 'focus', function ()
		{
			var display = queries.style.display;
			queries.style.display = display == 'block' ? 'none' : 'block';
		} );
		
		ah.addHandler( document.body, 'click', function( e ) {
			e = e || window.event;
			var target = e.target || e.srcElement;
			if ( target != query ) queries.style.display = 'none';
		} );
	},
	
	/**
	 *  Find a DOM element by its id or name.
	 *
	 *  @param    string   Element id or name
	 *  @return   node     Element
	 */
	find: function ( id )
	{
		var node;
		node = document.getElementById( id );
		if ( node ) return node;
		node = document.getElementsByName( id );
		if ( node && node[0] ) return node[0];
		return null;
	},
	
	/**
	 *  Add an event handler.
	 *
	 *  Multiple handlers can be registered for the same event.
	 *
	 *  @param   node      DOM node
	 *  @param   string    Event name (without the "on" prefix)
	 *  @param   handler   Callback
	 */
	addHandler: function( node, event, handler )
	{
		// Standards compliant version
		if ( node && node.addEventListener )
		{
			node.addEventListener( event, handler, 0 );
		}
		// IE version (works with IE5.5+)
		else if ( node && node.attachEvent && Function.prototype.call )
		{
			// Make the this keyword available inside handler
			node.attachEvent( 'on' + event, function () {
				handler.call( node );
			} );
		}
	}
};



/**
 *  Setup.
 */
window.onload = function()
{
	ah.setupExternalLinks();
	ah.setupSettings();
	ah.setupQueryPlaceholder();
	ah.setupQueryList();
}
