
/* ********************************
 * Define functions
 * ******************************** */
function isDefined(variable) {
	return (typeof(window[variable]) == "undefined")? false: true;
}

function define(variable, value) {
	if(!isDefined(variable)) window[variable] = value;
}

/* ********************************
 * ElementById functions
 * ******************************** */
function hideElementById(id) {
	if(document.getElementById(id)) {
		document.getElementById(id).style.display = "none";
	}
}
function showElementById(id) {
	if(document.getElementById(id)) {
		document.getElementById(id).style.display = "block";
	}
}

function setInnerHTMLById(id, value) {
	var obj = document.getElementById(id);
	if(!obj) return;

	try {
		obj.innerHTML=value;
	} catch(err) {
		var txt="There was an error on this page.\n\n";
		txt+="Error description: " + err.description + "\n\n";
		txt+="Click OK to continue.\n\n";
		// alert(txt);

		var newdiv = document.createElement("div");
		newdiv.innerHTML = value;
		obj.appendChild(newdiv);
	}
}

/* ********************************
 * ElementsByClassName functions
 * ******************************** */
function getElementsByClassName(className, parent) {
	if(parent == undefined)
		parent = document;

	if (parent.getElementsByClassName != undefined)
		return parent.getElementsByClassName(className);

	var hasClassName = new RegExp("(?:^|\\s)" + className + "(?:$|\\s)");
	var allElements = parent.getElementsByTagName("*");
	var results = [];

	var element;
	for (var i = 0; (element = allElements[i]) != null; i++) {
		var elementClass = element.className;
		if (elementClass && elementClass.indexOf(className) != -1 && hasClassName.test(elementClass))
			results.push(element);
	}

	return results;
}

function stopEventsByClassName(className, parent) {
	var allElements;
	if(className == undefined || className == null) {
		if(parent == undefined) parent = document;
		allElements = parent.getElementsByTagName("*");
	} else {
		allElements = getElementsByClassName(className, parent);
	}
	var element;
	for (var i = 0; (element = allElements[i]) != null; i++) {
		if(element.onclick == undefined &&
			element.ondblclick == undefined &&
			element.onmousedown == undefined &&
			element.onmousemove == undefined &&
			element.onmouseup == undefined) continue;

		var returnFunc = function(e) {
			if(e && e.preventDefault) e.preventDefault();
			return false;
		};

		if(element.onclick == undefined) element.onclick = returnFunc;
		if(element.ondblclick == undefined) element.ondblclick = returnFunc;
		if(element.onmousedown == undefined) element.onmousedown = returnFunc;
		if(element.onmousemove == undefined) element.onmousemove = returnFunc;
		if(element.onmouseup == undefined) element.onmouseup = returnFunc;
		if(element.onselectstart == undefined) element.onselectstart = returnFunc;
		if(element.ondragstart == undefined) element.ondragstart = returnFunc;
	}
}

/* ********************************
 * Class functions
 * ******************************** */
function hasClass(ele,cls) {
	return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}

function addClass(ele,cls) {
	if (!hasClass(ele,cls)) ele.className += " "+cls;
}

function removeClass(ele,cls) {
	if (hasClass(ele,cls)) {
		var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
		ele.className=ele.className.replace(reg,' ');
	}
}

/* ********************************
 * Switch Class function
 * ******************************** */
function switchSelectedClass(obj, parentDept, optionClass, selectedClass) {
	var parent = obj.parentNode;
	while(--parentDept > 0) parent=parent.parentNode;

	var tabs = getElementsByClassName(selectedClass, parent);

	for(var i=0; i<tabs.length; i++)
		removeClass(tabs[i], selectedClass);

	addClass(obj, selectedClass);
}

/* ********************************
 * LoadEvent function
 * ******************************** */
function addLoadEvent(func) {
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		var oldonload = window.onload;
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		};
	}
}

/* ********************************
 * Debouncer function
 * ******************************** */
function debounce(func, timeout, execAsap) {
	var timer;
	var count = 0;

	return function debounced () {
		function delayed () {
			if (!execAsap) func(count);
			timer = null;
		};

		if (timer) {
			clearTimeout(timer);
			count++;
		} else {
			count = 0;
			if (execAsap) func(0);
			else count++;
		}

		timer = setTimeout(delayed, timeout || 1000);
	};
}
/* ********************************
 * Cookie functions
 * ******************************** */
