$(document).ready(onReady);

function onReady()
{
	if($('div#header ul li a').length > 0) $('div#header ul li a').not('.current').mouseover(menuOver).mouseleave(menuOut);		
	if($('div#homeHigh div.homeMidias ul li a').length > 0) $('div#homeHigh div.homeMidias ul li a').css( {backgroundPosition: "100% 0"} ).mouseover(midiasOver).mouseleave(midiasOut);
	if($('div#cinzaFull div.wrapper div.destaques div.movel a, div.allPortfs div.portfolios a').length > 0) $('div#cinzaFull div.wrapper div.destaques div.movel a, div.allPortfs div.portfolios a').live('mouseover', destaqueOver).live('mouseleave', destaqueOut);	
	
	if($('div.whiteTheme div.clientes ul li a').length > 0) {
		$('div.whiteTheme div.clientes ul li a').live('mouseover', clientesOver).live('mouseleave', clientesOut).each(function(index, value){
			var todos = $('div.whiteTheme div.clientes ul li a').length;
			if((index % 3) == 2) $(value).addClass('noRightBorder');
			if(index == 0 || index == 1 || index == 2) $(value).addClass('noTopBorder');
		});
	}
	
	if($('div.cinzaFull div.categorias div.comboClientes ul.all li a').length > 0) {
		$('div.cinzaFull div.categorias div.comboClientes ul.all li a').each(function(index, value){
			var todos = $('div.cinzaFull div.categorias div.comboClientes ul.all li a').length;
			if((index % 8) == 7) $(value).addClass('notRightBorder');
		});
	}
	
	if($('a.maps').length > 0) $('a.maps').mouseover(mapsHover).mouseleave(mapsOut)
	
	if($('div.cinzaFull div.categorias div.comboClientes a.foco').length > 0) $('div.cinzaFull div.categorias div.comboClientes a.foco').click(comboClientes)
	
	Shadowbox.init({
		overlayOpacity: 0.7,
		handleOversize: 'drag'
	});
	
	if($('div.allPortfs div.portfolios').length > 0) moreJobs();
	destaques();
	
	$("form[name=contato], form[name=trabalhe]").ajaxForm({bindString: 'obrigatorio', bindAttr: 'class', loading: true});
	
	//([http|ftp|https]+:\/\/)([a-zA-Z0-9\-\.]+)?([a-zA-Z0-9\-]*)(\.[a-zA-Z0-9]*)(\.[a-zA-Z0-9]*)?(\/[a-zA-Z0-9?&=]+)
	/*var links = new RegExp("([http|ftp|https]+:\/\/)([a-zA-Z0-9\-\.]+)?([a-zA-Z0-9\-\.]+)([a-zA-Z0-9\/]+)?([a-zA-Z0-9?=]*)?");
	
	$('div.autoLink p').each(function(index, value){
		var texto = $(value).html();
		if(links.test(texto)) {
			$(value).html(texto.replace(links, '<a href="$1" target="_blank">$1</a>'));
		}
	});*/
}

function Alert(mensagem, hideOk)
{
	if(typeof(hideOk) == "undefined") hideOk = false;
	
	$('body').append('<div class="a-fade"></div><div class="a-mensagem"><p>'+ mensagem +'</p><a href="javascript:;" class="ok">OK</a></div>');
	
	if(hideOk) $('div.a-mensagem a.ok').css('opacity', '0');
	
	var topPos = ($(window).height() / 2) - ($('div.a-mensagem').height() / 2);
	$('div.a-fade').animate({opacity: 0.6}, {duration: 500, easing: 'easeOutExpo'});
	$('div.a-mensagem').animate({top: topPos}, {duration: 1000, easing: 'easeOutBack'});
	$('div.a-mensagem a.ok').click(function(){
		$('div.a-fade').animate({opacity: 0}, {duration: 500, easing: 'easeOutExpo'});
		$('div.a-mensagem').animate({top: -400}, {duration: 1000, easing: 'easeInBack', complete: function(){ 
			$('div.a-fade, div.a-mensagem').remove();
		}});
	});
}

