
/* COMPARTILHADORES */

var arrCompartilhar = [];

arrCompartilhar.push({
	'img': 'iconDelicious.gif',
	'link': 'http://del.icio.us/post?url='
});

arrCompartilhar.push({
	'img': 'iconDigg.gif',
	'link': 'http://digg.com/submit?phase=2&url='
});

arrCompartilhar.push({
	'img': 'iconFaceBook.gif',
	'link': 'http://www.facebook.com/share.php?u='
});

arrCompartilhar.push({
	'img': 'iconTechnorati.gif',
	'link': 'http://www.technorati.com/faves?add='
});

function goToCompartiha(i){
	window.open(arrCompartilhar[i].link+escape(window.location));
}

// Varivel que define a posio do scroll
$D.global.POSICAO = 0;
$D.global.POSICAO2 = 0;
$D.global.TOTAL_SIZE = 0;

// Cria o Drag n' Drop do Scroll
$D.listener.push(window,'onload',function(){
	$D.global.ddBarra = $D.dragdrop.push('scrollSite','scrollSite',{
		'type': 'normal',
		'block': true,
		'left': 0,
		'top': 0,
		'width': 20,
		'height': 1000,
		'ondragmove': function() {
			if($('itenNewsOpen').open){
				$D.global.POSICAO2 = (parseInt($S('itenNewsOpen').top)*100)/($D.global.SIZE_BARRA-$D.global.SIZE_SCROLL);	
			}else{
				$D.global.POSICAO = (parseInt($S('scrollSite').top)*100)/($D.global.SIZE_BARRA-$D.global.SIZE_SCROLL);
			}
		}
	});
});


$D.global.REFRESH_SIZE = function(){
	//if($D.page.visibleSize['y'] != $('total').height){
	$S('total').height = ($D.page.visibleSize['y'])+'px';
	$S('total').width =($D.page.visibleSize['x'])+'px';
	//}	
}


$D.listener.push(window,'onresize',$D.global.REFRESH_SIZE);
$D.listener.push(window,'onload',$D.global.REFRESH_SIZE);

//Atualiza as dimensoes da noticia aberta
$D.global.REFRESH_NOTABERTA = function(){
	$S('itenNewsOpen').height = ($D.page.visibleSize['y'])+'px';
	$S('itenNewsOpen').width =($D.page.visibleSize['x']-226)+'px';
}
$D.listener.push(window,'onresize',$D.global.REFRESH_NOTABERTA);
$D.listener.push(window,'onload',$D.global.REFRESH_NOTABERTA);


// Atualiza a altura das colunas
$D.global.REFRESH_COLLS = function() {
	$S('backColl').height = $D.page.visibleSize['y']+'px';
	$S('startColl').height = $D.page.visibleSize['y']+'px';
	$S('itenNewsTable').width = ($D.page.visibleSize['x']-226)+'px';
};
$D.listener.push(window,'onresize',$D.global.REFRESH_COLLS);
$D.listener.push(window,'onload',$D.global.REFRESH_COLLS);

// Atualiza o tamanho e posio da barra de scroll quando necessrio
$D.global.POSICIONA_BARRA = function() {	
	$D.global.SIZE_SCROLL = 207;
	$D.global.SIZE_BARRA = $D.page.visibleSize['y'];
	$D.global.CENTER_BARRA = Math.ceil($D.global.SIZE_BARRA/2);
	$D.global.INIT_BARRA = Math.ceil($D.page.visibleSize['y']/2)-$D.global.CENTER_BARRA;
	
	$S('areaScroll').height = $D.global.SIZE_BARRA+'px';
	$S('areaScroll').left = ($D.page.visibleSize['x']-22)+'px';
	$S('areaScroll').top = 0+'px';
	
	$D.global.ddBarra.height = $D.global.SIZE_BARRA;
	
	if($('itenNewsOpen').open){
		$S('scrollSite').top = (($D.global.SIZE_BARRA-$D.global.SIZE_SCROLL)*$D.global.POSICAO2/100)+'px';
	}else{
		$S('scrollSite').top = (($D.global.SIZE_BARRA-$D.global.SIZE_SCROLL)*$D.global.POSICAO/100)+'px';
	}
	
};
$D.listener.push(window,'onresize',$D.global.POSICIONA_BARRA);
$D.listener.push(window,'onload',$D.global.POSICIONA_BARRA);

