
/*********************************************************
 *   Copyright 2012 Giovanni D. - www.rockbelluno.it
 ********************************************************/

var open_announce_id =false;
var first_load = true;
var last_home_refresh = 0;
var refresh_timeout = false;
var auto_refresh_home_time = 1000*60; // 1 min
var auto_refresh_home_time_long = 1000*60*20; // 20 min
var auto_refresh_count = 0;
var notify_count = 0;


$(document).ready(function() {
	$('#hide_upcoming_events').click(hideUpcomingEvents);
	$('a[id^="show_announce_"]').click(toggleAnnounceInfo);

	var d = new Date();
	last_home_refresh = d.getTime() / 1000;

	$(".alert-box").delegate("a.go_to_recent_actv", "click", function(e) {
			e.preventDefault();
			goToRecentActivities();
	});
});

$(window).load(function() { // page fully loaded
	$('.new_item').effect("pulsate", {times: 2}, 800);
	first_load = false;
	setHpAutoRefreshTimer(auto_refresh_home_time);
});


$(window).focus(function() { // page fully loaded
	if (!first_load) {
		auto_refresh_count = 0;
		var d = new Date();
		var now = d.getTime() / 1000;

		if (now-last_home_refresh > 30) {
//			$('h4').effect("pulsate", {times:2}, 800);
			console.log('refresh on focus (30 sec): '+now);
			homepageContentRefresh();
		}
	}
});

// -----------------------------------------------------------------------------


function hideUpcomingEvents(e) {
	e.preventDefault();

	$('#upcoming_events').slideUp();

	var sUrl =this.href;
	$.ajax({
		url: sUrl,
		type: 'POST',
		data: csrf_data,
		dataType: 'json',
		success: function(data) {
			if (data.res) {
				//$('html,body').animate({scrollTop: $('#recent_actv').offset().top},'slow');
				setTimeout("$('#show_upcoming_events').fadeIn()", 500);
			}
		}
	});
}


function toggleAnnounceInfo(e) {
	e.preventDefault();

	id =e.target.id.substr(14); // skip the show_announce_ part

	if (open_announce_id > 0) {
		$('#box_announce_'+open_announce_id).slideUp();
	}

	if (open_announce_id == id) { // just toggle current
		open_announce_id =false;
	}
	else {
		$('#box_announce_'+id).slideDown();
		open_announce_id =id;
	}

}


function homepageContentRefresh() {
	var d = new Date();
	var now = d.getTime() / 1000;
	last_home_refresh = now;
	auto_refresh_count++;

	var refresh_time;
	if (auto_refresh_count < 20) {
		refresh_time = auto_refresh_home_time;
	}
	else {
		refresh_time = auto_refresh_home_time_long;
	}

	setHpAutoRefreshTimer(refresh_time);

	$.ajax({
		url: site_base+'/site/getHomeNew',
		type: 'POST',
		data: csrf_data+'&last_check_id='+last_action_id,
		dataType: 'json',
		success: function(data) {
			if (data.last_action) {
				$('#recent_actv_content').html(data.last_action);
			}
			if (data.last_topics) {
				$('#last_topics_content').html(data.last_topics);
			}

			if (data.new > 0) {
				notify_count = (notify_count+parseInt(data.new));
				last_action_id = data.last_check_id;

				var notify_link = '<a href="#" class="go_to_recent_actv" >';
				notify_link+=notify_count+' '+(notify_count > 1 ? 'nuove notifiche' : 'nuova notifica')+'</a>';

				$('#user_notify_content').html(notify_link);

				$('#user_notify_box').show();
				$('#user_notify_box').effect("highlight", {color: '#FFDD88'}, 3000);
				$('.new_item').effect("pulsate", {times: 2}, 800);

				// window title
				removeTitleNotifyCount();
				document.title = '('+notify_count+') '+document.title;
			}
		}
	});
}


function goToRecentActivities() {
	notify_count = 0;
	$('#user_notify_box').fadeOut();
	$('html,body').animate({scrollTop: $('#recent_actv_content').offset().top-60}, 'slow');
	removeTitleNotifyCount();
}


function removeTitleNotifyCount() {
	document.title = document.title.replace(/^\(\d+\)\s+/, '');
}


function setHpAutoRefreshTimer(refresh_time) {
	if (refresh_timeout) {
		clearTimeout(refresh_timeout);
	}
	refresh_timeout = setTimeout("runHpAutoRefreshTimer();", refresh_time);
}


function runHpAutoRefreshTimer() {
	var d = new Date();
	var now = d.getTime() / 1000;
	console.log('auto refresh: '+now);

	homepageContentRefresh();
}
