$('.barra-rolagem').mousedown(function(){
	
	$('.barra-rolagem').bind('mousemove click', function(e){
		
		$(this).css('cursor','pointer');
		
		var barr = $(this).find('.barra');
		var men = $(this).parent('div').find('.men:visible');
		
		var off = $(this).offset();
		
		var y = e.pageY - off.top;

		var curso = itensAlt(men) - $(this).height();
		
		rolaMenu(y, barr, men, curso);
	});
});

function rolaMenu(y, barr, men, curso){
	
		var alt = barr.parent('div').height()-10;
		
		if(y>=10 && y<=alt){
			
			barr.css('top', y-10+'px');
			men.css('top', '-'+((y-10)*(curso)/(alt-10))+'px');
			
		}else if(y<10){
			
			barr.css('top', '0px');
			men.css('top', '0px');
		
		}else if(y>alt){
			
			barr.css('top', (alt-10)+'px');
			men.css('top', '-'+(curso)+'px');
			
		}
		
}

var z=999, numAtual="Home", animando=false, tituloPag;

function trocaFundo(num){
	
	if(numAtual!=num && $('#f'+num).attr('rel')){
	
		var dir = $('#f'+num).attr('rel');
		
		var repete = 0;
		
		animando = true;
		
		dirAtual = dir;
		numAtual = num;
		
		$('#f'+num).css('left',esqP+'px');
		$('#f'+num).css('z-index',++z);
		
		var cl = $('#f'+num).find('.cl');
		var pb = $('#f'+num).find('.pb');
		var masq = $('#f'+num).find('.masq');
		
		var velocidade = 1500;
		var animacao = 'easeInOutCubic';
		
		masq.width(pb.width()*0.5);
		
		switch(dir){
			
			case 'esq':
							
				$('.fnd').not('#f'+num).animate({'left':'-=300px'}, velocidade, animacao, function(){
					if(repete++ < 1){
						$(this).css('left',esqP+'px');
						if(num != 'Home' && num != 'News' && num != 'Cont')$('#conteudo').fadeIn(300, function(){
							animando = false;
							tituloPag = $(this).find('.titP:visible').text();
							document.location.hash = tituloPag.replace(/\s/g, "_");
							hashAtual = tituloPag.replace(/\s/g, "_");
							document.title = tituloPag + " - PORTIERE";
						});
						else if(num == 'News' || num == 'Cont'){
							animando = false;
						}else{
							animando = false;
							document.location.hash = "";
							hashAtual = "";
							document.title = "PORTIERE";
						}
					}
				});
				
				cl.css('left',$('body').width()-esqP+'px');
				masq.css('left',$('body').width()-esqP+'px');
				pb.css('left','-'+masq.width()+'px');
				
				cl.show();
				pb.show();
				
				cl.animate({'left':pb.width()+'px'}, velocidade, animacao);
				masq.animate({'left':'0px', 'width':pb.width()+'px'}, velocidade, animacao);
				pb.animate({'left':'0px'}, velocidade, animacao);
				
			break;
			
			case 'dir':
			
				$('.fnd').not('#f'+num).animate({'left':'+=300px'}, velocidade, animacao, function(){
					if(repete++ < 1){
						$(this).css('left',esqP+'px');
						if(num != 'Home' && num != 'News' && num != 'Cont')$('#conteudo').fadeIn(300, function(){
							animando = false;
							tituloPag = $(this).find('.titP:visible').text();
							document.location.hash = tituloPag.replace(/\s/g, "_");
							hashAtual = tituloPag.replace(/\s/g, "_");
							document.title = tituloPag + " - PORTIERE";
						});
						else if(num == 'News' || num == 'Cont'){
							animando = false;
						}else{
							animando = false;
							document.location.hash = "";
							hashAtual = "";
							document.title = "PORTIERE";
						}
					}
				});
				
				cl.css('left','-'+cl.width()-esqP+'px');
				masq.css('left','-'+masq.width()-esqP+'px');
				
				cl.show();
				pb.show();
				
				cl.animate({'left':'0px'}, velocidade, animacao);
				masq.animate({'left':cl.width()+'px', 'width':pb.width()+'px'}, velocidade, animacao);
				
			break;
		}
		
	}else{
		
		$('#conteudo').fadeIn(300);
		
	}
	
}

var hashAtual = "";

