/**
 * shakenandstirred - modal.js
 *
 * Modal.
 *
 * @author Daniel Mullin daniel@nuguru.com
 * @author Brent McDowell brent@nuguru.com
 * @version 0.1
 * @todo ???
 */

$(document).ready(function()
{
	
	$('#enter-the-site').click(function()
	{

		day = $('#age-verification-form-day').val();
	
		month = $('#age-verification-form-month').val();
	
		year = $('#age-verification-form-year').val();
	
		country = $('#age-verification-form-country').val();

		if ($('#age-verification-remember-me').is(':checked'))
		{

			remember = 1;

		}
		else
		{
			
			remember = 0;
			
		}

		url = 'http://' + document.location.hostname + '/api/?p=user-verify-age|1|' + day + '|' + month + '|' + year + '|' + country + '|' + remember;

		$.getJSON(url, function(data)
		{	    	
			if (data == "1")
			{

				$('#age-verification').jqm().jqmHide();

			}

		});
	
	});

});


function ageVerificationActivateMonths()
{

	$("#age-verification-form-month").attr("disabled", false);

	$('#age-verification-form-month').css('color', '#ffffff');
    
	$('#age-verification-form-month').css('border', '#A0A0A0');

}

/**
* 
* @param    int                 $month
* @param    int                 $year 
* 
*/

function ageVerificationChangeMonthDays()
{
	
    month = $("#age-verification-form-month").val();
    
    year = $("#age-verification-form-year").val();
   
    var days_menu = document.getElementById('age-verification-form-day');

    if (month == -1)
    {
    	
        $("#age-verification-form-month").focus();
        
        return false;
        
    }
    else if (year == -1)
    {
	
        $("#age-verification-form-year").attr("disabled", false);
        
        $('#age-verification-form-year').css('color', '#ffffff');
        
        $('#age-verification-form-year').css('border', '#a0a0a0');
        
        $("#age-verification-form-year").focus();

    }
    else
    {
    	
        $("#age-verification-form-day").attr("disabled", false);
        
        $('#age-verification-form-day').css('color', '#ffffff');
        
        $('#age-verification-form-day').css('border', '#a0a0a0');
        
        $("#age-verification-form-day").focus();
        
       url = 'http://' + document.location.hostname + '/api/?p=dates-calendar-days|1|' + month + '|' + year;
         
       dataString = $.ajax({type: "POST", url: url, async: false}).responseText;

       var data = eval(dataString);
       
       if (data == "0") 
       {

            populateDays(31);
                
       }
       else
       {
                
    	   populateDays(data);

       }
        
    }

}

function hideModalAgeVerificationShowModalLogin()
{

	$('#age-verification').jqmHide();
	
	$('#modal-login').jqm({modal : true, overlay: 90}).jqmShow();
	
	return false;
	
}