function mapsHover()
{
	$(this).children('img.ponto').stop().animate({top: -15}, {duration: 500, easing: 'easeOutExpo'});
	$(this).children('img.ponto-s').stop().animate({opacity: 0.3, left: 21, top: 8}, {duration: 500, easing: 'easeOutExpo'});
}

function mapsOut()
{
	$(this).children('img.ponto').stop().animate({top: -8}, {duration: 500, easing: 'easeOutBounce'});
	$(this).children('img.ponto-s').stop().animate({opacity: 1, left: 17, top: 10}, {duration: 500, easing: 'easeOutBounce'});
}

function comboClientes()
{
	$(this).parent().children('ul.all').fadeIn(500).mouseleave(function(){ $(this).fadeOut(500); });
}

function clientesOver()
{ $(this).find('span').stop().animate({bottom: 0}, {duration: 500, easing: 'easeOutExpo'}); }

function clientesOut()
{ $(this).find('span').stop().animate({bottom: -36}, {duration: 500, easing: 'easeOutExpo'}); }

function moreJobs()
{
	var pagina = 0;
	$('div.allPortfs a.moreWork').click(moreJobsPortfolio);
	
	var reg = "page=portfolio&";
	var params = window.location.href;
	params = params.split('?');
	
	function moreJobsPortfolio()
	{
		var querys = "pagina=" + pagina + "&" + params[1].replace(reg, '');
		var childs = $('div.allPortfs div.portfolios a').length;
		//if(childs < 8) { $('div.allPortfs a.moreWork').slideUp(500); return false; }
		
		$.ajax({
			url: "moreJobs.php",
			type: "POST",
			data: querys,
			dataType: "json",
			error: function() { alert('Erro ao carregar mais Jobs'); },
			success: function(data) { 
				if(data.status == 'fim') $('div.allPortfs a.moreWork').slideUp(500);
				var divisao = (394 / 1); 				
				if(data.total <= 4 && childs < 4) { divisao = 0; } else if(data.total <= 4) { divisao = (394 / 2); } else { divisao = (394 / 1); }

				$('div.allPortfs div.portfolios').animate({height: '+=' + divisao}, {duration: 1000, complete: function(){
					$(this).append(data.dados);
					$(this).find('a').fadeIn(500);
					$('html, body').animate({scrollTop: $(document).height()}, {duration: 1500, ease: 'easeout'});
				}});	
				pagina++;
			}
		});
	}
}

function destaques()
{
	var childs = $('div.destaques div.movel a').length;
	var dWidth = $('div.destaques').width() + 28;
	var width = 253;
	var pages = Math.ceil(childs / 3);
	var page = 0;
	
	$('div.destaques div.movel').width(childs * width);
	
	$('div#cinzaFull a.left').click(function(){
		page = (page <= 0) ? pages - 1 : page - 1;
		animar();
		return false;
	});
	
	$('div#cinzaFull a.right').click(function(){
		page = (page > (pages-2)) ? 0 : page + 1;
		animar();
		return false;
	});
	
	function animar()
	{
		$('div.destaques div.movel').animate({left: -(page * dWidth)}, {duration: 500, easing: 'easeOutExpo'});
	}
}

function destaqueOver()
{ $('span.borda', this).stop().animate({backgroundColor: '#FF5214'}, {duration: 200, easing: 'easeOutExpo'}); }

function destaqueOut()
{ $('span.borda', this).stop().animate({backgroundColor: '#FFFFFF'}, {duration: 250, easing: 'easeOutExpo'}); }

function midiasOver()
{ $(this).stop().animate({backgroundPosition:"(100% -35px)"}, {duration: 400, easing: 'easeOutExpo'}); }

function midiasOut()
{ $(this).stop().animate({backgroundPosition:"(100% 0)"}, {duration: 400, easing: 'easeOutExpo'}); }

function menuOver()
{
	$(this).animate({color: '#FFFFFF'}, {duration: 300}).children('span.fundo').animate({height: '100%'}, {duration: 300, easing: 'easeOutExpo'});
}

function menuOut()
{
	var cor = ($(this).parent().is('.orange')) ? '#FF5214' : '#6F6E6E';
	$(this).animate({color: cor}, {duration: 300}).children('span.fundo').animate({height: 0}, {duration: 300, easing: 'easeOutExpo'});
}
