jQuery(document).ready(function(){
	jQuery('a[rel=external]').attr('target','_blank');
	
	jQuery('.bt-show-search').toggle(function(){
		jQuery(this).attr('title','Ocultar campo de busca');
		jQuery('#search').fadeIn(250);
	},function(){
		jQuery(this).attr('title','Exibir campo de busca');
		jQuery('#search').fadeOut(250);
	});
	
	jQuery('.sahre-recipe h3').html(function(i,html){
	    return html.replace(/(\S)(\S*)/g, '<span class="first-letter">$1</span><span class="word">$2</span>');
	});
	jQuery('.header-section h2').html(function(i,html){
	    return html.replace(/(\S)(\S*)/g, '<span class="first-letter">$1</span><span class="word">$2</span>');
	});
		
	jQuery('#asider').animate({top: -50}, 750);
	
	jQuery('a:contains("IFRAME Embed for Youtube")').css('display','none');
	
	jQuery('.recipes a:contains("Dia a dia")').addClass('link-drinks');
	jQuery('.recipes a:contains("Todos os momentos")').addClass('link-sweet');
	jQuery('.recipes a:contains("Para o final de semana")').addClass('link-salty');
	
	jQuery('a[href="#faleconosco"]').toggle(function(){
		jQuery('.contact-form').stop().animate({top: 60}, 400);
		return false;
	},function(){
		jQuery('.contact-form').stop().animate({top: -472}, 400);
		return false;
	});
	
	jQuery('a[href="#assessoria"]').toggle(function(){
		jQuery('.advice').stop().animate({top: 8}, 400);
		return false;
	},function(){
		jQuery('.advice').stop().animate({top: -472}, 400);
		return false;
	});
	
	jQuery('#content .cafeterias ul').children('li:even').addClass('line-end');
	
	jQuery('img[alt*="filtro"]').parent().addClass('filtro');
	
	jQuery('input[type="checkbox"]').attr('checked','checked');
	
	jQuery('.wpcf7-submit').click(function(){		
		jQuery('.wpcf7-response-output').fadeIn(500);
		setTimeout("jQuery('.wpcf7-response-output').fadeOut('fast');", 3750);
		jQuery(this).parent().parent().fadeOut(250,function(){
			jQuery(this).delay(4000).fadeIn(500,function(){				
				jQuery('.Nome input').attr('value','Nome');
				jQuery('.E-mail input').attr('value','E-mail');
				jQuery('.Telefone input').attr('value','Telefone');
				jQuery('.CEP input').attr('value','CEP');
				jQuery('.Endereco input').attr('value','Endereço');
				jQuery('.Bairro input').attr('value','Bairro');
				jQuery('.UF input').attr('value','UF');
				jQuery('.Cidade input').attr('value','Cidade');				
				jQuery('.Assunto input').attr('value','Assunto');
				jQuery('.Comentario textarea').attr('value','Comentário');
				jQuery('.ckb-produtos input').attr('checked','checked');
				jQuery('.ckb-news input').attr('checked','checked');
			});
		});
	});
	
	jQuery('.widget_rss span').prepend('<a href="http://twitter.com/damasco_cafe" target="_blank">@damasco_cafe </a>');
	jQuery('.widget_rss a').attr('target','_blank');
	jQuery('.rsswidget:eq(1)').addClass('twitter-title');
});


//Cufon.replace('.header-section h2 em:not(:first-child) .first-letter,.header-section h2 em .word,.institutional ul li strong,.list-recipes li a,.sahre-recipe h3 .word,.sahre-recipe h3 .first-letter:not(:first-child)', { fontFamily: 'Garamond' });
//Cufon.replace('.header-section h2 em:first-child .first-letter,.sahre-recipe h3 .first-letter:first-child, .cafeterias h2 em .first-letter', { fontFamily: 'Edwardian Script ITC' });

function resetForm(){
	//jQuery('.wpcf7-response-output').fadeOut('fast');
	
};