function hideModalJoinShowModalLogin()
{

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

/**
 * shakenandstirred - modal.js - hideModalLoginShowModalForgotPassword()
 * 
 * Hide the login modal and show the forgot password modal instead.
 * 
 * @author Brent McDowell brent@nuguru.com
 * @version 0.1
 * @todo ???
 * 
 * @param void
 * 
 * @return bool
 */

function hideModalLoginShowModalForgotPassword()
{

	$('#modal-login').jqmHide();

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

	return false;

}

/**
 * shakenandstirred - modal.js - hideModalForgotPasswordConfirmShowModalForgotPassword()
 * 
 * Hide the forgot password confirm modal and show the forgot password modal instead.
 * 
 * @author Brent McDowell brent@nuguru.com
 * @version 0.1
 * @todo ???
 * 
 * @param void
 * 
 * @return bool
 */

function hideModalForgotPasswordConfirmShowModalForgotPassword()
{

	$('#modal-forgot-password-confirm').jqmHide();

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

	return false;

}

/**
 * shakenandstirred - modal.js - logout()
 * 
 * Logout.
 *
 * @author Daniel Mullin daniel@nuguru.com	
 * @version	0.9
 * @todo ???
 * 
 * @param void
 * 
 * @return void
 */

function logout()
{

	url = 'http://' + document.location.hostname + '/api/?p=user-logout';
	
	var data = $.ajax({type: "POST", url: url, async: false}).responseText;
	
	var response = data;
	
	if(response == 1)
	{

		// redirect to the index
		
		window.location = 'http://' + document.location.hostname;

	}

}

/**
 * 
 * functions for signup page
 *
 * @author	Daniel Mullin daniel@nuguru.com	
 * @version	0.9
 * @todo	???
 * 
 */


function modalJoin()
{

	username = $('#joinUsername').val();
	
	email    = $('#joinEmail').val();
		
    url = 'http://' + document.location.hostname + '/api/?p=user-add|1|' + username + '|' + email;
    
    data = $.ajax({type: "POST", url: url, async: false}).responseText;

	if (data == 1)
	{

		$('#join-success').show();
		
		$('#joinUsername').val("");

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

		$('#joinTerms').attr('checked', false);
	
		$('#joinCaptcha').val("");

	}
	
}

/**
 * shakenandstirred - modalLogin()
 * 
 * Login.
 *
 * @author Daniel Mullin daniel@nuguru.com	
 * @version	0.9
 * @todo ???
 * 
 * @param void
 * 
 * @return void
 */

function modalLogin()
{

	$('#user-login-error').hide();

	username = $('#loginUsername').val();
	
	password = $('#loginPassword').val();
		
    jsonUrl = 'http://' + document.location.hostname + '/api/?p=user-login|2|' + username + '|' + password;
    
    $.getJSON(jsonUrl, function(data) 
    {		
	
    	if(data.userId > 0)
    	{

			$('#modal-login').jqmHide();

    		$('#navigation-username').html('<a class="color-red" href="/user">' + data.username + '</a>');
    		
    		$('#navigation-login').html('<a onclick="logout(); return false;" href="#">LOGOUT</a>');

    		// Re-bind time.
    		
    		// User drink favourites.
    		$('div[class=item-wide-drink-add-to-menu right] > a').each(function()
    		{
    			
    			var drinkId = $(this).parent().attr('drink-id');
    			
    			$(this).attr('onClick', '');
    			
    			$(this).unbind('click');

    			$(this).bind('click', function()
    			{

    				url = 'http://' + document.location.hostname + '/api/?p=user-favourite|1|' + drinkId + '|drink|' + data.token;

    		        $.getJSON(url, function(data) 
    		        {
   
    		        });

    			});
    			
    		});

    		$('#drink-add-to-menu > a').attr('onClick', '');

    		$('#drink-add-to-menu > a').unbind('click');
    		
    		$('#drink-add-to-menu > a').bind('click', function()
    		{

    			url = 'http://' + document.location.hostname + '/api/?p=user-favourite|1|' + $(this).parent().attr('drink-id') + '|drink|' + data.token;

		        $.getJSON(url, function(data) 
		        {

		        });
    			
    		});

    		// Drink rating.
    		$('div[class=item-wide-drink-rating right] > ul > li').each(function()
    		{
    			
    			var drinkId = $(this).parent().parent().prev().attr('drink-id');

    			$(this).children().each(function()
    			{
    				
    				$(this).attr('onClick', '');
    				
    				$(this).unbind();
    				
    				$(this).bind('click', function()
    				{	
    					
    					var parent = $(this).parent();
    					
    					var id = $(parent).attr('id');

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

    					url = 'http://' + document.location.hostname + '/api/?p=rate|1|drink|' + drinkId + '|' + rating + '|' + data.token;

    					$.getJSON(url, function(data) 
    					{
    						
    						$(parent).parent().children().each(function(index)
    						{
    							if (this.id.match(/^rating$/))
    							{

    								$(this).val(data[drinkId].rating);
    								
    							}
    							
    							if (index < data[drinkId].rating)
    							{
    						
    								$(this).addClass('rating-active');
    								
    							}
    							else
    							{
    							
    								$(this).removeClass('rating-active');
    								
    							}
    							 
    						});	
    				
    					});

    				});
    				
    			});
    			
    		});

    		$('#drink-rating > ul > li').each(function()
    		{
    			
    			var drinkId = $(this).parent().parent().prev().attr('drink-id');

    			$(this).children().each(function()
    			{
    				
    				$(this).attr('onClick', '');
    				
    				$(this).unbind();
    				
    				$(this).bind('click', function()
    				{	
    					
    					var parent = $(this).parent();
    					
    					var id = $(parent).attr('id');

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

    					url = 'http://' + document.location.hostname + '/api/?p=rate|1|drink|' + drinkId + '|' + rating + '|' + data.token;

    					$.getJSON(url, function(data) 
    					{
    						
    						$(parent).parent().children().each(function(index)
    						{
    							if (this.id.match(/^rating$/))
    							{

    								$(this).val(data[drinkId].rating);
    								
    							}
    							
    							if (index < data[drinkId].rating)
    							{
    						
    								$(this).addClass('rating-active');
    								
    							}
    							else
    							{
    							
    								$(this).removeClass('rating-active');
    								
    							}
    							 
    						});	
    				
    					});

    				});
    				
    			});
    			
    		});
    		
    		// User bartender favourites.
    		$('div[class=item-wide-bartender-add-to-menu right] > a').each(function()
    		{
    			
    			var bartenderId = $(this).parent().attr('bartender-id');

    			$(this).attr('onClick', '');
    			
    			$(this).unbind('click');

    			$(this).bind('click', function()
    			{

    				url = 'http://' + document.location.hostname + '/api/?p=user-favourite|1|' + bartenderId + '|bartender|' + data.token;

    		        $.getJSON(url, function(data) 
    		        {

    		        });

    			});

    		});

    		$('#bartender-add-to-menu > a').attr('onClick', '');

    		$('#bartender-add-to-menu > a').unbind('click');
    		
    		$('#bartender-add-to-menu > a').bind('click', function()
    		{

    			url = 'http://' + document.location.hostname + '/api/?p=user-favourite|1|' + $(this).parent().attr('bartender-id') + '|bartender|' + data.token;

		        $.getJSON(url, function(data) 
		        {

		        });
    			
    		});

    		// Bartender rating.
    		$('div[class=item-wide-bartender-rating right] > ul > li').each(function()
    		{
    			
    			var bartenderId = $(this).parent().parent().prev().attr('bartender-id');

    			$(this).children().each(function()
    			{
    				
    				$(this).attr('onClick', '');
    				
    				$(this).unbind();
    				
    				$(this).bind('click', function()
    				{	
    					
    					var parent = $(this).parent();
    					
    					var id = $(parent).attr('id');

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

    					url = 'http://' + document.location.hostname + '/api/?p=rate|1|bartender|' + bartenderId + '|' + rating + '|' + data.token;

    					$.getJSON(url, function(data) 
    					{
    						
    						$(parent).parent().children().each(function(index)
    						{
    							if (this.id.match(/^rating$/))
    							{

    								$(this).val(data[bartenderId].rating);
    								
    							}
    							
    							if (index < data[bartenderId].rating)
    							{
    						
    								$(this).addClass('rating-active');
    								
    							}
    							else
    							{
    							
    								$(this).removeClass('rating-active');
    								
    							}
    							 
    						});	
    				
    					});

    				});
    				
    			});
    			
    		});
    		
    		$('#bartender-rating > ul > li').each(function()
    		{
    			
    			var bartenderId = $(this).parent().parent().prev().attr('bartender-id');

    			$(this).children().each(function()
    			{
    				
    				$(this).attr('onClick', '');
    				
    				$(this).unbind();
    				
    				$(this).bind('click', function()
    				{	
    					
    					var parent = $(this).parent();
    					
    					var id = $(parent).attr('id');

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

    					url = 'http://' + document.location.hostname + '/api/?p=rate|1|bartender|' + bartenderId + '|' + rating + '|' + data.token;

    					$.getJSON(url, function(data) 
    					{
    						
    						$(parent).parent().children().each(function(index)
    						{
    							if (this.id.match(/^rating$/))
    							{

    								$(this).val(data[bartenderId].rating);
    								
    							}
    							
    							if (index < data[bartenderId].rating)
    							{
    						
    								$(this).addClass('rating-active');
    								
    							}
    							else
    							{
    							
    								$(this).removeClass('rating-active');
    								
    							}
    							 
    						});	
    				
    					});

    				});
    				
    			});
    			
    		});
    		
    		// User ingredient favourites.
    		$('div[class=item-wide-ingredient-add-to-menu right] > a').each(function()
    		{
    			
    			var ingredientId = $(this).parent().attr('ingredient-id');

    			$(this).attr('onClick', '');
    			
    			$(this).unbind('click');

    			$(this).bind('click', function()
    			{

    				url = 'http://' + document.location.hostname + '/api/?p=user-favourite|1|' + ingredientId + '|ingredient|' + data.token;

    		        $.getJSON(url, function(data) 
    		        {

    		        });

    			});

    		});

    		$('#ingredient-add-to-menu > a').attr('onClick', '');

    		$('#ingredient-add-to-menu > a').unbind('click');
    		
    		$('#ingredient-add-to-menu > a').bind('click', function()
    		{

    			url = 'http://' + document.location.hostname + '/api/?p=user-favourite|1|' + $(this).parent().attr('ingredient-id') + '|ingredient|' + data.token;

		        $.getJSON(url, function(data) 
		        {

		        });
    			
    		});

    		// Ingredient rating.
    		$('div[class=item-wide-ingredient-rating right] > ul > li').each(function()
    		{
    			
    			var ingredientId = $(this).parent().parent().prev().attr('ingredient-id');

    			$(this).children().each(function()
    			{
    				
    				$(this).attr('onClick', '');
    				
    				$(this).unbind();
    				
    				$(this).bind('click', function()
    				{	
    					
    					var parent = $(this).parent();
    					
    					var id = $(parent).attr('id');

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

    					url = 'http://' + document.location.hostname + '/api/?p=rate|1|ingredient|' + ingredientId + '|' + rating + '|' + data.token;

    					$.getJSON(url, function(data) 
    					{
    						
    						$(parent).parent().children().each(function(index)
    						{
    							if (this.id.match(/^rating$/))
    							{

    								$(this).val(data[ingredientId].rating);
    								
    							}
    							
    							if (index < data[ingredientId].rating)
    							{
    						
    								$(this).addClass('rating-active');
    								
    							}
    							else
    							{
    							
    								$(this).removeClass('rating-active');
    								
    							}
    							 
    						});	
    				
    					});

    				});
    				
    			});
    			
    		});
    		
    		$('#ingredient-rating > ul > li').each(function()
    		{
    			
    			var ingredientId = $(this).parent().parent().prev().attr('ingredient-id');

    			$(this).children().each(function()
    			{
    				
    				$(this).attr('onClick', '');
    				
    				$(this).unbind();
    				
    				$(this).bind('click', function()
    				{	
    					
    					var parent = $(this).parent();
    					
    					var id = $(parent).attr('id');

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

    					url = 'http://' + document.location.hostname + '/api/?p=rate|1|ingredient|' + ingredientId + '|' + rating + '|' + data.token;

    					$.getJSON(url, function(data) 
    					{
    						
    						$(parent).parent().children().each(function(index)
    						{
    							if (this.id.match(/^rating$/))
    							{

    								$(this).val(data[ingredientId].rating);
    								
    							}
    							
    							if (index < data[ingredientId].rating)
    							{
    						
    								$(this).addClass('rating-active');
    								
    							}
    							else
    							{
    							
    								$(this).removeClass('rating-active');
    								
    							}
    							 
    						});	
    				
    					});

    				});
    				
    			});
    			
    		});
    		
    		// User glass favourites.
    		$('div[class=item-wide-glass-add-to-menu right] > a').each(function()
    		{
    			
    			var glassId = $(this).parent().attr('glass-id');

    			$(this).attr('onClick', '');
    			
    			$(this).unbind('click');

    			$(this).bind('click', function()
    			{

    				url = 'http://' + document.location.hostname + '/api/?p=user-favourite|1|' + glassId + '|glass|' + data.token;

    		        $.getJSON(url, function(data) 
    		        {

    		        });

    			});

    		});

    		$('#glass-add-to-menu > a').attr('onClick', '');

    		$('#glass-add-to-menu > a').unbind('click');
    		
    		$('#glass-add-to-menu > a').bind('click', function()
    		{

    			url = 'http://' + document.location.hostname + '/api/?p=user-favourite|1|' + $(this).parent().attr('glass-id') + '|glass|' + data.token;

		        $.getJSON(url, function(data) 
		        {

		        });
    			
    		});
    		
    		// Glass rating.
    		$('div[class=item-wide-glass-rating right] > ul > li').each(function()
    		{
    			
    			var glassId = $(this).parent().parent().prev().attr('glass-id');

    			$(this).children().each(function()
    			{
    				
    				$(this).attr('onClick', '');
    				
    				$(this).unbind();
    				
    				$(this).bind('click', function()
    				{	
    					
    					var parent = $(this).parent();
    					
    					var id = $(parent).attr('id');

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

    					url = 'http://' + document.location.hostname + '/api/?p=rate|1|glass|' + glassId + '|' + rating + '|' + data.token;

    					$.getJSON(url, function(data) 
    					{
    						
    						$(parent).parent().children().each(function(index)
    						{
    							if (this.id.match(/^rating$/))
    							{

    								$(this).val(data[glassId].rating);
    								
    							}
    							
    							if (index < data[glassId].rating)
    							{
    						
    								$(this).addClass('rating-active');
    								
    							}
    							else
    							{
    							
    								$(this).removeClass('rating-active');
    								
    							}
    							 
    						});	
    				
    					});

    				});
    				
    			});
    			
    		});

    		$('#glass-rating > ul > li').each(function()
    		{
    			
    			var glassId = $(this).parent().parent().prev().attr('glass-id');

    			$(this).children().each(function()
    			{
    				
    				$(this).attr('onClick', '');
    				
    				$(this).unbind();
    				
    				$(this).bind('click', function()
    				{	
    					
    					var parent = $(this).parent();
    					
    					var id = $(parent).attr('id');

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

    					url = 'http://' + document.location.hostname + '/api/?p=rate|1|glass|' + glassId + '|' + rating + '|' + data.token;

    					$.getJSON(url, function(data) 
    					{
    						
    						$(parent).parent().children().each(function(index)
    						{
    							if (this.id.match(/^rating$/))
    							{

    								$(this).val(data[glassId].rating);
    								
    							}
    							
    							if (index < data[glassId].rating)
    							{
    						
    								$(this).addClass('rating-active');
    								
    							}
    							else
    							{
    							
    								$(this).removeClass('rating-active');
    								
    							}
    							 
    						});	
    				
    					});

    				});
    				
    			});
    			
    		});
    		
    		page = document.location.pathname;
    		
    		// Comments on cocktail, bartender, ingredient or glass page.
    		if (page.match(/cocktail\//) || page.match(/bartender\//) || page.match(/ingredient\//) || page.match(/glass\//))
    		{

    			parts = page.split('/');

    			$('#post-comment').attr('onClick', '');

    			$('#post-comment').unbind('click');
    			
    			$('#post-comment').bind('click', function()
    			{

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

    				$('#token').val(data.token);

    				if (parts[1] == 'cocktail')
    				{
    					
    					$('#objectType').val('drink');
    				
    				}
    				else
    				{
    					
    					$('#objectType').val(parts[1]);

    				}
    					
    				$('#objectId').val(parts[2]);

    			});

    		}
    		
    		Cufon.refresh();

    	}
    	else
    	{
    		
    		$('#user-login-error').show();

    	}    	
    	
	})
	
}

/**
 * shakenandstirred - modal.js - modalChangePassword()
 * 
 * Modal to send a new temporary password.
 * 
 * @author Brent McDowell brent@nuguru.com
 * @version 0.1
 * @todo ???
 * 
 * @param void
 * 
 * @return void
 */

function modalForgotPassword()
{

	var username = $('#forgotUsername').val();

	jsonUrl = 'http://' + document.location.hostname + '/api/?p=user-forgot-password|2|' + username;

    $.getJSON(jsonUrl, function(data) 
    {	

		if (data == 1)
		{

			$('#forgot-password-success').show();

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

		}

	})

}

/**
* Friction TV - join.js - populateDays()
* 
* populate days for a month
* 
* @author						Daniel Mullin
* @version						0.9
* @todo							???
* 
* 
* @param 	int					$days
* 
*/

function populateDays(days)
{
	
	var selectedDays = $("#age-verification-day").value;
	
	$("#age-verification-form-day").html('');

	$("#age-verification-form-day").append("<option value='-1' selected='selected'>Days</option>");

	for (var i = 1; i <= days; i++)
	{

		if (selectedDays == i)
		{
			
			$("#age-verification-form-day").append("<option value='" + i + "' selected='selected'>" + i + "</option>");
			
		}
		else
		{
			
			$("#age-verification-form-day").append("<option value='" + i + "'>" + i + "</option>");
			
		}
	
	}
	
}

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

	return false;
	
}

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

/**
 * shakenandstirred - modal.js - showModalChangePassword()
 * 
 * Show the change password modal.
 * 
 * @author Brent McDowell brent@nuguru.com
 * @version 0.1
 * @todo ???
 * 
 * @param void
 * 
 * @return bool
 */
 
function showModalChangePassword()
{

	$('#change-password-success').hide();
	
	$('#modal-change-password').jqm({modal : true, overlay: 90}).jqmShow();

	return false;
	
}

/**
 * shakenandstirred - modal.js - checkJoinUsername()
 * 
 * Check if the username is already in use.
 * 
 * @author					Brent McDowell brent@nuguru.com
 * @version					0.1
 * @todo					???
 * 
 * @param		string		username
 * 
 * @return		bool
 * 
 */

function checkJoinUsername(username)
{

	url = "http://" + document.location.hostname + "/api/?p=user-username-available|1|" + username

	var response = $.ajax({type: "POST", url: url, async: false}).responseText;

	if (response == 1)
	{
		
		return true;
		
	}

	return false;

}

/**
 * shakenandstirred - modal.js - checkJoinEmail()
 * 
 * Check if the email address is already in use.
 * 
 * @author					Brent McDowell brent@nuguru.com
 * @version					0.1
 * @todo					???
 * 
 * @param		string		emailAddress
 * 
 * @return		bool
 * 
 */

function checkJoinEmail(emailAddress)
{

	url = "http://" + document.location.hostname + "/ajax/?p=user-metas-email-exists|1|" + emailAddress

	var response = $.ajax({type: "POST", url: url, async: false}).responseText;

	if (response == 1)
	{
		
		return true;
		
	}

	return false;

}

/**
 * shakenandstirred - modal.js - checkCaptcha()
 * 
 * Check the captcha the user entered with their session
 * 
 * @author					Brent McDowell brent@nuguru.com
 * @version					0.1
 * @todo					???
 * 
 * @param		string		captcha
 * 
 * @return		bool
 * 
 */

function checkCaptcha(captcha)
{

	url = "http://" + document.location.hostname + "/api/?p=check-captcha|" + captcha;

	var response = $.ajax({type: "POST", url: url, async: false}).responseText;

	if (response == 1)
	{
		
		return true;
		
	}

	return false;

}