//!current_user.nil?
var loggedIn;

$(function() {
	/* tofix: deprecated login page init */ loginPage();
	
	loggedIn = $('#logged-in').attr('value') == 'true' ? true : false;
	var $flash = $("#flash"); if ($flash.html() != "") $.noticeAdd({text:$flash.html(), stay: true}); 
	
	var $n = $('#select_namespace'); $n.change(function(){var target = $n.attr('value'); window.location.replace(target);});
	
	var dir = {n: '.north_tip', s: '.south_tip', w: '.west_tip', e: '.east_tip'};	 
	for (d in dir) $(dir[d]).tipsy({fade: true, gravity: d});
	
	//default global tips	
	$('.popupContactClose').tipsy({fade: true, gravity: 's'});	
	
	//rollover
	$('#slideshow').cycle();	

	//cap result
	$(".ajax_cap").keyup(ajax_cap);
	
	//make the radiobutton stars
	$('input.stars').rating();
	
	//fix for smart_link_to helper
	$('#navigation-container .menu a.selected').parents('.menu').addClass('selected');	
	var $menu = $('#navigation-container .menu a.selected');
	if ($menu.length == 0) $($('#navigation-container .menu')[0]).addClass('selected');
	
	$('.user-menu a.selected').parents('.user-menu').addClass('selected');	
	
	//submission links
	$('.form').find('#submit').click(function(e) { 
		$(this).parents('.form').submit(); 
		e.preventDefault();
		return false;
	});
	
	//logo redirect
	$('#logo').click(function(e) { window.location.replace("http://www.annuncifree.org"); });
	
	$('#facebook').click(function(e) { window.open("http://www.facebook.com/pages/AnnunciFree/146425248273"); });
	$('#twitter').click(function(e)  { window.open("http://www.twitter.com/mytable_it"); });
	
	//province
	$('#left-selected-province strong, #left-current-province .select-province').click(function(){
		if ($('#left-province-selector').is(":visible"))
			$('#left-province-selector').slideUp(1000);
		else
			$('#left-province-selector').slideDown(1000);
	});

	//faq
	$('.faq').toggle(function(e) { $(this).find('div').slideDown('slow');}, function(e) { $(this).find('div').slideUp('slow');} );
	
	if ($('#flash-popup-container').html() != "") $('#flash-popup-link').smoothPopup({useAjax:false, showCloseButton:true});
	if ($('#flash-popup-success .reservation-created-popup').html() != "") $('#flash-popup-success-link').smoothPopup({useAjax:false, showCloseButton:true});
	
	//login
	$("[data-popup='login_popup']").click(function(e) { $(this).smoothPopup({ useAjax:true }); e.preventDefault(); });
});


var ajax_cap = function(e) {
	var $result = $("#"+this.getAttribute('data-result')); 
	
	$result.html('').haml(['%img', {src: '/images/ajax-loader.gif', style: 'padding-bottom:7px'}]);
	text = this.value;
			
	if (text.length == 5) {
		jQuery.getJSON("/cities/cap/"+text, function(data) {
			if (data == null) $result.html('').haml(['%h5', 'Nessuna località trovata.']);
			else 
				if (data.city.city == '')
					$result.html('').haml(['%h5', data.city.province]);
				else
					$result.html('').haml(['%h5', data.city.city + ', ' + data.city.province]);				
		});
	}
}

//deprecated initialization for the login page
function loginPage() {
	var lgn = 'E-mail'
	var pwd = 'Password'
	$('#user_session_email').click(function(e){ if (this.value == lgn) this.value = ''; }); 
	$('#user_session_password').click(function(e){ if (this.value == pwd) this.value = ''; });
}


