$(document).ready(function() 
{

	// Should we display the login modal? If someone has gone to /login, then yes.
	// Or, if they've gone to /join then show the join modal. 
	var currentPage = document.location.href;
	
	if (currentPage.match(/login$/))
	{

		$('#modal-login').jqm({modal : true, overlay: 90}).jqmShow();

	}
	else if (currentPage.match(/join$/))
	{
		
		$('#modal-join').jqm({modal : true, overlay: 90}).jqmShow();
		
	}
	
	$('li').mouseover(function() 
	{

		var id = $(this).attr('id');

		if (id.match(/^rating/))
		{

			var rating = id.replace(/rating-/g, '');

			if (rating == 1)
			{

				$(this).parent().children().each(function()
				{
						
					var childId = this.id.replace(/rating-/g, '');
						
					if (childId > rating)
					{

						$(this).attr({'id': $(this).id, 'class': ''});

					}
					else if (childId == rating)
					{

						$(this).attr({'id': $(this).id, 'class': 'rating-first rating-active'});
	
					}

				});

			}
			else if (rating == 2)
			{
	
				$(this).parent().children().each(function()
				{
						
					var childId = this.id.replace(/rating-/g, '');
	
					if (childId > rating)
					{

						$(this).attr({'id': $(this).id, 'class': ''});

					}
					else if (childId < rating)
					{

						if (childId == 1)
						{

							$(this).attr({'id': $(this).id, 'class': 'rating-first rating-active'});

						}
						else
						{

							$(this).attr({'id': $(this).id, 'class': 'rating-active'});
						
						}
						
					}

				});

			}
			else if (rating == 3)
			{
					
				$(this).parent().children().each(function()
				{
						
					var childId = this.id.replace(/rating-/g, '');
						
					if (childId > rating)
					{

						$(this).attr({'id': $(this).id, 'class': ''});

					}
					else if (childId < rating)
					{

						if (childId == 1)
						{

							$(this).attr({'id': $(this).id, 'class': 'rating-first rating-active'});

						}
						else
						{

							$(this).attr({'id': $(this).id, 'class': 'rating-active'});
						
						}

					}

				});

			}
			else if (rating == 4)
			{

				$(this).parent().children().each(function()
				{
						
					var childId = this.id.replace(/rating-/g, '');
	
					if (childId > rating)
					{

						$(this).attr({'id': $(this).id, 'class': ''});

					}
					else if (childId < rating)
					{

						if (childId == 1)
						{

							$(this).attr({'id': $(this).id, 'class': 'rating-first rating-active'});

						}
						else
						{

							$(this).attr({'id': $(this).id, 'class': 'rating-active'});

						}

					}

				});

			}
			else if (rating == 5)
			{
	
				$(this).parent().children().each(function()
				{

					var childId = this.id.replace(/rating-/g, '');
	
					if (childId == 1)
					{

						$(this).attr({'id': $(this).id, 'class': 'rating-first rating-active'});

					}
					else
					{

						$(this).attr({'id': $(this).id, 'class': 'rating-active'});
						
					}

				});

			}

		}

	});
	
});

function expand(click)
{

	$(click).parent().parent().parent().parent().hide();
	
	//$(click).parent().parent().hide();
	
	$(click).parent().parent().parent().parent().next().show();

	return false;

}

function rate(rate, id, rating, token, click)
{
	
	if(token == '')
	{
		
		$('#modal-join').jqm({modal : true, overlay: 90}).jqmShow();
		
	}
	else
	{
	
		url = 'http://' + document.location.hostname + '/api/?p=rate|1|' + rate + '|' + id + '|' + rating + '|' + token;
	
		$.getJSON(url, function(data) 
		{

			$(click).parent().parent().children().each(function (i)
			{
			
				if(i < data[id].rating)
				{
			
					$(this).addClass('rating-active');
					
				}
				else
				{
				
					$(this).removeClass('rating-active');
					
				}
				 
			});	
	
		});
		
	}
	
	return false;
	
}