function entraURL(urlHash){
	
	var pagEnd = unescape(urlHash);

	if(pagEnd!="")var num = pagArr[pagEnd];
	
	if(num>0 && urlHash!="#"+hashAtual){
		
		limpaMenu();
		
		abrePagina(num);
		
		var esteCam = camArr[num].split('.');
		
		$('.menu[rel|="'+esteCam[0]+'"]').css('background-color', '#000000');
		
		idMi = esteCam[0];
		
		if(esteCam[0]=='new'){
	
			$('#menu-news').fadeIn(300);
			
			var mn = $('.menu-new[rel|="P'+num+'"]');
			idMn=num;
			
			mn.css('background-color', '#000000');
			
			$('.menu-new').find('.set').hide();
			mn.find('.set').css('top', (mn.height()/2)-3+'px');
			mn.find('.set').show();
			
			trocaFundo('News');
			
			document.location.hash = pagEnd;
	
		}else{
			
			$('.menu-int').hide();
			$('.M'+esteCam[0]).show();
			$('#menu-interno').fadeIn(300);
			
			if(esteCam[2]){
				
				var mi = $('.menu-int[rel|="'+esteCam[1]+'"]');
				idMs=esteCam[1];
				
				mi.css('background-color', '#000000');
				
				$('.menu-int').find('.set').hide();
				mi.find('.set').css('top', (mi.height()/2)-3+'px');
				mi.find('.set').show();
				
				$('.menu-sec').hide();
				$('.M'+esteCam[1]).show();
				$('#menu-secundario').fadeIn(300);
				
				if(esteCam[3]){
					
					var ms = $('.menu-sec[rel|="'+esteCam[2]+'"]');
					idMt=esteCam[2];
					
					ms.css('background-color', '#000000');
					
					$('.menu-sec').find('.set').hide();
					ms.find('.set').css('top', (ms.height()/2)-3+'px');
					ms.find('.set').show();
					
					$('.menu-ter').hide();
					$('.M'+esteCam[2]).show();
					$('#menu-terciario').fadeIn(300);
					
					var mt = $('.menu-ter[rel|="P'+num+'"]');
					idMp=esteCam[3];
					
					mt.css('background-color', '#000000');
					
					$('.menu-ter').find('.set').hide();
					mt.find('.set').css('top', (mt.height()/2)-3+'px');
					mt.find('.set').show();
					
				}else{
					
					var ms = $('.menu-sec[rel|="P'+num+'"]');
					idMt=esteCam[2];
					
					ms.css('background-color', '#000000');
					
					$('.menu-sec').find('.set').hide();
					ms.find('.set').css('top', (ms.height()/2)-3+'px');
					ms.find('.set').show();
				}
				
			}else{
				
				var mi = $('.menu-int[rel|="P'+num+'"]');
				idMs=esteCam[1];
				
				mi.css('background-color', '#000000');
				
				$('.menu-new').find('.set').hide();
				mi.find('.set').css('top', (mi.height()/2)-3+'px');
				mi.find('.set').show();
				
			}
	
		}

	}
	
}

$(window).bind('load', function() {
	atualiza();	
	entraURL(document.location.hash);
});


var esqP;
$(window).bind('resize', function() {
	atualiza();	
});

function atualiza(){
	
	var ratioF  = 1280/720;
	var bodyW = $('body').width();
	var bodyH = $('body').height();
	var ratioP = bodyW/bodyH;
	var W, H;
	
	if(ratioF>=ratioP){
		W = parseInt(bodyH*1280/720);
		H = bodyH;
		esqP = -parseInt((W-bodyW)/2);
		$('#fundo .fnd').css('left',esqP+'px');
	}else{
		W = bodyW;
		H = parseInt(bodyW*720/1280);
		esqP = 0;
		$('#fundo .fnd').css('left',esqP+'px');
	}
	$('#fundo .fnd').width(W);
	$('#fundo .fnd').height(H);
	
	var cl = $('#f'+numAtual).find('.cl');
	var pb = $('#f'+numAtual).find('.pb');
	var masq = $('#f'+numAtual).find('.masq');
	
	switch(dirAtual){
		case 'esq':
			cl.css('left',pb.width()+'px');
			masq.css({'left':'0px', 'width':pb.width()+'px'});
			pb.css('left', '0px');
		break;
		
		case 'dir':
			cl.css('left','0px');
			masq.css({'left':cl.width()+'px', 'width':pb.width()+'px'});
		break;
	}
	
	loadImage();
	
	if(++entraHomeCont==2)entraHome();
	
}
var entraHomeCont=0;

function entraHome(){
	
	$('#carregando').fadeOut(100);
	$('#logo').fadeIn(500, function(){
		$('#menu-principal').fadeIn(500, function(){
			if(document.location.hash=='')$('#home').fadeIn(500);
			$('#redes-sociais').fadeIn(200);
		});
	});
	
}

