/*
da midia, esperar imparcialidade é besteira,
imparcialidade não existe, mas a mídia preocupa qdo p/ela 
a democracia só é verdadeira se está do seu lado,de sua classe,casta,caixa, ...
*/
var pg = new pag('pg');
setTimeout(pg.init,100);
//***************************************
//***************************************
function pag(Nome) {
	var nome = Nome;
	var eu = this;
	var url = ''+window.location;
	var ped = new pedido(url);
	var dev = ped.url.indexOf('.com.br')==-1;
	var dp = ped.get('dp');
	var nt = false,nt1;//ultima div pra not tec
	//para o zoom
	var jan,aberto,opa,opat;
	var tamFonte=100;
	//carregar imagens
	var lixo = new Image();lixo.src='css/img/carregando.gif';
	var dps = {
		10:'villaverde'
		,11:'bordignon'
		,12:'marcon'
		,13:'bohngass'
		,14:'fabiano'
		,15:'ivarpavan'
		,17:'marisa'
		,18:'raulpont'
		,19:'zulke'
		,20:'stela'
	};
	var visCnt = 0;
	var aTv = new Array(),nTv = -1,tTv;
	var pubSoc  = false;
	var nErInit = 0; //erros no init..., tentar até 4 vezes
	//****************************************************
	this.tw = function (o) {
		var tx = getParentAttr(o,'id_txt');
		//lert('tx='+tx);
		window.open('social.php?id_txt='+tx+'&rede=');
		return;
		url = leftAt(url,'=')+'='+escape(substrAt(url,'='));
		//lert('tw='+url);
		window.open(url);
	}
	//****************************************************
	// elimina cookies ptsul www.ptsul subdom.ptsul
	function acertaCookie() {
		var db = 'ptsul.com.br'	;
		var v = Array('visId','visUg','visClic','visCnt','visClicI');
		for (var i=0;i<v.length;i++) {
			var n = v[i];
			var va = cookieGet(n);
			if (va) {
				cookiePut(n,'0',-100); //retira do padrao
				cookiePut(n,'0',-100,db); //retira do dom sem .
				var vn = cookieGet(n);
				if (va!=vn) {
					cookiePut(n,''+va,1000,'.'+db);
				}
			}
		}
		//alert('fim');
	}
	//****************************************************
	// elimina cookies ptsul www.ptsul subdom.ptsul
	function acertaCookieK() {
		var db = 'ptsul.com.br'	;
		var ho = host(window.location);
		var vd = Array('','www.');
		if (db.length!=ho.length) {
			vd[vd.length] = leftAt(ho,'.')+'.';
		}
		var v = Array('visId','visUg','visClic','visCnt','visClicI');
		for (var i=0;i<v.length;i++) {
			var n = v[i];
			for (var c=0;c<vd.length;c++) {
				var da = vd[c];
				var va = 1*cookieGet(n);
				cookiePut(n,'0',-100,da+db);
				var vn = 1*cookieGet(n);
				//debJ('n='+n+' do='+da+db+' va='+va+' vn='+vn);
				if (vn==0 && va!=0) {
					cookiePut(n,''+va,1000,'.'+db);
				}
			}
		}
		//alert('fim');
	}
	//****************************************************
	function pubSocial() {
		if (!pubSoc) {
			pubSoc = new redesS();
		}
		var v = pubSoc.getProvedores();
		var alvo = getElementsByClassName(document,'pubSocial');
		for (var i=0;i<alvo.length;i++) {
			var obj = alvo[i];
			var t = '<table><tr>';
			for (var x=0;x<v.length;x++) {
				t += '<td onclick="pg.tw(this);" class="pubSocial'+v[x][0]
					+'" title="'+v[x][0]
					+'" style="width:25px;height:25px;background-image:url(img/social.jpg);background-position:-'+(x*25)+'px;">'
				;
			}
			obj.innerHTML = t+'</table>';
			//<img src=img/social.jpg>';
			//lert('t='+t);
		}
		return;
		for (var i=0;i<alvo.length;i++) {
			var obj = alvo[i];
			for (var x=0;x<v.length;x++) {
				var a = document.createElement('span');
				//a.src = v[x][3];
				a.innerHTML = '<img class="pubSocial'+v[x][0]+'" src="'+v[x][3]+'">';
				a.onclick = function() {tw(this)};
				obj.appendChild(a);
			}
		}
	}
	//****************************************************
	this.tv = function(ar) {
		//lert(typeof(ar));
		if (typeof(ar)=='object') {
			ar[9] = new Image();
			ar[9].src = 'http://i2.ytimg.com/vi/'+ar[0]+'/hqdefault.jpg';
			aTv[aTv.length] = ar;
			if (nTv==-1) {
				//inicia clock
				setTimeout(eu.tv,100);
				nTv = -2;
			}
			return;
		}
		if (ar==9) {
			//clicou
			var xx = ''
				+'<object width="640" height="490">'
				+	'<param name="movie" value="http://www.youtube.com/v/'+aTv[nTv][0]+'&color1=0xb1b1b1&color2=0xcfcfcf&hl=pt-br&feature=player_embedded&fs=1" />'
				+	'<param name="allowFullScreen" value="true" />'
				+	'<param name="allowScriptAccess" value="always" />'
				+	'<embed src="http://www.youtube.com/v/'+aTv[nTv][0]+'&autoplay=1&color1=0xb1b1b1&color2=0xcfcfcf&hl=pt-br&feature=player_embedded&fs=1" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="640" height="480">'
				+	'</embed>'
				+'</object>'
			;
			if (true) {
				var d = new dialogo(xx,'zoomVer');
				d.tipoTexto = true;
				d.opaIni = 0.8;
				d.ver();
			} else {
				clearTimeout(tTv);
				browse.getId('TV').innerHTML = xx;
			}
			
		} else {
			nTv = (nTv+1>=aTv.length || nTv<0?0:nTv+1);
			browse.getId('TV').innerHTML = '<a href=javascript:pg.tv(9);>'
				+'<img src="http://i2.ytimg.com/vi/'+aTv[nTv][0]+'/hqdefault.jpg" width="320" height="240">'
				+'</a>'
			;
			tTv = setTimeout(eu.tv,2000);
		}
		browse.getId('TV1').innerHTML = '<a href="http://www.youtube.com/ptsul2">'
        +'<h2>'+aTv[nTv][1]+'</h2>'
        +'<p>'+aTv[nTv][2]+'</p>'
			+'</a>'
		;
	}
	//****************************************************
	function bodyOver(ev) {
		if (!ev) {
			ev = event;
		}
		if (false && bdOver==0) {
			objNav(ev);
			alert('oi');
		}
		//ebJ((visCnt)+'<br>'+ev.type+'<br>'+(ev&&ev.target?ev.target.tagName:'?'),'bodyOver');
		visCnt++;
		if (visCnt%20==0) {
			cookiePut('visCnt',visCnt,360,'.ptsul.com.br');
		}
	}
	//****************************************************
	this.busca = function(o,a,txRet) {
		//lert('busca');
		window.location = 'noticias.php?q='+o.value;
		return false;
	}
	//****************************************************
	this.altCampo = function(o,a,txRet) {
		if (txRet) {
			//recebe resposta
			if (txRet!='ok') {
				alert('ERRO: '+txRet);
			}
			return;
		}
		var v = o.value;
		if (o.type=='checkbox') {
			v = o.checked?'on':'';
		}
		var ur = 'alterar_db.php?updCampo='+o.name+'&val='+v;
		//lert(ur);
		var aj = new carregaUrl();
		aj.abre(ur,eu.altCampo);
	}
	//****************************************************
	this.vai = function(o) {
		if (o=='ini') {
			window.location = !dev?'http://www.ptsul.com.br':'./';
			return;
		}
		var d = dps[o];
		if (d) {
			window.location = !dev?'http://'+d+'.ptsul.com.br':'./?dp='+o;
			return;
		}
		alert('onde?'+o);
	}
	//****************************************************
	this.atualEnder = function(o) {
		var v = o.value;
		if (o.type=='checkbox') {
			v = (o.checked?'on':'');
		}
		//lert(o.type+' v='+v);
		var p = new pedido();
		if (p.get(o.name)!=v) {
			p.set(o.name,v);
			window.location = p.atalho();
		}
	}
	//****************************************************
	this.imprimir = function(n) {
		window.open('t_print.php?id_txt='+id_txt(n)
			,'_blank'
			,'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=yes, width=490, height=600'
		);
	}
	//****************************************************
	this.enviar = function(n) {
		window.open('indica.php?acao=entrar&id_txt='+id_txt(n)
			,'_blank'
			,'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=yes, width=300, height=300'
		);
	}
	//****************************************************
	function id_txt(n) {
		var t = ''+window.location;
		t = t.substring(t.lastIndexOf('id_txt=')+7);
		t = leftAt(t,'#');
		t = leftAt(t,'&');
		t = 1*t;
		//lert(t);
		if (isNaN(t) || t<1 ) {
			//lert('cod invalido='+t+" "+(1*t));
			return n;
		}
		return t;
	}
	//****************************************************
	this.fonte = function(n) {
		var ii = 'tamfon';
		var o = browse.getId(ii);
		tamFonte = tamFonte+n;
		//lert('fs='+o.style.fontSize);
		o.style.fontSize = tamFonte+'%';
	}
	//****************************************************
	this.naoImpl = function() {
		alert('Opção não disponível, aguarde!');
	}
	//****************************************************
	this.notTecA = function(cod) {
		var alvo = getElementsByClassName(document,'ler')[0];
		alvo.innerHTML = '<h1>Aguarde</h1>';
		//indow.location = leftAt(''+window.location+'#','#')+'#id_txt='+cod;
		//ebJ('alvo='+alvo);
		var aj = new carregaUrl();
		aj.abre('inc/noticia_aberta.php?obj=1&id_txt='+cod,alvo);
	}
	//****************************************************
	this.notTec = function(cmp,cod) {
		//lert(cmp);
		if (nt) {
			//nt.innerHTML = '';
			domRemove(nt);
			if (nt1==cmp) {
				nt1 = false;
				nt = false;
				return;
			}
		}
		nt1 = cmp;
		
		nt = document.createElement('div');
		nt.className = 'notaTecAss';
		getParentByTagName(cmp,'div').appendChild(nt);
		
		var aj = new carregaUrl();
		aj.abre('?ass='+cod,nt);
	}
	//****************************************************
	this.inputLimpa = function(cmp) {
		if (equals(cmp.value,' Seu ')) {
			cmp.value = '';
		}
	}
	//****************************************************
	this.comenta1 = function(a,b,tx) {
		//lert(tx);
		var d = new dialogo(tx,'zoomVer');
		d.tipoTexto = true;
		d.opaIni = 0.8;
		d.ver();
		//tem erro
		if (tx.indexOf('>ERRO')!=-1) {
			return;
		}
		//recarrega coments
		var alvo = getElementsByClassName('comentarios')[0];
		//lert('alvo='+typeof(alvo)+' ='+alvo);
		var aj = new carregaUrl();
		aj.abre('inc/comenta.php?acao=lista&id_txt='+(new pedido()).get('id_txt'),alvo);
		eu.frmL['nome'].value='';
		eu.frmL['email'].value='';
		eu.frmL['comentario'].value='';
	}
	//****************************************************
	this.comenta = function(frm) {
		//bjNav(frm);
		var r = '',tr='';
		eu.frmL = frm;
		if (!valMail(frm['email'])) {
			r += '\nendereço eletrônico errado!';
		}
		eu.inputLimpa(frm['nome']);
		if (vazio(frm['nome'].value)) {
			r += '\nnome errado!';
		}
		eu.inputLimpa(frm['comentario']);
		if (vazio(frm['comentario'].value)) {
			r += '\ncomentário errado!';
		}
		if (r!='') {
			alert(r);
			return false;
		}
		
		var aj = new carregaUrl();
		var a = eval('fr'+tr+'m.a'+tr+'ction');
		//lert(a);
		var b = a;
		a  = substrAt(substrAt(a,'//'),'/');
		a += '&js='+tr+'foi';
		eval('fr'+tr+'m.a'+tr+'ction'+tr+'=a;');
		//lert('a='+a+' \nb='+b+' \nfrm'+frm.action);
		aj.form = frm;
		aj.abre('',eu.comenta1);
		return false;
	}
	//****************************************************
	this.galeriaC = function(ev) {
		//bjNav(ev);
		if (!ev) {
			ev = event;
		}
		var im = targetEvent(ev);
		im = getParentByTagName(im,'a');
		if (!im) {
			return;
		}
		//lert('aa='+ev+' '+im.href);
		//var d = new dialogo('<h1>Aguarde . . . </h1>','zoomVerImg');
		var d = new dialogo('<img src="css/img/carregando.gif"/>','zoomVerImg');
		d.tipoImg = ''+im.href;
		d.imgCarregando = 500;
		d.opaIni = 0.8;
		d.limH = 1;
		d.ver();
		return false;
	}
	//****************************************************
	this.galeria = function() {
		var v = document.getElementsByTagName('a');
		for (var i=0;i<v.length;i++) {
			var u = ''+v[i].href;
			if (substrRat(u,'.').toLowerCase()=='jpg') {
				//debJ(u);
				if (true || !browse.ie) {
					v[i].onclick = eu.galeriaC;
				} else {
					v[i].href = 'javascript:pg.galeriaC(event);';
				}
			}
		}
		//lert('fim='+i);
	}
	//****************************************************
	this.zoom = function(o,ev) {
		var tx = browse.getId('tamfon').innerHTML;
		var d = new dialogo(tx,'zoomVer');
		d.tipoTexto = true;
		d.opaIni = 0.8;
		d.ver();
	}
	//***************************************
	this.init = function() {
		try {
			if ( document.addEventListener ) {
				document.addEventListener('mousemove',bodyOver,false);
				document.addEventListener('scroll',bodyOver,false);
				document.body.addEventListener('DOMMouseScroll',bodyOver,false);
			} else if (document.attachEvent) {
				//lert('ini'+document.attachEvent);
				document.attachEvent('onmousemove',bodyOver);
				document.attachEvent('onscroll',bodyOver);
				document.body.onmousewheel = bodyOver;
			}
		} catch (e) {
			//tenta até 4 vezes...
			if (nErInit<10) {
				nErInit++;
				setTimeout(eu.init,nErInit*100);
				return;
			}
			//lert('erro init..'+nErInit);
		}
		visCnt = 1*cookieGet('visCnt');
		eu.maxIni = 0;
		acertaCookie();
		atalhosMenu();
		pubSocial();
	}
	//***************************************
	function atalhosMenu() {
		var v = document.getElementsByTagName('a');
		if (  v.length==0 ||  ((''+v[v.length-1].href).indexOf('ptsul_api.php')==-1 && eu.maxIni<10) ) {
			//ebJ('+ 100 ms '+v.length);
			setTimeout(atalhosMenu,300);
			eu.maxIni++;
			return;
		}
		//ebJ('tam='+v.length);
		for (var i=0;i<v.length;i++) {
			var o = v[i];
			var ur = ''+o.href;
			//ebJ(i+' = '+ur);
			//adiciona dp no atalho?
			if (dp && ur.indexOf('dp='+dp)==-1 && ur.indexOf('.php')!=-1) {
				ur = ur+(ur.indexOf('?')==-1?'?':'&')+'dp='+dp;
				o.href = ur;
			}
			//é atalho atual
			if (o.className.indexOf('menug')!=-1) {
				//do menu, teste string parcial
				if (equals(url,ur)) {
					v[i].className += ' menuSel';
				}
			} else if (url==ur) {
				v[i].className += ' atalhoSel';
			}
		}
	}
}

