var menu_atu = "";

$().ready(function() {
	$(window).bind("hashchange",function(e) {
		if(window.location.hash) {
			href = window.location.hash.split("#!");
			navegation(href[1]);
		}
	});

	$(window).trigger("hashchange");

	menu();
	menu_scroll();
	trabalhos();
	fotos();
	equipe();
	solucoes();
	contato();
	forms();
	combobox();
});

function navegation(href) {
	menu_atu = href;

	$('html, body').stop(false, true).animate({scrollTop : $('#mod_' + href).offset().top - 52}, {
		duration: 2000,
		easing: "easeOutCubic"
	});
}

function menu() {
	$('#menu-h ul li a, #menu-v ul li a, #logo, #icone').click(function() {
		lin_href = $(this).attr('href').split("#!");

		if(menu_atu == lin_href[1])
			navegation(lin_href[1]);
	});

	$("a[rel^='prettyPhoto']").prettyPhoto({
		animation_speed: 'slow',
		slideshow: 5000,
		autoplay_slideshow: false,
		opacity: 0.30,
		show_title: true,
		allow_resize: true,
		default_width: 640,
		default_height: 480,
		counter_separator_label: '/',
		theme: 'pp_default',
		horizontal_padding: 17,
		hideflash: true,
		wmode: 'opaque',
		autoplay: true,
		modal: false,
		deeplinking: false,
		overlay_gallery: true,
		keyboard_shortcuts: true,
		changepicturecallback: function(){},
		callback: function(){},
		ie6_fallback: true,
		markup: '<div class="pp_pic_holder"> \
					<div class="ppt">&nbsp;</div> \
					<div class="pp_top"> \
						<div class="pp_left"></div> \
						<div class="pp_middle"></div> \
						<div class="pp_right"></div> \
					</div> \
					<div class="pp_content_container"> \
						<div class="pp_left"> \
						<div class="pp_right"> \
							<div class="pp_content"> \
								<div class="pp_loaderIcon"></div> \
								<div class="pp_fade"> \
									<a href="#" class="pp_expand" title="Expand the image">Expand</a> \
									<div class="pp_hoverContainer"> \
										<a class="pp_next" href="#">next</a> \
										<a class="pp_previous" href="#">previous</a> \
									</div> \
									<div id="pp_full_res"></div> \
									<div class="pp_details"> \
										<div class="pp_nav"> \
											<a href="#" class="pp_arrow_previous">Previous</a> \
											<p class="currentTextHolder">0/0</p> \
											<a href="#" class="pp_arrow_next">Next</a> \
										</div> \
										<p class="pp_description"></p> \
										{pp_social} \
										<a class="pp_close" href="#">Close</a> \
									</div> \
								</div> \
							</div> \
						</div> \
						</div> \
					</div> \
					<div class="pp_bottom"> \
						<div class="pp_left"></div> \
						<div class="pp_middle"></div> \
						<div class="pp_right"></div> \
					</div> \
				</div> \
				<div class="pp_overlay"></div>',
		gallery_markup: '<div class="pp_gallery"> \
							<a href="#" class="pp_arrow_previous">Previous</a> \
							<div> \
								<ul> \
									{gallery} \
								</ul> \
							</div> \
							<a href="#" class="pp_arrow_next">Next</a> \
						</div>',
		image_markup: '<img id="fullResImage" src="{path}" />',
		flash_markup: '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="{width}" height="{height}"><param name="wmode" value="{wmode}" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="{path}" /><embed src="{path}" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="{width}" height="{height}" wmode="{wmode}"></embed></object>',
		quicktime_markup: '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab" height="{height}" width="{width}"><param name="src" value="{path}"><param name="autoplay" value="{autoplay}"><param name="type" value="video/quicktime"><embed src="{path}" height="{height}" width="{width}" autoplay="{autoplay}" type="video/quicktime" pluginspage="http://www.apple.com/quicktime/download/"></embed></object>',
		iframe_markup: '<iframe src ="{path}" width="{width}" height="{height}" frameborder="no"></iframe>',
		inline_markup: '<div class="pp_inline">{content}</div>',
		custom_markup: '',
		social_tools: false
	});
}

var scrolled = $(window).scrollTop();