function setCookie(name, value, days, path, domain, secure) {
	if (days) {
		var date=new Date();
		date.setDate(date.getDate() + days);
		var expires = "; expires="+date.toUTCString();
	} else expires = "";

	document.cookie = name + "=" + escape(value) + expires +
		( ( path ) ? ";path=" + path : "" ) +
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

function getCookie(name) {
	var cookies=document.cookie.split(";");
	for (var i=0;i<cookies.length;i++) {
		var cookie = cookies[i];

		var cName=cookie.substr(0,cookie.indexOf("="));
		cName=cName.replace(/^\s+|\s+$/g,"");
		var value=cookie.substr(cookie.indexOf("=")+1);
		if (cName==name) return unescape(value);
	}
}

function removeCookie(name) {
	setCookie(name,"",-1);
}

/* ********************************
 * URL Parameter function
 * ******************************** */
function getUrlParameter(name) {
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null ) return "";
	else return results[1];
}
/* ********************************
 * Math functions
 * ******************************** */
function floatingHighestCommonFactor(n1, n2) {
	var mult = 1;
	while(n1 != Math.round(n1) || n2 != Math.round(n2)) {
		n1 *= 10;
		n2 *= 10;
		mult *= 10;
	}
	return highestCommonFactor(n1, n2) / mult;
}

function highestCommonFactor(n1,n2){
	var gcd=1;
	if (n1>n2) {
		n1=n1+n2;
		n2=n1-n2;
		n1=n1-n2;
	}
	if ((n2==(Math.round(n2/n1))*n1)) {
		gcd=n1;
	} else {
		for (var i = Math.round(n1/2) ; i > 1; i=i-1) {
			if ((n1==(Math.round(n1/i))*i) &&
				(n2==(Math.round(n2/i))*i)) {
				gcd=i;
				i=-1;
			}
		}
	}
	return gcd;
}


function lowestCommonMultiple(n1,n2){
	return n1*n2/highestCommonFactor(n1,n2);
}

/* ********************************
 * Slider functions
 * ******************************** */
function getObjLeft(obj){
	var leftValue= 0;
	while(obj){
		leftValue+= obj.offsetLeft;
		obj= obj.offsetParent;
	}
	return leftValue;
}

function sliderValue(obj, min, max, step, defaultValue, newValue) {
	var totalSteps = (max-min)/step;
	var objvalToVal = function(value) {
		// o 10000000000 e' solução para o seguinte problema: alert(0.1 * 0.2);
		return Math.round((Math.round(value*totalSteps)*step + min)*10000000000)/10000000000;
	};

	var valToObjval = function(value) {
		return (value-min)/(max-min);
	};

	var value = obj.objValue;
	if(value == undefined) newValue = defaultValue;
	if(newValue == undefined) newValue = objvalToVal(value);

	// var oldNewValue = newValue;
	// var origObj = obj.objValue;
	if(newValue < min) newValue = min;
	if(newValue > max) newValue = max;
	var value = valToObjval(newValue);
	obj.objValue = value;
	var sliderWidth = obj.parentNode.offsetWidth;
	var barWidth = obj.offsetWidth;
	var pos = value*(sliderWidth-barWidth);
	obj.style.left = pos + "px";
	// return "calcValue=" + oldNewValue + " newValue=" + newValue + " obj=" + obj.objValue + " origObj=" + origObj;
	return obj.value = newValue;
}

function makeSliders(className) {
	var divs = getElementsByClassName(className);

	for(var i=0; i<divs.length; i++) {
		var sliderDiv = divs[i];
		if(!sliderDiv || sliderDiv.getElementsByTagName('div').length != 1) return;

		var barTag = sliderDiv.getElementsByTagName('div')[0];
		barTag.style.position="relative";

		try {
			if(barTag.onmouseup) barTag.onmouseup();
		} catch(err) {}

		sliderDiv.onmousedown = function(event) {
			//if(!event || !event.currentTarget) return;
			//var sliderDiv2 = event.currentTarget;
			var sliderDiv2 = this;
			if(sliderDiv2.getElementsByTagName('div').length != 1) return;
			var barTag2 = sliderDiv2.getElementsByTagName('div')[0];
			if(!barTag2) return;

			document.onmousemove = function(eventMove) {
				if (!eventMove && window.event) eventMove = window.event;
				if(!eventMove) return;

				var sliderLeft = getObjLeft(sliderDiv2);
				var sliderWidth = sliderDiv2.offsetWidth;

				var barLeft = getObjLeft(barTag2);
				var barWidth = barTag2.offsetWidth;

				var pos = eventMove.clientX - sliderLeft - barWidth/2;
				if(pos<0) pos = 0;
				if(pos>sliderWidth-barWidth) pos = sliderWidth-barWidth;
				barTag2.objValue=pos/(sliderWidth-barWidth);
				try {
					if(barTag2.onmouseup) barTag2.onmouseup(eventMove);
				} catch(err) {}
				barTag2.style.left = pos + "px";
				return false;
			};
			document.onmouseup = function(event) {
				document.onmousemove=null;
				document.onmouseup=null;
				try {
					if(barTag2.onmouseup) barTag2.onmouseup(event);
				} catch(err) {}
				return false;
			};

			document.onmousemove(event);
			return false;
		};
	}
}

