/*
 * forums.js -- Crèdits
 * Copyright 2008, Xin <xin_at_poble_dot_cat>
 *
 * @created 16/01/2008
 *
 */


/*
 * This file contain functions of work with forums
 *
 * @author Xin <xin_at_poble_dot_cat>
 * @created 16/01/2008
 *
 */


/*
 * Load and return the lasts post for each topic
 *
 * @created 16/01/2008 by Xin <xin_at_poble_dot_cat>
 */
function loadLastsPostOfTopic() {
    // Read the forum cookie and decode it
	var forum_cookie = getCookie('topic_posts');
	var topics_list = new Array();
	if ( forum_cookie )
		topics_list = forum_cookie.split('\n');
	
	var lasts_posts = new Array();
  	for ( var i = 0; i < topics_list.length; i++ ) {
		var values = topics_list[i].split(',');
		var topic_id = parseInt(values[0]);
		var last_post = parseInt(values[1]);
		var position = parseInt(values[2]);
		lasts_posts.push( new Array(topic_id,last_post,position) );
	}
    
	return lasts_posts;
}


/*
 * Set lasts post for each topic
 *
 * @created 16/01/2008 by Xin <xin_at_poble_dot_cat>
 */
function saveLastsPostOfTopic(lasts_posts, path) {
    // Mount the value
	value = "";
  	for ( var i = 0; i < lasts_posts.length; i++ ) {
	    if ( i )  value += "\n";
	    value += lasts_posts[i].join(',');
	}
    
    // Set the cookie with 30 days expire
  	expire = new Date();
  	expire.setTime( expire.getTime() + (365*24*60*60*1000) );
	setCookie('topic_posts', value, expire, path);
}


/*
 * Load and return the lasts post for each forum
 *
 * @created 17/01/2008 by Xin <xin_at_poble_dot_cat>
 */
function loadLastsPostOfForum() {
    // Read the forum cookie and decode it
	var group_cookie = getCookie('forum_posts');
	var forum_list = new Array();
	if ( group_cookie )
		forum_list = group_cookie.split('\n');
	
	var lasts_posts = new Array();
  	for ( var i = 0; i < forum_list.length; i++ ) {
		var values = forum_list[i].split(',');
		var forum_id = parseInt(values[0]);
		var last_post = parseInt(values[1]);
		lasts_posts.push( new Array(forum_id,last_post) );
	}
    
	return lasts_posts;
}


/*
 * Set lasts post for each forum
 *
 * @created 17/01/2008 by Xin <xin_at_poble_dot_cat>
 */
function saveLastsPostOfForum(lasts_posts, path) {
    // Mount the value
	value = "";
  	for ( var i = 0; i < lasts_posts.length; i++ ) {
	    if ( i )  value += "\n";
	    value += lasts_posts[i].join(',');
	}
    
    // Set the cookie with 30 days expire
  	expire = new Date();
  	expire.setTime( expire.getTime() + (365*24*60*60*1000) );
	setCookie('forum_posts', value, expire, path);
}


/*
 * Set the new last post of topic at first position
 *
 * @created 16/01/2008 by Xin <xin_at_poble_dot_cat>
 */
function setLastPostOfTopic(lasts_posts, topic, post, position) {
    // Make the last post
    var last_post = new Array(topic,post,position);
    
    // Init array lasts posts
    var new_lastsposts = new Array(last_post);
  	for ( var i = 0; i < lasts_posts.length && new_lastsposts.length < 100; i++ ) {
  	    if ( lasts_posts[i][0] != topic ) {
            new_lastsposts.push(lasts_posts[i]);
  	    }
        else {
            if ( lasts_posts[i][1] > post ) {
                new_lastsposts[0][1] = lasts_posts[i][1];
                new_lastsposts[0][2] = lasts_posts[i][2];
            }
        }
	}
    
    return new_lastsposts;
}


/*
 * Set the new last post of topic
 *
 * @created 16/01/2008 by Xin <xin_at_poble_dot_cat>
 */
function getLastPostOfTopic(lasts_posts, topic) {
  	for ( var i = 0; i < lasts_posts.length; i++ ) {
  	    if ( lasts_posts[i][0] == topic ) {
  	        return lasts_posts[i];
  	    }
	}
	
	return new Array(topic,0,0);
}


/*
 * Set the new last post of topic at first position
 *
 * @created 17/01/2008 by Xin <xin_at_poble_dot_cat>
 */
function setLastPostOfForum(lasts_posts, forum, post) {
    // Make the last post
    var last_post = new Array(forum,post);
    
    // Init array lasts posts
    var new_lastsposts = new Array(last_post);
  	for ( var i = 0; i < lasts_posts.length; i++ ) {
  	    if ( lasts_posts[i][0] != forum ) {
            new_lastsposts.push(lasts_posts[i]);
  	    }
        else {
            if ( lasts_posts[i][1] > post ) {
                new_lastsposts[0][1] = lasts_posts[i][1];
            }
        }
	}
    
    return new_lastsposts;
}


/*
 * Set the new last post of forum
 *
 * @created 17/01/2008 by Xin <xin_at_poble_dot_cat>
 */
function getLastPostOfForum(lasts_posts, forum) {
  	for ( var i = 0; i < lasts_posts.length; i++ ) {
  	    if ( lasts_posts[i][0] == forum ) {
  	        return lasts_posts[i];
  	    }
	}
	
	return new Array(forum,0,0);
}


/*
 * Set the link in newer links
 *
 * @created 16/01/2008 by Xin <xin_at_poble_dot_cat>
 */
function setNewerTopicLinks(topic_list, newer, postsPage) {
  	for ( var i = 0; i < topic_list.length; i++ ) {
  	    // Trobem els id_s dels enllaços
  	    var new_id = 'new_' + topic_list[i][0];
  	    var old_id = 'old_' + topic_list[i][0];
  	    
  	    // Obtenim les dades del tema
  	    var newer_post = getLastPostOfTopic(newer,topic_list[i][0])
        
        // Canviem els enllaços
		var page = Math.floor((newer_post[2] ) / postsPage) + 1;
		var new_url = $(new_id).href;
		if ( page > 1 ) 
		    new_url += page + "/";
		if ( newer_post[2] > 0 )
            new_url += "#id_" + newer_post[1];
        $(new_id).href = new_url;
    }
}


/*
 * Display the icon of new posts or no
 *
 * @created 16/01/2008 by Xin <xin_at_poble_dot_cat>
 */
function showNewerTopicPosts(topic_list, newer) {
  	for ( var i = 0; i < topic_list.length; i++ ) {
  	    new_id = 'new_' + topic_list[i][0];
  	    old_id = 'old_' + topic_list[i][0];
  	    newer_post = getLastPostOfTopic(newer,topic_list[i][0])
        
        // Mostrem els enllaços adecuats
  	    if ( newer_post[1] < topic_list[i][1] )
  	        $(new_id).style.display = "inline";
  	    else
  	        $(old_id).style.display = "inline";
    }
}


/*
 * Display the icon of new forums or no
 *
 * @created 17/01/2008 by Xin <xin_at_poble_dot_cat>
 */
function showNewerForumPosts(forum_list, newer) {
  	for ( var i = 0; i < forum_list.length; i++ ) {
  	    new_id = 'new_' + forum_list[i][0];
  	    old_id = 'old_' + forum_list[i][0];
  	    newer_post = getLastPostOfForum(newer,forum_list[i][0])
        
        // Mostrem els enllaços adecuats
  	    if ( newer_post[1] < forum_list[i][1] )
  	        $(new_id).style.display = "inline";
  	    else
  	        $(old_id).style.display = "inline";
    }
}

