// JavaScript Document

var box_mutex = false;
var tab_box = new Array();
var posCurrent;
var IdTab;

function Random(limit) {
    today = new Date;
    num = today.getTime();
    num = Math.round(Math.abs(Math.sin(num) * 1000000)) % limit;
    return num;
}

function box_hide_content(DivContent) {
	DivContent.style.display = 'none';
}

function box_show_content(DivContent) {
    DivContent.style.display = 'block';
}

function construct_tab(ContainerID) {	
	var ContainerObject = document.getElementById(ContainerID);		
    if (ContainerObject) {
        var lListChild = ContainerObject.childNodes;
		var j=0;
        for (var i = 0; i < lListChild.length; i++)
		{     
			if (lListChild[i].tagName == 'DIV' && lListChild[i].parentNode.getAttribute('id') == ContainerObject.id) 
			{
				tab_box[j]= lListChild[i];        
				j++;
			}                        			
        }
    }
}

function box_closeAll(ContainerID) {
    if (!box_mutex) {
        box_mutex = true;
        var ContainerObject = document.getElementById(ContainerID);		
        if (ContainerObject) {
            for (var i = 0; i < tab_box.length; i++)
			{   
				if(i != posCurrent-1)
				{
					box_hide_content(tab_box[i]);
				}
            }
        }
        box_mutex = false;
		tab_box[posCurrent-1].style.position = 'absolute';
		tab_box[posCurrent-1].style.zIndex = 0;
    }
}

function box_RandomOpen(ContainerID) {	
	if(tab_box=='')
    {
		construct_tab(ContainerID);
		
		posCurrent = Random(tab_box.length) + 1;
		IdTab = -1;
		box_closeAll(ContainerID);
		box_show_content(tab_box[posCurrent-1]);		
    }
		
	if(posCurrent != IdTab && IdTab != -1)
	{
		box_closeAll(ContainerID);
		var ContainerObject = document.getElementById(ContainerID);
		if (ContainerObject) {       
			
			tab_box[IdTab-1].style.height = 0;
			tab_box[IdTab-1].style.opacity = 0;
			tab_box[IdTab-1].style.overflowY ='hidden';
			tab_box[IdTab-1].style.position ='absolute';
			tab_box[IdTab-1].style.zIndex = 99;
			box_show_content(tab_box[IdTab-1]);
			
			box_sliding_effect(tab_box[IdTab-1],130,3000);
			posCurrent = IdTab;
		}
	}
	
	while(posCurrent == IdTab || IdTab == -1)
	{
		IdTab = Random(tab_box.length) + 1;	
	}
	
	setTimeout("box_RandomOpen(\"" + ContainerID + "\")", 15000);
} 


function box_sliding_effect(el, newHeight, time) {
    if (el == null) {
        return;
    }
    var cHeight;
    if (el.style.height == "") {
        cHeight = 0;
    } else {
        cHeight = parseInt(el.style.height);
    }
    var totalFrames = 1;
    if (time > 0) {
        totalFrames = time / 20;
    }
    var fHeight = newHeight - cHeight;
    if (fHeight != 0) {
        fHeight /= totalFrames;
    }
    doFrame(el.id, cHeight, newHeight, fHeight);
}

function doFrame(eID, cHeight, nHeight, fHeight) {
    var el = document.getElementById(eID);
    if (el == null) {
        return;
    }
    if (cHeight != null) {
        cHeight = moveSingleVal(cHeight, nHeight, fHeight);
        el.style.height = Math.round(cHeight) + "px";
    }
    el.style.opacity = cHeight / nHeight;
    el.style.filter = "alpha(opacity=" + Math.round(cHeight / nHeight * 100) + ")";
    if (cHeight == nHeight) {
        el.style.height = "";
        vertical_tabList_mutex = false;
        return;
    }
    setTimeout("doFrame(\"" + eID + "\"," + cHeight + "," + nHeight + "," + fHeight + ")", 10);
}

function moveSingleVal(currentVal, finalVal, frameAmt) {
    if (frameAmt == 0 || currentVal == finalVal) {
        return finalVal;
    }
    currentVal += frameAmt;
    if (frameAmt > 0 && currentVal >= finalVal ||
        frameAmt < 0 && currentVal <= finalVal) {
        return finalVal;
    }
    return currentVal;
}