/* ********************************
 * ResizeDiv function
 * ******************************** */

function resizeDiv(mainDivName, centerScreenDivName, overflowDivName, minOverflowHeight, margins) {
	var mainDiv = document.getElementById(mainDivName);
	var overflowDiv = document.getElementById(overflowDivName);
	var centerScreenDiv = document.getElementById(centerScreenDivName);
	if(!mainDiv || !overflowDiv || !centerScreenDiv) return false;

	mainDiv.style.display = "block";
	overflowDiv.style.height = "0px";
	var extraHeight = centerScreenDiv.offsetHeight + 2*margins;

	var getWindowInnerHeight = function() {
		var windowInnerHeight = window.innerHeight;
		if(windowInnerHeight == undefined) // para o IE
			windowInnerHeight = document.documentElement.clientHeight;
		return windowInnerHeight;
	};

	var getOverflowHeight = function() { return getWindowInnerHeight() - extraHeight; };

	if(window.innerWidth < centerScreenDiv.offsetWidth + 2*margins ||
			getOverflowHeight() < minOverflowHeight) {
		mainDiv.style.display = "none";
		alert("Resolu&ccedil;&atilde;o insuficiente.");
		return;
	}

	centerScreenDiv.style.marginLeft = '-' + (centerScreenDiv.offsetWidth/2) + 'px';

	var autoResizeDiv = function() {
		var newOverflowHeight = getOverflowHeight();

		var overflowScroll = overflowDiv.scrollTop;

		overflowDiv.style.height = '';
		var maxOverflowHeight = overflowDiv.offsetHeight;

		if(newOverflowHeight < minOverflowHeight)
			overflowDiv.style.height = minOverflowHeight + "px";
		else if(newOverflowHeight < maxOverflowHeight)
			overflowDiv.style.height = newOverflowHeight + "px";

		overflowDiv.scrollTop = overflowScroll;

		if(newOverflowHeight > maxOverflowHeight) {
			centerScreenDiv.style.top = "50%";
			centerScreenDiv.style.marginTop = '-' + (centerScreenDiv.offsetHeight/2) + 'px';
		} else {
			centerScreenDiv.style.top = "";
			marginTop = (getWindowInnerHeight() - centerScreenDiv.offsetHeight)/2;
			centerScreenDiv.style.marginTop = '' + (marginTop>0?marginTop:0) + 'px';
		}
	};

	autoResizeDiv();
	window.onresize = debounce(autoResizeDiv, 300);
};

/* ********************************
 * Video functions
 * ******************************** */
function showVideo(url) {
	if(!document.getElementById("videoDiv")) {
		var divTag = document.createElement("div");
		divTag.id = "videoDiv";
		divTag.className ="fullScreenDiv";
		divTag.innerHTML = '\n\
			<div class="fullScreenDiv" style="background-color:#000000; opacity: .7; filter: alpha(opacity=70); -moz-opacity: .7;"></div>\n\
			<div class="centerScreenDiv" style="height: 534px; width: 610px;">\n\
				<span class="cts_layer_top"><span>&nbsp;</span></span>\n\
				<div class="cts_layer_cont">\n\
					<div class="mg_bt_fechar" style="height: 28px;">\n\
						<a href="#" onclick="hideVideo(); return false;">Fechar</a>\n\
					</div>\n\
					<div id="videoEmbed" class="mg_layer_int" style="margin: 0 0 0 14px;">\n\
						\n\
					</div>\n\
				</div>\n\
				<span class="cts_layer_bot"><span>&nbsp;</span></span>\n\
			</div>';
		document.body.appendChild(divTag);
	}
	showElementById('videoDiv');
	if(document.getElementById("videoEmbed")) {
		document.getElementById("videoEmbed").innerHTML = '\n\
			<embed src="' + url + '" type="application/x-shockwave-flash" \
				autoplay="true" autostart="true" allowFullScreen="true" width="580" height="483"></embed>';
	}
}
function hideVideo() {
	hideElementById('videoDiv');
	if(document.getElementById("videoEmbed")) {
		document.getElementById("videoEmbed").innerHTML = "";
	}
}

