/**
 * Copyright (C) 2009 Nikola Posa (http://www.nikolaposa.in.rs)
 *
 * This program is free software: you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
 
$(document).ready(function() {
	$('#search_form').submit(function() {
		var searchQuery = $('#search_query').val();
		
		if (searchQuery == '') {
			return false;
		}
		else {
			$('#search_form').attr('action', $('#search_form').attr('action') + '/' + searchQuery);
			
			return true;
		}
    });
	
	//"Back to search results" functionality.
	
	$('.search_result h2 a').click(
		function() {
			$.ajax({
				type: 'POST',
				url: $('#base_url').val() + '/front-end/ajax_handlers/webfolio-search/back_to_results_marker.php',
				data: 'searchUrl=' + window.location.href
			});
			
			return true;
		}
	);
	
	//Appending "Back to search results" box.
	if ($('#back_to_results') && Boolean($('#back_to_results').val()) == true) {
		$('body').prepend('<div id="back_to_results"><img id = "close_message" src = "' + $('#base_url').val() + '/front-end/img/webfolio-search/close.png" /><a href = "' + $('#search_url').val() + '">' + $('#back_to_results_label').val() + '</a></div>');
		$('#back_to_results').css('left', $(window).width() / 2 - $('#back_to_results').width() / 2);
	}
	
	//... and making it static and floating on user's screen.
	$(window).scroll(function()
	{
		$('#back_to_results').animate({top: $(this).scrollTop() + "px" }, {queue: false, duration: 200});
	});
	
	//When the close button at right corner of the message box is clicked.
	$('#close_message').click(function()
	{
		$('#back_to_results').animate({top: "+=15px", opacity: 0}, "slow");
	});
});