var ipinternet = "";
var ipinterno = "";
var bodyheight = 0;
var bodywidth = 0;
var portalheight = 0;
var portaltop = 0;
var portalleft = 0;

$(function(){
	$('img[src$=.png]').each(function() {
       fixPng(this);
    });
});

$(window).bind("resize",function(){
	bodyheight = $(document).height();
	bodywidth = $(document).width();
	centerHor("#portal");
	centerHor("#footermiolo");
	portalleft = parseInt($("#portal").css("left"));
	portalleft = portalleft + 205 + "px";
	$("#buscabox").css("left",portalleft);
	$("#clockbar").css("left",portalleft);
});

$(document).ready(function(){
	portaltop = $("#portal").css("top");
	portalleft = $("#portal").css("left");
	bodyheight = $(document).height();
	bodywidth = $(document).width();
	getCursor();
	putFooter();
	centerHor("#portal");
	centerHor("#footermiolo");

	portalleft = parseInt($("#portal").css("left"));
	portalleft = portalleft + 205 + "px";
	$("#buscabox").css("left",portalleft);
	$("#clockbar").css("left",portalleft);

	$('.btnnavtop').each(function() {
		var selected = $(this).attr("longdesc");
		if (selected == "selected"){
			$(this).css("background-position","-95px 0px");
			$(this).css("color","#FF802B");
			$(this).css("font-size","13px");
		}else{
			$(this).css("background-position","1px 0px");
		}
    });

	var optionsEST = {
		utc: true,
		utc_offset: -3,
		fontFamily: 'verdana',
		fontSize: '10px',
		foreground: '#505050'		
	}

	getGeoPositionByIp("location");

	setCursor("#btnbusca");
	setCursor(".btnnavtop");
	
	$('#clockit').jclock(optionsEST);
	
	$(".btnnavtop").bind("mouseover", function(){
		$(this).css("background-position","-191px 0px");
		$(this).css("color","#000000");
		$(this).css("padding-top","15px");
		$(this).css("font-size","12px");
	});
	$(".btnnavtop").bind("mouseout", function(){
		var selected = $(this).attr("longdesc");
		if (selected == "selected"){
			$(this).css("background-position","-95px 0px");
			$(this).css("color","#FF802B");
			$(this).css("font-size","13px");
		}else{
			$(this).css("background-position","1px 0px");
			$(this).css("color","#FFffff");
			$(this).css("font-size","12px");
		}
		$(this).css("padding-top","12px");
	});

	$(".btnnavtop").bind("click", function(){
		clearAllNavTop();
		$(this).attr("longdesc","selected");
		$(this).css("background-position","-95px 0px");
		$(this).css("color","#FF802B");
		$(this).css("font-size","13px");
		$.ajax({
			type: "GET",
			url: "mainpages/" + $(this).attr("rel") + ".miolo.inc.php",
			beforeSend: bigAjaxLoading("#portalmiolo"),
			success:function(dadosPagina){
				$("#portalmiolo").html(dadosPagina);
			},
			error:function(){
				$("#portalmiolo").html("<span style='color:#C63232; margin-top: -20%; padding-top: 50%; min-width: 100%; min-height: 100%; width: 100%; height: 100%; text-align: center;'>Em desenvolvimento...</span>");
				putFooter();
			}
		});
	});

	$("#btnbusca").bind("mouseover", function(){
		$(this).css("background-position","-12px 0px");
	});
	$("#btnbusca").bind("mouseout", function(){
		$(this).css("background-position","0px 0px");
	});
	$("#btnbusca").bind("click", function(){
		var txtbusca = $("#txtbusca").val();
		if (txtbusca != "seu texto aqui..."){
			if(txtbusca.lenght < 5){
				alert("Para buscas no portal informe opções acima de cinco (5) caracteres");
			}
		}else{
			alert("Para efetuar uma busca no portal informe o seu texto com no mínimo de cinco (5) caracteres");
		}
	});
});

function clearAllNavTop(){
	$(".btnnavtop").each(function(){
		$(this).attr("longdesc","unselected");
		$(this).css("background-position","1px 0px");
		$(this).css("color","#FFffff");
		$(this).css("font-size","12px");
	});
}

