var z = 999;
var $j = jQuery.noConflict();

checkExternalClick = function(event)
{
	if ($j(event.target).parents('.activedropdown').length === 0)
	{
		$j('.activedropdown').removeClass('activedropdown');
		$j('.options').hide();
	}
};

$j(document).ready(function()
{
	$j(document).mousedown(checkExternalClick);

	$j("#search_select").each(function() 
	{
		if(!$j(this).parent().hasClass('enhanced'))
		{
			targetselect = $j(this);
			targetselect.hide();

			// set our target as the parent and mark as such
			var target = targetselect.parent();
			target.addClass('enhanced');

			// prep the target for our new markup
			target.append('<dl class="search_select"><dt><a class="dropdown_toggle" href="#"></a></dt><dd><div class="options"><ul></ul></div></dd></dl>');
			target.find('.search_select').css('zIndex',z);
			z--;

			// we don't want to see it yet
			target.find('.options').hide();

			// parse all options within the select and set indices
			var i = 0;
			targetselect.find('option').each(function() 
			{
				// add the option
				target.find('.options ul').append('<li><a href="#"><span class="value">' + $j(this).text() + '</span><span class="none index">' + i + '</span></a></li>');

				// check to see if this is what the default should be
				if($j(this).attr('selected') == true)
				{
					targetselect.parent().find('a.dropdown_toggle').append('<span></span>').find('span').text($j(this).text());
				}
				i++;
			});
		}
	});


	// let's hook our links, ya?
	$j('a.dropdown_toggle').live('click', function() 
	{
		var theseOptions = $j(this).parent().parent().find('.options');
		if(theseOptions.css('display')=='block')
		{
			$j('.activedropdown').removeClass('activedropdown');
			theseOptions.hide();
		}
		else
		{
			theseOptions.parent().parent().addClass('activedropdown');
			theseOptions.show();
		}
		return false;
	});

	// bind to clicking a new option value
	$j('.options a').live('click', function(e)
	{
		$j('.options').hide();

		var enhanced = $j(this).parent().parent().parent().parent().parent().parent();
		var realselect = enhanced.find('select');

		// set the proper index
		realselect[0].selectedIndex = $j(this).find('span.index').text();

		// update the pseudo selected element
		enhanced.find('.dropdown_toggle').empty().append('<span></span>').find('span').text($j(this).find('span.value').text());
    
    // update the action and method for submitting the form
    var newaction = $j("#frm_search select option:selected").val();
    var selectoption = $j(this).find('span.index').text();
    

    if (selectoption == '0') { // Blog Posts
    
      $j("#frm_search").attr("method","get");
      $j("#frm_search input").attr("name","s");
      
    } else if (selectoption == '1') { // Job Adverts
    
      $j("#frm_search").attr("method","get");
      $j("#frm_search input").attr("name","q");
      
    } else if (selectoption == '2') { // Freelancers
    
      $j("#frm_search").attr("method","get");
      $j("#frm_search input").attr("name","search[keywords]");
      
    } else if (selectoption == '3') { // Forums
    
      $j("#frm_search").attr("method","get");
      $j("#frm_search input").attr("name","search");
    
    } else if (selectoption == '4') { // Podcasts
    
      $j("#frm_search").attr("method","get");
      $j("#frm_search input").attr("name","s");
        
    } else if (selectoption == '5') { // Resources
    
      $j("#frm_search").attr("method","post");
      $j("#frm_search input").attr("name","searchTerm");
      
    }

    
    $j("#frm_search").attr("action",newaction);

		return false;
	});
});