function goBackForm(){
	jQuery('.wpcf7-response-output').fadeIn('fast');
	jQuery(this).parent().fadeOut('fast',function(){
		//jQuery('.wpcf7-response-output').delay(3000).fadeOut('fast');
		jQuery('form.wpcf7-form p').fadeIn('fast',function(){
			resetForm();
		});			
	});
};

function remove(str, strToRemove) {
   i = str.indexOf(strToRemove);
   r = "";
   if (i == -1) return str;
   r += str.substring(0,i) + remove(str.substring(i + strToRemove.length), strToRemove);
   return r;
}

function validateEmail(email) {
	var ret = false;
	var em = new String();
	em = String(email);
	var RegExPattern = /^[\w-]+(\.[\w-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{ 1,3}){3}\])$/; 
	if ( em.match(RegExPattern) ) {
		ret = true;
	}
	return ret;
}

function trim(str){
	if (typeof(str) == "string") {
		return str.replace(/^\s+|\s+$/g,"");
	} else {
		return str;
	}	
}

function maskFormat(mask,obj){
	if(obj.value != ""){
		sObj = obj.value.toString();
		nString = "";
		for(i=0;i<(sObj.length);i++){
			if(!isNaN(sObj.charAt(i)) && (sObj.charAt(i) != " ")) {
				nString = nString + sObj.charAt(i);
			}
		}
		j = 0;
		k = 0;
		mString = "";
		while(j < nString.length && k < mask.length){
			if(mask.charAt(k).toUpperCase() == "N"){
				mString = mString + nString.charAt(j);
				j = j+1;
			} else {
				mString = mString + mask.charAt(k);
			}
			k = k+1;
		}
		obj.value = mString;
	}
}

function validarCPF(cpf){
	var filtro = /^\d{3}.\d{3}.\d{3}-\d{2}$/i;
	if(!filtro.test(cpf)){
		return false;
	}
	cpf = remove(cpf, ".");
	cpf = remove(cpf, "-");
	if(cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" ||
		cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" ||
		cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" ||
		cpf == "88888888888" || cpf == "99999999999"){
			return false;
	}
	soma = 0;
	for(i = 0; i < 9; i++)
		soma += parseInt(cpf.charAt(i)) * (10 - i);
	resto = 11 - (soma % 11);
	if(resto == 10 || resto == 11)
		resto = 0;
	if(resto != parseInt(cpf.charAt(9))){
		return false;
	}
	soma = 0;
	for(i = 0; i < 10; i ++)
		soma += parseInt(cpf.charAt(i)) * (11 - i);
	resto = 11 - (soma % 11);
	if(resto == 10 || resto == 11)
		resto = 0;
	if(resto != parseInt(cpf.charAt(10))){
		return false;
	}
	return true;
}

function entraNoEdit(obj, txt) {
	if ($.trim(obj.value) == txt) {
		obj.value =	"";
	}
}
function saiDoEdit(obj,	txt) {
	if ($.trim(obj.value) == "") {
		obj.value =	txt;
	}
}

function getEndereco(cep) {
	var arrCep = new Array();
	var cepLimpo = $.trim(cep);
	cepLimpo = cepLimpo.replace("-","");
	if(cepLimpo != ""){
		$.getScript("http://cep.republicavirtual.com.br/web_cep.php?formato=javascript&cep=" + cepLimpo, function(){
			if(resultadoCEP["resultado"]){
				var txtLogradouro = ($.trim(unescape(resultadoCEP["tipo_logradouro"]) + " " + unescape(resultadoCEP["logradouro"])));
				var txtBairro = (unescape(resultadoCEP["bairro"]));
				var txtCidade = (unescape(resultadoCEP["cidade"]));
				var slEstado = (unescape(resultadoCEP["uf"]));
				arrCep.push(txtLogradouro);
				arrCep.push(txtBairro);
				arrCep.push(txtCidade);
				arrCep.push(slEstado);
			}
		});				
	}
	return arrCep;
}