// Timer para o easing da rolagem
$D.global.TIMER_BARRA = new $D.timer();
$D.global.TIMER_BARRA.interval = 20;
$D.global.TIMER_BARRA.ontime = function() {
	if ($D.global.DADOS_STATUS2 != "read") return;
	
	//$D.trace.add($('itenNewsOpen').open);
	
	var ease = 0.3;
	
	
	
	
	if($('itenNewsOpen').open){
		var ST = parseInt($D.basic.getSize('itenNewsOpen').y);
		var SV = $D.page.visibleSize['y'];
		var PA = parseInt($S('itenNewsOpen').top);
		var dif = ST - SV;
		
		if (dif < 0) {
			//$D.global.POSICAO = 0;
			//$S('areaScroll').left = '-100px';
		}
		
		var newPos = dif * $D.global.POSICAO2/-100;
		
		//newPos = Math.round(newPos/61)*61;
		
		//$D.trace.add(newPos);
		
		$S('itenNewsOpen').top = PA + ((newPos-PA)*ease)+'px';	
	}else{
		var ST = $D.global.TOTAL_SIZE;
		var SV = $D.page.visibleSize['y'];
		var PA = parseInt($S('itenNews').top);
		var dif = ST - SV;
		
		if (dif < 0) {
			$D.global.POSICAO = 0;
			$S('areaScroll').left = '-100px';
		}
		
		var newPos = dif * $D.global.POSICAO/-100;
		
		newPos = Math.round(newPos/61)*61;
		
		//$D.trace.add($D.global.POSICAO);
		
		$S('itenNews').top = PA + ((newPos-PA)*ease)+'px';
	}
	
};
$D.listener.push(window,'onload',function(){
	$D.global.TIMER_BARRA.play();
});

// Varivel de controle de leitura dos dados
$D.global.DADOS_STATUS = "waiting";
$D.global.DADOS_STATUS2 = "read";

// Funo que inicia a leitura dos dados
$D.listener.push(window,'onload',function(){
	$('itenNewsOpen').open = false;
	
	if ($D.global.DADOS_STATUS == "waiting") {
		$D.global.DADOS_STATUS = "loading";
		
		
		var busca = false;
		
		try{
			if($('busca').value != "") busca = true;
		}catch(e){ busca = false; }
		
		if(busca == true){
			var ajax = new $D.ajax({
				'method': 'GET',
				'url': '/noticias/buscaJson/',
				'oncomplete': function() {
				
					var json = $D.json.decode(this.text);
					if (json.erro) {
						alert(json.erro);
						return false;
					}
					$D.global.DADOS_PORTFOLIO = json;
					$D.global.CRIA_PROJETOS();
					delete ajax;
					//window.onmousewheel = document.onmousewheel = wheel;
				}
			});
			ajax.push('q', $('busca').value);
		}else{
			var ajax = new $D.ajax({
				'method': 'POST',
				'url': '/noticias/json/',
				'oncomplete': function() {
				
					var json = $D.json.decode(this.text);
					if (json.erro) {
						alert(json.erro);
						return false;
					}
					$D.global.DADOS_PORTFOLIO = json;
					$D.global.CRIA_PROJETOS();
					if($('idNot').value!=''){
						$D.global.ABRIR_NOTICIA($('idNot').value);
					}
					delete ajax;
					//window.onmousewheel = document.onmousewheel = wheel;
				}
			});
		}
		
		ajax.open();
	}
});

// Funcao que inicia a leitura dos dados de uma notcia
$D.global.NOTICIAS_LOADED = [];
var controle = "";

$D.global.NOTICIA_OPENED = function(id){
	for (var i = 0; i < $D.global.NOTICIAS_LOADED.length; i++) {
		if ($D.global.NOTICIAS_LOADED[i].id == id) {
			return i;
		}
	}
	return "false";
}

