$(document).ready(function () {
	$('#serves').jSel({cssTag:'s'});
	$('#ratings').jSel({cssTag:'s'});

	$('.glossary_default_opt').click(function () {
		$('.glossary_default_opt').each(function () {
			$(this).show().next().hide();
		});
		$(this).hide().next().show();
	}).css('cursor','pointer');	

	$('.glossary_default_opt').first().hide().next().show();
	
	if ('#howToBuy' == window.location.hash) 
		chg_nav(2);
	else if ('#ratings' == window.location.hash) 
		chg_nav(3);
	else  
		chg_nav(1);

	if ('#shoppingList' == window.location.hash) 
		chg_tab('toolbox', 0); 
	else 
		chg_tab('toolbox', 1);
		
	suDrpShd();	

	$('.hSMS').click(function() {$('#TB_ajaxContent').html(gLd); ti_url('/recipe.php?c=sms&s=l&id='+$('#recipeId').val());});
	$('.hPrint').click(function() {$('#TB_ajaxContent').html(gLd); ti_url('/recipe.php?c=print');});
	$('.hShare').click(function() {$('#TB_ajaxContent').html(gLd); ti_url('/recipe.php?c=share&s=l&id='+$('#recipeId').val());});
	$('.hNutr').click(function() {$('#TB_ajaxContent').html(gLd); ti_url('/recipe.php?c=nutr');});
});

function chg_nav(num) {
	if (1 == num) {
		$('#recipeDetails').show();
		$('#recipe_ratings').hide();
		$('#recipe_glossary').hide();
	} else if (2 == num) {
		$('#recipeDetails').hide();
		$('#recipe_ratings').hide();
		$('#recipe_glossary').show();
	} else {
		$('#recipeDetails').hide();
		$('#recipe_glossary').hide();
		$('#recipe_ratings').show();
	}
}

function bskUpdCb(t) {
	bskUpd(t);
}

function usrv() {
	var newServes = $('#serves').get(0).options[$('#serves').get(0).selectedIndex].value;
	$('#rPrDatSrv').html(newServes);
	$.ajax({type:'post', url:'/recipe.php', data:'c=s&m=r&l=1&id='+$('#recipeId').val()+'&s='+newServes, success:function(t){$('#rIngs').html(t);$('#rPrIngs').html(t);}});
}

function rta(id) {
	var c = $('#commentText').val();
	var r = $('#ratings').val();
	var p = 'c=c&id='+id+'&t='+encodeURIComponent(c)+'&r='+r;

	if (c.length > 1024) {
		alert('Please enter a comment less than 1024 characters long.');
		$('#commentText').focus();
		return;
	}
	
	if (c.length == 0) {
		alert('Please enter a comment.');
		$('#commentText').focus();
		return;
	}
	$.ajax({type:'post', url:'/recipe.php', data:p, success:function (t){rtu(t)}});
}

function rtu(t) {
	$('#comments').html(t);
}

function togCbk(id) {
	var i = $('#recipeInCbk').html(), u = $('#modifiedFromBase').val();
	var p = "c=f&id="+id+"&add="+((parseInt(i) == 0)?1:0);

	if (parseInt(i) == 1) {
		if ((u == 1) && (!confirm('Are you sure you want to remove this meal from your cookbook? This will delete any changes you have made to this recipe.')))
			return;		
    	} 
   
	window.location='/recipe.php?'+p;
 	return;
}

function rni(prev) {
	c = parseInt($('#recipeImgCurrent').val());
	t = parseInt($('#recipeImgCount').val());
	if (2 < t)
	{
		c += prev;
		if (c < 0) c = t-1;
		if (c >= t) c = 0;
		i = '#recipeImg' + c;
	}
	else
		i = '#recipeImg';
	
	$('#recipeImg').html($(i).html());
	$('#recipeImgCurrent').val(c);
}

function doEmail(id) {
	var f = $('#lb_email_form').get(0);
	var p = 'c=share&s=1&id='+id+'&n='+f.n.value+'&e='+f.e.value+'&t='+f.t.value;
	
	$('#lb_email_box').hide();
	$('#lb_email_load').show();

	$.ajax({type:'post', url:"/recipe.php", data:p, 
		success:function (t) {
			if (t == 'OK')
			{
				$('#lb_email_load').hide();
				$('#lb_email_done').show();
			}
			else {
				$('#lb_email_err').html("<b>Error:</b> " + t);
				$('#lb_email_load').hide();
				$('#lb_email_box').show();
			}
		}
	});
}

function rAdd(id) {
	bskTog(id, 1, 0);
	chg_tab('toolbox', 0);
}

function rUse() {
	var d = $('#useForDay').val();
	if (d != -1) {
		bskTog($('#recipeId').val(), 1, di2n(d));
		chg_tab('toolbox', 0);
	}
}

function suDrpShd() {
	$('#rRelBox > div > .rlnk')
		.mouseover(function() {$(this).next('.onhover').show();})
		.mouseout(function() {
			if ($(this).next('.onhover').children(':input[name=shadow]').val() != 1)
				$(this).next('.onhover').hide();
		})
		.click(function() {
			$('#rRelBox > div').each(function () {
				$(this).children('.onhover').hide().children(':input[name=shadow]').val(0);
			});
		});
}

function glsDsp($e) {
	var $ents = $('#glsEnts > .glsEnt'), $n;

	if ($e) {
		$ents.each(function() {
			$t = $('.glsEntN', $(this));
			if ($t.html() == $e.html()) $n = $(this);
		});
		$e.html($('#glsEntN').html());
	} else {
		$n = $ents.first();
		$('#glsLnks > ul > li > a').each(function() {
			if ($(this).html() == $('.glsEntN', $n).html()) $(this).parent().hide();
		});
	}		

	$('#glsEntN').html($('.glsEntN', $n).html());
	$('#glsEntD').html($('.glsEntD', $n).html());
	$('#glsEntI').html($('.glsEntI', $n).html());

//	$e = $('#glsLnks > ul > li').first();
//	alert($e.html());
}
