jx = {
	//Create a xmlHttpRequest object - this is the constructor. 
	getHTTPObject : function() {
		var http = false;
		//Use IE's ActiveX items to load the file.
		if(typeof ActiveXObject != 'undefined') {
			try {http = new ActiveXObject("Msxml2.XMLHTTP");}
			catch (e) {
				try {http = new ActiveXObject("Microsoft.XMLHTTP");}
				catch (E) {http = false;}
			}
		//If ActiveX is not available, use the XMLHttpRequest of Firefox/Mozilla etc. to load the document.
		} else if (window.XMLHttpRequest) {
			try {http = new XMLHttpRequest();}
			catch (e) {http = false;}
		}
		return http;
	},
	// This function is called from the user's script. 
	//Arguments - 
	//	url	- The url of the serverside script that is to be called. Append all the arguments to 
	//			this url - eg. 'get_data.php?id=5&car=benz'
	//	callback - Function that must be called once the data is ready.
	//	format - The return type for this function. Could be 'xml','json' or 'text'. If it is json, 
	//			the string will be 'eval'ed before returning it. Default:'text'
	load : function (url,callback,format) {
		var http = this.init(); //The XMLHttpRequest object is recreated at every call - to defeat Cache problem in IE
		if(!http||!url) return;
		if (http.overrideMimeType) http.overrideMimeType('text/xml');

		if(!format) var format = "text";//Default return type is 'text'
		format = format.toLowerCase();
		
		//Kill the Cache problem in IE.
		var now = "uid=" + new Date().getTime();
		url += (url.indexOf("?")+1) ? "&" : "?";
		url += now;

		http.open("GET", url, true);

		http.onreadystatechange = function () {//Call a function when the state changes.
			if (http.readyState == 4) {//Ready State will be 4 when the document is loaded.
				if(http.status == 200) {
					var result = "";
					if(http.responseText) result = http.responseText;
					
					//If the return is in JSON format, eval the result before returning it.
					if(format.charAt(0) == "j") {
						//\n's in JSON string, when evaluated will create errors in IE
						result = result.replace(/[\n\r]/g,"");
						result = eval('('+result+')'); 
					}
	
					//Give the data to the callback function.
					if(callback) callback(result);
				} else { //An error occured
					if(error) error(http.status);
				}
			}
		}
		http.send(null);
	},
	init : function() {return this.getHTTPObject();}
}


lib = {
	//-----------------------------------------------//
	// SCRIPT de Carregamento da MINIGRADE
	//-----------------------------------------------//	
	minigrade: {
		load: function(){
			jx.load('/grid.json',function(data){
				dados = eval(data);
				dados = dados["grid"];
				var cnt = 1;
				for(dado in dados){
					if(cnt==1){
						document.getElementById('hour_A').innerHTML = dados[dado]["hour"];
						document.getElementById('image_A').src = '/rederecord/images/programas/' + dados[dado]["key"]+'.jpg';
					}
					if(cnt==2){
						document.getElementById('hour_B').innerHTML = dados[dado]["hour"];
						document.getElementById('image_B').src = '/rederecord/images/programas/' + dados[dado]["key"]+'.jpg';					
					}
					if(cnt==3){
						document.getElementById('hour_C').innerHTML = dados[dado]["hour"];
						document.getElementById('image_C').src = '/rederecord/images/programas/' + dados[dado]["key"]+'.jpg';										
					}
					cnt++;
				}			
			}, 'JSON');
		}
	}
}

/********************** Troca Chamadas TV **********************/

var chTvSet;
var chTvTempo = 5200;
function trocaChamadaTv(x){
	clearTimeout(chTvSet);
	
	area = x.parentNode.parentNode;	
	for(b=0; b<area.getElementsByTagName('ul').length; b++){
		if (area.getElementsByTagName('ul')[b].className == "controle" ){
			controle = area.getElementsByTagName('ul')[b];
			chTvNum = 0;
		}
		if (area.getElementsByTagName('ul')[b].className == "chamadas" ){
			chamadas = area.getElementsByTagName('ul')[b];
		}
	}
	
	for(a=0; a<chamadas.getElementsByTagName('li').length; a++){
		if (chamadas.getElementsByTagName('li')[a].parentNode == chamadas){
			chamadas.getElementsByTagName('li')[a].className = '';
			controle.getElementsByTagName('li')[chTvNum].className = '';
			if (controle.getElementsByTagName('li')[chTvNum] == x){
				x.className = 'ativo';
				chamadas.getElementsByTagName('li')[a].className = 'ativo';
			}
			chTvNum++;
		}
	}
	chTvSet = setTimeout('trocaChamadaTvRandom2()',chTvTempo);	
}

/***************** Troca Chamadas TV Randômico *****************/
function trocaChamadaTvRandom(){
	chTvSet = setTimeout('trocaChamadaTvRandom2()',chTvTempo);
}
function trocaChamadaTvRandom2(){
	area = document.getElementById('chamadas_tv');
	
	for(b=0; b<area.getElementsByTagName('ul').length; b++){
		if (area.getElementsByTagName('ul')[b].className == "controle" )
			controle = area.getElementsByTagName('ul')[b];
	}
	
	for(a=0; a<controle.getElementsByTagName('li').length; a++) {
		if (controle.getElementsByTagName('li')[a].className == 'ativo'){
			if (a == (controle.getElementsByTagName('li').length - 1)){
				trocaChamadaTv(controle.getElementsByTagName('li')[0]);
			}
			else{
				trocaChamadaTv(controle.getElementsByTagName('li')[a+1]);
			}
		}
	}
}
/***************************************************************/

/************************** Troca Aba **************************/
function trocaAba(x){
	aba = x;
	abas = aba.parentNode;
	
	for(a=0; a<abas.getElementsByTagName('li').length; a++){
		abas.getElementsByTagName('li')[a].className='';
	}
	
	conteudo = 0;
	area = aba.parentNode.parentNode;
	
	for(b=0; b<area.getElementsByTagName('div').length; b++){
		if(area.getElementsByTagName('div')[b].className.indexOf('abas_conteudo') == 0 ){
			classe = area.getElementsByTagName('div')[b].className;
			if(classe.indexOf(' ativo') > 0){
				area.getElementsByTagName('div')[b].className = tiraTrecho(classe,' ativo');
			}
			if (abas.getElementsByTagName('li')[conteudo] == aba){
				area.getElementsByTagName('div')[b].className = classe+' ativo';
			}
			conteudo++;
		}
	}
	aba.className='ativo';
}

/******************** tira trecho da string ********************/
function tiraTrecho(string,trecho){
	if(string.indexOf(trecho)>0){
		string = string.substring(0, string.indexOf(trecho))
	}
	return string;
}
/***************************************************************/
