jQuery.fn.nospam = function(settings)
{
	settings = jQuery.extend({
		replaceText: false, 	// optional, accepts true or false
		filterLevel: 'normal' 	// optional, accepts 'low' or 'normal'
	}, settings);
	
	return this.each(function()
	{
		e = null;
		if(settings.filterLevel == 'low')
		{ // Can be a switch() if more levels added
			if($(this).is('a[rel]'))
			{
				e = $(this).attr('rel').replace('//', '@').replace(/\//g, '.');
			}
			else
			{
				e = $(this).text().replace('//', '@').replace(/\//g, '.');
			}
		}
		else
		{ // 'normal'
			if($(this).is('a[rel]'))
			{
				e = $(this).attr('rel').split('').reverse().join('').replace('//', '@').replace(/\//g, '.');
			}
			else
			{
				e = $(this).text().split('').reverse().join('').replace('//', '@').replace(/\//g, '.');
			}
		}
		if(e)
		{
			if($(this).is('a[rel]'))
			{
				$(this).attr('href', 'mailto:' + e);
				if(settings.replaceText)
				{
					$(this).text(e);
				}
			}
			else
			{
				$(this).text(e);
			}
		}
	});
};

function set_font()
{
	if (!$.browser.msie)
	{
		Cufon.replace('h1, h2, h3, h4, h5, p, .txt, .txtnotl',
		{
			hover : true
		});
	}
	else
	{
		Cufon.replace('h1, h2, h3, h4, h5, p, .txt, .txtnotl',
		{
			hover: false
		});
	}
};
function set_font_no_tl() //cufon só no q não tiver tradução (nomes, localidades, etc )
{
	if (!$.browser.msie)
	{
		Cufon.replace('.txtnotl',
		{
			hover : true
		});
	}
	else
	{
		Cufon.replace('.txtnotl',
		{
			hover: false
		});
	}
};

function super_destaque()
{
	$('ul#sd li').hide(); // hide imgs
	$('ul#sd li:first').css({opacity: 0.0}).show().addClass('show').animate({opacity: 1.0}, 500); // show first

	/*
	$('ul#sd li:first').children('div.captionbg').fadeTo('fast', 0.4);
	$('ul#sd li:first').children('div.caption').fadeTo('fast', 1);
	*/

	if ($("ul#sd > li").size() > 1)
	{
		setInterval('sd_gallery()', 8000);
	}
};

function sd_gallery()
{
	var current = ($('ul#sd li.show') ? $('ul#sd li.show') : $('ul#sd li:first')); // Get first or current img
	var next = ((current.next().length) ? current.next() : $('ul#sd li:first')); // Define next img & rotate at end

	if (current.next().length)
	{
		next.css('margin-top', '-354px');
	}
	else
	{
		current.css('margin-top', '-354px');
	}

	next.children('div.captionbg').hide();
	next.children('div.caption').hide();
	next.css('z-index', '10').addClass('show').fadeIn(1000);
	
	current.children('div.captionbg').fadeOut('fast');
	current.children('div.caption').fadeOut('fast');
	
	current.removeClass('show').fadeOut(1000, function()
	{
		next.css('margin-top', '0').css('z-index', 'auto');
		next.children('div.captionbg').css('opacity', 0.4).fadeIn('fast');
		next.children('div.caption').fadeIn('fast');
	});

	/*
	current.children('div.caption').fadeOut(100, function()
	{
		current.children('div.captionbg').fadeTo(100, 0.1, function()
		{
			current.removeClass('show').fadeOut(1500);
			next.css('z-index', '10').addClass('show').fadeIn(1500, function()
			{
				next.css('margin-top', '0').css('z-index', 'auto');
				next.children('div.captionbg').fadeTo(100, 0.4, function()
				{
					next.children('div.caption').fadeTo(100, 1);
				});
			});
		});
	});
	*/

	/*
	next.css('z-index', '10').addClass('show').fadeIn(1500, function(){
		current.children('div.caption').fadeTo('fast', 0.1);
		current.children('div.captionbg').fadeTo('fast', 0.1);
	});			
	
	current.removeClass('show').fadeOut(1500, function()
	{
		next.css('margin-top', '0').css('z-index', 'auto');
		next.children('div.captionbg').fadeTo('fast', 0.4);
		next.children('div.caption').fadeTo('fast', 1);
		
	});
	*/

	/*
	current.children('div.caption').fadeOut('fast', function()
	{
		current.children('div.captionbg').fadeTo('fast', 0, function()
		{
			next.css('z-index', '10').addClass('show').fadeIn(1500);
			current.removeClass('show').fadeOut(1500, function()
			{
				next.css('margin-top', '0').css('z-index', 'auto');
				next.children('div.captionbg').fadeTo('fast', 0.4, function()
				{
					next.children('div.caption').fadeIn('fast');
				});
			});
		});
	});
	*/
};

function destaque()
{
	$('ul#d li').hide(); // hide imgs
	$('ul#d li:first').css({opacity: 0.0}).show().addClass('show').animate({opacity: 1.0}, 500); // show first

	if ($("ul#d > li").size() > 1)
	{
		setInterval('d_gallery()', 5500);
	}
};
function d_gallery()
{
	var current = ($('ul#d li.show')?  $('ul#d li.show') : $('ul#d li:first')); // Get first img
	var next = ((current.next().length) ? current.next() : $('ul#d li:first')); // Define next img & rotate at end

	if (current.next().length)
	{
		next.css('margin-top', '-224px');
	}
	else
	{
		current.css('margin-top', '-224px');
	}

	next.children('div.captionbg').hide();
	next.children('div.caption').hide();
	next.css('z-index', '10').addClass('show').fadeIn(1000);
	
	current.children('div.captionbg').fadeOut('fast');
	current.children('div.caption').fadeOut('fast');
	
	current.removeClass('show').fadeOut(1000, function()
	{
		next.css('margin-top', '0').css('z-index', 'auto');
		next.children('div.captionbg').css('opacity', 0.4).fadeIn('fast');
		next.children('div.caption').fadeIn('fast');
	});

	/*
	current.children('div.caption').fadeOut(100, function()
	{
		current.children('div.captionbg').fadeTo(100, 0.1, function()
		{
			current.removeClass('show').fadeOut(1500);
			next.css('z-index', '10').addClass('show').fadeIn(1500, function()
			{
				next.css('margin-top', '0').css('z-index', 'auto');
				next.children('div.captionbg').fadeTo(100, 0.4, function()
				{
					next.children('div.caption').fadeTo(100, 1);
				});
			});
		});
	});
	*/
};


/* translate */
var langFrom = "pt";
var langTo = "";
var ieFlag = false;
var transObject;
var busy_msg = '<h3>The page is being translated.</h3><h3>Please wait.</h3><br /><br /><h3>Click page anywhere to cancel.</h3><br />';

function setLangCookie(){
	if ($.cookie("langCookie")){
		langTo = $.cookie("langCookie");
		$.cookie("langFlag", "true",{
			path : "/"
		});
		$(function()
		{
			if (langTo != 'pt')
			{
				traduzir(p);
			}
			$('#langsel').val(langTo);
		});
	}
}
function deleteCookies(){
	$.cookie("langCookie", null,{
		path : "/"
	})
}

function traduzir(p){
	switch(p){
		case 'i':
			/* define o que se traduz e o que não se traduz */
			$('#box_veja, .tl').translate( langFrom, langTo, { 
				  not: '.samp', 
				  start : beforeTranslate,
				  error : errorTranslate,
				  complete : afterTranslate,
				  async: false,
				  toogle: true
			})
		break;
		case 'd':
			/* define o que se traduz e o que não se traduz */
			$('#texto, .tl').translate( langFrom, langTo, { 
				  not: '.samp', 
				  start : beforeTranslate,
				  error : errorTranslate,
				  complete : afterTranslate,
				  async: false,
				  toogle: true
			})
		break;
	}
}

function beforeTranslate(){
	transObject = this;
	
	$.blockUI({ message: busy_msg, css: { 
		border: 'none', 
		padding: '15px', 
		backgroundColor: '#fff', 
		'-webkit-border-radius': '10px', 
		'-moz-border-radius': '10px', 
		opacity: '.5', 
		color: '#333' 
		}
	});
	$("div.blockUI") . click(function (){
		transObject . stop();
		$.unblockUI();
		deleteCookies();
		window . location . reload()
	}); 
}
function errorTranslate(){
	$.unblockUI();
	alert("There was an error with the translation.  Please try again by refreshing your web browser.")
}
function afterTranslate(){
	langFrom = langTo;
	$.unblockUI();
	Cufon.refresh();
	if (!($.cookie("langCookie")))
	{
		alert("The language cookie has NOT been set.  In order to maintain your language preference, please modify your browser's cookie settings and try again.")
	}
	
}
/* end of translate */


$(function()
{
	
	/*translate */
	$ . browser . msie ? ieFlag = true : ieFlag = false;
		
	function initTranslate(A)
	{
		langTo = A;
		$.cookie("langCookie", langTo,{ // guardar preferencia da linguagem em cookie
			path : "/"
		});
		window . location . reload();
		return false;
		/*
		if(langTo == 'Portuguese'){
			deleteCookies();
			location.reload(true);
			return false;
		}
		*/
		return true;
	}

	$('#langsel').change(function()
	{
		if (!initTranslate($(this).val()))
		{
			return false;
		}
		if (langTo != 'pt')
		{
			traduzir(p);/* ou a secção q n seja o index */
		}
	});
	
	//if(!ieFlag){
		setLangCookie();
	//}
	/* end of tranlator*/
	
	// do not apply cufon on this languages:
	
	var nocufonx;
	var nocufon = new Array('ar', 'be', 'bg', 'zh-CN', 'zh-TW', 'el', 'iw', 'hi', 'ja', 'ko', 'mk', 'fa', 'ru', 'sr', 'th', 'uk', 'yi');
	Array.prototype.in_array = function(p_val) {
		for(var i = 0, l = this.length; i < l; i++)
		{
			if(this[i] == p_val)
			{
				return true;
			}
		}
		return false;
	}
	
	if(nocufon.in_array(langTo))
	{
		set_font_no_tl();
	}
	else
	{
		set_font();
	}

	$('.sbjs').show();

	if (p == 'i')
	{
		super_destaque();
	}
	destaque();
	
	if (p == 'd')
	{
		$('a.email, span.email').nospam();
		
		var galleries = $('.ad-gallery').adGallery(
		{
			width: 893,
			height: 354,
			start_at_index: 0,
			animation_speed: 500,
			display_next_and_prev: false,
			display_back_and_forward: false,
			scroll_jump: 0,
			slideshow:
			{
				enable: true,
				autostart: true,
				speed: 5000,
				stop_on_scroll: true
			},
			effect: 'fade',
			enable_keyboard_move: true,
			cycle: true,
			callbacks:
			{
				beforeImageVisible: function(new_image, old_image)
				{
					set_font();
				}
			}
		});
	}
});
