$(function() {
	$('.hidden').hide();
	$('.visible').show();
	$("#loginBox :text, #loginBox :password").labelify();
	$("#loginBox :text, #loginBox :password, #loginBox :checkbox").tipsy();
	
	$('.ico').tipsy({source: "alt"});
	$('.listNum').tipsy({gravity: "w"});
	$('.error2, .tip2').tipsy();
	$('textarea.growing').autogrow();
	
	$('button.cancel').click(function() {
		var location = $(this).attr('value');
		if(location) {
			window.location = location;
		} else {
			history.back();
		}
	});
	
	$('#signin').click(function() {
		$('#loginBox').slideToggle('fast', function() {
			if($('#loginBox').css('display') == 'none') {
				$('#signin').removeClass('selected');
				$('#navi li a.prevSel').addClass('selected').removeClass('prevSel');
			} else {
				$('#navi li a.selected').addClass('prevSel').removeClass('selected');
				$('#signin').addClass('selected');
			}
		});
		return false;
	});
	
	
	$('#comments li.comment').hover(
		function() {
			$(this).addClass('hover');
		},
		function() {
			$(this).removeClass('hover');
		}
	);
	
	$('a.external').click(function() {
		window.open(this.href);
		return false;
	});
	
	$('a.confirm').click(function() {
		var answer = confirm('Do you really want to '+$(this).children('img').attr('alt').toLowerCase()+'?');
		return answer;
	});
	
	$('a.sh').click(function() {
		var div = $(this).attr('name').toLowerCase().replace(' ', '_');
		$('#'+div).slideToggle();
		return false;
	});
	
	$('a.shc').click(function() {
		var div = $(this).attr('name').toLowerCase().replace(' ', '_');
		$('.'+div).slideToggle();
		return false;
	});
	
	$('a.print').click(function() {
		window.open(this.href);
		return false;
	});
	
	$('form.required').submit(function() {
		$('form.required div.error2').remove();
		$('form.required li.buttons button').attr('disabled','disabled');
		var hasError = false;

		jQuery.each($('form.required ol.forms li.required'),function() {
			var labelText = $(this).children('label').text();
			labelText = labelText.replace(':','');
			
			if($(this).hasClass('grouping')) {
					var numSelected = 0;
					jQuery.each($(this).find('input'),function() {
						if($(this).attr('checked') == true) {
							numSelected++;
						}
					});

					if(numSelected == 0 && $(this).hasClass('tos')) {
						$(this).append('<div class="error2" title="You must agree to the terms and privacy policy."></div>');
						hasError = true;
					} else
					if(numSelected == 0) {
						$(this).append('<div class="error2" title="You must select '+labelText+'."></div>');
						hasError = true;
					}
			} else {
				if(jQuery.trim($(this).children('input, textarea').val()) == '') {
					$(this).append('<div class="error2" title="'+labelText+' cannot be blank!"></div>');
					hasError = true;
				}
			}

			
		});
		$('.error2').tipsy();
		
		if(hasError) {
			$('form.required li.buttons button').removeAttr('disabled');
			return false;
		} else {
			$('form.required li.buttons button').removeAttr('disabled');
			return true;
		}
	});
});

function checker(url, css, type, value)
{
	if(type=="login")
		name = "Login";
	else if(type=="email")
		name = "E-mail";
	
	$("."+css+" .checking2, ."+css+" .error2, ."+css+" .success2").remove();
	$("."+css).append('<div class="checking2"></div>');
	$.post(url+"?type="+type+"&value="+value ,function(data)
	{
		if(data=='no') {
			$("."+css+" .checking2").remove();
			$("."+css).append('<div class="error2" title="This '+name+' already exists."></div>');
			$('.error2').tipsy();
		} else if(data=='yes') {
			$("."+css+" .checking2").remove();
			$("."+css).append('<div class="success2" title="This '+name+' is available to register."></div>');
			$('.success2').tipsy();
		} else if(data=='error') {
			$("."+css+" .checking2").remove();
			$("."+css).append('<div class="error2" title="Are you a human?"></div>');
			$('.error2').tipsy();
		} else {
			$("."+css+" .checking2").remove();
			$("."+css).append('<div class="error2" title="'+data+'"></div>');
			$('.error2').tipsy();
		}
	});
}