/*
http://www.w3.org/TR/DOM-Level-3-Events/#event-flow
5.1.1 List of DOM3 Event Types

type  	Bubbling phase  	Cancelable  	Target node types  	DOM interface
abort 	Yes 	No 	Element 	Event
blur 	No 	No 	Element 	UIEvent
change 	Yes 	No 	Element 	Event
click 	Yes 	Yes 	Element 	MouseEvent
compositionstart 	Yes 	Yes 	Element 	CompositionEvent
compositionupdate 	Yes 	Yes 	Element 	CompositionEvent
compositionend 	Yes 	Yes 	Element 	CompositionEvent
dblclick 	Yes 	Yes 	Element 	MouseEvent
DOMActivate 	Yes 	Yes 	Element 	UIEvent
DOMAttributeNameChanged 	Yes 	No 	Element 	MutationNameEvent
DOMAttrModified 	Yes 	No 	Element 	MutationEvent
DOMCharacterDataModified 	Yes 	No 	Text, Comment, CDATASection, ProcessingInstruction 	MutationEvent
DOMElementNameChanged 	Yes 	No 	Element 	MutationNameEvent
DOMFocusIn 	Yes 	No 	Element 	UIEvent
DOMFocusOut 	Yes 	No 	Element 	UIEvent
DOMNodeInserted 	Yes 	No 	Element, Attr, Text, Comment, CDATASection, DocumentType, EntityReference, ProcessingInstruction 	MutationEvent
DOMNodeInsertedIntoDocument 	No 	No 	Element, Attr, Text, Comment, CDATASection, DocumentType, EntityReference, ProcessingInstruction 	MutationEvent
DOMNodeRemoved 	Yes 	No 	Element, Attr, Text, Comment, CDATASection, DocumentType, EntityReference, ProcessingInstruction 	MutationEvent
DOMNodeRemovedFromDocument 	No 	No 	Element, Attr, Text, Comment, CDATASection, DocumentType, EntityReference, ProcessingInstruction 	MutationEvent
DOMSubtreeModified 	Yes 	No 	Document, DocumentFragment, Element, Attr 	MutationEvent
error 	Yes 	No 	Element 	Event
focus 	No 	No 	Element 	UIEvent
focusin 	Yes 	No 	Element 	UIEvent
focusout 	Yes 	No 	Element 	UIEvent
keydown 	Yes 	Yes 	Element 	KeyboardEvent
keypress 	Yes 	Yes 	Element 	KeyboardEvent
keyup 	Yes 	Yes 	Element 	KeyboardEvent
load 	No 	No 	Document, Element 	Event
mousedown 	Yes 	Yes 	Element 	MouseEvent
mouseenter 	No 	Yes 	Element 	MouseEvent
mouseleave 	No 	Yes 	Element 	MouseEvent
mousemove 	Yes 	Yes 	Element 	MouseEvent
mouseout 	Yes 	Yes 	Element 	MouseEvent
mouseover 	Yes 	Yes 	Element 	MouseEvent
mouseup 	Yes 	Yes 	Element 	MouseEvent
mousewheel 	Yes 	Yes 	Document, Element 	MouseWheelEvent
reset 	Yes 	Yes 	Element 	Event
resize 	Yes 	No 	Document, Element 	UIEvent
scroll 	No 	No 	Document, Element 	UIEvent
select 	Yes 	No 	Element 	Event
submit 	Yes 	Yes 	Element 	Event
textInput 	Yes 	Yes 	Element 	TextEvent
unload 	No 	No 	Document, Element 	Event
wheel 	Yes 	Yes 	Document, Element 	WheelEvent 
*/
