/**
 * common jquery functions - needs to be included in the site templates after the jquery includes
 */
$(document).ready(function(){								 
	//initFormFocus(); // SM 10Nov09: Disabled
	initArbSearch();
	
	// SM 18Nov08: If you select a textfield, select all text if it's the default value
	// Handy for search forms etc...
	$("input, textarea").focus(function(){
    // only select if the text has not changed
    //if(this.value == this.defaultValue) { this.select(); }
    if(this.value == this.defaultValue) { $(this).select(); }
  });
  
});

/**
 * Generic method to set the input focus to the first element on any page.
 */
function initFormFocus() {
  // SM 20Aug07: Try setting the cursor focus on the first element of any form.
  //$(":input:first").focus();
	$(":text:first").focus().select();
	//$("input[name='frm_uname']").focus();	
}

/**
 * Setup the hide/show behaviour for arbsearch pages. Needs jquery cookie plugin.
 */
function initArbSearch() {
  
  if(0 == $('.srch_table').length) { return false; }
  
	// arbsearch hideshow toggle function
	// todo: use a cookie to remember state between paginations
	$('.arbsearch_hideshow').css({cursor: 'pointer'});
	//$('.srch_table').css('margin', '10px 0px 10px 20px');
	
	var img_toggle_hide = "http://images.regional.org.au/shared/images/as_toggle_hide.gif";
	var img_toggle_show = "http://images.regional.org.au/shared/images/as_toggle_show.gif";
	
	var as_toggle_hide = function() {
			$.cookie('arbsearch_toggle', 'hide'); // get cookie
			//$('.srch_table').slideUp();
			$('.srch_table').hide();
			//$(this).html('show');
			$(this).attr('src', img_toggle_show);
	};
	var as_toggle_show = function() {
			$.cookie('arbsearch_toggle', 'show'); // get cookie
			//$('.srch_table').slideDown();
			$('.srch_table').show();
			//$(this).html('hide');
			$(this).attr('src', img_toggle_hide);
	};
	
	// Now bind these functions in the correct order when the page loads.
	var initial_state = $.cookie('arbsearch_toggle') ? $.cookie('arbsearch_toggle') : 'show';
	switch(initial_state) {
		case 'hide':
			$('.srch_table').hide();
			//$('.arbsearch_hideshow').html('show');
			$('.arbsearch_hideshow').attr('src', img_toggle_show);
			$('.arbsearch_hideshow').toggle(as_toggle_show,as_toggle_hide);		
			break;
		case 'show':
			$('.srch_table').show();
			//$('.arbsearch_hideshow').html('hide');
			$('.arbsearch_hideshow').attr('src', img_toggle_hide);
			$('.arbsearch_hideshow').toggle(as_toggle_hide, as_toggle_show);
			break;
	}	
}

/**
 * SM 20/05/2010 12:21:00 PM
 * Send ajax command to server to set flag
 * Hide display now
 */
function hideNewSiteMsg(elem) {
  $.get('/my/admin/ajax', { action: 'new_site_msg_hide' }); // send command to server to remember for this session
  $(elem).parents('div.flash_message:first').fadeOut('slow', function() { $(this).remove() });
}


