var switch_explain = null;

window.addEvent('domready', function()
{
	var scope = this;

	if($('salary-search-content-wrapper'))
	{
		this.switch_explain = new SwitchExplain();		
		//this.switch_explain.
		$$('.switch_explain span').setStyle('behavior: url(../css/iepngfix.htc)');	
		this.switch_explain.hide();
	}

	//$('buttons').getElements('a').addEvent('click',function(e)
	//{	
		// close pop-up explanation
	//	scope.switch_explain.hide();
	//}

	//action listener for the "contact us" form
	if($('officeLocatorForm'))
	{
		$('office_specialism').addEvent('change', function()
		{
			return contactus_getLocationsBySpecialism();
		});

		$('officeLocatorForm').addEvent('submit', function()
		{
			//onclick="this.form.target='_blank';return true;"
			this.set('target', '_blank');
		//	return contactus_sendOfficeSearch();
		});
		
		//contactus_setOfficeMapLinks();
		//contactus_setOfficeMapBackLinks();
	}

});	

function SwitchExplain()
{
	// get DOM objects
	var scope = this;
	this.explanation = $('switch_explain_container');
	this.open_links = $$('.switch_explain a');
	this.close_links = $$('#switch_explain .close a');
	
	this.hide = function()
	{
		this.explanation.setStyle('display', 'none');
	}
	this.show = function(e)
	{
		this.explanation.setStyle('display', 'block');
	}
	
	// if objects exist, set events
	if(this.explanation != null)
	{
		for(i=0, maxi=this.open_links.length; i<maxi; i++)
		{
			this.open_links[i].addEvent('click', function(e)
			{
				scope.show(e);
				return false;
			});
		}
		for(i=0, maxi=this.close_links.length; i<maxi; i++)
		{
			this.close_links[i].addEvent('click', function(e)
			{
				scope.hide();
				return false;
			});
		}
		
		var display=Cookie.read('switchexplain');
		display=null;
		if (display==null || display=="")
		{
			this.explanation.setStyle('display', 'block');
		}
		//Cookie.write('switchexplain','shown', {duration:120, domain:siteDomain, path:'/'});
		
	}
}

/* contact us search and google maps */
function contactus_getLocationsBySpecialism()
{
	var myHTMLRequest = new Request.HTML({
		url:includesPath+'/office_locator.php',
		onComplete:function(){
			var loc_vals = this.response.html.split('|');
			var locations = $('office_location');
			
			locations.options.length = 0;
			locations.options[0] = new Option('All locations', '');
			for(i=0, maxi=loc_vals.length; i<maxi; i++)
			{
				locations.options[1+i] = new Option(loc_vals[i], loc_vals[i]);
			}
		}
	}).post({'office_specialism':$('office_specialism').value});
	return false;
}

function contactus_sendOfficeSearch()
{
	$('contact_search_results').set('html', '<p class="loading"><img src="'+includesPath+'/images/ajax-loader.gif" alt="loading..." /> Searching for offices</p>');

	var search_data = '';

// if we passed the search parameters to this functions, use them
	if(arguments.length>0)
	{
		search_data = arguments[0];
	}
	// otherwise just send the form
	else
	{
		search_data = $('contact_matrix');
	}
	/*
	var myHTMLRequest = new Request.HTML({
		method:'post',
		url:includesPath+'/office_locator.php',
		update:'contact_search_results',
		onComplete:function(){
			contactus_setOfficeMapLinks();
			contactus_setOfficePaginationLinks();
		}
	}).send(search_data);
	*/
	return false;
}
function contactus_getOfficeDetails(querystring)
{
	$('contact_search_results').set('html', '<p class="loading"><img src="'+includesPath+'/images/ajax-loader.gif" alt="loading..." /> Getting office map</p>');

	var myHTMLRequest = new Request.HTML({
		method:'post',
		url:includesPath+'/office_locator.php',
		update:'contact_search_results',
		onComplete:function(){
			contactus_setOfficeMapBackLinks();
			googleMapsInit();
		}
	}).send(querystring);
	return false;
}

function contactus_setOfficeMapLinks()
{
	var links = $$('#contact_search_results .office_listing a');
	
	for( var i=0, maxi=links.length; i<maxi; i++ )
	{
		var sLink = links[i];
		
		$( sLink ).addEvent('click', function(e)
		{
			e = new Event(e).stop();
			contactus_scrollToTop()
			contactus_getOfficeDetails(this.href.replace(/^.*\?([^\?]*)$/, '$1'));
		});
	}
}

function contactus_setOfficeMapBackLinks()
{
	var links = $$('#contact_search_results .back a');
	
	for( var i=0, maxi=links.length; i<maxi; i++ )
	{
		var sLink = links[i];
		
		$( sLink ).addEvent('click', function(e)
		{
			e = new Event(e).stop();
			contactus_sendOfficeSearch(this.href.replace(/^.*\?([^\?]*)$/, '$1'));
		});
	}
}