$D.global.NOTICIA_COMPLETE = function(id){
	
	controle = "FECHAR";
	
	$('itenNewsOpen').open = true;
	$D.global.POSICIONA_BARRA();
	
	var json = $D.global.NOTICIAS_LOADED[id].json;

	$('blockbg').style.display = "block";

	if (json.erro) {
		alert(json.erro);
		return false;
	}
	
	$D.global.DADOS_NOTICIA = json;
	
	
	SWFAddress.setTitle(json.titulo);
	
	// SETAR DADOS DA NOTICIA AQUI
	$('dataNoticia').innerHTML = json.data;
	$('tituloNoticia').innerHTML = json.titulo;
	$('conteudoNoticia').innerHTML = json.texto;
	
	$S('foto').display = "none";
	var imgLoader = new Image();
	imgLoader.onload = function(){
		$('foto').src = this.src;
		$S('foto').display = "block";
	}
	imgLoader.onerror = function(){
		$S('foto').display = "none";
	}
	imgLoader.src = json.foto;
	
	$('form1.id').value = json.id;
	
	setTimeout(function(){$D.global.EFEITO_NOTICIA_ABRIR();}, 200);
	$D.global.DADOS_STATUS2 = "read";
}

$D.global.ABRIR_NOTICIA = function(id){
	$D.global.voltarNoticias = false;
	
	$("blockbg").style.display = "none";
	
	if ($D.global.DADOS_STATUS2 == "read") {
		controle = "ABRIR";
		
		var idIsOpened = $D.global.NOTICIA_OPENED(id);
		
		if(idIsOpened == "false"){
			$D.global.DADOS_STATUS2 = "loading";
			var ajax = new $D.ajax({
				'method': 'POST',
				'url': '/noticias/json/open/',
				'oncomplete': function() {
					var json = $D.json.decode(this.text);
					
					$D.global.NOTICIAS_LOADED.push({
						'id': id,
						'json': json
					});
					
					$D.global.NOTICIA_COMPLETE(($D.global.NOTICIAS_LOADED.length-1));
					
					delete ajax;
					
				}
			});
			ajax.push('id',id);
			ajax.open();
		}else{
			$D.global.NOTICIA_COMPLETE(idIsOpened);
		}
	}
};

$D.global.TWEEN_EFEITO = new $D.tween({
	'time': 0.4,
	'type': 'easeOutQuad',
	'ontime': function(a) {
		$S('itenNewsOpen').left = Math.round(a)+'px';
	},
	'callback': function() {
		$('blockbg').style.display = "none";
		
		if(controle=="FECHAR"){
			controle = "ABRIR";	
		}else{
			controle = "FECHAR";	
		}
		
		if($D.global.voltarNoticias) $('conteudoNoticia').innerHTML = '';
		
	}
});





// Funo que faz o deslizar da notcia

$D.global.EFEITO_NOTICIA_ABRIR = function(){
	$D.global.POSICAO2 = 0;
	$D.global.TWEEN_EFEITO.init = $D.page.visibleSize['x'];
	$D.global.TWEEN_EFEITO.end = 226;
	$S('itenNewsOpen').left = $D.global.TWEEN_EFEITO.init+'px';
	$S('itenNewsOpen').display = '';
	$D.global.TWEEN_EFEITO.play();
};

$D.global.EFEITO_NOTICIA_FECHAR = function(event){
	$('blockbg').style.display = "none";
	$D.global.TWEEN_EFEITO.init = 226;
	$D.global.TWEEN_EFEITO.end = $D.page.visibleSize['x'];
	$S('itenNewsOpen').left = $D.global.TWEEN_EFEITO.init+'px';
	$S('itenNewsOpen').display = '';
	$D.global.TWEEN_EFEITO.play();
	$('itenNewsOpen').open = false;
	$D.global.POSICIONA_BARRA();
};





function swfaddress(id){
	SWFAddress.setValue('/'+id+'/');
	return false;
}

