function copyFunction(func, arg)
{
    return function()
    {
	func(arg);
    }
}


/**
 * A Chart betolteset es frissiteset kezelo JavaScript osztaly.
 * @version $Id$
 */
addLoadEvent(ChartImageInit);

var chartList=new Array();
function ChartImageInit() {
    chartList=new Array();
    var divL=document.getElementsByTagName("DIV");
    for( d=0; d<divL.length; d++ ) {
	if( divL[d].className=="ChartImg" ) {
	    chartList[d] = new ChartDiagrammManager(divL[d],d);
	}
    }
}

/**
 * A kepek frissiteset kezelo osztaly.
 * - Ez kezeli a chart kepek frissiteset, ezzel az osztallyal kell inicializalni.
 * - A kep tartalmazza a frissitesi ratat, ennyi idonkent tolti le ujra a kepet.
 * - Hogy ne a bongeszo cache-bol adja vissza a kepet, ezert mindig hozzafuz egy
 *   uj parametert.
 * - A kepet eloszor preLoad-olja, igy ha a kep nem all rendelkezesre, nem tunik 
 *   el a kep, hanem ujratolti az egesz oldalt, igy ha portal idokozben ujraindul, 
 *   es megvaltoznak a chartIndex-ek, akkor is helyes kepet ad vissza.
 *
 * @param parentDiv Az a DIV ami tartalmazza a chart kepet.
 *
 */
 
 
function ChartDiagrammManager(parentDiv,id) {
    this.pDiv=parentDiv;
    this.visibleImg=parentDiv.getElementsByTagName("IMG")[0];
    this.imgSrc=this.visibleImg.src;
    this.imgWidth=this.visibleImg.width;
    this.preInt = 10*1000;
    this.rInt=60*1000; // Fixen beallitottuk a frissitest, ezt valahogyan majd kezelnunk kell
    this.pr = Math.random()*1000000;
    //this.preImaged = false;
    this.counter = 0;
    var time = this.rInt;
    if( this.imgWidth < 100 ) {
    //	this.preImaged = true;
	time = this.preInt;
    }
    
    setTimeout(copyFunction(refreshChartImage,this),time);
}
/**
 * A Chart kepek ujratolteset vegzi, preLoad-al es hibakezelessel.
 */
function refreshChartImage(p) {
    /*
     * Ujratolti a kepet, de mindig valtoztatja a parameter erteket, hogy a bongyeszo ne cache-bol adja vissza.
     */
//    if( p.preImaged && 
if(10 < p.counter ) {
	window.location.reload(false);
	return;
    }
    
    var time = p.rInt;
//    if( p.preImaged && 
if(p.visibleImg.width < 100 ) {
	time = p.preInt;
        p.counter++;
    }
/*else
    if( p.preImaged ) {
	p.preImaged = false;
    }*/
    p.pr++;
    
    p.visibleImg.src=p.imgSrc+"?newParam="+p.pr;
    setTimeout(copyFunction(refreshChartImage, p),time);
}