$(function() {
	
	$('.menus, .menu-fotos, .txtP').mousewheel(function(e, delta) {

		var barr = $(this).find('.barra-rolagem').find('.barra');
		var men = $(this).find('.men:visible');
		var y = parseInt(barr.css('top'));
		
		var curso = itensAlt(men) - $(this).height();
		
		if(delta<0)y += 41;
		
		else if(delta>0)y -= 21;
		
		rolaMenu(y, barr, men, curso);
		
	});
	
	$(window).hashchange(function(){
	
		var hashA = document.location.hash;
		
		entraURL(hashA);
		
		_gaq.push(['_trackEvent', 'PageView', unescape(hashA), horaAgora]);
		
	});
	
	atualiza();	
});

$(window).mouseup(function(){
	$('.barra-rolagem').unbind('mousemove');
	$(this).css('cursor','auto');
});

function itensAlt(men){
	var h = 0;
	men.each(function(){
		h += $(this).outerHeight();
	});
	return h;
}

$('#menu-news, #menu-interno, #menu-secundario, #menu-terciario, .menu-fotos, .txtP').mouseenter(function(){
	if(itensAlt($(this).find('.men:visible'))>$(this).height())$(this).find('.barra-rolagem').fadeIn(300);
});

$('#menu-news, #menu-interno, #menu-secundario, #menu-terciario, .menu-fotos, .txtP').mouseleave(function(){
	$(this).find('.barra-rolagem').fadeOut(300);
});

function abrePagina(num){
	
	if(num>0){
	
		trocaFundo(num);
		
		var estaPag = $('.P'+num);
		
		var contW, titW, allW;
		
		allW = 0;
		
		if(num>=9990){
			
			var quantMenu = 2;
			var lw = 0;
			
			$('#conteudo').css('height', '315px');
			estaPag.find('.menu-fotos').css('height', '315px');
			
			allW = 180;
			
		}else if(num>=8880){
			
			var quantMenu = 1;
			var lw = 139;
			
			$('#conteudo').css('height', '315px');
			
		}else{
			
			var esteCam = camArr[num].split('.');
			var quantMenu = esteCam.length-1;
			var lw = 0;
			
			$('#conteudo').css('height', '175px');
			estaPag.find('.menu-fotos').css('height', '175px');
			
		}
		
		
		contW = 960 - (quantMenu*139);
		titW = 170;
		
		$('#conteudo').css({'width' : (contW-lw)+'px', 'left' : (960-contW)+'px'});
		
		$('.paginas').hide();
		
		if(estaPag.find('.menu-fotos').length){
			
			if(quantMenu==3)titW = 100;
			$('#conteudo').find('.lente').css('width', (contW-130)+'px');
			estaPag.find('.txtP').css('width', (contW-titW-20-130+allW)+'px');
			estaPag.find('.menu-txt').css('width', (contW-titW-20-30-130+allW)+'px');
			
		}else{
			
			$('#conteudo').find('.lente').css('width', contW-(lw*2)+'px');
			estaPag.find('.txtP').css('width', (contW-lw-titW-20+allW)+'px');
			estaPag.find('.menu-txt').css('width', (contW-lw-titW-20-30+allW)+'px');
			
		}
		
		estaPag.find('.titP').css('width', titW+'px');
		
		estaPag.show();
		
	}
	
}

var idMn=0, idMi=0, idMs=0, idMt=0, idMp=0;

function limpaMenu(){
	
	$('.menu, .menu-int, .menu-sec, .menu-ter, .menu-new').css('background-color', '');
	
	$('.menu-int, .menu-sec, .menu-ter, .menu-new').find('.set').hide();
	
	$('#menu-news:visible, #menu-interno:visible, #menu-secundario:visible, #menu-terciario:visible, #conteudo:visible, #home:visible').fadeOut(100);

	idMi=0;
	idMs=0;
	idMt=0;
	idMp=0;
	idMn=0;
	
}

$('#logo').click(function(){
	
	if(!animando){
		
		limpaMenu();		
	
		$.timer(100, function (timer) {
			
			$('#home').fadeIn(300);
			trocaFundo('Home');
			
			timer.stop();
		});
		
	}
	
});