/*
1 - From a user
2 - Mentioning a user
3 - Replying to a user
4 - Containing a word
5 - Containing a hashtag
*/
var _key; 
var _atualize; 
var _timeout;
var _tammax;
var _nohRetorno;
var _option;

function Twitter(key, atualize, timeout, tammax, nohRetorno, option) {
	_key = key;
	_atualize = atualize;
	_timeout = timeout;
	_tammax = tammax;
	_nohRetorno = nohRetorno;
	_option = option;
	execute();
}

function execute() {
	if (this._atualize) {
		setInterval('_updateTwitts();', _timeout * 1000);
	}
	_updateTwitts();
}

function _updateTwitts() {
	var s = "<scr"+"ipt src=\"http://search.twitter.com/search.json?callback=_up"+_option+"&";
	switch(_option)
	{
		case 1:
			s += "q=from:"+ _key;
			break;
		case 2:
			s += "q=%40"+ _key;
			break;
		case 3:
			s += "q=to:"+ _key;
			break;
		case 4:
			s += "q="+ _key;
			break;
		case 5:
			s += "q=%23"+ _key;
			break;
		default:
	} 
	s += "&c="+Math.random()+"\"></scr"+"ipt>";
	$("head").append(s);
}

function _up1(d){
	var t = "";
	if (d.results) {
		var tamlst = d.results.length;
		var max = _tammax;
		if (tamlst < max) {
			max = tamlst;
		}
		for (var ix=0;ix<max;ix++) {
			t += "<li>";
			t += _getLink(d.results[ix]['text']);
			t += "</li>";
			t += "<li><span><a href='http://twitter.com/damasco_cafe' target='_blank'>@damasco_cafe</a> "+_formatDateTw(d.results[ix]['created_at'])+"</span></li>";
		}
	}
	$("#"+_nohRetorno).html(t);
	/*$('a[rel=external]').attr('target','_blank');*/
}

function _up2(d){
	var t = "";
	if (d.results) {
		var tamlst = d.results.length;
		var max = _tammax;
		if (tamlst < max) {
			max = tamlst;
		}
		for (var ix=0;ix<max;ix++) {
			t += "<li>";
			t += "<div>";
			t += "<a href=\"http://twitter.com/"+d.results[ix]['from_user']+"\" rel=\"external\"><img src=\""+d.results[ix]['profile_image_url']+"\" /></a> ";
			t += "<strong><a href=\"http://twitter.com/"+d.results[ix]['from_user']+"\" rel=\"external\">@"+d.results[ix]['from_user']+"</a></strong> ";
			t += _getLink(d.results[ix]['text']);
			t += "<span>"+_formatDateTw(d.results[ix]['created_at'])+"</span>";
			t += "</div>";
			t += "</li>";
		}
	}
	$("#"+_nohRetorno).html(t);
	/*$('a[rel=external]').attr('target','_blank');*/
}

function _up3(d){
	var t = "";
	if (d.results) {
		var tamlst = d.results.length;
		var max = _tammax;
		if (tamlst < max) {
			max = tamlst;
		}
		for (var ix=0;ix<max;ix++) {
			t += "<li>";
			t += "<div>";
			t += "<a href=\"http://twitter.com/"+d.results[ix]['from_user']+"\" rel=\"external\"><img src=\""+d.results[ix]['profile_image_url']+"\" /></a> ";
			t += "<strong><a href=\"http://twitter.com/"+d.results[ix]['from_user']+"\" rel=\"external\">@"+d.results[ix]['from_user']+"</a></strong> ";
			t += _getLink(d.results[ix]['text']);
			t += "<span>"+_formatDateTw(d.results[ix]['created_at'])+"</span>";
			t += "</div>";
			t += "</li>";
		}
	}
	$("#"+_nohRetorno).html(t);
	/*$('a[rel=external]').attr('target','_blank');*/
}

