function $(i){
	return document.getElementById(i);
};

/*
 * Funkcja odswieza wybrany box ze strony glownej FX.
 * 
 */
function updatePageBox(page, box_name, interval, tabId) {
	var _parameters =  {'layout' : 'ajax', 'page' : page, 'interval' : interval, 'mainpage' : 'true'};
	var parent = 'parent'+page
	var parentTabId = $(tabId).innerHTML;
	_parameters[parent] = parentTabId;	
	advAJAX.get({
		url : '/ajax.jsp',
		parameters : _parameters,
		timeout : 10000,
		onSuccess : function(obj){ 
			FxMainPageRequest.FxMainPageTimeOutRefresh();
			$(box_name).innerHTML = obj.responseText;
			//Zapamietanie wybranej zakladki
			$(tabId).innerHTML = parentTabId;
		},
		onTimeout : function(){ 						
			FxMainPageRequest.FxMainPageTimeOutRefresh();						
		},
		onError : function(){
			FxMainPageRequest.FxMainPageTimeOutRefresh();
		}	
	});
	
};

//Funkcja ustawia wartosc aktualnegie trzymanego placa;
function setDivValue(divId, value) {
	var v = $(divId);
	v.innerHTML = value;	
}

var FxMainPageRequest = {

		config  : null,
		event   : null,
		counter : 0,

		conf : function(config){
			this.config = config;
		},

		init : function(){
			if(window.addEventListener) window.addEventListener('load', function(){ FxMainPageRequest.start(); }, false);
			else if(window.attachEvent) window.attachEvent('onload', function(){ FxMainPageRequest.start(); });
			else window['onload'] = function(){ FxMainPageRequest.start(); }
		},

		start : function(){
			FxMainPageRequest.stop();
			if(parseInt(FxMainPageRequest.config.interval) > 0){
				FxMainPageRequest.event = window.setInterval("FxMainPageRequest.tick();", 1000);
				this.counter = 0;
			}
		},

		stop : function(){
			try{
				window.clearInterval(this.event);
				this.event = null;
			}catch(e){}
		},

		setInterval : function(interval){
			this.stop();
			this.config.interval = parseInt(interval);
			this.start();
		},

		getInterval : function(){
			return parseInt(this.config.interval);
		},

		tick : function(){
			this.counter++;
			var interval = this.getInterval();
			if(this.counter % interval == 0) {
				this.update();
			} 
		},
		
		FxMainPageTimeOutRefresh : function(){
			FxMainPageRequest.restart();					
		},
		
		update : function(){
			this.stop();
			var upInterval = '120';
			
			updatePageBox('ch02', 'wykresyPlace', upInterval, 'parentch02TabId');
			updatePageBox('ch04', 'ch04', upInterval, 'parentch04TabId');
			updatePageBox('ch05', 'ch05', upInterval, 'parentch05TabId');
			
			FxMainPageRequest.FxMainPageTimeOutRefresh();							
		},
		
		restart : function(){
			this.start();
		}
		
	};
