$(document).ready(function(){
	$(".btGreen").mouseover(function(){ $(this).parent().find(".btGreen").addClass("btGreenOver"); } );
	$(".btGreen").mouseout( function(){ $(this).parent().find(".btGreen").removeClass("btGreenOver"); } );
	$(".btRed").mouseover( function(){ $(this).parent().find(".btRed").addClass("btRedOver"); } );
	$(".btRed").mouseout( function(){ $(this).parent().find(".btRed").removeClass("btRedOver"); } );
	$(".btGray").mouseover( function(){ $(this).parent().find(".btGray").addClass("btGrayOver"); } );
	$(".btGray").mouseout( function(){ $(this).parent().find(".btGray").removeClass("btGrayOver"); } );
	$(".navFlash").mouseover( function(){ $(this).addClass("navFlashOver"); } );
	$(".navFlash").mouseout( function(){ $(this).removeClass("navFlashOver"); } );
	$(window).scroll( function(){ restaura(); } );
	restaura();
});
jQuery.preloadImages = function(){
	for(var i = 0; i<arguments.length; i++){
		//jQuery("<img>").attr("src", arguments[i]);
	}
}
var altura = 0;
var largura = 0;
var scrollTop = 0;
function restaura(){
	altura = $(window).height();
	scrollTop = $(window).scrollTop();
	largura = $(window).width();
	$("#grayBG").height(altura);
	$("#grayBG").width(largura);
	$("#grayBG").css( "top",scrollTop);
	$("#grayBG").css( "left",0);
	$("#overAjax").height(altura);
	$("#overAjax").width(largura);
	$("#overAjax").css( "top",scrollTop);
	$("#overAjax").css( "left",0);
	//$("#imgAlt").height(altura - 175);
	//$("#sizeH").height(altura - 160);
}
function Get_Cookie( check_name ) {
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false;
	var i = '';
	for ( i = 0; i < a_all_cookies.length; i++ ){
		a_temp_cookie = a_all_cookies[i].split( '=' );
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
		if ( cookie_name == check_name ){
			b_cookie_found = true;
			if ( a_temp_cookie.length > 1 ){
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found ){ return null; }
}

function Set_Cookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ){ expires = expires * 1000 * 60 * 60 * 24; }
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) + 
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

function Delete_Cookie( name, path, domain ) {
	if ( Get_Cookie( name ) ) document.cookie = name + "=" +
		( ( path ) ? ";path=" + path : "") +
		( ( domain ) ? ";domain=" + domain : "" ) +
		";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function setData(diaNew,mesNew,anoNew,sufF){
	document.getElementById("dia"+sufF).value = diaNew;
	document.getElementById("mes"+sufF).value = mesNew;
	document.getElementById("ano"+sufF).value = anoNew;
	showBoxPop(0,0,"immediate");
}

function processPermalink(elVal){
	var charn = new Array(225,224,227,229,226,233,234,237,243,244,245,250,252,231,193,192,195,194,201,202,205,211,212,213,218,220,199,732,241,232);
	var chars = new Array("a","a","a","a","a","e","e","i","o","o","o","u","u","c","a","a","a","a","e","e","i","o","o","o","u","u","c","_","n","e");
	
	var valSplit = elVal.toLowerCase().split('');
	var valNChar = new Array();
	var newch = "";
	var t = valSplit.length;
	for(t in valSplit){
		var tC = valSplit[t].charCodeAt();
		newch = valSplit[t];
		if(valSplit[t] == " "){ newch = "-"; }
		if(valSplit[t] == "/"){ newch = "_slash_"; }
		for(n in charn){
			if(charn[n] == tC){
				newch = chars[n];
			}
		}
		valNChar.push(newch);
	}
	val = valNChar.join('');
	val = val.replace(/[^a-zA-Z0-9_-]/g, function(c) { return ''; });
	val = val.replace("_slash_","/");
	return val;
}
function completePermalink(form, fieldFrom, fieldTo){
	var elFrom = eval("document."+form+"."+fieldFrom);
	var elTo = eval("document."+form+"."+fieldTo);
	if(elTo.value.length == 0){
		elTo.value = processPermalink(elFrom.value);
		//elTo.value = val;
	}
}

