/*
iRadio .::.::. 4 anos de sucesso
iradio.com.br

Rafael Carvalho :: carvalho@rafaelcarvalho.net
*/

fila=[];
ifila=0;

function getXMLHTTPRequest(){var xml=null;try{xml=new ActiveXObject("Msxml2.XMLHTTP");}catch(e){try{xml=new ActiveXObject("Microsoft.XMLHTTP");}catch(sc){xml=null;}}if(!xml && typeof XMLHttpRequest!="undefined"){xml=new XMLHttpRequest();}return xml;}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function bt_MOver(img,caminho){
	img_endereco = img.src;
	//alert(img_endereco);
	img_partes_barras = img_endereco.split("/");
	img_nome = img_partes_barras[img_partes_barras.length-1];
	img_partes = img_nome.split(".");
	if(img_partes[0].substr(img_partes[0].length-4,4) != "over"){
		nova_img = caminho+"images/"+img_partes[0]+"_over."+img_partes[1];
		img.src = nova_img;
	}
}

function bt_MOut(img){
	img_nome = img.src;
	img_partes = img_nome.split("_over");
	if(img_partes.length == 2){
		nova_img = img_partes[0]+"_over."+img_partes[1];
		img.src = nova_img;
	}
	nova_img = img_partes[0]+img_partes[1];
	img.src = nova_img;
}

function submeterFormulario(formulario){
	if(formulario == "per_interior"){
		area = document.getElementById('per_area');
		nome = document.getElementById('per_nome').value;
		cidade = document.getElementById('per_cidade').value;
		uf = document.getElementById('per_uf').value;
		p_r = document.getElementById('per_p_r').value;
		txt = document.getElementById('per_txt').value;
		if(!is_branco([nome,cidade,uf,p_r,txt])){
			do {
				txt = txt.replace('\n', "<br />");
			} while (txt.indexOf('\n') != -1);
			ajaxCall(2,"enviarPedido.php","carregarPeR","per_area",0);
		}
	} else if(formulario == "enquete"){
		area = document.getElementById('enquete_area');
		voto = document.getElementById('enquete_voto').value;
		eid = document.getElementById('enquete_id').value;
		if(!is_branco([voto])){
			ajaxCall(2,"votar.php",carregarEnqueteResul,"enquete_area",0,[eid,voto]);
		}
	} else if(formulario == "entrar_webchat"){
		nick = document.getElementById('webchat_nick').value;
		if(!is_branco([nick])){
			window.open("http://www.brlink.org/webchat/chat.php?canal=#iradio&nick="+nick,"WebChat","width=800,height=600");
		} else {
			alert("Escolha um nick!");	
		}		
	}
}

function is_branco(vars){
	for(i=0;i<vars.length;i++){
		if(vars[i] == ""){
			alert('Não deixe campos em branco');
			$retorno = true;
			break;
		} else {
			$retorno = false;
		}
	}
	return $retorno;
}

function selecionarRadio(formulario,campo){
	if(formulario=="per"){
		pedido = document.getElementById('per_pedido');
		recado = document.getElementById('per_recado');
		p_r = document.getElementById('per_p_r');
		if(campo == "pedido"){
			pedido.src = "images/form_radio_on.jpg";
			recado.src = "images/form_radio_off.jpg";
			p_r.value = "pedido";
		} else if(campo == "recado"){
			recado.src = "images/form_radio_on.jpg";
			pedido.src = "images/form_radio_off.jpg";
			p_r.value = "recado";
		}
	} else if(formulario=="per2"){
		pedido = document.getElementById('per_pedido');
		recado = document.getElementById('per_recado');
		p_r = document.getElementById('per_p_r');
		if(campo == "pedido"){
			pedido.src = "../images/form_radio_on.jpg";
			recado.src = "../images/form_radio_off.jpg";
			p_r.value = "pedido";
		} else if(campo == "recado"){
			recado.src = "../images/form_radio_on.jpg";
			pedido.src = "../images/form_radio_off.jpg";
			p_r.value = "recado";
		}
	} else if(formulario=="enquete"){
		alternativas = document.getElementById('enquete_alternativas');
		voto = document.getElementById('enquete_voto');
		voto.value = campo;
		resposta = new Array();
		for(i=0;i<alternativas.value;i++){
			resposta[i] = document.getElementById('enquete_resposta'+(i+1));
			if((i+1) == campo){
				resposta[i].src = "../images/form_radio_on.jpg";
			} else {
				resposta[i].src = "../images/form_radio_off.jpg";
			}
		}
	}
}

function envioStatus(status,area,resposta){
	areaOBJ = document.getElementById(area);
	carregando = "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0' width='25' height='25'><param name='movie' value='swf/carregando.swf'><param name='quality' value='high'><param name='wmode' value='transparent'><embed src='swf/carregando.swf' wmode='transparent' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='25' height='25'></embed></object>";
	if(status=='carregando'){
		areaOBJ.innerHTML = carregando;
	} else if(status=='carregado'){
		areaOBJ.innerHTML = resposta;
	}
}

function carregarPeR(resposta){
	ajaxCall(1,"per.php",'carregarPeR',"per_area",2000);
}

function carregarPeRInterior(estado){
	ajaxCall(1,estado+"/per.php",'carregarPeRInterior',"per_area",2000);
}

function carregarEnquete(){
	ajaxCall(1,"enquete.php",'carregarEnquete',"enquete_area",2000);
}

function carregarEnqueteResul(resposta){
	ajaxCall(1,"enqueteResul.php",'carregarEnqueteResul',"enquete_area",2000);
}

function ajaxCall(tipo,url,operacao,area,tempo,args){
	//Adiciona à fila
	if(args==null){ args = new Array; }
	if(operacao!="rodaFila"){
		fila[fila.length]=[tipo,url,operacao,area,tempo,args];
	}
	//Se não há conexões pendentes, executa
	if((ifila+1)<=fila.length){
		tipo = fila[ifila][0];
		url = fila[ifila][1];
		operacao = fila[ifila][2];
		area = fila[ifila][3];
		tempo = fila[ifila][4];
		args = fila[ifila][5];
		for(i=0;i<args.length;i++){
			if(i==0){ url += "?args["+i+"]="+args[i]; }
				else { url += "&args["+i+"]="+args[i]; }
			}
		req=getXMLHTTPRequest();
			if(req){
				req.open("GET",url,true);
				if(tipo==1 || tipo==2){ envioStatus('carregando',area); }
				req.onreadystatechange=function(){
					if(req.readyState == 4){
						 if(tipo==1){
							 setTimeout("envioStatus('carregado','"+area+"','"+req.responseText+"')",tempo);
						 } else {
							operacao(req.responseText); 
						 }
						 ifila++;
         			     if(ifila<fila.length){ setTimeout("ajaxCall(0,'','rodaFila')",20); }
					}
				};
				req.send(null);
			}
	}
}