/* ********************************
 * Scroller functions
 * ******************************** */
var mover = 0;
var maxscroll = 0;

function goToTab(name) {
	if(!name)
		return;
	var tabs = jq('#scrolabbleslider li a');
       
	for(var i = 0; i < tabs.length; ++i) {
		if(jq.trim(jq(tabs[i]).html()) == name) {
			jq(tabs[i]).click();
			return;
		}
	}
}

function showCompleteTab(obj) {
	window.location.hash = $(obj).attr("href").substr(1);

	var slider = document.getElementById('scrolabbleslider');
	if(!slider) return;
	var container = document.getElementById('slidercontainer');
	if(!container) return;

	var beforeWidth = 0;
	var parentli = obj.parentNode;
	var allLi = parentli.parentNode.getElementsByTagName("LI");
	for(var i=0; i<allLi.length; i++) {
		if(allLi[i] == parentli) break;
		beforeWidth += allLi[i].offsetWidth;
	}

	if(mover > beforeWidth)
		moveTabsTo(beforeWidth);
	if(mover + container.offsetWidth < beforeWidth + obj.offsetWidth)
		moveTabsTo(beforeWidth + obj.offsetWidth - container.offsetWidth);
}

var moveTimeout;
function moveTabsTo(to) {
	if(to==mover) return;
	var slider = document.getElementById('scrolabbleslider');
	if(!slider) return;
	to=Math.round(to);
	var length=to-mover;
	var step = Math.round(length/10);
	if(step==0) step=length>0?1:-1;
	mover = Math.round(mover + step);
	slider.style.marginLeft = -(mover) +'px';
	if(moveTimeout) window.clearTimeout(moveTimeout);
	if(to!=mover) moveTimeout=setTimeout('moveTabsTo(' + to + ')', 15);
}

function startMove(obj, direccao){
	var slider = document.getElementById('scrolabbleslider');
	if(!slider) return;
	if(moveTimeout) window.clearTimeout(moveTimeout);

	var lastTime = new Date().getTime();
	var windowsInterval;

	var stopMove = function() {
		window.clearInterval(windowsInterval);
		obj.onmouseup = obj.onmouseout = obj.onmousemove = null;
		return false;
	};

	var move = function(){
		var newTime = new Date().getTime();

		var step = 2*(newTime - lastTime)/10;
		if(step < 1) step = 10;

		mover = mover + direccao*step;

		if (mover > maxscroll) {
			stopMove();
			mover = maxscroll;
		}

		if (mover < 1) {
			stopMove();
			mover = 0;
		}

		slider.style.marginLeft = -(mover) +'px';
		lastTime = newTime;
	};
	move();
	stopMove();
	obj.onmouseup = obj.onmouseout = stopMove;
	windowsInterval = window.setInterval(move, 10);
	return false;
}

addLoadEvent(function() {
	var slider = document.getElementById('scrolabbleslider');
	if(!slider) return;
	var container = document.getElementById('slidercontainer');
	if(!container) return;
	var scrolldir = document.getElementById('deta_vartab_dir');
	if(!scrolldir) return;
	var scrollesq = document.getElementById('deta_vartab_esq');
	if(!scrollesq) return;

	stopEventsByClassName('deta_vartab_dir');
	stopEventsByClassName('deta_vartab_esq');

	slider.style.marginLeft = '-10000px';
	slider.style.width = "auto";
	var larguracontainer = slider.offsetWidth;
	slider.style.marginLeft = '0px';
	slider.style.width = "10000px";

	maxscroll = larguracontainer - container.offsetWidth;
	if(maxscroll < scrolldir.offsetWidth - scrollesq.offsetWidth) {
		container.style.width = (scrolldir.offsetWidth + scrollesq.offsetWidth + container.offsetWidth) + "px" ;
		scrolldir.style.display = "none";
		scrollesq.style.display = "none";
	}
});

addLoadEvent(function() {
	if(!isDefined("$")) return;
	if(!$(".tabs")) return;
	if(!$(".tabs").tabs) return;
	$(".tabs").tabs("div.pane > div");
});

/* ********************************
 * SwitchMenu functions for faq
 * ******************************** */
