function ById(id){return document.getElementById(id);}
function getBrowser(){if(window.XMLHttpRequest){return "mozilla";}else if(window.ActiveXObject){return "ie";}}
function doXMLRequester(){if(getBrowser() == "ie"){return new ActiveXObject("Microsoft.XMLHTTP");}else if(getBrowser() == "mozilla"){return new XMLHttpRequest();}}

function mostraCatalogo(id){window.open('sis.catalogo.asp?bId='+id,'Catalogo'+id,'scrollbars=no,fullscreen=yes');}

/*CHECAGEM DO FORMULÁRIO DE CONTATO*/
function checkContato(){
	
	var nome 			= ById('nome').value;
		nome 			= replaceAll(nome," ","");
	var email 			= ById('email').value;
		email 			= replaceAll(email," ","");
	var mensagem 		= ById('mensagem').value;
	
	var FormData = new Boolean("true");
	var ConteudoErro = "Foram detectados os seguintes erros:\n\n";
	
	if(nome==""){
		ConteudoErro+="- Campo requerido [ NOME ]\n";
		FormData=false;
	}
	if(!eh_email(email)){
		ConteudoErro+="- Campo requerido [ E-MAIL ]\n";
		FormData=false;
	}
	if(mensagem==""){
		ConteudoErro+="- Campo requerido [ MENSAGEM ]\n";
		FormData=false;
	}
	if(FormData){
		ById('frmContato').submit();
	}else{
		alert(ConteudoErro);
	}
}
/*FIM DA CHECAGEM DO FORMULÁRIO DE CONTATO*/

/*CHECAGEM DO FORMULÁRIO DE CADASTRO*/
function CheckCad(){
	
	var nome 	= ById('nome').value;
		nome 	= replaceAll(nome," ","");
	var email 	= ById('email').value;
		email 	= replaceAll(email," ","");
	
	var FormData = new Boolean("true");
	var ConteudoErro = "Foram detectados os seguintes erros:\n\n";
	
	if(nome==""){
		ConteudoErro+="- Campo requerido [ NOME ]\n";
		FormData=false;
	}
	if(!eh_email(email)){
		ConteudoErro+="- Campo requerido [ E-MAIL ]\n";
		FormData=false;
	}
	if(FormData){
		ById('frmCadastro').submit();
	}else{
		alert(ConteudoErro);
	}
}
/*FIM DA CHECAGEM DO FORMULÁRIO DE CADASTRO*/



//Checa se uma data é válida

/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2078;
function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}
function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}
function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strDay=dtStr.substring(0,pos1)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("O formato da data terá de ser: dd/mm/aaaa")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Entre com um mês válido!")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Entre com um dia válido!")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Entre com um ano de quatro dígitos válido entre  "+minYear+" e "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Entre com uma data válida!");
		return false
	}
return true
}
/*xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx*/


/*
	Função para validação de e-mail
*/
function eh_email(mail){
	var ret = false;
	if (typeof(mail) != "undefined"){
		mail = mail.match(/(\w+)@(.+)\.(\w+)$/);
	if (mail != null){
		if ((mail[3].length==2) || (mail[3].length==3))
			ret = true;
		}
	}
	return ret;
}

/*
	Função Para Replace!
*/
function replaceAll( str, from, to ) {
    var idx = str.indexOf( from );

    while ( idx > -1 ) {
        str = str.replace( from, to ); 
        idx = str.indexOf( from );
    }

    return str;
}


/*
	Retorna um array com largura da página, altura e largura e altura da janela
*/
function getPageSize()
{
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY)
	{
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}

/*
	Retorna um objeto com aposição left e top de um elemento na página.	
*/
function getPosicaoElemento(elemID){
    var offsetTrail = document.getElementById(elemID);
    var offsetLeft = 0;
    var offsetTop = 0;
    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }
    if (navigator.userAgent.indexOf("Mac") != -1 && 
        typeof document.body.leftMargin != "undefined") {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }
    return {left:offsetLeft, top:offsetTop};
}



/*Código B*/
// $(".item img").css({"display":"none");

// On window load. This waits until images have loaded which is essential
function load_social_effect(){
/*$(window).load(function(){*/
	
	// Fade in images so there isn't a color "pop" document load and then on window load
	$(".social img").animate({opacity:1},500);
	
	// clone image
	$('.social img').each(function(){
		var el = $(this);
		el.css({"position":"absolute"}).wrap("<div class='img_wrapper' style='display: inline-block'>").clone().addClass('img_grayscale').css({"position":"absolute","z-index":"998","opacity":"0"}).insertBefore(el).queue(function(){
			var el = $(this);
			el.parent().css({"width":this.width,"height":this.height});
			el.dequeue();
		});
		this.src = grayscale(this.src);
	});
	
	// Fade image 
	$('.social img').mouseover(function(){
		$(this).parent().find('img:first').stop().animate({opacity:1}, 1000);
	})
	$('.img_grayscale').mouseout(function(){
		$(this).stop().animate({opacity:0}, 1000);
	});		
}

// Grayscale w canvas method
function grayscale(src){
	var canvas = document.createElement('canvas');
	var ctx = canvas.getContext('2d');
	var imgObj = new Image();
	imgObj.src = src;
	canvas.width = imgObj.width;
	canvas.height = imgObj.height; 
	ctx.drawImage(imgObj, 0, 0); 
	var imgPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
	for(var y = 0; y < imgPixels.height; y++){
		for(var x = 0; x < imgPixels.width; x++){
			var i = (y * 4) * imgPixels.width + x * 4;
			var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;
			imgPixels.data[i] = avg; 
			imgPixels.data[i + 1] = avg; 
			imgPixels.data[i + 2] = avg;
		}
	}
	ctx.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
	return canvas.toDataURL();
}


/*Carrega e Video do YouTube em uma camada e dimensoes específicas*/
function loadYTVideo(url,desc,width,height,camadaVideo,camadaDesc)
{
	document.getElementById(camadaVideo).style.display='block';
	document.getElementById(camadaVideo).style.width=width.toString()+'px';
	document.getElementById(camadaVideo).style.height=height.toString()+'px';

	htmlVideo = '<object height="'+height+'" width="'+width+'"> ';
	htmlVideo +='	<param name="movie" value="'+url+'" />';
	htmlVideo +='	<param name="allowFullScreen" value="true" />';
	htmlVideo +='	<param name="allowscriptaccess" value="always"/>';
	htmlVideo +='	<embed src="'+url+'" height="'+height+'" width="'+width+'" allowfullscreen="true" allowscriptaccess="always" type="application/x-shockwave-flash"></embed>';
	htmlVideo +='	</object>';

	document.getElementById(camadaVideo).innerHTML = htmlVideo;	
	document.getElementById(camadaDesc).innerHTML=desc;
}