$('.menu').click(function(){
	
	var idM = $(this).attr('rel');

	if(idMi!=idM && !animando){
		
		$('.menu-ter').css('background-color', '');
		$('.menu-sec').css('background-color', '');
		$('.menu-int').css('background-color', '');
		$('.menu-new').css('background-color', '');
		$('.menu').css('background-color', '');
		$(this).css('background-color', '#000000');
		
		$('.menu-ter').find('.set').hide();
		$('.menu-sec').find('.set').hide();
		$('.menu-int').find('.set').hide();
		$('.menu-new').find('.set').hide();
		
		idMi = idM;

		idMs=0;
		idMt=0;
		idMp=0;
	
		$('#menu-news:visible, #menu-interno:visible, #menu-secundario:visible, #menu-terciario:visible, #conteudo:visible, #home:visible').fadeOut(100);
		
		$.timer(100, function (timer) {
			
			if(idMi != 'new'){
				
				$('.menu-int').hide();
				$('.M'+idMi).show();
				$('.menu-int').css('top', '0px');
				$('#menu-interno').find('.barra').css('top', '0px');
				
				$('#menu-interno').fadeIn(300);
				
				if(idMi == 'fale'){
					
					trocaFundo('Cont');
					
				}
				
			}else{
				
				$('.menu-new').css('top', '0px');
				$('#menu-news').find('.barra').css('top', '0px');
				$('#menu-news').fadeIn(300);
				trocaFundo('News');
				
			}
			
			timer.stop();
		});
		
	}
	
});


$('.menu-new').click(function(){
	
	var idM = $(this).attr('rel');
	
	tituloPag = $('.'+idM).find('.titNews').text();
	
	if(idMn != idM && !animando){
		
		idMn = idM;
		
		$('.menu-new').css('background-color', '');
		$(this).css('background-color', '#000000');
		
		$('.menu-new').find('.set').hide();
		$(this).find('.set').css('top', ($(this).height()/2)-3+'px');
		$(this).find('.set').show();
		
		$('#conteudo:visible').fadeOut(100);
			
		$.timer(100, function (timer) {
			
			abrePagina(idMn.substring(1));
			
			document.location.hash = tituloPag.replace(/\s/g, "_");
			hashAtual = tituloPag.replace(/\s/g, "_");
			document.title = tituloPag + " - PORTIERE";
				
			timer.stop();
			
		});	
			
	}
	
});	

$('.menu-int').click(function(){

	var idM = $(this).attr('rel');

	tituloPag = '';
	
	if(idM.substring(1)>8880)tituloPag = $(this).find('.menuTxt').text();
	
	if(idMs != idM && !animando){
		
		$('.menu-ter').css('background-color', '');
		$('.menu-sec').css('background-color', '');
		$('.menu-int').css('background-color', '');
		$(this).css('background-color', '#000000');
		
		$('.menu-ter').find('.set').hide();
		$('.menu-sec').find('.set').hide();
		$('.menu-int').find('.set').hide();
		$(this).find('.set').css('top', ($(this).height()/2)-3+'px');
		$(this).find('.set').show();
			
		idMt=0;
		idMp=0;
		
		idMs = idM;
		
		$('#menu-secundario:visible, #menu-terciario:visible, #conteudo:visible, #home:visible').fadeOut(100);
			
		$.timer(100, function (timer) {
			
			if(idMs.charAt(0)!='P'){
				
				$('.menu-sec').hide();
				$('.M'+idMs).show();
				$('.menu-sec').css('top', '0px');
				$('#menu-secundario').find('.barra').css('top', '0px');
				
				$('#menu-secundario').fadeIn(300);
				
			}else{
				
				abrePagina(idMs.substring(1));
				
				if(tituloPag != ''){
					document.location.hash = tituloPag.replace(/\s/g, "_");
					hashAtual = tituloPag.replace(/\s/g, "_");
					document.title = tituloPag + " - PORTIERE";
				}

			}
			
			timer.stop();
			
		});
		
	}
	
});

$('.menu-sec').click(function(){

	
	var idM = $(this).attr('rel');

	if(idMt != idM && !animando){
		
		$('.menu-ter').css('background-color', '');
		$('.menu-sec').css('background-color', '');
		$(this).css('background-color', '#000000');
		
		$('.menu-ter').find('.set').hide();
		$('.menu-sec').find('.set').hide();
		$(this).find('.set').css('top', ($(this).height()/2)-3+'px');
		$(this).find('.set').show();
			
		idMp=0;
		
		idMt = idM;
		
		$('#menu-terciario:visible, #conteudo:visible, #home:visible').fadeOut(100);
			
		$.timer(100, function (timer) {
			
			if(idMt.charAt(0)!='P'){
				
				$('.menu-ter').hide();
				$('.M'+idMt).show();
				$('.menu-ter').css('top', '0px');
				$('#menu-terciario').find('.barra').css('top', '0px');
				
				$('#menu-terciario').fadeIn(300);
				
			}else{
				
				abrePagina(idMt.substring(1));
				
			}
			
			timer.stop();
			
		});	
			
	}
	
});	

