$(function() {
/**
 * Global app object
 */
	GL = (typeof GL == 'undefined' ? {} : GL);

	// Clear activeZone cookie
	$('a.gateway').click(function() {
		document.cookie = 'CakeCookie[activeZone]=; expires=; path=/';
	});

	// Flash message
	$('div.message')
	.slideDown('slow').delay(5000).slideUp('slow')
	.click(function() {
		$(this).queue([]).slideUp('fast');
	});
	/*.mouseenter(function() {
		$(this).queue([]).slideUp('fast');
	});*/
	$('div.debug-message')
	.slideDown('slow')
	.click(function() {
		$(this).queue([]).slideUp('fast');
	});

	// Search form
	$('#glSearch form').submit(function() {
		if ($('#search_query').val() == '') {
			$('#search_query').focus();
			return false;
		}
		if ($('#search_zoneid').val() == '' && $('#search_zone').val() == '') {
			$('#search_zone').focus();
			return false;
		}
		//window.location.replace("/search/"+$.trim($("#search_query").val())+"/"+$.trim($("#search_zone").val())+"/"+$("#search_zoneid").val());
		//return false;
	});

	// Search Zone
	$('#search_zone')
	// Search zones autocomplete
	.autocomplete({
		source: GL.search.autocomplete.url,
		minLength: 2,
		select: function(event, ui) {
			GL.search.autocomplete.lastSelected = ui.item.value;
			$('#search_zoneid').val(ui.item.id);
		}
	})
	// Clear selected zone
	.focus(function() {
		if ($(this).val() == $(this)[0].defaultValue) {
			$(this).val('');
			$('#search_zoneid').val('');
		}
	})
	// Restore default zone if nothing was written
	.blur(function() {
		if ($(this).val() == '') {
			$(this).val($(this)[0].defaultValue);
			$('#search_zoneid').val(GL.activeZone.id);
		}
	})
	// ID should be unset if the zone field is modified
	.keydown(function() {
		var zone = $.trim($('#search_zone').val()).replace(/(\s+)/g, ' ');
		// TODO must trigger ajax call only if minLenght valid chars are set
		if (zone != GL.search.autocomplete.lastSelected) {
			$('#search_zoneid').val('');
		}
	});

	// Tooltips
	$('.ttr[title]').tooltip({
		position: 'center right',
		offset: [0,10],
		effect: 'slide',
		direction: 'right',
		tipClass: 'tooltip rounded shadowed'
	}).dynamic({
		left: { direction: 'left', bounce: true }
	});

	$('.ttt[title]').tooltip({
		position: 'top center',
		offset: [0,0],
		effect: 'slide',
		direction: 'top',
		tipClass: 'tooltip rounded shadowed'
	}).dynamic({
		bottom: { direction: 'down', bounce: true }
	});

	$('#glUser').cluetip({
		local: true,
		width: 300,
		cluezIndex: 9999,
		clickThrough: true,
		//sticky: true,
		closePosition: 'title',
		closeText: '[x]',
		positionBy: 'bottomTop',
		topOffset: 30,
		leftOffset: 0,
		dropShadow: false,
		//tracking: true,
		showTitle: false
	});
});

function facebookReady() {
	$('#bttFbAdd').click(function() {
		FB.login(function(response) {
			if (response.authResponse) {
				form = document.forms['UserAddForm'];
				form.action = form.action + '?facebook';
				form.submit();
			}
		}, {scope: GL.fb.permissions});
	});
	$('#bttFbQuickAdd').click(function() {
		FB.login(function(response) {
			if (response.authResponse) {
				window.location.href=GL.fb.quickAdd;
			}
		}, {scope: GL.fb.permissions});
	});
	$('#bttFbAssociate').click(function() {
		FB.login(function(response) {
			if (response.authResponse) {
				window.location.href=GL.fb.associate;
			}
		}, {scope: GL.fb.permissions});
	});
	$('#bttFbLogin').click(function() {
		FB.login(function(response) {
			if (response.authResponse) {
				window.location.href=GL.fb.login;
			}
		}, {scope: GL.fb.permissions});
	});
	$('#bttFbDirectLogin').click(function() {
		FB.login(function(response) {
			if (response.authResponse) {
				window.location.href=GL.fb.directLogin;
			}
		}, {scope: GL.fb.permissions});
	});
}