function unexpand(click)
{

	$(click).parent().parent().parent().parent().prev().show();
	
	$(click).parent().parent().parent().parent().prev().prev().show();
	
	$(click).parent().parent().parent().parent().hide();

	return false;

}

function userFavourite(rate, id, token, click)
{
    
    if(token == '')
    {
        
        $('#modal-join').jqm({modal : true, overlay: 90}).jqmShow();
        
    }
    else
    {
    
        jsonUrl = 'http://' + document.location.hostname + '/api/?p=user-favourite|1|' + rate + '|' + id + '|' + token;
    
        $.getJSON(jsonUrl, function(data) 
        {
        
           // alert('added');
            
        });
        
    }
    
    return false;
    
}

/**
 * global.js - showComment()
 * 
 * Show the comment.
 * 
 * @author					Brent McDowell brent@nuguru.com
 * @version					0.1
 * @todo					???
 * 
 * @param	object			this
 * 
 * @return	void
 */
 
function showComment(click)
{

	$('#item-wide-response > p > span').each(function()
	{
		if (this.id.match(/^item-wide-response-comment-/))
		{
			$(this).hide();
		}
	});

	responseId = $(click).attr('response-id');

	$('#item-wide-response-comment-' + responseId).show();
	
}

/**
 * global.js - showCommentForm()
 * 
 * Show the add comment form.
 * 
 * @author					Brent McDowell brent@nuguru.com
 * @version					0.1
 * @todo					???
 * 
 * @param	object			click
 * @param	string			token
 * @param	string			objectType
 * @param	int				objectId
 *  
 * @return	void
 */
 
function showCommentForm(click, token, objectType, objectId)
{

	if(token == '')
	{
		
		$('#modal-join').jqm({modal : true, overlay: 90}).jqmShow();
		
	}
	else
	{

		$(click).parent().hide();

		$('#item-wide-response-add-comment').show();
	
		$('#commentTitle').focus();

		$('#token').val(token);
		
		$('#objectType').val(objectType);
		
		$('#objectId').val(objectId);

	}

}

/**
 * global.js - addComment()
 * 
 * Add a comment.
 *
 * @author					Brent McDowell brent@nuguru.com
 * @version					0.1
 * @todo					???
 *
 * @param	string			token
 * @param	string			type
 * @param	int				typeId
 * 
 * @return	void
 */

function addComment()
{
	
	var token = $('#token').val();
	
	var objectType = $('#objectType').val();
	
	var objectId = $('#objectId').val();
	
	var commentTitle = $('#commentTitle').val();
	
	var commentContent = $('#commentContent').val();
	
	if(token == '')
	{
		
		$('#modal-join').jqm({modal : true, overlay: 90}).jqmShow();
		
	}
	else
	{

		var url = 'http://' + document.location.hostname + '/api/?p=response-add|1|' + objectType + '|' + objectId + '|0|' + commentTitle + '|' + commentContent + '|' + token;

		$.ajax({type: "GET", url: url, async: false}).responseText;

		location.reload(true);

	}

}

/**
 * shakenandstirred - global.js - changePassword()
 * 
 * Change a user's password.
 * 
 * @author Brent McDowell brent@nuguru.com
 * @version 0.1
 * @todo ???
 * 
 * @param void
 * 
 * @return void
 */

function changePassword()
{

	var password = $('#password').val();

	var token = $('#token').val();

    url = 'http://' + document.location.hostname + '/api/?p=user-change-password|2|' + token + '|' + password;
    
    $.getJSON(url, function(data) 
    {		

		if (data == 1)
		{

			$('#user-change-password-success').show();

			$('#password').val("");

			$('#confirmPassword').val("");

		}

	})

}