function clearPermalink(form, fieldVal){
	var elVal = eval("document."+form+"."+fieldVal);
	if(elVal.value.length > 0){
		elVal.value = processPermalink(elVal.value);
	}
}

function lettersonly(e){
	var unicode=e.charCode? e.charCode : e.keyCode
	if (unicode!=8){
		if((unicode > 64 && unicode < 91) || (unicode > 96 && unicode < 123) || (unicode == 95) || (unicode == 45)){ return true; } else { return false; }
	}
}

function elementFocusBlur(el, inival){
	if(eval(el).value == inival){
		eval(el).value = "";
	}
	el.style.backgroundColor = "#FFFFFF";
	$(el).bind("blur", function(){
		if(el.value == ""){ el.value = inival; }
	});
}

function getSelected(form, field){
	var elForm = eval("document."+form+"."+field);
	return elForm.options[elForm.options.selectedIndex].value;
}

function fieldDestaque(Tipo, El, Set){
	var cor = "#FFFFFF";
	if(Set == 1){ cor = "#fffdfe"; }
	if(Set == 2){ cor = "#fffbeb"; }
	if(Tipo == "Field"){ El.style.backgroundColor = cor; } else { $(El).css({"backgroundColor" : cor}); }
}

function camposForm(form, keys){
	var nfail = 0;
	$.each(keys, function(){
		var vk = this;
		var campoV = vk[0];
		var valorV = vk[1];
		var alertV = vk[2];
		if(campoV != undefined){
			if(eval("document."+form+"."+campoV)){
				var elForm = eval("document."+form+"."+campoV);
				var setCor = 0;
				if(vk[1] == "select"){ if(parseInt(elForm.options[elForm.options.selectedIndex].value) == 0){ setCor = 1; }
				} else if(vk[1] == "number"){ if(elForm.value == 0){ setCor = 1; }
				} else if(valorV == "check"){ if(elForm.checked === false){ setCor = 1; }
				} else if(valorV.toString().match("^!")){ if(elForm.value == valorV.substr(1)){ setCor = 1; }
				} else if(valorV == "email"){ if(!elForm.value.toString().match(/^[a-zA-Z0-9][a-zA-Z0-9\._-]+@([a-zA-Z0-9\._-]+\.)[a-zA-Z-0-9]{2}/)){ setCor = 1; }
				} else { if(elForm.value.length < parseInt(vk[1])){ setCor = 1; }
				}
				if(setCor == 1){ nfail++; }
				if(setCor == 0) fieldDestaque("Field", elForm, setCor);
			}
		}
	});
	if(nfail > 0){ return false; } else { return true; }
}

function verificaForm(form, keys, minimo){
	var recadoForm  = "";
	var numValores = 0;
	$.each(keys, function(){
		var vk = this;
		var campoV = vk[0];
		var valorV = vk[1];
		var alertV = vk[2];
		if(campoV != undefined){
			if(eval("document."+form+"."+campoV)){
				var elForm = eval("document."+form+"."+campoV);
				var setCor = 0;
				if(valorV == "select"){ if(parseInt(elForm.options[elForm.options.selectedIndex].value) == 0){ setCor = 1; }
				} else if(valorV == "number"){ if(elForm.value == 0){ setCor = 1; }
				} else if(valorV == "check"){ if(elForm.checked === false){ setCor = 1; }
				} else if(valorV.toString().match("^!")){ if(elForm.value == valorV.substr(1)){ setCor = 1; }
				} else if(valorV == "email"){ if(!elForm.value.toString().match(/^[a-zA-Z0-9][a-zA-Z0-9\._-]+@([a-zA-Z0-9\._-]+\.)[a-zA-Z-0-9]{2}/)){ setCor = 1; }
				} else { if(elForm.value.length < parseInt(valorV)){ setCor = 1; }
				}
				if(minimo > 0){
					if(setCor == 0) numValores++;
				} else {
					if(setCor == 1){
						recadoForm += "\n" + alertV;
					}
					fieldDestaque("Field", elForm, 2);
				}
			}
		}
	});
	if(minimo > 0){
		if(numValores < minimo){
			alert("Favor preencher no mínimo " + minimo + " informa" + (minimo>1?"ções":"ção"));
			return false;
		} else {
			return true;
		}
	} else {
		if(recadoForm.length > 0){
			alert("Preencha corretamente o campos:\n" + recadoForm);
			return false;
		} else {
			return true;
		}
	}
}