$D.global.voltarNoticias = false;

function voltarNoticia(){
	controle = "";
	SWFAddress.setValue(''); 
	SWFAddress.setTitle('»{ Pub News }«');
	$D.global.voltarNoticias = true;
	$D.global.EFEITO_NOTICIA_FECHAR();
}


// Funo que cria os elementos HTML do portiflio
$D.global.DADOS_HTML = new Array();
$D.global.MENU_TIPOS = new Array();
$D.global.DADOS_HTML_PROP = new Array();
$D.global.CRIA_PROJETOS = function() {
	
	json = $D.global.DADOS_PORTFOLIO.news;
	html = '<table border="0" cellpadding="0" cellspacing="0" id="itenNewsTable">';
	for (var i = 0; i < json.length; i++) 
		html += '<tr rel="'+json[i].id+'" onclick="swfaddress('+json[i].id+')" style="cursor:pointer; background-image:url(/media/img/bgNews.gif);"><th><div id="bordafoto"><img src="'+json[i].img+'" width="65" height="46" style="margin:2px;" /></div></th><td id="tituloItem" style="padding-left:15px;">'+json[i].data+' &#8250;&nbsp; '+json[i].titulo+'</td><td width="140" align="right"><img style="margin-right:30px; border:none;" src="/media/img/setaNoticia.gif" alt="Ver na integra" /></td></tr>';
	html += '</table>';
	$('itenNews').innerHTML = html;
	
	//Atualiza o tamanho da tabela
	$D.global.REFRESH_COLLS();
	$D.global.TOTAL_SIZE = (json.length+1)*61;
	
	$D.global.DADOS_STATUS = "read";
	
};

function handle(delta) {

	if (delta < 0) $D.global.POSICAO += 5; else $D.global.POSICAO -= 5;
	
	$D.global.POSICAO = Math.min(Math.max(0,$D.global.POSICAO),120);
	
	$S('scrollSite').top = (($D.global.SIZE_BARRA-$D.global.SIZE_SCROLL)*$D.global.POSICAO/100)+'px';

}

/** Event handler for mouse wheel event.
 */
function wheel(event){
        var delta = 0;
        if (!event) /* For IE. */
                event = window.event;
        if (event.wheelDelta) { /* IE/Opera. */
                delta = event.wheelDelta/120;
                /** In Opera 9, delta differs in sign as compared to IE.
                 */
                if (window.opera)
                        delta = -delta;
        } else if (event.detail) { /** Mozilla case. */
                /** In Mozilla, sign of delta is different than in IE.
                 * Also, delta is multiple of 3.
                 */
                delta = -event.detail/3;
        }
        /** If delta is nonzero, handle it.
         * Basically, delta is now positive if wheel was scrolled up,
         * and negative, if wheel was scrolled down.
         */
        if (delta)
                handle(delta);
        /** Prevent default actions caused by mouse wheel.
         * That might be ugly, but we handle scrolls somehow
         * anyway, so don't bother here..
         */
        if (event.preventDefault)
                event.preventDefault();
	event.returnValue = false;
}

if (window.addEventListener) window.addEventListener('DOMMouseScroll', wheel, false);
window.onmousewheel = document.onmousewheel = wheel;
//window.onmousewheel = document.onmousewheel = wheel;

function handleChange(event) {
	SWFAddress.setTitle($('tituloNoticia').innerHTML);
	
	
	var id = event.path.replace(/\//g,'');	
	
	if(id != ''){
		$D.global.ABRIR_NOTICIA(id);
	}else if(controle!=""){
		$D.global.EFEITO_NOTICIA_FECHAR();
	}
	
}

SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleChange);

//SETAS

function getKey(e) {
	try{
		return event?(event.keyCode?event.keyCode:(event.which?event.which:event.charCode)):null;
	}catch(f){

		return e.keyCode;	
	}
}

window.onkeyup = function(event){
	var c = getKey(event);
	
	if(!/_38_|_40_/.test('_'+c+'_')) return;
	
	if(c == 38){
		handle(-1);
	}else{
		handle(1);
	}
};
