// Being nice to other libraries
jQuery.noConflict();

// In here $ is jQuery regardless of what other libraries are used
(function($) {

	/* Display a user Flash message */
	show_flash = function() {

		$("#FlashNotice").hide().fadeIn();

		$('#FlashNotice a.close').click(function(e) {
			e.preventDefault();
			$('#FlashNotice').fadeOut('normal', function() {
				$("#FlashNotice").remove();
			});
		});
	}

	/* takes care of the tabs on the search page */
	search_tabs = function(){

		// hide all forms except for post_form
		$('#search_divs > :not(.active)').hide();

		// holds the name of the tab in focus
		var focus = "#" + $('#search_divs > .active').eq(0).attr('id');

		// opens the tab with the name "name",
		// @param name			The id of the form to show (without #)
		// @param to_highlight	The element to highlight (usually "this")
		open_tab = function(name, to_highlight){

			// is our tab already open?
			if(focus == name)
			{
				return false;
			}

            // IE, you won't have the nice animation ;)
            if( ! $.browser.msie)
            {
                $('#search_divs > div').hide('normal');
				$(name).show('normal');
            }
            else
            {
                $('#search_divs > div').hide();
				$(name).show();
            }

			// move the tab highlight
			$('#search_tabs a').removeClass('active');
			$(to_highlight).addClass('active');

			// now we have a new focus
			focus = name;
		}

		$('#search_tabs a').click(function(){
			open_tab($(this).attr('href'), this);
			return false;
		});
	}

	search_ajax = function(){
		$('#search_divs .pagination a').click(function(){
			var body_container = $(this).parent().parent();
			var count_container = $('#' + $(body_container).attr('id') + '_tab');

			// fade it
			$('#busy_image').fadeIn();

            if( ! $.browser.msie)
			{
				$(body_container).fadeTo('normal', 0.2);
			}

			$.ajax({
				url: $(this).attr('href'),
				dataType: "json",
				success: function(data){
					$(body_container).html(data.body);
					$('small', count_container).html(data.count);
					search_ajax(); // reload

					// animate
					$('#busy_image').fadeOut();

                    if( ! $.browser.msie)
					{
						$(body_container).fadeTo('normal', 1);
					}
				}
			});
			return false;
		});
	}

	init = function(){

		// Show a flash message if it exists
		show_flash();

		// Open images using Fancybox
		$('.fancy .pic').fancybox(
		{
			'hideOnContentClick': true,
			'frameWidth': 600
		});

		// Enable shar tabs if they exists
		//share_tabs();

		// Open links that contain http:// and point to another domain in a new window
		$('a:not([@href*='+BASE_URL+'])[@href^="http://"], .outlink').attr('target', '_blank');

		// Scroll to the messages if there are
		$('#FlashNotice, .messages:first').each(function(){
			$.scrollTo(this);
			$('input:first', $(this).nextAll()).focus();
		});

        // Empty the search value on select
		$('#search').focus(function() {
			if($(this).val() == 'Enter a phrase, a word or an emotion')
        		$(this).val('');
		});

		search_tabs();

		search_ajax();

		//bind_ajax_to_share_forms();
		$('body').append('<div id="busy_image"></div>');
		$('#busy_image').hide();
	}

	/* Call Init function when DOM is ready */
	$(document).ready(init);

})(jQuery);
