// JavaScript Document
function getURL ()
{
	var str = window.location.pathname.split("/");
	var rt = "";
	for(var i = 0; i < str.length-1; i++)
	{
		rt += 	str[i]+"/";
	}
	
	rt = "http://"+window.location.host+""+rt;
	return rt;
}

var is_send = false;
var file_name = "";

function SendComplete (name)
{
	file_name = name;
	$("#fname").attr('value',name);
	is_send = true;
}

function sendContact (form)
{
	var elements = form.elements;
	
	var out = "";
	
	var values = new Array();
	
	for(var i =0 ; i < elements.length; i++)
	{
		var obj = elements[i];
		values.push(new Array(obj.name,obj.value));
	}
	
	$.post("send_contact.nzk.php",{'data':$.toJSON(values)}, function (response){
	
		$("#resultForm").html("Mensagem Enviada, em breve entraremos em contato");
		$("#resultForm").hide().fadeIn();		
												
	});
		
	$("#resultForm").html("Processando Mensagem");
	$("#resultForm").hide().fadeIn();
	form.reset();
}

function sendWork (form)
{
	var elements = form.elements;
	
	var out = "";
	
	var values = new Array();
	
	for(var i =0 ; i < elements.length; i++)
	{
		var obj = elements[i];
		values.push(new Array(obj.name,obj.value));
	}
	
	$.post("send_work.nzk.php",{'data':$.toJSON(values)}, function (response){
	
		$("#resultFormWork").html("Mensagem Enviada, em breve entraremos em contato");
		$("#resultFormWork").hide().fadeIn();		
												
	});
		
	$("#resultFormWork").html("Processando Mensagem");
	$("#resultFormWork").hide().fadeIn();
	form.reset();
}


function workFormValidation (formulario) 
{
	var camposOb = new Array ("nome","email","telefone","cidade");
	var tipos = new Array("string","email","string","string");
	
	var val = true;
	
	var a = null;
	
	for(a=0; a < camposOb.length; a++) {
	
		if(!caseValidate(camposOb[a],tipos[a])) {
			val = false;
			break;		
		}
	
	}
	//
	if(val) {
		sendWork(formulario);
	}
}



//
var show = 0;
var tid = 0;
var last = "";

var obj = null;

function openSubMenu (table)
{
	
	if(last != table)
	{
		last = table;
		$("#infosub").fadeOut();
		$.post('getSubMenu.php', {'table':table}, function (response) {
											  
			$("#submenu").html(response);
			$("#submenu").fadeIn();
			$("#submenu").slideDown("slow");
			obj = null;	

			
											  
		});
	}
	if($("#submenu").css('display') == "none")
	{	
		$("#submenu").fadeIn();
	}
}





function checkHover() {
	if (obj) {
		$("#submenu").fadeOut('fast');	
	} //if
} //checkHover


function initIcons ()
{
	var icons = new Array("ico_money","ico_tempo","ico_map");
	
	for(var i = 0; i < icons.length; i++)
	{
		$("#"+icons[i]).mouseover(function () { $(this).attr('src','imgs/'+$(this).attr('id')+"_color.gif"); });
		$("#"+icons[i]).mouseout(function () { $(this).attr('src','imgs/'+$(this).attr('id')+"_gray.gif"); });
		$("#a_"+icons[i]).easyTooltip({ content: "<div id='tt_default'><img src='imgs/tt_"+icons[i]+".gif' /></div>"});
	}
}
function isout () {
		obj = $("#submenu");
		setTimeout(
			"checkHover()",
			2000);
}

$(document).ready(function() {
	// Submenu					   
	$('#submenu').hover(function() {
		if (obj) {
			//$("#submenu").fadeOut('fast');	
			obj = null;
		} //if
		
		$("#submenu").fadeIn('fast');
	}, isout);
	/**
	Tooltips
	**/
	$("#sistema").easyTooltip({
	
		content:"<img src='imgs/tooltipSistema.gif' />"
	
	});
	
	initIcons();
	
	
	
});

function hideSubMenu ()
{
	checkHover();
}

function getScrollXY() {
    var x = 0, y = 0;
    if( typeof( window.pageYOffset ) == 'number' ) {
        // Netscape
        x = window.pageXOffset;
        y = window.pageYOffset;
    } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
        // DOM
        x = document.body.scrollLeft;
        y = document.body.scrollTop;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
        // IE6 standards compliant mode
        x = document.documentElement.scrollLeft;
        y = document.documentElement.scrollTop;
    }
    return [x, y];
}

var pg = 0;
var nlock = false;
var b = '';

function setScroll ()
{
	window.onscroll = function () {	
		
		var s = (document.documentElement.scrollTop+document.body.clientHeight);
		var t = document.documentElement.scrollHeight;
		
		if(s >= t-100)
		{			
			getNews();			
		}	
	};
}

function removeScroll ()
{
	window.onscroll = null;
}
function getNews () {
	if(pg <= 3)
	{
	  if(nlock == false)
	  {
		nlock = true;
		
		if($("#target_news").html() == "")
		{
			pg = 0;
		}
		
		$.post('news.ajax.nzk.php',{'pg':pg, 'b':b},function (response) {
			if(response.length > 0)
			{
				var div = document.createElement("div");
				$(div).html(response);
				$("#target_news").append(div);		
				$(div).hide().fadeIn();
				
				nlock = false;
			}		  
			else
			{				
				removeScroll();
			}
		});
		pg++;
	  }
	}
	else
	{
		removeScroll();	
		pg = 0;
	}
}
function reloadNews ()
{
	nlock = false;
	$("#target_news").html("");
	getNews();
	
}
function setSizeText (ismais)
{
	var $max = 16;
	var $min = 10;
	
	var size = parseInt($("#text_main").css('font-size'));
	
	if(ismais)
	{
		if(size <= $max)
		{
			$("#text_main").css('font-size', (size+1)+"px")	;
			$("#text_main").hide().fadeIn();
		}
		
	}
	else
	{
		if(size >= $min)
		{
			$("#text_main").css('font-size', (size-1)+"px")	;	
			$("#text_main").hide().fadeIn();
		}
	}
	
}
function searchNews (busca) {
	  b = busca;
	  pg = 0;
	  $.post('news.ajax.nzk.php',{'pg':pg, 'b':b},function (response) {	
		  if(response)
		  {
		  	$("#target_news").html(response);		
			$("#target_news").hide().fadeIn();
		  	pg++;
		  	setScroll();
		  }
		  else
		  {
			  $("#target_news").html("<div align='right'>Sua busca por  <b><u>"+busca+"</u></b> não retornou resultado.</div>");
			  $("#target_news").hide().fadeIn();
			  pg = 0;
			  b = "";
			  nlock = true;
			  setTimeout("reloadNews()",5000);
		  }
		  
	  });
}


var last = null;
var totalT = 0;

function setTopics (num) {
	
	totalT = num;
}

function insertNews ()
{
	$.post("register.ajax.nzk.php", {"nome": $("#n_nome").attr('value'), "email":$("#n_email").attr('value')}, function (response) {	
		$("#n_resp").html(response);
		$("#n_resp").hide().fadeIn();
		
		$("#n_nome").attr('value', '');
		$("#n_email").attr('value', '');
		
		setTimeout(function () { $("#n_resp").fadeOut();}, 5000);
	});
}

function openTopics (tg,obj)
{	
	$("#"+tg).fadeIn();	
	$('html,body').animate({scrollTop: $(obj).offset().top}, 3000);	
}
function getSearch(ob)
{
	var q = encodeURI($("#"+ob).val());
	window.open("#/busca/"+q,"_self");		
}