// Being nice to other libraries
jQuery.noConflict();

// In here $ is jQuery regardless of what other libraries are used
(function($) {

	/* blockUI global overrides */
	$.blockUI.defaults.css.padding			= '5px';
	$.blockUI.defaults.css.color			= '#29261b';
	$.blockUI.defaults.css.border			= '2px solid #2e2c23';
	$.blockUI.defaults.css.backgroundColor	= '#f0f0f0';
	$.blockUI.defaults.css.cursor			= 'default';
	$.blockUI.defaults.overlayCSS.cursor	= 'default';
	$.blockUI.message						= '<h1>Please wait...</h1>';

	/* Show Tips */
	show_tip = function() {
		if($.browser.msie) {
			$('#tip').text(this.title)
		}
		else {
			$('#tip').queue('fx', []);
			$('#tip').stop();

			$('#tip').text(this.title)
			.show()
			.animate({opacity: 1}, 100);
		}
	}

	/* Hide tips */
	hide_tip = function() {
		if(! $.browser.msie) {
			$('#tip')
			.animate({opacity: 1}, 500)
			.fadeOut('slow');
		}
	}

	/* Observe Confirmations */
	observe_confirm = function() {
		var link = this.href;

		$.ajax({
			url: link,
			cache: false,
			complete: function(html) {
				$.blockUI({ message: '<p>'+html.responseText+'</p>'});
			}
		});

		$('#confirm_cancel').click(function() {
			$.unblockUI();
			return false;
		});

		return false;
	}

	/* Display forms below the clicked link */
	show_inline = function() {
		var link = this.href;
		var el = $(this);

		$.ajax({
			url: link,
			cache: false,
			complete: function(html) {
				$('.ajax_add').remove();

				$('<div class="ajax_add hidden"></div>')
				.insertAfter(el)
				.html(html.responseText);

				$('.ajax_add > :not(form)').remove();

				$('.ajax_add').slideDown('fast');
			}
		});

		return false;
	}

	/* Create Flashdata Container */
	create_container = function() {
		$('<div id="flashdata"><span class="message_content"></span></div>').prependTo('#primary');
		return $('#flashdata');
	}

	/*
	 * Fade out flashdata div (or create if it doesn't exist)
	 * Update message
	 * Fade back in
	 */
	show_flash = function(msg) {
		// Clear timer - just in case
		if(window.timer)
			clearTimeout(window.timer);

		var container = $('#flashdata');

		if (! container.get(0)) {
			container = create_container();
		}

		container.fadeOut('fast', function() {
			$('.message_content').html(msg);
		});

		container.fadeIn('fast', hide_flash);
	}

	/* Hide flashdata after 10 seconds (or immediately if it's empty) */
	hide_flash = function() {
		// Clear existing timer to avoid flickering
		if(window.timer)
			clearTimeout(window.timer);

		window.timer = window.setTimeout(function() {
			$('#flashdata').fadeOut("slow", function() {
				$('#flashdata').remove();
			});
		}, 10000);
	}

	/* Hijack empty categories */
	observe_empty = function() {
		$('.empty').click(function() {
			var cat = this.id;
			cat = cat.split('_')[1];

			var add_url = BASE_URL + 'member/listings/add/' + cat;

			show_flash('This category is empty.  Be the first to <a href="' + add_url + '">add an entry</a>.');
			return false;
		});
	}

	init = function() {
		// Open links that contain http:// and point to another domain in a new window
	    $('a:not([@href*='+BASE_URL+'])[@href^="http://"]').attr('target', '_blank');

		// Hide/show stuff
		$('.js_hide').hide();
		$('.js_show').show();

		// Registration Optional Field toggle
		$('#show_optional').toggle(
			function() {
				$('#optional').show();
				$(this).toggleClass('less');
				$(this).text('Hide Optional Fields');
				return false;
			},
			function() {
				$('#optional').hide();
				$(this).toggleClass('less');
				$(this).text('Show Optional Fields');
				return false;
			}
		);

		// Hide flash message
		hide_flash();

		// Hijack clicks on empty categories - show flash_message
		observe_empty();

		// Show/hide inline forms
		$('a.inline').toggle(
			show_inline,
			function() {
				$('.ajax_add').slideUp('fast');
				return false;
			}
		);

		// Tips
		$('.tip').mouseover(show_tip);
		$('.tip').mouseout(hide_tip);

		// Add confirmations to delete links
		$('.confirm').click(observe_confirm);

        // Scroll to the errors if there are
		$('.field_error:first').each(function(){
			$.scrollTo(this);
			$('.field_error :input:first').focus();
		});

		// Portfolio
        $('.portfolio .post').hover(
        function(){
        	$(this).children('.moredetails').slideDown('fast');
        },
        function(){
        	$(this).children('.moredetails').slideUp('fast');
        });

        // Form protection check
        $('input[@name=check]').val('pass');

	}

	$(document).ready(init);


})(jQuery);