function SwitchMenu(obj) {
	if(!document.getElementById) return;

	var faqlist = document.getElementById("list_faqs");
	if(!faqlist) return;
	var ar = faqlist.getElementsByTagName("span");
	if(!ar) return;

	var el = document.getElementById(obj);
	var display = el?el.style.display:"";

	for (var i=0; i<ar.length; i++) {
		if (ar[i].className=="faqs_resp")
			ar[i].style.display = "none";
	}

	if(!el) return;
	if (display != "block") {
		el.style.display = "block";
	} else {
		el.style.display = "none";
	}
}

addLoadEvent(function() {
	SwitchMenu("faqs" + getUrlParameter("faqs"));
});


/* ********************************
 * 
 * SLIDE TOGGLER FOR CONTENT
 * 
 * ******************************** */
 $(document).ready(function(){ 
	$(".ptneg_toggler").click(function(){ 
		if ($(this).hasClass("ptneg_toggled")) { $(this).removeClass("ptneg_toggled").next().slideUp();  } 
		else { 
			$(".ptneg_toggler").removeClass("ptneg_toggled").next().slideUp(); 
			$(this).toggleClass("ptneg_toggled").next().slideDown("slow"); 
		} 
		return false;
	});  
	$(".ptneg_toggler_in").click(function(){ 
		if ($(this).hasClass("ptneg_toggled_in")) { $(this).removeClass("ptneg_toggled_in").next().slideUp();  } 
		else { 
			$(this).siblings(".ptneg_toggler_in").removeClass("ptneg_toggled_in").next().slideUp(); 
			$(this).toggleClass("ptneg_toggled_in").next().slideDown("slow"); 
		} 
		return false;
	});
	/* SLIDE GALLERY FOR NEWS */










		$(".ptn_noticias_galeria").before("<br/>");
	$(".ptn_noticias_galeria").wrap("<table border='0' cellspacing='0' cellpadding='0' width='462px'><tr><td></td></tr></table>");
	$(".ptn_noticias_galeria").closest("table").append("<tr><td><div class='clear'>&nbsp;</div><div class='ptn_noticias_galeria_nav'></div></td></tr>");
	$(".ptn_noticias_galeria").prepend("<div class='ptn_noticias_galeria_nav_prev'>&laquo;</div><div class='ptn_noticias_galeria_nav_next'>&raquo;</div>").each(function(){
		function ptn_noticias_galeria_pager(i,slide) {
			return '<a href="#" id="'+ i +'">&#9679;</a>'
		}
		function ptn_noticias_galeria_after(curr, next, opts) {
			var parent = $(this).parent().parent();
			var index = opts.currSlide;
			if (index == 0) {parent.find('.ptn_noticias_galeria_nav_prev').css({"visibility":"hidden"});}
			else {parent.find('.ptn_noticias_galeria_nav_prev').css({"visibility":"visible"});}
			if (index == opts.slideCount - 1) {parent.find('.ptn_noticias_galeria_nav_next').css({"visibility":"hidden"});}
			else {parent.find('.ptn_noticias_galeria_nav_next').css({"visibility":"visible"});}
		}
		var p = this.parentNode;
		$(this).cycle({
			pause:1,
			fx: 'fade',
			speed:  600,
			timeout: 0,
			pager:  $(this).closest("table").find('.ptn_noticias_galeria_nav'),
			pagerAnchorBuilder: ptn_noticias_galeria_pager,
			slideExpr: 'img',
			after:ptn_noticias_galeria_after,
			prev: $('.ptn_noticias_galeria_nav_prev', p),
			next: $('.ptn_noticias_galeria_nav_next', p),
			activePagerClass:'ptn_noticias_galeria_nav_active'
		});
	});
	/* CHAMADA DESTAQUE TABS */
	$(".destaque_tabs").tabs("> .pane");
}) 

function HideContent(d) {
if(d.length < 1) { return; }
document.getElementById(d).style.display = "none";
}
function ShowContent(d) {
if(d.length < 1) { return; }
document.getElementById(d).style.display = "block";
}
function ReverseContentDisplay(d) {
if(d.length < 1) { return; }
if(document.getElementById(d).style.display == "none") { document.getElementById(d).style.display = "block"; }
else { document.getElementById(d).style.display = "none"; }
}
function HideMenu(divName) {
var objDiv = document.getElementById(divName)
if (objDiv.style.display == 'none') {objDiv.style.display = '';}
else {objDiv.style.display = 'none'; }
}
