/*
	Turim
	...

	por: M2BRNET (http://www.m2brnet.com)
\* ......................................................................................................................................... */

/*
	Namespace
\* ......................................................................................................................................... */

var M2 = {};

M2.Util = {

	init: function() {
		M2.Util.externalLinks();
		M2.Util.placeholder();
	},

	externalLinks: function() {
		$('a[rel*="external"]').attr('target','_blank');
	},

	placeholder: function()
	{
		if (!M2.Util.nativePlaceholderSupport())
		{
			$('[placeholder]').focus(function() {
				var input = $(this);
				if (input.val() == input.attr('placeholder'))
				{
					input.val('');
					input.removeClass('placeholder');
				}
			}).blur(function() {
				var input = $(this);
				if (input.val() == '' || input.val() == input.attr('placeholder'))
				{
					input.addClass('placeholder');
					input.val(input.attr('placeholder'));
				}
			}).blur().parents('form').submit(function() {
				$(this).find('[placeholder]').each(function() {
					var input = $(this);
					if (input.val() == input.attr('placeholder'))
					{
						input.val('');
					}
				});
			});
		}
	},

	nativePlaceholderSupport: function()
	{
		var i = document.createElement('input');
		return ('placeholder' in i);
	},

	validaEmail: function(s)
	{
		var regexpEmail = /\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/;
		return regexpEmail.test(s);
	}

};

/*
	DOM Loaded
\* ......................................................................................................................................... */

$(function(){

	M2.Util.init();

	$('#pilares').flash({
		src    : raiz_site + '_swf/quebra_cabeca.swf',
		wmode  : 'transparent',
		width  : 257,
		height : 180
	});

	$('#mycarousel').jcarousel({
		wrap         : 'circular',
		vertical     : true,
		scroll       : 1,
		auto         : 5,
		animation    : 700,
		initCallback : function(carousel)
		{
			carousel.clip.hover(function()
			{
				carousel.stopAuto();
			}, function()
			{
				carousel.startAuto();
			});
		},
		itemFirstInCallback : function(carousel, item, idx, state)
		{
			var nextItem   = $(item).next(),
				linkObj    = $('a' , nextItem);

			rodar(linkObj);
		}
	});

	$('#mycarousel a').click(function(e)
	{
		rodar($(this));
		e.preventDefault();
	});


	function rodar(el)
	{
		var newIdx = $(el).data('banner');
		$('.main-banner li, #mycarousel a').removeClass('active');
		$('.main-banner li#' + newIdx + ', #mycarousel a[data-banner="'+newIdx+'"]' ).addClass('active');
	}


	// Intro Layer
	(function()
	{
		var open       = false,
			elPosition = $('.intro').position(),
			initHeight = $('.intro-wrapper').outerHeight(),
			endHeight  = $('.intro-copy').outerHeight();

		$('.intro .mais').click(function(e) {
			if (open)
			{
				$('.intro-wrapper').animate({ 'height' : initHeight } , 300 , function() {
					$('.intro').css({ 'position' : 'relative' , 'left' : '0' , 'top' : '0' });
					$('.intro .mais').text('Leia Mais');
				});
				open = false;
			}
			else
			{
				$('.intro').css({ 'position' : 'absolute' , 'left' : elPosition.left , 'top' : elPosition.top });
				$('.intro-wrapper').animate({ 'height' : endHeight } , 300 , function() {
					$('.intro .mais').text('Fechar');
				});
				open = true;
			}
			e.preventDefault();
		});
	})();

	/*
	// Carrossel Home
	(function()
	{
		var el      = $('.carrossel'),
			current = 0,
			step    = 1,
			size	= $('li' , el).outerHeight(true),
			max		= $('li' , el).size(),
			visible = parseInt( el.outerHeight(true) / size , 10 );

		// console.log(size + ' - ' + max + ' - ' + visible);

		$('.crs-wrapper' , el).css({ 'position' : 'relative' });
		$('.crs-thumbs' , el).css({ 'position' : 'absolute' , 'top' : '0' });

		$('.btn-crs-down' , el).click(function(e) {
			if ( current + step < 0 || current + step > max - visible ) return false;

			current = current + step;
			$('ul' , el).animate({ top: - ( size * current ) }, 300, null);

			e.preventDefault();
		});
		$('.btn-crs-up' , el).click(function(e) {
			if ( current - step < 0 || current - step > max - visible ) return false;

			current = current - step;
			$('ul' , el).animate({ top: - ( size * current ) }, 300, null);

			e.preventDefault();
		});

		$('.crs-thumbs a' , el).click(function(e) {
			var newHref    = $(this).attr('href')
				currentImg = $('.main-banner img').css({ 'z-index' : '10' })
				newImg     = $('<img />', { src : newHref });
			$('.banner-loader').fadeIn( '100' );

			$(newImg).load(function() {
				$('.banner-loader').fadeOut( 100 , function(){
					$(newImg).insertAfter(currentImg);
					$(currentImg).fadeOut( 200 , function(){
						$(this).remove();
					});
				});
			});

			e.preventDefault();
		});
	})();
	*/

	// Colorbox
	$('.cbox').colorbox({
		next     : 'Próximo',
		previous : 'Anterior',
		close    : 'Fechar',
		current  : ''
	});

	// Colorbox Textos
	$('.cbox-text').colorbox({
		inline : true,
		href   : function(){ return $(this).attr('href'); },
		//width  : 500
	});
	$('#pilares').removeClass("pilares_alt").hasClass("flash-replaced");
	// Accordions
	$('#accordion-list').each(function(){
		var el = $(this);
		$('.acc-content' , el).hide();

		$('.acc-trigger' , el).click(function(e){
			$('.acc-trigger').not(this).next('.acc-content').slideUp().end().parent().removeClass('on');
			$(this).next('.acc-content').slideToggle().end().parent().toggleClass('on');
			e.preventDefault();
		});
	});





});

