
$(function(){
	$('ul.submenu').css('left', '-510px');

	var hash = window.location.hash.substr(1);
	if ( !hash ){
		hash = window.location.pathname.replace(/.*\//, '');
		hash = hash.replace(/\.php$/i, '');
	}
	if ( !hash ){
		hash = 'index';
	}
	loader_on_mouse();
	
	$('ul.menu').myDock({'proximity': 60, 'scale':1.3});
	
	$('ul.menu a').each(function(){
		var href = $(this).attr('href');
		if ( hash == href.replace(/\.php$/i, '') ){
			if ( $(this).parents('ul.submenu').length )
				var content_only = true;
			else
				var content_only = false;
			load_content(href, content_only);
			animate_menus($(this));
		}
	});
	
	$('ul.menu li a').click(function(){
		if ( $(this).parents('ul.submenu').length )
			var content_only = true;
		else
			var content_only = false;
		load_content($(this).attr('href'), content_only);
		animate_menus($(this));
		return false;
	});
	
	$('div.middle')
	.mousemove(function(event){
		$('.pointer').stop();
		if ( event.pageX < 523 ) {
			if ( event.pageY <= 498 && event.pageY >= 398 ){
				var top = (event.pageY -8) +'px';
				$('.pointer').css('top', top);
			}
		} else {
			var top = $('ul.menu li.active').offset().top;
			$('.pointer').animate({'top': top+3+'px'}, 500);
		}
	})
	.mouseenter(function(event){
		$('.pointer').stop();
		if ( event.pageX < 523 ) {
			if ( event.pageY <= 498 && event.pageY >= 398 ){
				var top = (event.pageY -8) +'px';
			} else {
				if ( event.pageY > 498 )
					var top = '490px';
				else
					var top = '390px';
			}
		} else {
			var top = $('ul.menu li.active').offset().top +3 +'px';
		}
		$('.pointer').css('top', top);
	})
	.mouseleave(function(){
		var top = $('ul.menu li.active').offset().top;
		$('.pointer').animate({'top': top+3+'px'}, 500);
	});
});

var init_recaptcha = function(){
	Recaptcha.create("6LfFnLsSAAAAADenr5x2XchbGiVxCSntpXMVhHuV",
		"captcha", {
		theme: "custom",
		lang: 'pt',
		custom_theme_widget: 'recaptcha_widget',
		callback: Recaptcha.focus_response_field
	});
	
	$('.reload').click(function(){
		Recaptcha.reload();
		return false;
	});
	$('.audio').click(function(){
		Recaptcha.switch_type('audio');
		return false;
	});
	$('.image').click(function(){
		Recaptcha.switch_type('image');
		return false;
	});
	$('.help').click(function(){
		Recaptcha.showhelp();
		return false;
	});
	
	$('.toggle').click(function(){
		$('.toggle').toggleClass('active');
		return false;
	});
};

var animate_menus = function(link){
	$('ul.menu li').each(function(){
		var img = $(this).find('img');
		if ( img.length ) {
			img.each(function(){
				if ( $(this).attr('src').indexOf('_branco') === -1 )
					$(this).attr('src', $(this).attr('src').replace(/\.png$/, '_branco.png'));
			});
		}
	});
	
	if ( $(link).parents('ul.submenu').length ){
		$('ul.submenu li.active').removeClass('active');		
		$(link).parents('ul.submenu').animate({left: '530px'}, 500);
		$(link).parent().addClass('active');
		$(link).parents('ul.submenu').parent().addClass('active');
		var p_img = $(link).parents('li.item').find('img:first');
		if ( p_img.length )
			p_img.attr('src', p_img.attr('src').replace(/_branco/i,''));
			
		var top = $(link).parent().offset().top;
		$('.pointer').css('top', top+3+'px');
		
		var img = $(link).find('img');
		if ( img.length )
			img.attr('src', img.attr('src').replace(/_branco/i, ''));
	} else {
		if ( $('ul.submenu:visible').length ){
			$('ul.submenu:visible').animate({left: $(document).width()+'px'}, 500, function(){
				$(this).css('left', '-510px');
				$('ul.menu li.active').removeClass('active');
				$(link).parent().addClass('active');
				$(link).parent().find('ul.submenu').animate({left: '530px'}, 500);
				var top = $(link).parent().offset().top;
				$('.pointer').css('top', top+3+'px');
				
				var img = $(link).find('img');
				if ( img.length )
					img.attr('src', img.attr('src').replace(/_branco/i, ''));
			});
		} else {
			$('ul.menu li.active').removeClass('active');
			$(link).parent().addClass('active');
			$(link).parent().find('ul.submenu').delay(500).animate({left: '530px'}, 500);
			var top = $(link).parent().offset().top;
			$('.pointer').css('top', top+3+'px');
			
			var img = $(link).find('img');
			if ( img.length )
				img.attr('src', img.attr('src').replace(/_branco/i, ''));
		}
	}
};

var loader_on_mouse = function(){
	var div = '<div class="loading"><img src="img/loading/loading.gif"/></div>';
	if ( !$('.loading').length ){
		$('body').append(div);
	}
	$(document).mousemove(function(e){
		if ( e.pageX + 82 >= $(document).width() ||
			 e.pageY + 82 >= $(document).height() ){
			var offset = -32;
		} else
			var offset = 20;
		$('.loading').css('left', e.pageX+offset+'px');
		$('.loading').css('top', e.pageY+offset+'px');
	})
	.ajaxStart(function(){
		$('.loading').fadeIn('fast');
	})
	.ajaxStop(function(){
		$('.loading').fadeOut('fast');
	});
};

var update_header = function(data){
	$('.header h1').animate({'background-position':'-460px 0'}, 400, function(){
		$(this).html( $(data).find('.header h1').html() );
		$(this).removeClass('index portfolio clientes contato').addClass( $(data).find('.header h1').attr('class') );
		$(this).css('background-position','460px 0px').animate({'background-position': '0px 0px'}, 500);
	});
};

var update_content = function(data){
	$('.bottom .content').slideUp('normal', function(){
		$(this).html( $(data).find('.bottom .content').html() );
		$(this).slideDown('normal', function(){
			load_cover();
		});
	});
};

var load_content = function(url, only_content){
	$.get(url, function(data){
		if ( !only_content ){
			update_header(data);
		}
		update_content(data);
	});
	window.location.hash = url.replace(/\.php$/i,'');
};

var load_cover = function(){
	jQuery( '#galeria' ).jcoverflip({
		titles: $.ui.jcoverflip.defaults.titles,
		titleAnimateIn: $.ui.jcoverflip.defaults.titleAnimateIn,
		titleAnimateOut: $.ui.jcoverflip.defaults.titleAnimateOut,
		controls: $.ui.jcoverflip.defaults.controls,
		current: 0,
		time: 1000,
		beforeCss: function( el, container, offset ){
			return [
				$.jcoverflip.animationElement( el, { left: ( container.width( )/2 - 210 - 110*offset + 20*offset -20)+'px', bottom: '30px' }, { } ),
				$.jcoverflip.animationElement( el.find( 'img' ), { width: Math.max(10,100-20*offset*offset) + 'px' }, {} )
			];
		},
		afterCss: function( el, container, offset ){
			return [
				$.jcoverflip.animationElement( el, { left: ( container.width( )/2 + 110 + 110*offset +20)+'px', bottom: '30px' }, { } ),
				$.jcoverflip.animationElement( el.find( 'img' ), { width: Math.max(10,100-20*offset*offset) + 'px' }, {} )
			];
		},
		currentCss: function( el, container ){
			return [
				$.jcoverflip.animationElement( el, { left: ( container.width( )/2 - 100)+'px', bottom: '10px' }, { } ),
				$.jcoverflip.animationElement( el.find( 'img' ), { width: '200px' }, { } )
			];
		}
	}).slideDown();
		
	$('#galeria a.fancy').fancybox({
		transitionIn: 'elastic',
		transitionOut: 'elastic',
		titlePosition: 'inside',
		opacity: true,
		onStart: function(items, main){
			if ( $(items[main]).parent().index() != $('#galeria').jcoverflip('current') ){
				if ( $(items[main]).parent().index() < $('#galeria').jcoverflip('current') )
					$('#galeria').jcoverflip('previous');
				else
					$('#galeria').jcoverflip('next');
				return false;
			}
			return true;
		}
	});
	
	$('#galeria').mousewheel( function(event, delta){
		var idx = $('#galeria').jcoverflip('current');
		var len = $('#galeria').jcoverflip('length');
		if ( delta > 0 && idx > 0 )
			$('#galeria').jcoverflip('previous');
		
		if ( delta < 0 && idx < len-1 )
			$('#galeria').jcoverflip('next');
	});
	
	if ( $('.captcha').length )
		init_recaptcha();

	$('form').validate({
		errorPlacement: function(error, element){
			element.parent().find('cite').text(error.text());
		},
		rules: {
			name: 'required',
			email: {
				required: true,
				email: true
			},
			recaptcha_response_field: 'required'
		},
		messages: {
			name: 'Preencha seu nome',
			email: {
				required: 'Preencha seu email',
				email: 'Digite um email v&aacute;lido'
			},
			recaptcha_response_field: 'Digite as duas palavras abaixo'
		}
	});
};