/**
 * shakenandstirred - global.js - rebindRating()
 * 
 * Rebind the rating stars.
 * 
 * @author Brent McDowell brent@nuguru.com
 * @version 0.1
 * @todo ???
 * 
 * @param void
 * 
 * @return void
 */
function rebindRating()
{

	$('li').unbind('mouseover');

	$('li').bind('mouseover', function() 
	{

		var id = $(this).attr('id');

		if (id.match(/^rating/))
		{

			var rating = id.replace(/rating-/g, '');

			if (rating == 1)
			{

				$(this).parent().children().each(function()
				{
						
					var childId = this.id.replace(/rating-/g, '');
						
					if (childId > rating)
					{

						$(this).attr({'id': $(this).id, 'class': ''});

					}
					else if (childId == rating)
					{

						$(this).attr({'id': $(this).id, 'class': 'rating-first rating-active'});
	
					}

				});

			}
			else if (rating == 2)
			{
	
				$(this).parent().children().each(function()
				{
						
					var childId = this.id.replace(/rating-/g, '');
	
					if (childId > rating)
					{

						$(this).attr({'id': $(this).id, 'class': ''});

					}
					else if (childId < rating)
					{

						if (childId == 1)
						{

							$(this).attr({'id': $(this).id, 'class': 'rating-first rating-active'});

						}
						else
						{

							$(this).attr({'id': $(this).id, 'class': 'rating-active'});
						
						}
						
					}

				});

			}
			else if (rating == 3)
			{
					
				$(this).parent().children().each(function()
				{
						
					var childId = this.id.replace(/rating-/g, '');
						
					if (childId > rating)
					{

						$(this).attr({'id': $(this).id, 'class': ''});

					}
					else if (childId < rating)
					{

						if (childId == 1)
						{

							$(this).attr({'id': $(this).id, 'class': 'rating-first rating-active'});

						}
						else
						{

							$(this).attr({'id': $(this).id, 'class': 'rating-active'});
						
						}

					}

				});

			}
			else if (rating == 4)
			{

				$(this).parent().children().each(function()
				{
						
					var childId = this.id.replace(/rating-/g, '');
	
					if (childId > rating)
					{

						$(this).attr({'id': $(this).id, 'class': ''});

					}
					else if (childId < rating)
					{

						if (childId == 1)
						{

							$(this).attr({'id': $(this).id, 'class': 'rating-first rating-active'});

						}
						else
						{

							$(this).attr({'id': $(this).id, 'class': 'rating-active'});

						}

					}

				});

			}
			else if (rating == 5)
			{
	
				$(this).parent().children().each(function()
				{

					var childId = this.id.replace(/rating-/g, '');
	
					if (childId == 1)
					{

						$(this).attr({'id': $(this).id, 'class': 'rating-first rating-active'});

					}
					else
					{

						$(this).attr({'id': $(this).id, 'class': 'rating-active'});
						
					}

				});

			}

		}

	});
	
}

/**
 * shakenandstirred - global.js - rebindToolTips()
 * 
 * Rebind tooltips.
 * 
 * @author Brent McDowell brent@nuguru.com
 * @version 0.1
 * @todo ???
 * 
 * @param void
 * 
 * @return void
 */
function rebindTooltips()
{

	$('#drink-pdf a[title]').tooltip();
	
	$('#rating-1 a[title]').tooltip();
	$('#rating-2 a[title]').tooltip();
	$('#rating-3 a[title]').tooltip();
	$('#rating-4 a[title]').tooltip();
	$('#rating-5 a[title]').tooltip();

	$('#drink-add-to-menu a[title]').tooltip();
	$('.item-wide-bartender-add-to-menu a[title]').tooltip();
	$('.item-wide-drink-add-to-menu a[title]').tooltip();
	$('.item-wide-glass-add-to-menu a[title]').tooltip();
	$('.item-wide-ingredient-add-to-menu a[title]').tooltip();
	
	$('.add-this-service a[title]').tooltip();
	
}