$('.menu-ter').click(function(){

	var idM = $(this).attr('rel');

	if(idMp != idM && !animando){
		
		$('.menu-ter').css('background-color', '');
		$(this).css('background-color', '#000000');
		
		$('.menu-ter').find('.set').hide();
		$(this).find('.set').css('top', ($(this).height()/2)-3+'px');
		$(this).find('.set').show();
			
		idMp = idM;
		
		$('#conteudo:visible, #home:visible').fadeOut(100);
		
		$.timer(100, function (timer) {
		
			abrePagina(idMp.substring(1));
			
			timer.stop();
			
		});
	}
	
});	

var imagemID=0, imgs;

$(".foto").click(function(){
	
	var image = $(this).attr('src').split('p_')[1];
	
	imgs = $(this).parent().parent().find('.foto');
	
	imagemID = imgs.index($(this));
	
	$('#verImagem').fadeIn(500);
	
	$('.lgn').hide();
	$('.I'+image.split('.')[0]).show();
	$('#imageIMG').attr('src','fots/'+image);
});

$('#closeG').click(function(){
	
	$('#imageIMG').fadeOut(300, function(){
		$('#imageIMG').attr('src','');
		$('.lgn').hide();
	});
	$('#verImagem').fadeOut(500);
});

function loadImage(){
	
	var Wp = $('body').width();
	var Hp = $('body').height();
	
	if(Wp-150 <= $('#imageIMG').width()){
		
		$('#imageIMG').css('height', 'auto');
		$('#imageIMG').css('width', (Wp-150)+'px');
		
		if(Hp-40 <= $('#imageIMG').height()){
			
			$('#imageIMG').css('width', 'auto');
			$('#imageIMG').css('height', (Hp-40)+'px');
			
		}
		
	}else if(Hp-40 <= $('#imageIMG').height()){
			
			$('#imageIMG').css('width', 'auto');
			$('#imageIMG').css('height', (Hp-40)+'px');
			
	}else{
		
		$('#imageIMG').css('height', '');
		$('#imageIMG').css('width', '');
		
	}
	
	var lPos = (Wp - $('#imageIMG').width())/2;
	var tPos = (Hp - $('#imageIMG').height())/2;
	
	$('#legenda').width($('#imageIMG').width());
	$('#legenda').css('left', lPos);
	$('#legenda').css('top', (tPos+$('#imageIMG').height()-$('#legenda').height()));
	
	$('#imageIMG').css('left', lPos);
	$('#imageIMG').css('top', tPos);
	
	$('#closeG').css('left', lPos-22+$('#imageIMG').width());
	$('#closeG').css('top', tPos-22);
	
	$('#prevG').stop();
	$('#nextG').stop();
	
	if(imagemID>0){
		$('#prevG').fadeIn(300);
		$('#prevG').animate({left : lPos-50}, 300);
	}else{
		$('#prevG').fadeOut(300);
	}
	if(imgs && imagemID<(imgs.size()-1)){
		$('#nextG').fadeIn(300);
		$('#nextG').animate({left : lPos+$('#imageIMG').width()+25}, 300);
	}else{
		$('#nextG').fadeOut(300);
	}

	$('#closeG').fadeIn(100);
	$('#imageIMG').fadeIn(300);
	
}

$('#imageIMG').mouseenter(function(){
	if(!$('#legenda').is(':visible'))$('#legenda').fadeIn(500);
});

$('#verImagem .lente').mouseenter(function(){
	if($('#legenda').is(':visible'))$('#legenda').fadeOut(300);
});

$('#prevG').click(function(){
	
	var image = imgs.eq(--imagemID).attr('src').split('p_')[1];
	$('#closeG').fadeOut(100);
	$('#legenda').fadeOut(100);
	$('#imageIMG').fadeOut(200, function(){
		$('.lgn').hide();
		$('.I'+image.split('.')[0]).show();
		$('#imageIMG').attr('src','fots/'+image)
	});
	
});

$('#nextG').click(function(){
	
	var image = imgs.eq(++imagemID).attr('src').split('p_')[1];
	$('#closeG').fadeOut(100);
	$('#legenda').fadeOut(100);
	$('#imageIMG').fadeOut(200, function(){
		$('.lgn').hide();
		$('.I'+image.split('.')[0]).show();
		$('#imageIMG').attr('src','fots/'+image)
	});
	
});

$('.enviar').click(function(){
	
	$(".loading").css("display","block");
	$("#"+$(this).attr('rel')).submit();
	
});


$('.linkint').click(function(){
	
	entraURL($(this).attr('rel'));
	
});
