var acs = new Array();
var achs = new Array();
var acsts = new Array();
var maxh;
var caps = new Array();
var captxts = new Array();
var olds = new Array();
var actuali;
var FF2 = navigator.userAgent.indexOf('Firefox/2') != -1 ? true : false;
var oldff2max = 0;
var never_open = true;

var acplus = '<div style="margin:3px 5px 3px 0;overflow:hidden;float:left;width:9px;height:9px;background:url(/global/images/plus.gif) no-repeat -9px 0;"></div> '
var acminus = '<div style="margin:3px 5px 3px 0;overflow:hidden;float:left;width:9px;height:9px;background:url(/global/images/plus.gif) no-repeat 0 0;"></div> '

Array.prototype.max = function(){
	return Math.max.apply({},this)
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function FixFF2(){
	var max = olds ? olds.max() : 0;
	max = max > actuali ? max : actuali;

	if (oldff2max == max)
		return;

	var i;
	var len = acs.length;

	for (i = max; i >= 0; i--)
		caps[i].style.position = 'static';
	for (i = len - 1; i > max; i--){
		caps[i].style.position = 'absolute';
		caps[i].style.top = caps[i].offsetTop + 'px';
	}
	oldff2max = max;

}

var h = 30;
var newHeight;
var ac, aci;
var repeat = 0;

function ResizeTab(){
	repeat = 0;

	if (FF2)
		FixFF2();

	for (var i = 0, len = olds.length; i < len; i++){
		aci = olds[i];
		ac = acs[aci];
		if (!ac)
			continue;
		newHeight = achs[aci] -= h / len;
	        if (newHeight > 0){
			acsts[aci].height = newHeight + 'px';
			repeat = 1;
		} else {
			acsts[aci].display = 'none';
			achs[aci] = 0;
	  		olds.splice(i, 1);
			caps[aci].innerHTML = acplus + captxts[aci]
	       	}
	}

	ac = acs[actuali];
	if (ac.style.height != maxh){
	    	newHeight = achs[actuali] += h;
	       	if (newHeight <= maxh){
			acsts[actuali].height = newHeight + 'px';
			repeat = 1;
		} else {
			acsts[actuali].height = maxh + 'px';
			achs[actuali] = maxh;
			caps[actuali].innerHTML = acminus + captxts[actuali]
		}
	}

	if (repeat)
		setTimeout(ResizeTab, 30);
}

function ListTab(cap){
	for (var i = 0, len = acs.length; i < len; i++)
		if (caps[i] == cap){
			if (actuali == i)
				return;
			else {
				if (never_open){
					never_open = false;
                	for (var j = 0, len = acs.length; j < len; j++)
						if (caps[j] != actuali){
							caps[j].innerHTML = acplus + captxts[j]
						}
						else
							caps[j].innerHTML = acminus + captxts[j]
					}
				acsts[i].height = 0;
				acsts[i].display = 'block';
				olds.push(actuali);
				actuali = i;
				createCookie('NewsTab', actuali, 90);
			}
		break;
		}
	ResizeTab();
}

function OpenTab(cap, seeall){
	var j = 0;
	for (var i = 0, len = acs.length; i < len; i++)
		if (caps[i] != cap){
			acs[i].style.display = 'none';
			achs[i] = 0;
			caps[i].innerHTML = acplus + captxts[i] + ' ' + seeall
		}
		else
			j = i;
	acs[j].style.height = maxh + 'px';
	achs[j] = maxh;
	caps[j].innerHTML = acminus + captxts[j]
}

function InitAcord(acClass, mainId, seeall){
	var caps_height = 0;
	var main = document.getElementById(mainId);
	var oldstyle = main.style.overflow;
	main.style.overflow = 'hidden';
	var objs = document.getElementsByTagName('div');
	for (var i = 0, len = objs.length; i < len; i++)
		if (objs[i].className == acClass){
			var ac = objs[i];
			acs.push(ac);
			achs.push(ac.offsetHeight); //height dupa incarcarea totala a paginii difera putin
			acsts.push(ac.style);
		    	ac.style.overflow = 'hidden';
			var cap = (ac.previousSibling.nodeType == 1 ? ac.previousSibling : ac.previousSibling.previousSibling);
			caps.push(cap);
			caps_height += cap.offsetHeight;
			captxts.push(cap.innerHTML)
			cap.style.cursor = 'pointer';
			cap.onmouseover = function(){this.style.backgroundColor = '#DEF3F3'}
			cap.onmouseout = function(){this.style.backgroundColor = '#EDF8FA'}
			if (FF2)
				cap.style.width = cap.offsetWidth + 7 + 'px'; // FF2 fix ! + ^^^
			cap.onclick = function(){ListTab(this)};
		}
	main.style.overflow = oldstyle;

	maxh = achs.max() + 5;
	main.style.height = maxh + caps_height + 'px';
	main.style.overflow = 'hidden';

	actuali = readCookie('NewsTab') || 0;
	OpenTab(caps[actuali], seeall);
}

//window.onload = function() {InitAcord("acord", "newsCol");}

