try {
	$('.chzn-select').chosen();
} catch(notloadedyet) {
	
}

$('.chzn-single').click(function(){
	var menuhide = $('#adminlinks_chzn').hasClass('chzn-container-active');
	if(menuhide){
		$('.sf-menu').fadeOut('slow');
	}
});
$("body").click(function(){ 
	var menuhide = $('.chzn-single').hasClass('chzn-single-with-drop');
	if(!menuhide){
		$('.sf-menu').fadeIn(1000);
	}
});
$('[name=adminlinks]').change(function(){
	var aurl = $(this).val();
	if(aurl == 'logout'){
		$.post("/ajax.cfm",{component:'lobby.auth',method:'logOut'},reloadDoc,'html');
	} else {
		window.location.href = aurl;
	}
});

function goLogin() {
	FB.getLoginStatus(
		function(response) {
			if (response.authResponse) {
				FB.api(
					'/me',
					function(fbu_object) {								
						loginChange(response,fbu_object);
					}
				);	
			}
			else {
				FB.Event.subscribe(
					'auth.login',
					function(fbe_data) {
						FB.api(
							'/me',
							function(fbu_object) {
								loginChange(fbe_data,fbu_object);
							}
						);		    
					}
				);
			}	
		}
	);
}
function reloadDoc() {
	window.location.reload();
}

$('#facebookButton').children().click(function() {
	goLogin();
});
$('.modal').live('click', function() {
    $.fancybox({
        scrolling : false,
        href : $(this).attr('href'),
        onComplete: function() {
	        $('#loginForm input[name=user]').select();
        	$('#loginForm input[name=user]').focus();
        }
    });
    return false;
});
$('#loginSubmit').click(function(e) {
	e.preventDefault();
	obj = $('#loginForm').serialize();
	$.ajax({
			type: "post",
			url: '/ajax.cfm',
			data: obj,
			dataType: 'json',
			success: function(d) {
				if (d.SUCCESS)
					window.location.reload();
				if (d.MESSAGE.length) {
					$(".loginError").html(d.MESSAGE);
					$(".loginError").show();
				}
			}
	});
});
$('#regSubmit').click(function(e) {
	e.preventDefault();
	obj = $('#registerForm').serialize();
	$.ajax({
			type: "post",
			url: '/ajax.cfm',
			data: obj+'&component=cityguides.register&method=register',
			dataType: 'json',
			success: function(d) {
				if (d.SUCCESS)
					$('#registrationComplete').fancybox();
				if (d.MESSAGE.length) {
					$(".loginError").html(d.MESSAGE);
					if ($(".loginError:first").text().toString().indexOf("Our records show") != -1) {
						$('#user').val($('#user_name').val());
						$('#login').trigger('click');
					}
					$(".loginError").show();
				}
			}
	});
});
$('#forgotForm').submit(function(){
	obj = $('#forgotForm').serialize();
	$.ajax({
			type: "post",
			url: '/ajax.cfm',
			data: obj+'&component=lobby.auth&method=forgotPassword',
			dataType: 'json',
			success: function(d) {
				$("#forgetError").html(d.MESSAGE);
				$("#forgetError").css('display','block');
			}
	});
	return false;
});
$('#resetForm').submit(function(){
	obj = $('#resetForm').serialize();
	$.ajax({
			type: "post",
			url: '/ajax.cfm',
			data: obj+'&component=lobby.auth&method=resetPassword',
			dataType: 'json',
			success: function(d) {
				if(d.SUCCESS == true){
					$('#resetError').html(d.MESSAGE);
					$("#resetError").css('display','block');
					$('#resetForm').hide();
				} else {
					$("#resetError").html(d.MESSAGE);
					$("#resetError").css('display','block');
				}
			}
	});
	return false;
});

var urlOnly = /^[A-Za-z0-9_]$/g;
function restrictCharacters(myfield, e, restrictionType) {
	if (!e)
		var e = window.event
	if (e.keyCode)
		code = e.keyCode;
	else if (e.which)
		code = e.which;
	var character = String.fromCharCode(code);
	if (code==27) {
		this.blur();
		return false;
	}
	if (!e.ctrlKey && code!=8 && (code!=39 || (code==39 && character=="'")) && code!=40) {
		if (character.match(restrictionType))
			return true;
		else
			return false;
	}
}
$('.logout').click(function(e) {
	e.preventDefault();
	try {
		FB.logout(function(response) {});
	}
	catch (e) {}
	$.post("/ajax.cfm",{component:'lobby.auth',method:'logOut'},reloadDoc,'html');
});
function reloadDoc() {
	window.location.reload();
}
