/* var cat, col_width_margin, home_posts_col1, home_max_posts
defined in index.php */

Event.observe(window, 'load', set_page);
var num_posts_loaded;

function get_window_size() { //IE does not implement window.innerWidth;
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return [ myWidth, myHeight ];
}

function find_num_col(p_colwidth) {
	wid=get_window_size()[0];
	threshold=5*p_colwidth+60;
	if(wid<threshold)
		m_numcol = 4;
	else
		m_numcol = 5+Math.floor((wid-threshold) / p_colwidth);
	return m_numcol;
}

function create_cols(p_numcol) {
	if(!home_posts_col1) $$('.singlecol')[0].insert('&nbsp;');
	page = $('wpage');
	if(p_numcol>4)
		cols_to_create=p_numcol-4;
	else
		cols_to_create=0;
	for(i=0; i<cols_to_create; i++) {
		col=document.createElement('div');
		col.className='singlecol';
		page.appendChild(col);
	}
}

function set_page() {
	num_posts_loaded=0;
	num_col=find_num_col(col_width_margin); //col_width_margin from index.php
	create_cols(num_col);
	load_post(num_col,cat); //cat from index.php
}

function find_shortest_col(p_numcol) {
	start=home_posts_col1 ? 0 : 1;
	dim_shortest_col=$$('.singlecol')[start].getHeight();
	index_shortest_col=start;
	for(i=start+1;i<p_numcol;i++) {
		dim_col=$$('.singlecol')[i].getHeight();
		if(dim_col<dim_shortest_col) {
			dim_shortest_col=dim_col;
			index_shortest_col=i;
		}
	}
	return index_shortest_col;
}

function load_post(p_numcol,p_cat) {
	new Ajax.Request('fetch.php', {
		method:'get',
		parameters: {cat:p_cat,index:num_posts_loaded},
		requestHeaders: {Accept: 'application/json'},
		onSuccess: function(transport){
			post=transport.responseText.evalJSON(true);
			if(post!=0) {
				col_nr=find_shortest_col(p_numcol);
				write_post(post,col_nr,p_cat);
				num_posts_loaded++;
				if(p_cat || num_posts_loaded<home_max_posts || home_max_posts<1)
					load_post(p_numcol,p_cat);
			} else if(num_posts_loaded==0) no_posts(p_cat);
		}
	});
}

function write_post(p_post,p_col,p_cat) {
	singlepost=document.createElement('div');
	singlepost.className='removable singlepost rbox';
	abstract=p_post.abstract.replace(/\r/g,'\n').replace(/\n\n/g,'\n').replace(/\n/g,'<br />'); /*abilita 'a capo'*/
	text_home=p_post.text_home.replace(/\r/g,'\n').replace(/\n\n/g,'\n').replace(/\n/g,'<br />'); /*abilita 'a capo'*/
	postcontent=
		'<a href="post.php?post='+p_post.id+'"><img src="'+p_post.thumb_url+'" alt="'+p_post.title+'" title="'+p_post.title+'" /></a>'+
		'<h3><a href="post.php?post='+p_post.id+'">'+p_post.title+'</a></h3>'+
		'<p class="italictext">'+abstract+'</p>'+
		'<p>'+text_home+' <a href="post.php?post='+p_post.id+'">[...]</a></p>'+
		'<p class="small">';
	if(p_post.footnote!='') postcontent+=p_post.footnote+'<br />';
	postcontent+=
		'Categorie &mdash; ';
	for(i=0; i<p_post.catlist.length; i++) {
		catid=p_post.catlist[i].id;
		catname=p_post.catlist[i].name;
		if(i!=0) postcontent+=', ';
		postcontent+='<a href="index.php?cat='+catid+'">'+catname+'</a>';
	}
	postcontent+='</p>';
	singlepost.innerHTML=postcontent;
	$$('.singlecol')[p_col].appendChild(singlepost);
}

function no_posts(p_cat) {
	singlepost=document.createElement('div');
	singlepost.className='singlepost rbox';
	singlepost.innerHTML='Non ci sono post';
	if(p_cat) singlepost.innerHTML+=' in questa categoria';
	singlepost.innerHTML+='.';
	$$('.singlecol')[1].appendChild(singlepost);
}