$(document).ready(function(){
	$('#info-submit').click(function(e){
		e.preventDefault();
		//
		$.ajax({
			url: $('#newsletter form').attr('action'),
			type: 'POST',
			data: $('#newsletter form').serialize(),
			dataType: 'json',
			success: function(retData){
				alert((retData.success == true ? 'obrigado por se cadastrar' : 'ocorreu um erro, tente outra vez'));
			}
		});
	});
	$('#enviarform').click(function(e){
		e.preventDefault();
		//
		$.ajax({
			url: $('#formulario form').attr('action'),
			type: 'POST',
			data: $('#formulario form').serialize(),
			dataType: 'json',
			success: function(retData){
				alert((retData.success == true ? 'obrigado por entrar em contato' : 'ocorreu um erro, tente outra vez'));
			}
		});
	});
	$('.menu li ul').hover(
		function(){ $(this).parent().find('a:first').addClass('grayb'); },
		function(){ $(this).parent().find('a:first').removeClass('grayb'); }
	);
	$('#slider').nivoSlider({
		effect:'fade',
		animSpeed:500, // Slide transition speed
		pauseTime:4000, // How long each slide will show
		directionNav:false,
		controlNav:false
	});
	$('#img1').nivoSlider({
		effect:'fade',
		animSpeed:500, // Slide transition speed
		pauseTime:6000, // How long each slide will show
		directionNav:false,
		controlNav:false
	});
	$('#img2').nivoSlider({
		effect:'fade',
		animSpeed:500, // Slide transition speed
		pauseTime:5000, // How long each slide will show
		directionNav:false,
		controlNav:false
	});
	$(":input[placeholder]").placeholder();
	$("a#contact-form, a#contact-footer").fancybox({
		'padding': 0,
		'titleShow': false,
		'transitionIn': 'elastic',
		'transitionOut': 'elastic',
		'easingIn': 'easeInQuad',
		'easingOut': 'easeOutQuad',
		'overlayOpacity': 0.7,
		'overlayColor': '#333'
	});
	// -- Ini: Busca --
	$('#busca-form #submit').click(function(e){
		e.preventDefault();
		$('#busca-form').submit();
	});
	//
	$('#field').focus(function(){
		$('#field').val('');
	}).focusout(function(){
		if($('#field').val() == '') $('#field').val('Busca');
	});
	// -- End: Busca --
	$.each($('.artistas li h3'), function(i, elm){
		var artistaCharCount = $(elm).text().length;
		if(artistaCharCount > 20)
			$(elm).css({
				fontSize : '12px',
				overflow : 'hidden'
			});
	});
});
var Slider = {
	ease: 'easeInOutCubic',
	speed: 750,
	box: 293,
	current: 1,
	leng:{},
	checkDisable: function()
	{
		if(Slider.current == 1)
			$('#news-left').animate({ opacity : 0.7},300);
		else if(Slider.current == Slider.leng-2)
			$('#news-right').animate({ opacity : 0.7},300);
		else
			$('#news-left, #news-right').animate({ opacity : 1},300);
	},
	init: function()
	{
		Slider.leng = $('#noticias-slider a').size();
		//check disable
		Slider.checkDisable();
		$('#noticias-slider').css({ width: Slider.box * Slider.leng });
		$('#news-left').bind('click', Slider.leftclick);
		$('#news-right').bind('click', Slider.rightclick);
	},
	leftclick: function()
	{
		$('#news-left').unbind();
		if(Slider.current != 1){
			Slider.current--;
			Slider.checkDisable();
			$('#noticias-slider').stop(true).animate({
				marginLeft: '+='+Slider.box
			}, Slider.speed, Slider.ease, function(){
				//animate callback
				$('#news-left').bind('click', Slider.leftclick);
			});
		}else{
			$('#news-left').bind('click', Slider.leftclick);
		}
	},
	rightclick: function()
	{
		$('#news-right').unbind();
		if(Slider.current != (Slider.leng)-2 ){
			Slider.current++;
			Slider.checkDisable();
			$('#noticias-slider').stop(true).animate({
				marginLeft: '-='+Slider.box
			}, Slider.speed, Slider.ease, function(){
				//animate callback
				$('#news-right').bind('click', Slider.rightclick);
			});
		}else{
			$('#news-right').bind('click', Slider.rightclick);
		}
	}
};
var Banner = {
	ease: 'easeInOutCubic',
	interval: {},
	intervalTime: 6000,
	speed: 400,
	init: function()
	{
		$('#destaques-nav li a').bind('click', Banner.click);
		$('#destaques').find('.destaque').bind('mouseenter', Banner.overIn);
		$('#destaques').find('.destaque').bind('mouseleave', Banner.overOut);
		Banner.loop();
	},
	loop: function()
	{
		Banner.interval = window.setInterval(Banner.anima, Banner.intervalTime);
	},
	clear: function()
	{
		window.clearInterval(Banner.interval);
	},
	anima: function()
	{
		var $elm = $('#destaques-nav').find('.selected').parent();
		var $a;
		var index = $elm.index();
		var len = $elm.siblings().size();
		if(index == len)
			$a = $elm.siblings().first().find('a');
		else
			$a = $elm.next().find('a');
		$a.trigger('click');
	},
	click: function(e)
	{
		e.preventDefault();
		Banner.clear();
		var $elm = $(this);
		var $li = $elm.parent();
		$li.parent().find('.selected').removeClass('selected');
		$elm.addClass('selected');
		$('#destaques').find('.destaque a:visible').hide();
		$('#destaques').find('.destaque a').eq($li.index()).show();
		Banner.loop();
	},
	overIn: function(e)
	{
		e.preventDefault();
		Banner.clear();
		var $elm = $(this);
		var $a = $elm.find('a:visible');
		$a.find('.info')
			.stop()
			.animate({
				marginLeft:0
			}, Banner.speed, Banner.ease);
	},
	overOut: function(e)
	{
		e.preventDefault();
		var $elm = $(this);
		var $a = $elm.find('a:visible');
		$a.find('.info')
			.stop()
			.animate({
				marginLeft: -352
			}, Banner.speed, Banner.ease);
		Banner.loop();
	}
};
/* módulo noticias */
var boxSlider =
{
	box: 323,
	ease: 'easeInOutQuint',
	speed: 900,
	size: {},
	animating: false,
	current: 1,
	init: function()
	{
		boxSlider.checkDisable();
		boxSlider.size = $('.m-noticias .block').size();
		$('.block').css('width', boxSlider.size*boxSlider.box+'px');
		$('#m-news-left').bind('click', boxSlider.clickLeft);
		$('#m-news-right').bind('click', boxSlider.clickRight);
	},
	checkDisable: function()
	{
		if(boxSlider.current == 1)
			$('#m-news-left').animate({ opacity : 0.7},300);
		else if(boxSlider.current == boxSlider.size)
			$('#m-news-right').animate({ opacity : 0.7},300);
		else
			$('#m-news-left, #m-news-right').animate({ opacity : 1},300);
	},
	clickRight: function(e)
	{
		e.preventDefault();
		if(boxSlider.animating === false && boxSlider.current != boxSlider.size)
		{
			boxSlider.animating = true;
			boxSlider.current++;
			boxSlider.checkDisable();
			var ani1 = $('.m-noticias .block:nth-child(1)').animate({ marginLeft : '-='+boxSlider.box }, boxSlider.speed, boxSlider.ease);
			var ani2 = $('.m-noticias .block:nth-child(2)').delay(75).animate({ marginLeft : '-='+boxSlider.box }, boxSlider.speed, boxSlider.ease);
			var ani3 = $('.m-noticias .block:nth-child(3)').delay(150).animate({ marginLeft : '-='+boxSlider.box }, boxSlider.speed, boxSlider.ease);
			$.when( ani1, ani2, ani3 ).done(boxSlider.animateFinished);
		}
	},
	clickLeft: function(e)
	{
		e.preventDefault();
		if(boxSlider.animating === false && boxSlider.current != 1)
		{
			boxSlider.animating = true;
			boxSlider.current--;
			boxSlider.checkDisable();
			var ani1 = $('.m-noticias .block:nth-child(1)').animate({ marginLeft : '+='+boxSlider.box }, boxSlider.speed, boxSlider.ease);
			var ani2 = $('.m-noticias .block:nth-child(2)').delay(75).animate({ marginLeft : '+='+boxSlider.box }, boxSlider.speed, boxSlider.ease);
			var ani3 = $('.m-noticias .block:nth-child(3)').delay(150).animate({ marginLeft : '+='+boxSlider.box }, boxSlider.speed, boxSlider.ease);
			$.when( ani1, ani2, ani3 ).done(boxSlider.animateFinished);
		}
	},
	animateFinished: function()
	{
		boxSlider.animating = false;
	}
}
/* troca de estilos, menu usabilidade */
var usabilidade = {
	cookieText: {},
	cookieContraste: {},
	init: function()
	{
		usabilidade.cookieText = $.cookie('cookieText');
		usabilidade.cookieContraste = $.cookie('cookieContraste');
		$('.aac a').bind('click', usabilidade.click);
		usabilidade.change();
	},
	click: function(e)
	{
		e.preventDefault();
		var self = $(this);
		var cookie_value;
		self.toggleClass('active');
		if(self.hasClass('active'))
			cookie_value = 'has';
		else
			cookie_value = null;
		var button_name = self.attr('id');
		switch(button_name)
		{
			case 'ux-fonte':
				$.cookie('cookieText', cookie_value, { expires: 7, path:'/' });
				break;
			case 'ux-contraste':
				$.cookie('cookieContraste', cookie_value, { expires: 7, path:'/' });
				break;
		}
		usabilidade.change();
	},
	change: function()
	{
		if($.cookie('cookieText') != null)
		{
			$('body').addClass('fontemaior');
			$('#ux-fonte').addClass('active');
		}
		else
		{
			$('body').removeClass('fontemaior');
			$('#ux-fonte').removeClass('active');
		}
		if($.cookie('cookieContraste') != null)
		{
			$('body').addClass('contraste');
			$('#ux-contraste').addClass('active');
		}
		else
		{
			$('body').removeClass('contraste');
			$('#ux-contraste').removeClass('active');
		}
	}
};
var getSocial = {
	init: function()
	{
		$('.share a').bind('click', getSocial.click);
	},
	click: function(e)
	{
		e.preventDefault();
		var c	= $(this).attr('class'),
			id	= $(this).attr('rel'),
			pt	= $('.dark').find('a').text(),
			p1	= $('.content div').find('p:first').text(),
			p2	= $('.content div').find('p:nth-child(2)').text();
		var desc= p1+' '+p2.substring(0,140)+'...',
			path = (pt == 'Componentes') ? 'componentes/' : 'artista/';
		var url		= box3document.baseURL + path + id,
			title	= $('.content h1:first').text(),
			share	= '',
			findImg	= $('.content').find('.img').find('img').attr('src');
		var thumb = (findImg != undefined) ? $('.content .img:first img').attr('src') : box3document.baseURL + 'images/bienal_mercosul.png';
		switch(c)
		{
			case 'orkut':
				share = 'http://promote.orkut.com/preview?nt=orkut.com&tt='+title+'&cn='+desc+'&du='+url+'&tn='+thumb;
				break;
			case 'facebook':
				share = 'http://www.facebook.com/sharer.php?s=100&p[title]='+title+'&p[url]='+url+'&p[summary]='+desc+'&p[images][0]='+thumb;
				break;
			case 'twitter':
				share = 'http://twitter.com/share?lang=pt_BR&text='+title+'&url='+url;
				break;
		}
		window.open(share);
	}
}
$(function(){
	Banner.init();
	Slider.init();
	boxSlider.init();
	usabilidade.init();
	getSocial.init();
	/**/
	Artistas.init();
	/**/
	$('.leng-opacity').click(function(e){ e.preventDefault(); });
});
/**
* HIC SUNT DRACONES + HIC SUNT DRACONES + HIC SUNT DRACONES + HIC SUNT DRACONES
*/
var Artistas = {
	ease: 'easeInOutCubic',
	interval: {},
	intervalTime: 3013,
	speed: 400,
	init: function()
	{
		$('.artistas li').bind('mouseenter', Artistas.overIn);
		$('.artistas li').bind('mouseleave', Artistas.overOut);
		Artistas.loop();
	},
	loop: function()
	{
		Artistas.interval = window.setInterval(Artistas.anima, Artistas.intervalTime);
	},
	clear: function()
	{
		window.clearInterval(Artistas.interval);
	},
	anima: function()
	{
		Artistas.clear();
		//
		var $a = $('.artistas li:visible');
		var $i = $a.index() + 1;
		$a.hide();
		if ( $i == $('.artistas li').size() ) $i = 0;
		$('.artistas li').eq($i).show();
		//
		Artistas.loop();
	},
	overIn: function(e)
	{
		e.preventDefault();
		Artistas.clear();
	},
	overOut: function(e)
	{
		e.preventDefault();
		Artistas.loop();
	}
};

