jQuery = jQuery.noConflict(true);

jQuery(document).ready(function($) {

	$('div#container div#content_tabs div#portfolio').show(function(){

		$("a[rel=fancybox]").fancybox({
		
			'onComplete' : function() {
			
				$("#fancybox-wrap").hover(function() {
				
					$("#fancybox-title").show();
					
				}, function() {
				
					$("#fancybox-title").hide();
					
				});
				
			}
			
		});

		$('div#container div#nav ul#navigation li.tab1 a').addClass('current');

	});

	$('a.control_tabs').click(function(){

		var thisHref = $(this).attr('href');

		$('div#container div#nav ul#navigation li a').removeClass('current');

		$('div#container div#content_tabs div.tab').hide();

		$('div#content_tabs div#' + thisHref.substring(1)).fadeIn(1000);

		$('div#container div#nav ul#navigation li#' + thisHref.substring(1) + '_menu a').addClass('current');
		
		$("div#content_tabs div#" + thisHref.substring(1) + " div.accordion_wrap").each(function()
		{
			$("div.accordion div:first h3 a", this).trigger("click", function(){runAccordion();});
		});
		
		return false;

	});

	$('html, form.send_message, .text, .progressbar').noiser({ opacity: 0.4, contrast: 40, red: 230, green: 230, blue: 230, size: 128 });

	$("div.accordion h3 a").click(runAccordion);
	
	$('.text').each(function()
	{
		$(this).focus(function()
		{
			if($(this).val() == this.defaultValue)
			{
				$(this).val('');
			}
		});		
		$(this).blur(function()
		{
			if($(this).val() == '')
			{
				$(this).val(this.defaultValue);
			}
		});
	});
	
	$('.submit').click(submitForm);
	
	$(".data").mask("99/99/9999", {placeholder:"*"});
	
	$(".cpf").mask("999.999.999-99", {placeholder:"*"});
	
	$(".phone").mask("(99) 9999.9999", {placeholder:"*"});
	
	$('.selflabel').each(function()
	{
		$('.output', this).focus(function()
		{
			$(this).siblings('label').hide();
		});
		$('.output', this).blur(function()
		{
			if($(this).val() == '')
			{
				$(this).siblings('label').show();
			}
		});
	});
	
	$('.sexo').each(function()
	{
		label = $(this).children('label');
		label.each(function()
		{
			$(this).parent().append('<a href="#' + $(this).attr('for') +'">' + $(this).text() + '</a>');
		}).hide();
		$(this).children('a').click(function()
		{
			$(this).parent().find('.output').removeClass('output');
			$(this).addClass('selected').css('cursor', 'text').siblings('a').removeClass('selected').css('cursor', 'pointer');
			$('input#' + $(this).attr('href').substring(1)).addClass('output');
			return false;
		});
	});
	
	//Helper functions
	function runAccordion()
	{
		if(!$(this).parent().parent().hasClass('active')){

			$(this).parent().parent().siblings('.active').children('.acc').hide(0, resetGraphic).parent().removeClass('active');

			$(this).parent().siblings('.acc').slideToggle(250, loadGraphic).parent().toggleClass('active');

		}
		return false;
	}	
	function fillProgressBar()
	{
		progressBar = $(".progressbar", this);
		progressBarFill = progressBar.children(".progressbarfill");
		progressBarFill.append("<span>" + progressBar.attr("title") + "</span>").animate({width: progressBarFill.attr("title")}, 1000);
		
	}	
	function resetGraphic()
	{
		graphic = $(".graphic", this);
		progressBar = graphic.children(".progressbar");
		progressBarFill = progressBar.children(".progressbarfill");
		progressBarFill.css("width", "0px").html("");
	}	
	function loadGraphic()
	{
		graphic = $(this).children(".graphic");
		graphic.each(fillProgressBar);
	}	
	function submitForm()
	{
		form = $(this).parent().parent();
		request = form.attr('action');
		method = form.attr('method');
		overlay = form.siblings('.form_overlay');
		formdata = '';
		overlay.fadeIn(function()
		{
			form.children('.form_content').find('.output').each(function()
			{
				formdata += $(this).attr('name') + '=' + $(this).val() + '&';
			});
			
			$.ajax(
			{
				type: method,
				data: formdata,
				url: request,
				success: function(msg)
				{
					overlay.append('<div class="form_response"><a href="#close"></a><p>' + msg + '</p></div>');
					form_response = overlay.children('.form_response');
					form_response.css({'margin-top': '-' + form_response.innerHeight()/2 + 'px'}).children('a').click(function()
					{
						overlay.fadeOut(function()
						{
							form_response.detach();
						});
						return false;
					});
				}
			});
		});
		return false;
	}
});