function post_to_url(url, params){
	var form = document.createElement('form');
    form.action = url;
    form.method = 'POST';
    for(var i in params){
        if (params.hasOwnProperty(i)) {
            var input = document.createElement('input');
            input.type = 'hidden';
            input.name = i;
            input.value = params[i];
            form.appendChild(input);
        }
    }
    form.submit();
}

function flash(ID, arquivo, w, h) {
	document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" id="' + ID + '" width="' + w + '" height="' + h + '">');
	document.write('<param name="movie" value="' + arquivo + '">');
	document.write('<param name="quality" value="high">');
	document.write('<param name="wmode" value="transparent">');
	document.write('<embed src="' + arquivo + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="' + w + '" height="' + h + '" WMODE="transparent" NAME=' + ID + ' swLiveConnect=true></embed>');
	document.write('</object>');
}

function flashFLV(ID, arquivo, w, h) {
	document.write("<object width='"+w+"' height='"+h+"' id='"+ID+"'>");
	document.write("<param name='allowFullScreen' value='true'>");
	document.write("<param name='allowScriptAccess' value='always'>");
	document.write("<param name='movie' value='"+arquivo+"'>");
	document.write("<embed src='"+arquivo+"' width='"+w+"' height='"+h+"' allowFullScreen='true' type='application/x-shockwave-flash' allowScriptAccess='always'>");
	document.write("</object>");
}


function makeWindow(vURL,nameW,w,h,scr){
	if(w == 0 && h == 0){
		var vScreenWidth = window.screen.availWidth;
		var vScreenHeight = window.screen.availHeight;
		var lf = 0;
		var tp = 0;
		var vFeatures = "fullscreen=yes, toolbar=no, status=no, menubar=no, scrollbars="+scr+", resizable=yes, directories=no, location=no, left="+lf+", top="+tp+", width="+vScreenWidth+", height="+ vScreenHeight;
	} else {
		var vScreenWidth = w;
		var vScreenHeight = h;
		var lf = (window.screen.availWidth - w)/2;
		var tp = (window.screen.availHeight - h)/2;
		var vFeatures = "toolbar=no, status=yes, menubar=no, scrollbars="+scr+", resizable=yes, directories=no, location=no, left="+lf+", top="+tp+", width="+vScreenWidth+", height="+ vScreenHeight;
	}
	window.open(vURL,nameW,vFeatures);
}



var focusToogle = 0;
var showToogle = new Array();
function toogle(el){
	var hasKey = 0;
	focusToogle = 1;
	for(var ist=0;ist<showToogle.length;ist++){
		if(showToogle[ist] == el){ hasKey = 1; }
	}
	if(hasKey == 0){
		showToogle.push(el);
		showToogle[el] = 0;
	}
	var imageBT = $("#"+el+"BT").attr("src");
	if(showToogle[el] == 1){
		showToogle[el] = 0;
		$("#"+el).slideUp();
		$("#"+el+"BT").attr("src", imageBT.replace("_up", "_down") );
	} else {
		showToogle[el] = 1;
		$("#"+el).slideDown();
		$("#"+el+"BT").attr("src", imageBT.replace("_down", "_up") );
	}
	var oldel = "";
	for(var ist=0;ist<showToogle.length;ist++){
		oldel = showToogle[ist];
		//if(showToogle[oldel] == 1 && oldel != el){ setTimeout("toogle('"+oldel+"')",500); }
	}
	setTimeout("focusToogle=0",500);
}
function clearToogle(){
	if(focusToogle == 0){
		var oldel = "";
		for(var ist=0;ist<showToogle.length;ist++){
			oldel = showToogle[ist];
			if(showToogle[oldel] == 1){ setTimeout("toogle('"+oldel+"')",100); }
		}
	}
}




// FUNÇÕES PARA CONTROLE DE POPS
// ****** Mostrar e esconder os combos (para nao ocorrer sobreposicao indevida)
// ****** Pega a posicao na tela
// ****** insere, posiciona, remove divs
// ****** controla o drag

function MostraCombo(){
	qtdSelect = document.getElementsByTagName("select");
	for (i = 0; i < qtdSelect.length; i++){
		objSelect = qtdSelect[i];
		if(objSelect.style.visibility == "hidden"){
			objSelect.style.visibility = "visible";
		}
	}
}
function EscondeCombo(){
	qtdSelect = document.getElementsByTagName("select");
	for (i = 0; i < qtdSelect.length; i++){
		objSelect = qtdSelect[i];
		if(objSelect.style.visibility == "visible"){
			objSelect.style.visibility = "hidden";
		}
	}
}
var npop = 1;
var pyIni = 0;
function getX(el){
	var pos = $(el).offset();  
	return pos.left;
}
function getY(el){
	var pos = $(el).offset();  
	return pos.top;
}
function posBox(el,leftBox,topBox,wBox,hBox){
	var addTopBox = 0;
	if(topBox == 0){ addTopBox = 1; }
	if(leftBox == 0){ leftBox = (largura/2) - (wBox/2); }
	if(hBox == 0){ hBox = 200; }
	//var pyfim = (altura / 2) - (hBox / 2);
	var pyfim = (altura - hBox) / 2;
	if(topBox == 0){ topBox = pyfim + scrollTop; }
	if(addTopBox == 1){ topBox += pyIni; }
	$("#"+el).css("top",topBox);
	$("#"+el).css("left",leftBox);
	$("#"+el).css("width",wBox);
	$("#"+el).css("height",hBox);
	//if(addTopBox == 1){ pyIni += 20; }
	//alert("ADDING BOX: "+el+", leftBox: "+leftBox+", topBox: "+topBox+", wBox: "+wBox+", hBox: "+hBox+" === pyIni: "+pyIni+" --- altura: "+altura+" ---- scrollTop: "+scrollTop);
}

function addPopDiv(idBox,acaoAjax,x,y,wPop,hPop){
	npop++;
	var base = document.getElementById('base');
	var newElem = document.createElement("div");
	var nomeNewElem = "boxPop"+npop;
	if(idBox.length > 0){ nomeNewElem = idBox; }
	//alert(nomeNewElem);
	newElem.id = nomeNewElem;
	newElem.className = "boxPop";
	base.appendChild(newElem);
	posBox(nomeNewElem,x,y,wPop,hPop);
	$("#"+nomeNewElem).html("");
	$("#"+nomeNewElem).fadeIn(50);
	var acaoAjaxSplit = acaoAjax.split("|");
	if(acaoAjaxSplit[0] == "recado"){
		$("#"+nomeNewElem).html(acaoAjaxSplit[1]);
	} else {
		return nomeNewElem;
	}
}
function removePopDiv(boxname){
	var base = document.getElementById('base');
	var remdiv = document.getElementById(boxname);
	base.removeChild(remdiv);
}
function closePopDiv(boxname){
	$("#"+boxname).fadeOut(500, function(){
		$("#"+boxname).html("");
		removePopDiv(boxname);
		MostraCombo();
	});
}


var numPopJS=0;
var popDiv = document.createElement('div');
function createPopJS(base, divIdName, w, h, px, py, content, parametros){
	remove=false;
	if(divIdName==""){ numPopJS++; divIdName = "popJS"+numPopJS; }
	if(w == 0){ w = 300 + "px"; }
	if(h == 0){ h = 300 + "px"; }
	var midX = largura / 2;
	var midY = (altura / 2) + scrollTop;
	var midW = parseFloat(w) / 2;
	var midH = parseFloat(h) / 2;
	if(px == 0){ px = (midX - midW) + "px"; }
	if(py == 0){ py = (midY - midH) + "px"; }
	popDiv.setAttribute('id',divIdName);
	popDiv.style.width = w;
	popDiv.style.height = h;
	popDiv.style.fontSize = '10px';
	popDiv.style.left = px;
	popDiv.style.top = py;
	popDiv.style.position = "absolute";
	popDiv.style.zIndex = 30000;
	popDiv.style.background = "#FFFFFF";
	
	var box = "<div class='boxShd'>";
	box += "<div class='boxShdC boxShdC1'></div><div class='boxShdC boxShdC2'></div><div class='boxShdC boxShdC3'></div><div class='boxShdC boxShdC4'></div>";
	box += "<div class='boxShdH boxShdF1'></div><div class='boxShdV boxShdF2'></div><div class='boxShdH boxShdF3'></div><div class='boxShdV boxShdF4'></div>";
	if(divIdName == "produto") box += "<div class='boxShdSetaDown'></div>";
	box += "<div style='overflow:auto; height:"+h+"; padding:5px;'>";
	box += "<div id='content_"+divIdName+"'>";
	box += "</div>";
	box += "</div>";
	popDiv.innerHTML = box;
	if(base == "") base = "base";
	document.getElementById(base).appendChild(popDiv);
	if(content.indexOf(".php") > 0){
		$("#content_"+divIdName).load(content + "&divIdName=" + divIdName + "&rand=" + Math.ceil(Math.random()*100000));
	} else {
		document.getElementById("content_"+divIdName+"").innerHTML(content);
	}
}
function removePopJS(divIdName){
	if(document.getElementById(divIdName)){
		$("#"+divIdName).remove();
	}
}





function Browser() {
  var ua, s, i;
  this.isIE    = false;
  this.isNS    = false;
  this.version = null;
  ua = navigator.userAgent;
  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }
  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }
  // Treat any other "Gecko" browser as NS 6.1.
  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}