function _up4(d){
	var t = "";
	if (d.results) {
		var tamlst = d.results.length;
		var max = _tammax;
		if (tamlst < max) {
			max = tamlst;
		}
		for (var ix=0;ix<max;ix++) {
			t += "<li>";
			t += "<div>";
			t += "<a href=\"http://twitter.com/"+d.results[ix]['from_user']+"\" rel=\"external\"><img src=\""+d.results[ix]['profile_image_url']+"\" /></a> ";
			t += "<strong><a href=\"http://twitter.com/"+d.results[ix]['from_user']+"\" rel=\"external\">@"+d.results[ix]['from_user']+"</a></strong> ";
			t += _getLink(d.results[ix]['text']);
			t += "<span>"+_formatDateTw(d.results[ix]['created_at'])+"</span>";
			t += "</div>";
			t += "</li>";
		}
	}
	$("#"+_nohRetorno).html(t);
	/*$('a[rel=external]').attr('target','_blank');*/
}

function _up5(d){
	var t = "";
	if (d.results) {
		var tamlst = d.results.length;
		var max = _tammax;
		if (tamlst < max) {
			max = tamlst;
		}
		for (var ix=0;ix<max;ix++) {
			t += "<li>";
			t += "<div>";
			t += "<a href=\"http://twitter.com/"+d.results[ix]['from_user']+"\" rel=\"external\"><img src=\""+d.results[ix]['profile_image_url']+"\" /></a> ";
			t += "<strong><a href=\"http://twitter.com/"+d.results[ix]['from_user']+"\" rel=\"external\">@"+d.results[ix]['from_user']+"</a></strong> ";
			t += _getLink(d.results[ix]['text']);
			t += "<span>"+_formatDateTw(d.results[ix]['created_at'])+"</span>";
			t += "</div>";
			t += "</li>";
		}
	}
	$("#"+_nohRetorno).html(t);
	/*$('a[rel=external]').attr('target','_blank');*/
}

function _formatDateTw(time){
	var timeAtual = (new Date()).getTime();
	var date = new Date(time);
	var timeTw = date.getTime();
	var diff = ((timeAtual - timeTw) / 1000);
	var day_diff = Math.floor(diff / 86400);

	if ( isNaN(day_diff) || day_diff < 0 || day_diff >= 31 )
		return;
			
	return day_diff == 0 && (
			diff < 60 && "just now" ||
			diff < 120 && "1 minute ago" ||
			diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
			diff < 7200 && "1 hour ago" ||
			diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
		day_diff == 1 && "Yesterday" ||
		day_diff < 7 && day_diff + " days ago" ||
		day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago";
}

function _getLink(str) {
	var strFinal = "";
    var regExpUrl = /((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi;
    var regExpTwt = /[\@]+([A-Za-z0-9-_]+)/gi;
    var regExpTag = /(?:^| )[\#]+([A-Za-z0-9-_]+)/gi;
	var regExpEmail = /^[\w-]+(\.[\w-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{ 1,3}){3}\])$/;
	var arrStr = str.split(' ');
	for (var i=0;i<arrStr.length;i++) {
		var parte = arrStr[i];
		parte = parte.replace(regExpUrl,"<a href=\"$1\" rel=\"external\">$1</a>");
		if (regExpEmail.test(parte))
		{
			parte = parte.replace(parte,"<a href=\"mailTo:"+parte+"\" rel=\"external\">"+parte+"</a>");
		}
		else
		{
			parte = parte.replace(regExpTwt,"<a href=\"http://twitter.com/$1\" rel=\"external\">@$1</a>");
		}
		parte = parte.replace(regExpTag,"<a href=\"http://search.twitter.com/search?q=&tag=$1\" rel=\"external\">#$1</a>");
		
		
		strFinal += parte + " ";
    }
	return strFinal;
}

