// Global variable to prevent multiple AJAX requests
var bp_ajax_request = null;

$(document).ready(function() 
{
	
	$(".login-link").click(function(){
		$("#login-modal").modal({overlayClose:true,opacity:30});
	});
	

	$("select, input:checkbox, input:radio, input:file").uniform();

	/***********
	/ Selecting all, none, unread and read messages in inbox
	************/
	
	$("#select_read").click(function()
	{
		var selection = $(".conversations .read input[type='checkbox']");
		var checkboxes = $(".conversations input[type='checkbox']");
		
		checkboxes.each(function(i) 
		{
			checkboxes[i].checked = "";
		});
		
		selection.each(function(i) 
		{
			selection[i].checked = "checked";
		});
				
		$.uniform.update();
		
		// Whether a default action should be performed or not (i.e should the href be followed)
		return false;
	});
	
	$("#select_unread").click(function()
	{
		var selection = $(".conversations .unread input[type='checkbox']");
		var checkboxes = $(".conversations input[type='checkbox']");
		
		checkboxes.each(function(i) 
		{
			checkboxes[i].checked = "";
		});
		
		selection.each(function(i) 
		{
			selection[i].checked = "checked";
		});
		
		$.uniform.update();
				
		// Whether a default action should be performed or not (i.e should the href be followed)
		return false;
	});
	
	$("#select_none").click(function()
	{
		var selection = $(".conversations input[type='checkbox']");
		
		selection.each(function(i) 
		{
			selection[i].checked = "";
		});
		
		$.uniform.update();
	
		// Whether a default action should be performed or not (i.e should the href be followed)
		return false;
	});
	
	$("#select_all").click(function()
	{
		var selection = $(".conversations input[type='checkbox']");
		
		selection.each(function(i) 
		{
			selection[i].checked = "checked";
		});
		
		$.uniform.update();
	
		// Whether a default action should be performed or not (i.e should the href be followed)
		return false;
	});
	
	
	/***********
	/ Marking private messages as read and unread 
	************/
	
	$("a#mark_as_read, a#mark_as_unread").click(function() 
	{
		var checkboxes_tosend = '';
		var checkboxes = $(".conversations input[type='checkbox']");
		
		if ('mark_as_unread' == $(this).attr('id') )
		{
			var currentClass = 'read'
			var newClass = 'unread'
			//var unreadCount = 1;
			//var inboxCount = 0;
			//var unreadCountDisplay = 'inline';
			var action = 'messages_markunread';
		} 
		else 
		{
			var currentClass = 'unread'
			var newClass = 'read'
			//var unreadCount = 0;
			//var inboxCount = 1;
			//var unreadCountDisplay = 'none';
			var action = 'messages_markread';
		}

		checkboxes.each( function(i) 
		{
			if($(this).is(':checked')) 
			{
				if ( $('#m-' + $(this).attr('value')).hasClass(currentClass) ) 
				{
					checkboxes_tosend += $(this).attr('value');
					$('#m-' + $(this).attr('value')).removeClass(currentClass);
					$('#m-' + $(this).attr('value')).addClass(newClass);
					
					if (newClass == 'read')
					{
						$('#m-' + $(this).attr('value') + ' span.new_message').fadeOut(150);
					}
					else
					{
						$('#m-' + $(this).attr('value') + ' span.new_message').fadeIn(150);
					}
					
					if ( i != checkboxes.length - 1 ) 
					{
						checkboxes_tosend += ','
					}
				}
			}
		});
				
		$.post( ajaxurl, {
			action: action,
			'thread_ids': checkboxes_tosend
		});
		
		return false;
	});
	
	/* Bulk delete messages */
	function delete_messages()
	{
		checkboxes_tosend = '';
		checkboxes = $(".conversations input[type='checkbox']");

		//$('div#message').remove();
		$("a#delete_inbox_messages").addClass('loading');

		$(checkboxes).each( function(i) 
		{
			if( $(this).is(':checked') )
				checkboxes_tosend += $(this).attr('value') + ',';
		});
		
		if ( '' == checkboxes_tosend ) 
		{
			//$("a#delete_inbox_messages").removeClass('loading');
			return false;
		}

		$.post( ajaxurl, {
			action: 'messages_delete',
			'thread_ids': checkboxes_tosend
		}, function(response) {
			if ( response[0] + response[1] == "-1" ) {
				//$('#message-threads').prepend( response.substr( 2, response.length ) );
			} else {
				//$('#message-threads').before( '<div id="message" class="updated"><p>' + response + '</p></div>' );

				$(checkboxes).each( function(i) {
					if( $(this).is(':checked') )
						$(this).parent().parent().parent().parent().fadeOut(150);
				});
			}

			//$('div#message').hide().slideDown(150);
			$("a#delete_inbox_messages").removeClass('loading');
		});
		return false;
	}
	
	/* Single delete message */
	function delete_message(id)
	{	
		$.post( ajaxurl, {
			action: 'messages_delete',
			'thread_ids': id
		}, function(response) {
			if ( response[0] + response[1] == "-1" ) {
			} else {
				$('input[value=' + id + '][type="checkbox"]').parent().parent().parent().parent().fadeOut(150);
			}

			//$('div#message').hide().slideDown(150);
		});
		return false;
	}
	
	/*$("#dialog-delete-inbox-messages").dialog(
	{
		autoOpen: false,
		modal: true,
		buttons: {
			'Yes, delete these conversations': function() {
				$(this).dialog('close');
				delete_messages();
			},
			'cancel': function() {
				$(this).dialog('close');
			}
		}
	});*/
	
	$("a#delete_inbox_messages").click(function(){
		$("#modal-delete-inbox-messages").modal({overlayClose:true});
		$("#delete-inbox-messages").click(function(){
			delete_messages();
			$.modal.close();
			return false;
		});
		
		$("#cancel-delete-inbox-messages").click(function(){
			$.modal.close();
			return false;
		});
		return false;
	});
	
	$("a.delete_inbox_message").click(function()
	{
		var id = $(this).parent().parent().find("input[type='checkbox']").attr('value');
		
		$("#modal-delete-inbox-message").modal({overlayClose:true});
		
		$("#delete-inbox-message").click(function(){
			delete_message(id);
			$.modal.close();
			return false;
		});
		
		$("#cancel-delete-inbox-message").click(function(){
			$.modal.close();
			return false;
		});
		return false;
	});
	
	$("#dialog-delete-inbox-message").hide();
	
	$('.body-with-sidebar img').each(function() 
	{
        var maxWidth = 550; // Max width for the image
        var ratio = 0;  // Used for aspect ratio
        var width = $(this).width();    // Current image width
        var height = $(this).height();  // Current image height
 
        // Check if the current width is larger than the max
        if(width > maxWidth){
            ratio = maxWidth / width;   // get ratio for scaling image
            $(this).css("width", maxWidth); // Set new width
            $(this).css("height", height * ratio);  // Scale height based on ratio
            height = height * ratio;    // Reset height to match scaled image
            width = width * ratio;    // Reset width to match scaled image
        }
 
    });
    
    $('.body img').each(function() 
	{
        var maxWidth = 840; // Max width for the image
        var ratio = 0;  // Used for aspect ratio
        var width = $(this).width();    // Current image width
        var height = $(this).height();  // Current image height

        // Check if the current width is larger than the max
        if(width > maxWidth){
            ratio = maxWidth / width;   // get ratio for scaling image
            $(this).css("width", maxWidth); // Set new width
            $(this).css("height", height * ratio);  // Scale height based on ratio
            height = height * ratio;    // Reset height to match scaled image
            width = width * ratio;    // Reset width to match scaled image
        }
 
    });
    
    $(".sidebar .widget ul").each(function(){
		$(this).children("li:even").addClass("even");
	});
	
	$("blockquote").each(function(){
		$(this).children("p:last").addClass("last");
	});
	
});