function menu_scroll() {
	$(window).scroll(function () {
		scrolled = $(window).scrollTop();

		if(scrolled < $('#mod_trabalhos').offset().top - 73) {
			$("#bar").fadeOut(250);

			$("ul", "#menu-h").stop().animate({backgroundPosition : (($("ul li:eq(0)", "#menu-h").innerWidth() / 2) - 10.5) + "px 100%"}, 250);
		}

		if(scrolled > $('#mod_trabalhos').offset().top - 73 && scrolled < $('#mod_sobre').offset().top - 73) {
			$("#bar").fadeIn(250);

			$("ul", "#menu-h").stop().animate({backgroundPosition : ($("ul li:eq(0)", "#menu-h").innerWidth() + ($("ul li:eq(1)", "#menu-h").innerWidth() / 2) - 10.5) + "px 100%"}, 250);
		}

		if(scrolled > $('#mod_sobre').offset().top - 73 && scrolled < $('#mod_solucoes').offset().top - 73) {
			$("#bar").fadeIn(250);

			$("ul", "#menu-h").stop().animate({backgroundPosition : ($("ul li:eq(0)", "#menu-h").innerWidth() + $("ul li:eq(1)", "#menu-h").innerWidth() + ($("ul li:eq(2)", "#menu-h").innerWidth() / 2) - 10.5) + "px 100%"}, 250);
		}

		if(scrolled > $('#mod_solucoes').offset().top - 73 && scrolled < $('#mod_contato').offset().top - 73) {
			$("#bar").fadeIn(250);

			$("ul", "#menu-h").stop().animate({backgroundPosition : ($("ul li:eq(0)", "#menu-h").innerWidth() + $("ul li:eq(1)", "#menu-h").innerWidth() + $("ul li:eq(2)", "#menu-h").innerWidth() + ($("ul li:eq(3)", "#menu-h").innerWidth() / 2) - 10.5) + "px 100%"}, 250);
		}

		if(scrolled > $('#mod_contato').offset().top - 73) {
			$("#bar").fadeIn(250);

			$("ul", "#menu-h").stop().animate({backgroundPosition : ($("ul li:eq(0)", "#menu-h").innerWidth() + $("ul li:eq(1)", "#menu-h").innerWidth() + $("ul li:eq(2)", "#menu-h").innerWidth() + $("ul li:eq(3)", "#menu-h").innerWidth() + ($("ul li:eq(4)", "#menu-h").innerWidth() / 2) - 10.5) + "px 100%"}, 250);
		}
	});
}

function trabalhos() {
	$('.trabalho1, .trabalho2', '#trabalhos').hover(function() {
		$('.info', this).stop(false, true).slideDown({
			duration: 500,
			easing: "easeOutCubic"
		});
	}, function() {
		$('.info', this).stop(false, true).slideUp({
			duration: 500,
			easing: "easeOutCubic"
		});
	});

	$('.mais').click(function() {
		$.ajax({
			url : config_url + 'web/modulos/trabalhos/trabalhos.php?callback=?',
			dataType : 'json',
			success : function(res) {
				$.each(res[0].trabalhos, function(i, item) {
					$('<div class="trabalho2"></div>').html('<div class="foto"><img src=" ' + config_url + 'system/php/m2brimagem/crop.php?arquivo=filemanager/file_system/' + item.arquivo + '&amp;largura=320&amp;altura=160" alt="' + item.nome + '"></div><div class="info"><h3>' + item.nome.toUpperCase() + '</h3><p>' + item.data + '</p>' + (item.link ? '<div class="link"><a href="' + item.link + '" target="_blank">' + item.link + '</a></div>' : '') + '</div>').hover(function() {
						$('.info', this).stop(false, true).slideDown({
							duration: 500,
							easing: "easeOutCubic"
						});
					}, function() {
						$('.info', this).stop(false, true).slideUp({
							duration: 500,
							easing: "easeOutCubic"
						});
					}).appendTo("#trabalhos");
				});

				$("<div class=\"clear\"></div>").appendTo("#trabalhos");

				$('html, body').stop(false, true).animate({scrollTop : $('#mod_trabalhos .center nav').offset().top - 381}, {
					duration: 1000,
					easing: "easeOutCubic"
				});

				if(res[0].ult == 0) {
					$('#mod_trabalhos .center nav').fadeOut(250);
				}
			}
		});
	});
}

function fotos() {
	$('#fotos nav a').click(function() {
		if(!$(this).hasClass('select')) {
			$('#fotos nav a').removeClass('select');
			$(this).addClass('select');

			$("#galeria img").slideUp(250);
			$("#galeria img:eq(" + ($(this).text() - 1) + ")").slideDown(250);
		}
	});
}

function equipe() {
	$('.pessoa', '#equipe').hover(function() {
		$('.info', this).stop(false, true).slideDown({
			duration: 500,
			easing: "easeOutCubic"
		});
	}, function() {
		$('.info', this).stop(false, true).slideUp({
			duration: 500,
			easing: "easeOutCubic"
		});
	});
}