function pageLoad(pageurl, target, subdir){
	$.ajax({
		type: "POST",
		url: subdir + "/" + pageurl + ".html",
		beforeSend: bigAjaxLoading("#"+target),
		success:function(dadosPagina){
			$("#"+target).slideDown("fast");
			$("#"+target).html(dadosPagina);
			var newheight = $("#"+target).height();
			var newtop = parseInt($("#"+target).css("top"));
			$("#"+target).parent().css("height",(newheight+newtop+15)+"px");
			$("#"+target).parent().css("overflow-x","hidden");
		},
		error:function(){
			$("#"+target).slideDown("fast");
			$("#"+target).html("<span style='color:#C63232; margin-top: -20%; padding-top: 50%; min-width: 100%; min-height: 100%; width: 100%; height: 100%; text-align: center;'>Em desenvolvimento...</span>");
			var newheight = $("#"+target).height();
			var newtop = parseInt($("#"+target).css("top"));
			$("#"+target).parent().css("height",(newheight+newtop+15)+"px");
		}
	});	
}
function bigAjaxLoading(camada){
	$(camada).html("<span style='color:#C63232; margin-top: -20%; padding-top: 50%; min-width: 100%; min-height: 100%; width: 100%; height: 100%; text-align: center;'><img src='interface/images/bigloading.gif' hspace='10' align='absmiddle' border='0' /><br/>AGUARDE</span>");
}
function smallAjaxLoading(camada){
	$(camada).html("<img src='interface/images/smallloading.gif' hspace='10' align='absmiddle' border='0' />");
}
function getCursor(){
	$.browser.msie?cssCursor='hand':cssCursor='pointer';
}
function setCursor(object){
	$(object).css("cursor", cssCursor);
}
function getRelativeCoordinates(event, reference) {
	var x, y;
	event = event || window.event;
	var el = event.target || event.srcElement;

	if (!window.opera && typeof event.offsetX != 'undefined') {
	// Use offset coordinates and find common offsetParent
	var pos = { x: event.offsetX, y: event.offsetY };

	// Send the coordinates upwards through the offsetParent chain.
	var e = el;
	while (e) {
		e.mouseX = pos.x;
		e.mouseY = pos.y;
		pos.x += e.offsetLeft;
		pos.y += e.offsetTop;
		e = e.offsetParent;
	}
	// Look for the coordinates starting from the reference element.
	var e = reference;
	var offset = { x: 0, y: 0 }
	while (e) {
		if (typeof e.mouseX != 'undefined') {
			x = e.mouseX - offset.x;
			y = e.mouseY - offset.y;
			break;
		}
		offset.x += e.offsetLeft;
		offset.y += e.offsetTop;
		e = e.offsetParent;
	}

	// Reset stored coordinates
	e = el;
	while (e) {
		e.mouseX = undefined;
		e.mouseY = undefined;
		e = e.offsetParent;
	}
}else{
	// Use absolute coordinates
	var pos = getAbsolutePosition(reference);
	x = event.pageX  - pos.x;
	y = event.pageY - pos.y;
}
  // Subtract distance to middle
return { x: x, y: y };
} 
function getAbsolutePosition(element) {
	var r = { x: element.offsetLeft, y: element.offsetTop };
	if (element.offsetParent) {
		var tmp = getAbsolutePosition(element.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}
	return r;
}
function centerHor(object){
	var wObj = parseInt($(object).outerWidth());
	var leftObject = (bodywidth - wObj)/2;
	$(object).css("left", leftObject);
}
function certerVer(object){
	bodyheight = $(document).height();
	var topObject = (parseInt(bodyheight) - parseInt($(object).outerHeight())) / 2 + "px";
	$(object).css("top", topObject);
}
function getGeoPositionByIp(target){
	$.ajax({
		dataType: "json",
		url: "http://iplocationtools.com/ip_query.php?output=json&callback=?&ip=",
		beforeSend: smallAjaxLoading("#"+target),
		success:function(data){
			if(data['Status'] == 'OK'){
				var seuip = data['Ip'];
				var cidade = data['City'];
				var estado = data['RegionName'];
				var pais = data['CountryCode'];
				var saida = cidade;
				var dhoje = new Date();
				var dia = dhoje.getDate();
				var meses = Array("janeiro","fevereiro","março", "abril", "maio", "junho", "julho", "agosto", "setembro", "outubro", "novembro", "dezembro");
				var datafinal = ", " + dia + " de " + meses[dhoje.getMonth()] + " de " + dhoje.getYear() + ".";
				$("#"+target).html(cidade + datafinal);
			}
		},
		error:function(){
			alert("erro");
		}
	});
}
function putFooter(){
	$("#footer").css("top",parseInt($("#portal").height()) - parseInt($("#portal").css("top")) + parseInt($("#buscabox").height()) - 9 + "px");
}
String.prototype.replaceAll = function( strTarget, strSubString ){
	var strText = this;
	var intIndexOfMatch = strText.indexOf( strTarget );
	while (intIndexOfMatch != -1){
		strText = strText.replace( strTarget, strSubString )
		intIndexOfMatch = strText.indexOf( strTarget );
	}
	return( strText );
}
