var itemsPP = 10;
var sectionShown = 0;
var yearShown = 0;
var years = new Array();
var listElement = 'div';
var cookieName = 'news_room';

function countItems(year) {
	return year && document.getElementById(year) ? document.getElementById(year).getElementsByTagName(listElement).length : 0;
}
function numberOfSections(year) {
	return year?Math.ceil(countItems(year)/itemsPP):0;
}
function show(year, section) {
	if(section >= numberOfSections(year)) section = numberOfSections(year)-1;
	if(section < 0) section = 0;
   	for(var i = 0; i < years.length; i++) {
  		if(years[i] == year) {
			document.getElementById("ysa"+years[i]).className = "selected";
			document.getElementById(years[i]).style.display = "block";
			document.getElementById("fs"+years[i]).style.display = "block";
		} else {
			document.getElementById("ysa"+years[i]).className = "standardLink1";
			document.getElementById(years[i]).style.display = "none";
			document.getElementById("fs"+years[i]).style.display = "none";
		}
	}
	var arr = document.getElementById(year).getElementsByTagName(listElement);

	for(var i = 0; i < arr.length; i++) {
   		if(i >= itemsPP*section && i < itemsPP*(section+1)) {
			arr[i].style.display = "block";
   		} else {
			arr[i].style.display = "none";
		}
   	}
	if(numberOfSections(year) > 1) {
		for(var i = 0; i < numberOfSections(year); i++) {
  			if(i == section) {
				document.getElementById("ss"+year+"_"+i).className = "selected";
			} else {
				document.getElementById("ss"+year+"_"+i).className = "standardLink1";
			}
		}
	}
	sectionShown = section;
	yearShown = year;
	setCookie(cookieName, year+"_"+section);
 }
function createFooter(year) {
	var html = "";
	if(numberOfSections(year) > 1) {
		for(var i = 0; i < numberOfSections(year); i++) {
			html += "<a id='ss"+year+"_"+i+"' href='#' onclick='show(\""+year+"\", \""+i+"\");'>"+(i+1)+"</a> &#160; ";
		}
	} else {
		document.getElementById("fss"+year).style.display = "none";	
	}
	return html;
}
function moveForward() {
	show(yearShown, sectionShown+1);	
}
function moveBack() {
	show(yearShown, sectionShown-1);
}
function initialize() {
	for(var i = 0; i < years.length; i++) {
  		if(countItems(years[i]) == 0) {
			document.getElementById("ys"+years[i]).style.display = "none";	
			for(var j = i+1; j < years.length; j++) {
				years[j-1] = years[j];
			}
			years.length--;
			i--;
		} 
	}
	cookieValue = getCookie(cookieName);
	if (cookieValue != null ){
		arrSegments = cookieValue.split('_');
		show(arrSegments[0], arrSegments[1]);
	} else {
		show(years[0], 0);
	}
}


/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure)
{
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
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";
    }
}