function solucoes() {
	$('.servico', '#solucoes').hover(function() {
		$('.texto', this).stop(false, true).slideDown({
			duration: 500,
			easing: "easeOutCubic"
		});
	}, function() {
		$('.texto', this).stop(false, true).slideUp({
			duration: 500,
			easing: "easeOutCubic"
		});
	});
}

function contato() {
	var form_con = '#contato form';

	$(form_con).submit(function() {
		$(this).ajaxSubmit({
			url: config_url + 'web/modulos/contato/valida.php?callback=?',
			dataType: 'json',
			type: 'POST',
			success: function(res) {
				var mensagem = res[0].mensagem;
				var campo = res[0].campo;
				var erro = res[0].erro;

				$('.erro_msg', form_con).slideUp(250, function() {
					$(this).remove();
				});

				if(campo) {
					$('#' + campo).parent().parent().before('<div class="erro_msg" style="display:none;">' + mensagem + '</div>');
				}

				$('label', form_con).each(function() {
					$(this).removeClass('erro');

					if(campo) {
						$('#' + campo, this).focus().parent().addClass('erro');

						if($('#' + campo, this).val() != '') {
							$('span', $('#' + campo, this).parent()).fadeOut(200);
						}
					}
				});

				$('.erro_msg', form_con).fadeIn(250);

				if(!erro) {
					$('#con_nome').parent().parent().before('<div class="erro_msg" style="display:none;">' + mensagem + '</div>');

					$('.erro_msg', form_con).fadeIn(250);

					$('.enviar button', form_con).removeAttr('disabled').removeClass('load').html('ENVIAR');
					$('.enviar span', form_con).html('ENVIAR');

					$(form_con).resetForm();

					$('label span', form_con).fadeIn(200);
				} else {
					$('.enviar button', form_con).removeAttr('disabled').removeClass('load').html('ENVIAR');
					$('.enviar span', form_con).html('ENVIAR');
				}
			}
		});

		$('.enviar button', form_con).attr('disabled', 'disabled').addClass('load').html('carregando...');
		$('.enviar span', form_con).html('carregando...');

		return false;
	});
}

function forms() {
	$('input, textarea, select').hover(function() {
		$(this).parent().addClass('hover');
	}, function() {
		$(this).parent().removeClass('hover');
	});

	$('input, textarea, select').focus(function() {
		$(this).parent().addClass('focus');
	});

	$('input, textarea, select').blur(function() {
		$(this).parent().removeClass('focus');
	});

	$('input, textarea, select').keyup(function(e) {
		if($(this).val().length > 0) {
			$('span', $(this).parent()).stop(false, true).fadeOut(200);
		} else {
			$('span', $(this).parent()).stop(false, true).fadeIn(200);
		}
	});

	$('input, textarea, select').blur(function(e) {
		if($(this).val() == '') {
			$('span', $(this).parent()).fadeIn(200);
		}
	});

	$('.button').each(function() {
		var c = $(this).attr('class');
		$(this).removeClass('button').addClass('out').wrap('<span class="' + c + '"></span>');
		$(this).parent().append('<span class="over">' + $(this).text() + '</span>');
	});

	$('.button').hover(function() {
		$('.out', this).stop().animate({opacity : 0}, 250);
	}, function() {
		$('.out', this).stop().animate({opacity : 1}, 250);
	});
}

function combobox() {
	var count = 0;

	$('select').each(function() {
		count++;

		var sel = $(this).parent();

		$(this).css({'opacity' : '0', 'position' : 'absolute', 'z-index' : '20'});

		$(this).after('<span class="sel" style="width:' + (parseInt($(this).innerWidth()) || 0) + 'px"><span class="val"></span></span>');

		$('.val', sel).html($('option:selected', this).text());

		$(this).change(function() {
			$('.val', sel).html($('option:selected', this).text());
		});
	});

	$('select.sel_estado').change(function(){
		var sel_cid = $("#" + $(this).attr('rel'));
		var sel = $(sel_cid).parent();

		$('.val', sel).html('Carregando...');

		$.getJSON(config_url + 'system/php/cidade.php?callback=?', {id: $(this).val()}, function(res){
			var options = '';

			for (var i = 0; i < res.length; i++) {
				options += '<option value="' + res[i].optVal + '">' + res[i].optDis + '</option>';
			}

			$(sel_cid).html(options);
			$('option:first', sel_cid).attr('selected', 'selected');

			$('.val', sel).html('');

			$(sel_cid).change(function() {
				$('.val', sel).html($('option:selected', this).text());
			});
		});
	});
}

