jQuery(document).ready(function() {

	jQuery("form.focus input:text:visible:first").focus();
	//jQuery("input:text:visible:first").focus();

	handle_mouseovers("#header li a[href]");

	// toggle the sidebar cuisines list on clicking the noted link
	// link must be created, then set up for toggling
/* 
<ul id="sidebar-cuisines">
	<li><a href="/restaurants/cuisines/american">American</a></li>
	<li><a href="/restaurants/cuisines/chinese">Chinese</a></li>
	...
</ul>
 */

	showhide_widget('#sidebar-happyhours', 'show happy hour days', 'hide happy hour days');
	showhide_widget('#sidebar-cuisines', 'show cuisine options', 'hide cuisine options');

// convert any selector to be hidden by default, 
// and displayed by clicking a prepended show/hide link
function showhide_widget($selector, $showtext, $hidetext) {

	var $show_text	= '+ <a class="js">' + $showtext + '</a>';
	var $hide_text	= '- <a class="js">' + $hidetext + '</a>';
	//var $toggle_id	= $selector.replace(/^#/, '') + '-toggle';
	//alert($toggle_id);
	//var $toggle_id	= $selector + '-toggle';
	//var $toggle_id	= 'sidebar-cuisines-toggle';

	var $random_number	= Math.floor(Math.random()*1001);

	jQuery($selector).each(function() {
		var $toggle_id			= $selector.replace(/^#/, '') + '-toggle-' + $random_number;
		//var $toggle_id	= $selector.replace(/^#/, '') + '-toggle';
		jQuery($selector).parent().prepend('<p class="widget-toggle" id="' + $toggle_id + '">' + $show_text + '</p>');
		jQuery($selector).hide();
		jQuery('p#' + $toggle_id).toggle(function() {
			jQuery($selector).show(250);
			jQuery('p#' + $toggle_id).html($hide_text);
		}, function() {
			jQuery($selector).hide(250);
			jQuery('p#' + $toggle_id).html($show_text);
		});

	});

}

// $selector will usually be an "a[href]" tag
// function handles mouseovers, mouseouts, preloading of nested <img>s
function handle_mouseovers($selector) {
	//alert('ok');
	// preload images
	jQuery($selector).each(function() {
		// Set the original src
		rollsrc = jQuery(this).children("img").attr("src");
		rollON = rollsrc.replace('_off', '_on');
		newImg = new Image(); 				// create new image obj
		jQuery(newImg).attr("src", rollON);	// set new obj's src
	});

	jQuery($selector).mouseover(function(){
		imgsrc = jQuery(this).children("img").attr("src");
		if (typeof(imgsrc) != 'undefined') {
			imgsrcON = imgsrc.replace('_off', '_on');
			jQuery(this).children("img").attr("src", imgsrcON);
		}
	});

	jQuery($selector).mouseout(function(){
		if (typeof(imgsrc) != 'undefined') {
			jQuery(this).children("img").attr("src", imgsrc);
		}
	});

}

	// * NOT DEPLOYED *
	// redirect page when option is chosen from select box
	// assumes url is value of selected option
	jQuery("form#id-string select").change(function() {
		jQuery("form#id-string select option:selected").each(function() {
			location.href = jQuery(this).attr("value");
		});
	});


});

