// Copyright (c) 1996-1997 Athenia Associates.
// http://www.webreference.com/js/
// License is granted if and only if this entire
// copyright notice is included. By Tomer Shiran.

function setCookie (name, value, expires, path, domain, secure) {
	var curCookie = name + "=" + escape(value) + (expires ? "; expires=" + expires : "") +
		(path ? "; path=" + path : "") + (domain ? "; domain=" + domain : "") + (secure ? "secure" : "");
	document.cookie = curCookie;
}

function getCookie (name) {
	var prefix = name + '=';
	var c = document.cookie;
	var nullstring = '';
	var cookieStartIndex = c.indexOf(prefix);
	if (cookieStartIndex == -1)
		return nullstring;
	var cookieEndIndex = c.indexOf(";", cookieStartIndex + prefix.length);
	if (cookieEndIndex == -1)
		cookieEndIndex = c.length;
	return unescape(c.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

function deleteCookie (name, path, domain) {
	if (getCookie(name))
		document.cookie = name + "=" + ((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

function fixDate (date) {
	var base = new Date(0);
	var skew = base.getTime();
	if (skew > 0)
		date.setTime(date.getTime() - skew);
}

function rememberMe (f) {
	var now = new Date();
	fixDate(now);
	now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
	now = now.toGMTString();
	if (f.author != undefined)
		setCookie('mtcmtauth', f.author.value, now, '/', '', '');
	if (f.email != undefined)
		setCookie('mtcmtmail', f.email.value, now, '/', '', '');
	if (f.url != undefined)
		setCookie('mtcmthome', f.url.value, now, '/', '', '');
}

function forgetMe (f) {
	deleteCookie('mtcmtmail', '/', '');
	deleteCookie('mtcmthome', '/', '');
	deleteCookie('mtcmtauth', '/', '');
	f.email.value = '';
	f.author.value = '';
	f.url.value = '';
}

function hideDocumentElement(id) {
	var el = document.getElementById(id);
	if (el) el.style.display = 'none';
}

function showDocumentElement(id) {
	var el = document.getElementById(id);
	if (el) el.style.display = 'block';
}

var commenter_name;

function individualArchivesOnLoad(commenter_name) {




	if (document.comments_form) {
		if (document.comments_form.email != undefined &&
			(mtcmtmail = getCookie("mtcmtmail")))
			document.comments_form.email.value = mtcmtmail;
		if (document.comments_form.author != undefined &&
			(mtcmtauth = getCookie("mtcmtauth")))
			document.comments_form.author.value = mtcmtauth;
		if (document.comments_form.url != undefined && 
			(mtcmthome = getCookie("mtcmthome")))
			document.comments_form.url.value = mtcmthome;
		if (mtcmtauth || mtcmthome) {
			document.comments_form.bakecookie.checked = true;
		} else {
			document.comments_form.bakecookie.checked = false;
		}
	}
}

function writeTypeKeyGreeting(commenter_name, entry_id) {

}



/* Blogosfere */

/* Begin TagList */
var TAGS_DATA = "acquisizioni|1,ad maiora|1,advertising|5,agenzie|6,annunci|1,Assocomunicazione|1,awards|1,blog|1,comunicati|2,convegno|4,documenti|4,giornalismo|1,iab|24,IAB|8,idee|2,informazione|2,intervento|5,intervista|11,Interviste|5,live|12,Live|1,marketing|3,motori|3,novità|1,podcast|1,USA|2,video|4".split(',');
for (var i=0; i<TAGS_DATA.length; i++) TAGS_DATA[i] = TAGS_DATA[i].split('|');

function min(a, b) {
	return a < b ? a : b;
}

function fill_taglist(limit_par, sort_by) {
	if (!document.getElementById) return;
	
	var list = document.getElementById('taglist');
	list.innerHTML = '';
	
	var TAG_NUMBER = TAGS_DATA.length;
	
	if ((TAG_NUMBER == 0) || ((TAG_NUMBER == 1) && (TAGS_DATA[0] == ''))) {
		document.getElementById('tagheader').style.display = 'None';
		return;
	}
	
	if (sort_by == 'alpha') {
		var sort_func = function (a, b) {
			return (a[0].toLowerCase() < b[0].toLowerCase()) ? -1 : 1;
		}
	} else if (sort_by == 'post') {
		var sort_func = function (a, b) {
			var compared = b[1] - a[1];

			if (compared)
				return compared;
			else
				return (a[0].toLowerCase() < b[0].toLowerCase()) ? -1 : 1;
		}
	} else {
		alert('Wrong sort_by value');
		return;
	}

	TAGS_DATA.sort(sort_func);
	
	var limit = (limit_par <= 0) ? TAG_NUMBER : min(limit_par, TAG_NUMBER);
	
	for(var i=0; i<limit; i++) {
		var curData = TAGS_DATA[i];
		
		var newLink = document.createElement('a');
		newLink.appendChild(document.createTextNode(curData[0] + ' (' + curData[1] + ')'));
		newLink.setAttribute('href', '/tag/' + curData[0]);
		
		var newNode = document.createElement('li');
		newNode.appendChild(newLink);
		list.appendChild(newNode);
	}
	
	if (TAG_NUMBER>1) {
		var newNode = document.createElement('li');
		var alpha_link = (sort_by=='alpha') ? '<strong><u><span title="Ordina alfabeticamente">A-Z</span></u></strong>' : '<a title="Ordina alfabeticamente" href="javascript:fill_taglist(' + limit_par + ', \'alpha\');">A-Z</a>';
		var post_link = (sort_by=='post') ? '<strong><u><span title="Ordina per numero di post"># post</span></u></strong>' : '<a title="Ordina per numero di post" href="javascript:fill_taglist(' + limit_par + ', \'post\');"># post</a>';
		
		newNode.innerHTML = '<br /><em>Ordina:</em> ' + alpha_link + ' | ' + post_link;
		list.appendChild(newNode);
	}
	
	if (limit<TAG_NUMBER) {
		var newLink = document.createElement('a');
		var newStrong = document.createElement('em');
		newStrong.appendChild(document.createTextNode('Altri tag >'));
		
		newLink.appendChild(newStrong);
		newLink.setAttribute('href', '#tagheader');
		newLink.onclick = function () {fill_taglist(0, sort_by);};
		
		var newNode = document.createElement('li');
		newNode.appendChild(newLink);
		list.appendChild(newNode);
	} else if (limit==TAG_NUMBER && TAG_NUMBER>limit_par) {
		var newLink = document.createElement('a');
		var newStrong = document.createElement('em');
		newStrong.appendChild(document.createTextNode('< Nascondi tag'));
		
		newLink.appendChild(newStrong);
		newLink.setAttribute('href', '#tagheader');
		newLink.onclick = function () {fill_taglist(15, sort_by);};
		
		var newNode = document.createElement('li');
		newNode.appendChild(newLink);
		list.appendChild(newNode);
	}
}
/* End TagList */

function final_js() {
	make_popup_images();
}

/* AD functions */
function fill_ads() {
	make_popup_images();

	return 0;	// 2008-07-16
}

function show_ads() {
	return;		// 2008-07-16
}
/* End AD functions */

/* Begin Announcements */
var ANNOUNCEMENTS = new Array (
	'Segui con interesse la pallavolo?<br />Scrivici a <a href="mailto:bloggers@blogosfere.it">bloggers@blogosfere.it</a>',
	'Sei appassionato del mondo Apple?<br />Scrivici a <a href="mailto:bloggers@blogosfere.it">bloggers@blogosfere.it</a>',
	'Cani e gatti sono la tua passione?<br />Scrivici a <a href="mailto:bloggers@blogosfere.it">bloggers@blogosfere.it</a>',
	'Il fitness e la cura del corpo sono chiodi fissi?<br />Scrivici a <a href="mailto:bloggers@blogosfere.it">bloggers@blogosfere.it</a>'
);

function get_random(upper) {
    return Math.floor(Math.random() * upper);
}

function write_topper(topper) {
	return;	// Added on 2008-03-27
}

function write_announcement() {
	if (ANNOUNCEMENTS.length == 0) return;

	document.write('<div class="ann">' + ANNOUNCEMENTS[get_random(ANNOUNCEMENTS.length)] + '</div>');
}
/* End Announcements */

/* Begin Comments helpers */
var DAYS_TIMESTAMPS = new Array('Oggi', 'Ieri', '2 giorni fa', '3 giorni fa', '4 giorni fa', '5 giorni fa', '6 giorni fa');
var ONE_DAY = 86400000;

function strtotime(timestamp) {
	// Accepted format: yyyy-mm-dd hh:mm
	var a = timestamp.substring(0, 10).split('-');
	var b = timestamp.substring(11, 16).split(':');

	return new Date(parseInt(a[0], 10), parseInt(a[1], 10)-1, parseInt(a[2], 10), parseInt(b[0], 10), parseInt(b[1], 10), 0);
}

function pad_time(n) {
	return (n < 10) ? '0' + n : '' + n;
}

function setCommentTimestamp(comment) {
	var timestamps = comment.getElementsByTagName('span');
	if (!timestamps) return true;
	var timestamp = (timestamps) ? strtotime(timestamps[0].title) : '';

	if (timestamp) {
		var now = new Date();
		var comment_hour = timestamp.getHours();
		var comment_minute = timestamp.getMinutes();

		now.setHours(comment_hour);
		now.setMinutes(comment_minute);
		now.setSeconds(0);

		var time_diff = (now - timestamp);
		
		if (time_diff < ONE_DAY*7) {
			timestamps[0].innerHTML = DAYS_TIMESTAMPS[parseInt(time_diff / ONE_DAY)] + ', ore ' + pad_time(comment_hour) + ':' + pad_time(comment_minute);
			return true;
		}
	}

	return false;
}

function mark_author_comments() {
	/*
		Each comment is
		<div class="comment" id="comment-COMMENT_ID">
			<div class="entryTop">
				<h3><a href="#comment-COMMENT_ID">COMMENT_NUM</a>. <a href="COMMENTER_URL">COMMENTER_NAME</a></h3>
			</div>
			[...]
		</div>
	*/
	var comments = $('comments').getElementsByClassName('comment');
	var current_domain = 'http://' + window.location.host;
	var CUR_DOMAIN_LENGTH = current_domain.length;
	var continue_format_timestamp = true;
	var h3s, h3_links;

	for (var i=comments.length-1; i>=0; i--) {
		h3s = comments[i].getElementsByTagName('h3');

		if (continue_format_timestamp)
			continue_format_timestamp = setCommentTimestamp(h3s[0]);
		
		h3_links = h3s[0].getElementsByTagName('a');

		if (h3_links.length > 1) {
			if (current_domain == h3_links[1].href.toLowerCase().substring(0, CUR_DOMAIN_LENGTH)) {
				comments[i].className += ' comment-author';
			}
		}
	}
}

function block_comments() {
	document.getElementById('comments-wait-msg').style.display = 'block';
	document.getElementById('comment-post').disabled = true;
	return true;
}

function enable_comments_form() {
	if (get_random(2) == 0)
		document.comments_form.action = 'http://blogosfere01.blogosfere' + '.it/mte/mt-comments-0.cgi';
	else
		document.comments_form.action = 'http://blogosfere03.blogosfere' + '.it/mte/mt-comments-0.cgi';

// 	document.comments_form.action = 'http://blogosfere01.blogosfere' + '.it/mte/mt-comments-0.cgi';
}

function start_wysiwyg_editor_0() {
	tinyMCE_GZ.init({
		mode : "textareas",
		theme : "advanced",
		language : "it",
		plugins : "",
		theme_advanced_buttons1 : "link,unlink,separator,bold,italic,underline,separator,bullist,numlist,separator,undo,redo,separator",
		theme_advanced_buttons2 : "",
		theme_advanced_buttons3 : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_path_location : "bottom",
		theme_advanced_resizing : true,
		theme_advanced_resize_horizontal : false,
		safari_warning: false,
		content_css : "http://shared.blogosfere.it/css/tinymce.css"
	});
}

function start_wysiwyg_editor_1() {
	tinyMCE.init({
		mode : "textareas",
		theme : "advanced",
		language : "it",
		plugins : "",
		theme_advanced_buttons1 : "link,unlink,separator,bold,italic,underline,separator,bullist,numlist,separator,undo,redo,separator",
		theme_advanced_buttons2 : "",
		theme_advanced_buttons3 : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_path_location : "bottom",
		theme_advanced_resizing : true,
		theme_advanced_resize_horizontal : false,
		safari_warning: false,
		content_css : "http://shared.blogosfere.it/css/tinymce.css"
	});
}
/* End Comments helpers */