// Copyright 2008 JP7

var ad_predicta_i=0

$(document).ready(function(){
	setHeaderElement(elemento);
	
	if (isDef('init')) {
		init()
	}
	if (!isPopup && getElm('menuPosDiv')) {
		DO0.writeMenuItens();
	}
});

$(window).load(function(){
	//preChangeStyle(elemento);
	//init_ad_predicta(); // Necessário porque após onload posição se altera 
	if (is.ie && is.v < 7) {
		jp7_footer_repos();
	}
});

$(window).resize(function(){
	//init_ad_predicta();
	/*
	if ($('#googleMaps:visible').length) {
		setTimeout('google_maps_pos()', 100);
	}
	*/
	if (is.ie && is.v < 7) {
		jp7_footer_repos();
	}
});

function jp7_footer_repos() { // Fix para IE6
	$('div#footer').css('bottom', '0px'); // Força o browser perceber que mudou o valor
	$('div#footer').css('bottom', '-1px');
}

/**
 * Script de Publicidade Predicta
 * 
 * Atenção: Os targets servem apenas para posicionamento e espaçamento, a publicidade
 * será visualizada e clicável através do seu conteúdo original que está nos Buffers.
 */
var timeout_ad_predicta = [];
function init_ad_predicta() {
	for (var i = 1; i <= ad_predicta_i; i++) {
		timeout_ad_predicta[i] = 3000; 
		repos_ad_predicta(i);
	}
}

/**
 * Tenta reposicionar um banner, caso ele não possua altura ou largura tenta novamente
 * em 100 ms. Checa o timeout para parar o looping caso o banner realmente não carregue.
 * 
 * @param i Sufixo numérico no id dos buffers e targets.
 */
function repos_ad_predicta(i) {
	var target = jQuery('#ad_predicta_target_' + i);
	var buffer = jQuery('#ad_predicta_buffer_' + i);
	if (jQuery(buffer).width() && jQuery(buffer).height()) {
		buffer.css('left', target.offset().left);
		buffer.css('top', target.offset().top - 3);
		buffer.css('position', 'absolute');
		buffer.css('display', 'block');
	} else {
		timeout_ad_predicta[i] -= 100;
		if (timeout_ad_predicta[i] > 0) {
			setTimeout('repos_ad_predicta(' + i + ')', 100);
		}
	}
}

function init_login(){
	if ($('.box-viajante-ci-item').length) {
		var rand = Math.floor(Math.random() * $('.box-viajante-ci-item').length);
		$('.box-viajante-ci-item').eq(rand).show();
	}
	
	$.ajax({
		url: '../../xml/login.php',
		type: 'GET',
		dataType: 'xml',
		timeout: 2000,
		error: function(){
			//alert('Error loading XML document');
			getElm('header-login').style.display = 'none';
			getElm('header-logout').style.display = 'block';
		},
		success: function(xml) {
			var xml = $(xml);
			if (xml.find('id').text()) {
				getElm('header-login-nome').innerHTML = xml.find('nome').text();
				getElm('header-logout').style.display = 'none';
				getElm('header-login').style.display = 'block';
			} else {
				getElm('header-login').style.display = 'none';
				getElm('header-logout').style.display = 'block';
			}
			if ($('#form-comentario').length) {
				if (!$('#comentario_nome').val()) {
					$('#comentario_nome').val(xml.find('nome').text() + ' ' + xml.find('sobrenome').text());
				}
				if (!$('#comentario_email').val()) {
					$('#comentario_email').val(xml.find('id').text());
				}
			}
			if ($('#lojas-ci-busca').length) {
				if (!$('#lojasci_estado').val() && xml.find('estado').text()) {
					$('#lojasci_estado').val(xml.find('estado').text());
				}
				if (!$('#lojasci_cidade').val() && xml.find('cidade').text()) {
					$('#lojasci_cidade').find('option.' + xml.find('cidade').text()).attr('selected', true);
					
					if (xml.find('loja').text()) {
						ci_lojasCi_carregarLojas($('#lojasci_cidade')[0], '#lojaci', xml.find('loja').text());
					}
				}
			}
		}
	});
}

function ci_ancora_scroll(obj, plus) {
	var obj = $('#' + obj);
	if (obj) {
		var ypos = xpos = 0;
		ypos = obj.offset().top;		
		if (plus) ypos += plus;
		scrollTo(xpos, ypos);
	}
}

onscroll = function() {
	if (googlemaps_fs) {
		if (isDef('xx')) {
			clearTimeout(xx);
		}
		if (is.ie) {
			xx = setTimeout('getElm(\'googleMaps\').style.top = window.document.documentElement.scrollTop+10+\'px\'', 100);
		} else {
			xx = setTimeout('getElm(\'googleMaps\').style.top = window.pageYOffset+10+\'px\'', 100);
		} 
	}
}

// jQuery Abrir link externo com o header
/* Retirado temporariamente, sem layout
$(document).ready(function(){
	if ($('#container a')) {
	    $('#container a').each(function(i) {
		    if (this.id.indexOf('social_') == -1 && !$(this).hasClass('link_interno')) { // Ignora para a social bookmark inteira e links internos
			    if (this.href.indexOf('http://') == 0 && this.href.indexOf('http://' + document.location.hostname) == -1) {
				    this.href = '../../site/header_frame/?id_tipo=1017&url=' + URLEncode(this.href) + '&ref=' + URLEncode(document.location);
			    }
		    }
	    });
	}
});
*/

// Menu over
$(document).ready(function(){
	// Ajuste footer com tagcloud no IE6
	if (is.ie && is.v < 7) {
		if ($('div#footer').hasClass('footer-com-tagcloud')) {
			$('div#container').css('padding-bottom', $('div#footer').height());
			$('div#footer').height($('div#footer').height()); // IE6 - I know, it doesn´t seem to make sense
		}
		// Ajustes menu IE6
		$("#menu-principal li").hover(
			function() {
				$(this).addClass("hover");
				$(this).find('.submenu').show();
	    	},
			function() {
	    		$(this).removeClass("hover");
				$(this).find('.submenu').hide();
	    	}
		);
	}
});

// Links externos e Ads
$(document).ready(function(){
	ci_createExternalLinks()
	if(getElm('virgulaIframe')){
		setTimeout('getElm(\'virgulaIframe\').src=\'http://barras.virgula.com.br/barra_ci.html\'',100);
	}
	init_ad_predicta();
});
