/**
 * @package 	FV Community News
 * @version 	2.0
 * @author 		Frank Verhoeven
 * @copyright 	Coyright (c) 2008, Frank Verhoeven
 */

jQuery(document).ready( function($) {
	$('#wordpressLinksForm').submit(function() {
		
		// Disable AJAX for image uploading.
		if ('' != $('#wordpressLinksImage').val())
			return true;
		
		$('#wordpressLinksLoader').fadeIn('slow');
		
		var url = $('#wordpressLinks').val() + '?wordpressLinksAjaxRequest=true';
		var data = $('#wordpressLinksForm').serialize();
		
		$.ajax({
			type: 'POST',
			url: url,
			data: data,
			success: function(response) {
				$('#wordpressLinksLoader').hide();
				
				$('wordpressLinksAjaxResponse', response).each(function(){
					var message = $('message', this).text();
					
					$('#wordpressLinksForm > .wplinks_field_error').each(function() {
						$(this).removeClass('wplinks_field_error');
					});
					
					
					if ('error' == $('status', this).text()) {
					  $('.error').show();
					  $('#wordpressLinksAjaxResponse').hide();
						$('errorfields > field', this).each(function() {
							$('#' + $(this).text()).addClass('wplinks_field_error');
						});
						$('#wordpressLinksErrorResponse').html( message );
						scroll(0,0);
					} else {
						$('#wordpressLinksErrorResponse').hide();
						
						//$('#wordpressLinksForm').slideUp('slow');
						scroll(0,0);
						$('#wordpressLinksAjaxResponse').html('<p>' + message + '</p>');
						$('#wordpressLinksAjaxResponse').fadeIn('slow');
					}
					
				});
			},
			error: function() {
				$('#wordpressLinksAjaxResponse').html('<p>Unable to add your link, please try again later.</p>');
				$('#wordpressLinksLoader').fadeOut('slow');
			}
		});
		
		return false;
	});
	
	$('#wordpressLinksCaptchaReloadLink').click(function() {
		var element = $('#wordpressLinksCaptchaImage');
		var newSource = element.attr('src') + '&amp;dummy=true';
		
		$('#wordpressLinksCaptchaLoader').show();
		
		element.fadeOut();
		element.attr('src', function() {
			return newSource;
		});
		element.load(function() {
			element.fadeIn('slow');
			$('#wordpressLinksCaptchaLoader').hide();
		});
		
		return false;
	});
	
});


var z = 999;
var $j = jQuery.noConflict();

checkExternalClick = function(event)
{
	if ($j(event.target).parents('.activedropdown').length === 0)
	{
		$j('.activedropdown').removeClass('activedropdown');
		$j('.options').hide();
	}
};

$j(document).ready(function()
{
	$j(document).mousedown(checkExternalClick);

	$j("#wplinksort").each(function() 
	{
		if(!$j(this).parent().hasClass('enhanced2'))
		{
			targetselect = $j(this);
			targetselect.hide();

			// set our target as the parent and mark as such
			var target = targetselect.parent();
			target.addClass('enhanced2');

			// prep the target for our new markup
			target.append('<dl class="wplinksort_select"><dt><a class="dropdown_toggle" href="#"></a></dt><dd><div class="options"><ul></ul></div></dd></dl>');
			target.find('.wplinksort_select').css('zIndex',z);
			z--;

			// we don't want to see it yet
			target.find('.options').hide();

			// parse all options within the select and set indices
			var i = 0;
			targetselect.find('option').each(function() 
			{
				// add the option
				target.find('.options ul').append('<li><a href="#"><span class="value">' + $j(this).text() + '</span><span class="none index">' + i + '</span></a></li>');

				// check to see if this is what the default should be
				if($j(this).attr('selected') == true)
				{
					targetselect.parent().find('a.dropdown_toggle').append('<span></span>').find('span').text($j(this).text());
				}
				i++;
			});
		}
	});


	// let's hook our links, ya?
	$j('a.dropdown_toggle').live('click', function() 
	{
		var theseOptions = $j(this).parent().parent().find('.options');
		if(theseOptions.css('display')=='block')
		{
			$j('.activedropdown').removeClass('activedropdown');
			theseOptions.hide();
		}
		else
		{
			theseOptions.parent().parent().addClass('activedropdown');
			theseOptions.show();
		}
		return false;
	});

	// bind to clicking a new option value
	$j('.options a').live('click', function(e)
	{
		$j('.options').hide();

		var enhanced = $j(this).parent().parent().parent().parent().parent().parent();
		var realselect = enhanced.find('select');

		// set the proper index
		realselect[0].selectedIndex = $j(this).find('span.index').text();

		// update the pseudo selected element
		enhanced.find('.dropdown_toggle').empty().append('<span></span>').find('span').text($j(this).find('span.value').text());

		return false;
	});
});