var browser = new Browser();
//alert(browser);
// Global object to hold drag information.
var dragObj = new Object();
dragObj.zIndex = 1800;
function dragStart(event, id) {
  var el;
  var x, y;
  // If an element id was given, find it. Otherwise use the element being
  // clicked on.
  if (id)
    dragObj.elNode = document.getElementById(id);
  else {
    if (browser.isIE)
      dragObj.elNode = window.event.srcElement;
    if (browser.isNS)
      dragObj.elNode = event.target;
    // If this is a text node, use its parent element.
    if (dragObj.elNode.nodeType == 3)
      dragObj.elNode = dragObj.elNode.parentNode;
  }
  // Get cursor position with respect to the page.
  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }
  // Save starting positions of cursor and element.
  dragObj.cursorStartX = x;
  dragObj.cursorStartY = y;
  dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 10);
  dragObj.elStartTop   = parseInt(dragObj.elNode.style.top,  10);
  if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
  if (isNaN(dragObj.elStartTop))  dragObj.elStartTop  = 0;
  // Update element's z-index.
  dragObj.elNode.style.zIndex = ++dragObj.zIndex;
  // Capture mousemove and mouseup events on the page.
  if (browser.isIE) {
    document.attachEvent("onmousemove", dragGo);
    document.attachEvent("onmouseup",   dragStop);
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS) {
    document.addEventListener("mousemove", dragGo,   true);
    document.addEventListener("mouseup",   dragStop, true);
    event.preventDefault();
  }
}
function dragGo(event) {
  var x, y;
  // Get cursor position with respect to the page.
  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }
  // Move drag element by the same amount the cursor has moved.
  dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
  dragObj.elNode.style.top  = (dragObj.elStartTop  + y - dragObj.cursorStartY) + "px";

  if (browser.isIE) {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS)
    event.preventDefault();
}

function dragStop(event) {
  // Stop capturing mousemove and mouseup events.
  if (browser.isIE) {
    document.detachEvent("onmousemove", dragGo);
    document.detachEvent("onmouseup",   dragStop);
  }
  if (browser.isNS) {
    document.removeEventListener("mousemove", dragGo,   true);
    document.removeEventListener("mouseup",   dragStop, true);
  }
}