/*
 * Public facing site-wide functions
 *
 * @author	Max Wheeler, Icelab Pty Ltd (http://icelab.com.au)
 *
 */

// Functions

// Clear the default text 'Search …' out of input forms
function clearDefaultSearch()
{
	$("input.clear").each(function()
	{ 
	  var value = $(this).val();
		$(this).focus(function()
		{
			if($(this).val() == value) {
				$(this).val('');
			}
		})
		.blur(function() {
			if($(this).val() == '') {
				$(this).val(value);
			}
		});
	});
}

// Redirect search input forms to a clean URL /section/query/ rather than a ?query=string
function redirectCleanSearch(e)
{
	$(e).submit(function() {
	  location.href = '/search/' + $('[@name=section]', this).val() +'/'+ $('input[@name=query]', this).val();
	  return false;
	});
}

// Make dropdowns jump to URLs
function redirectSelect(e, prepend)
{
	$(e).change(function() {
		var value = $(this).val();
		if(value == "") {
			return false
		} else {
			location.href = prepend + $(this).val();
		}
	});
}


// Set redirect for edit-in-place news items
// I.e, when date or title changes, the redirect is incorrect
function redirectNewsEdit()
{
	// Get the initial values
	var title = $('.title input').val();
	var date = $('.date input').val();
	
	// Process them into correct (safe) formats
	title = title.replace(/\s/g,'-');
	title = title.replace(/[^a-zA-Z0-9\-]/g,'');
	title = title.toLowerCase();
	
	date = date.replace(new RegExp("-","g"), "/");
	
	// Append the redirect input field
//	$('form.edit-in-place input[@name=redirect]').val('/news/' + date + '/' + title + "/");
	$('form.edit-in-place').attr('action', '/blog/article/' + title + "/");
}


// Set redirect for edit-in-place news items
// I.e, when date or title changes, the redirect is incorrect
function redirectByTitle(value, path, target_form)
{
	// Get the initial values
	var title = value;
	
	// Process them into correct (safe) formats
	title = title.replace(/\s/g,'-');
	title = title.replace(/[^a-zA-Z0-9\-]/g,'');
	title = title.toLowerCase();
	// Append the redirect input field
	$(target_form).attr('action', path + '/' + title + "/");
}

// Change the content of the Search Highlight field based on an AJAX call
function updateSearchHighlight(handle, root, scroll)
{
	if (handle != $('#event-highlight .holder').attr('data-handle') && highlight_active == false) {
		highlight_active = true;
		$('#event-load').fadeIn();
		$.ajax({
		  url: root + '/ajax/events/' + handle,
		  cache: false,
		  success: function(html){
				$('#event-highlight .holder').fadeOut(function() {
					$("#event-highlight").append(html);
					$('#event-highlight .holder').hide().fadeIn('slow');
					$('#event-load').fadeOut('slow');
					$(this).remove();
					highlight_active = false;
					
					// Scroll if the window is small and we're at the top
					if($(window).scrollTop() < 300 && $(window).height() < 800) var scroll = true;
					
					if (scroll) {
						var targetOffset = $("#event-highlight").offset().top;
						$('html, body').animate(
						  {scrollTop: targetOffset}, 1200
						);
					}					
				});
		  }
		});
    if($.browser.msie) {
			var wh = $(window).scrollTop() + $(window).height()/2 - 100;
			$('#event-load').css("top", wh + "px");
		}
	}
}

// Add classes to submit buttons in IE
function addIESubmitClass()
{
	if($.browser.msie) {
		$('input[type="submit"]').addClass('ie_submit');
	}
}

function bind_pop_feedback()
{
  $("a.popup-feedback").click(function(e)
  {
    var w = window.open($(this).attr("href"),'feedbackWindow','width=500,height=700,menubar=no,location=no,status=no,scrollbars=yes,resizable=yes');
    e.preventDefault();
  });
}

function addExitMessage()
{
  $('a').click(function(e) {
    if(/^http:\/\//.test($(this).attr('href')) && !(/moadoph/.test($(this).attr('href')))) {
      return confirm('You are now leaving the Museum of Australian Democracy at Old Parliament House website.');
    }
  });
}

// Execution
$(document).ready(function()
{
	// Clear the #topnav search box on focus
	clearDefaultSearch();
	addIESubmitClass();
	bind_pop_feedback();
  addExitMessage();
});
