$(function(){
	posMenu();
})

/*****************************************************************************/
/**********************************  MENU   **********************************/
/*****************************************************************************/
var siteMenu = $('.menu','#siteHeader');
$('li',siteMenu).each(function(){
	$(this).append('<div class="bg"></div>');
});

$('a[href^=#]','.menu, #home').click(function(){
	var menuId = this.hash,
		menuTarget = $(menuId),
		menuAtivo = $('a[href='+menuId+']',siteMenu).parent();
		
	menuTarget = menuTarget.length && menuTarget || $('[name=' + menuId.slice(1) +']');
	if (menuTarget.length){
		var targetOffset = menuTarget.offset().top;
		$('html,body').stop(true,true).animate({scrollTop: targetOffset}, 3000);
	};
	
	this.blur();
	return false;
});

$('h3.logo','#siteHeader').hover(function(){
	$('<div class="bg"></div>').appendTo(this).fadeIn();
},function(){
	$('.bg',this).fadeOut(function(){ $(this).remove() });
	
});

function posMenu(){
	var menuAreas = new Array();
	for(i=0; i<$('a',siteMenu).length; i++){
		id = $('a',siteMenu).eq(i).attr('href');
		areaTop = $(id).position().top;
		areaSize = $(id).height();
		winSize = $(window).height()/2;
		
		menuAreas[i] = [ (areaTop + areaSize),(areaTop - winSize) ];
	}
	
	reposMenu(menuAreas);
	$(window).scroll(function(){
		reposMenu(menuAreas);
	});
}

function reposMenu(menuAreas){
	var sTop = $(window).scrollTop();
	
	for(i=0; i<menuAreas.length; i++){
		
		if( sTop < menuAreas[i].slice(0,1) ){
			if( sTop > menuAreas[i].slice(1) ){
				menuAtivo = i;
			}
		}
	}
	
	menuAtivo = $('li',siteMenu).eq(menuAtivo);
	menuAtivo.addClass('ativo');
	$('.ativo',siteMenu).not(menuAtivo).removeClass('ativo');
	$('.bg',menuAtivo).fadeIn(1000);
	$('.bg',siteMenu).not( $('.bg',menuAtivo) ).stop(true,true).hide();
}


/*****************************************************************************/
/****************************  MECHAS e NUANCES  *****************************/
/*****************************************************************************/
$('.caixa').ready(function(){
	setaAnte = $('.ativo','.boxNuances').prev('li').find('a');
	setaProx = $('.ativo','.boxNuances').next('li').find('a');
	$('.setaAnte',this).attr('href',setaAnte.attr('href')).text(setaAnte.text());
	$('.setaProx',this).attr('href',setaProx.attr('href')).text(setaProx.text());
});

$('a','.boxNuances, .boxMechas').hover(function(){
	var box = $(this).parent().parent();	
	var parent = $(this).parent();
	
	if( !parent.is('.ativo') ){
		$(this).fadeOut('',function(){		
			$(this).fadeIn();
			$('.hover',box).not(parent).show().removeClass('hover');	
			parent.addClass('hover');
		});
	}
},function(){
	var box = $(this).parent().parent();	
	var parent = $(this).parent();
	
	if( !parent.is('.ativo') ){
		$(this).fadeOut('',function(){		
			$(this).fadeIn();
			$('.hover',box).show().removeClass('hover');
		});
	}
});

$('a','.boxMechas').click(function(){
	if( $(this).parent().is('.ativo') ){ return false; };
	
	var box = $(this).parents('.boxMechas'),
		boxAtivo = $(this).parent(),
		boxFoto = $('.foto',box),
		boxLoading = $('.loading',box),
		boxImg = this.href,
		boxText = this.innerHTML;
	
	/***** ATIVO *****/
	$('.hover',box).removeClass('hover');
	$('.ativo',box).removeClass('ativo');
	boxAtivo.addClass('ativo');
	
	/***** LOADING *****/
	imgLoadingFoto(box, boxImg, boxText);
	
	this.blur();
	return false;
});

$('a','.boxNuances').click(function(){
	if( $(this).parent().is('.ativo') ){ return false; };
	
	var box = $('.boxNuances'),
		boxAtivo,
		boxImg = this.href,
		boxText = this.innerHTML;
	
	/***** ATIVO *****/	
	boxImg = boxImg.substring(boxImg.lastIndexOf('img/'), boxImg.length);	
	for(i=0; i<$('.nav a',box).length; i++){
		if( $('.nav a',box).eq(i).attr('href') == boxImg ){
			boxAtivo = $('.nav a',box).eq(i).parent();
		}
	}
	$('.hover',box).removeClass('hover');
	$('.ativo a',box).fadeOut( function(){
		$(this).parent().removeClass('ativo').removeClass('hover');
		$(this).fadeIn();
				
	});
	boxAtivo.addClass('ativo');
	
	/***** LOADING *****/
	imgLoadingFoto(box, boxImg, boxText);
	imgLoadingCaixa(box, boxImg, boxText);
	
	/***** SETAS *****/	
	var boxNext = boxAtivo.next(),
		boxPrev = boxAtivo.prev();
	if( !boxNext.html() ) boxNext = $('li:first-child',box);
	if( !boxPrev.html() ) boxPrev = $('li:last-child',box);	
	$('.setaProx',box).attr('href', $('a',boxNext).attr('href') );
	$('.setaAnte',box).attr('href', $('a',boxPrev).attr('href') );	
	$('.setaProx',box).text( $('a',boxNext).text() );
	$('.setaAnte',box).text( $('a',boxPrev).text() );	
	
	this.blur();
	return false;
});

function imgLoadingFoto(box, boxImg, boxText){
	var boxFoto = $('.foto',box),
		boxLoading = $('<div class="loading"></div>'),
		imgOut = $('img:eq(0)',boxFoto),
		boxImgOut = $('.boxImg',box).eq(0),
		boxImgIn = $('<div class="boxImg"></div>');
		
	$('.loading',box).remove();
	boxLoading.css('z-index','2');
	boxLoading.animate({ 'width': boxFoto.width() },5000);
	
	imgOut.animate({
		'opacity': '0.4',
		'filter': 'alpha(opacity=40)'
	},400);
	
	//$(boxImgOut).insertBefore(imgOut);
	$(boxImgIn).insertBefore(boxImgOut);
	boxImgOut.append(imgOut);
	
	boxImg = $('<img src="'+boxImg+'" alt="'+boxText+'" />');	
	boxImg.load(function(){
		
		boxImgOut.append(boxLoading);
		boxLoading.stop(true, true).animate({ 'width': boxFoto.width()}, function(){
			
			boxImgOut.stop(true,true).animate({
				'height': 0
			},1000,function(){
				boxImgOut.remove();
			});
			boxImgIn.append(boxImg);
			imgLabel(box, boxText);
			
		});
	})
	
}

function imgLoadingCaixa(box, boxImg, boxText){
	$('img',boxCaixa).stop(true,true).not(':last-child').remove();
	
	var boxCaixa = $('.caixa',box),
		boxLoading = $('.loading',box),
		imgCaixa = boxImg.replace('_pq','_caixa'),
		imgCaixaOut = $('img:eq(0)',boxCaixa);

	imgCaixaOut.animate({
		'opacity': '0.4',
		'filter': 'alpha(opacity=40)'
	},400);
	$('a',boxCaixa).hide();
	
	imgCaixa = $('<img src="'+imgCaixa+'" alt="'+boxText+'" />')
	imgCaixa.load(function(){
		
		boxCaixa.append(imgCaixa);
		imgCaixa.css('left',-boxCaixa.width()+'px');
		imgCaixaOut.css('left','0');
		
		imgCaixaOut.stop(true,true).animate({
			'left': boxCaixa.width()
		},1000,function(){
			imgCaixaOut.remove();
		});
		imgCaixa.stop(true,true).animate({
			'left': 0
		},1000,function(){
			$('a',boxCaixa).show();
		});
			
	})
}

function imgLabel(box,text){
	var label = $('.label',box),
		labelSize = 146,
		labelClass = text.substring(0,text.indexOf(' '));
	labelClass = 'label lbl_'+labelClass.replace('.','-');
	
	label.width('0');
	label.attr('class',labelClass);
	label.text(text);
	label.animate({'width':labelSize});
}



/*****************************************************************************/
/********************************  PARALLAX  *********************************/
/*****************************************************************************/
(function($){
    $.fn.parallax = function(options){
        var $$ = $(this);
        offset = $$.offset();
        var defaults = {
            "start": 0,
            "stop": offset.top + $$.height(),
            "coeff": 0.95,
            "initPos": $$.position().top,
            "type": "element"
        };
        var opts = $.extend(defaults, options);
        if(opts.type == "image"){
			if (navigator.appName=='Microsoft Internet Explorer'){
				opts.initPos =  parseInt($$.css('backgroundPositionY'));
			}
			else{
				opts.initPos = $$.css('background-position').split(' ');
        		opts.initPos = parseInt(opts.initPos[1]);
			}
        }
        return this.each(function(){
            $(window).bind('scroll', function() {
                var win = $(window),
                	windowTop = win.scrollTop();
				
                if((windowTop >= opts.start) && (windowTop <= opts.stop)) {
					
                	var prnt = $$.parent(),
                		parenttop = prnt.offset().top,
                		parentheight = prnt.height(),
                		winheight = win.height(),
                		parentloc;
                		
                	if (parenttop < 10) { parentloc = parenttop + winheight; } else { parentloc = parenttop; }
                	
                	if(opts.type == 'element'){
	                    newCoord = parseInt(opts.initPos) + ((parseInt(winheight) + parseInt(windowTop) - parseInt(parentloc)) * opts.coeff);
	                    $$.css({
	                        "top": newCoord + 'px'
	                	});
	                }
	                else if(opts.type == "image"){
	                	if (navigator.appName=='Microsoft Internet Explorer'){
	                		var newCoordY = $$.css('backgroundPositionY');
	                		newCoordY = opts.initPos + ((windowTop - parenttop) * opts.coeff);
	                		$$.css({
		                        "backgroundPositionY": newCoordY + 'px' 
		                	});
	                	}
	                	else{
	                		newCoord = $$.css('background-position').split(' ');
							newCoord[1] = opts.initPos + ((windowTop - parenttop) * opts.coeff);
							
							$$.css({
								"background-position": newCoord[0] + newCoord[1] + 'px' 
							});
							
	                	}
	                }
            	}
        	});
        });
    };
})(jQuery);

$('.parallax').each(function(){
	
	plxElemento = $('.elemento',this);
	plxFundo = $('.fundo',this);
	
	if( $(this).is('.par1') ){
		coeffElemento = '0.1';
		coeffFundo = '-0.2';
	}
	if( $(this).is('.par2') ){
		coeffElemento = '0.3';
		coeffFundo = '-0.3';	
	}
	if( $(this).is('.par3') ){
		coeffElemento = '0';
		coeffFundo = '-0.6';
	}	
	
	plxElemento.parallax({
		'coeff': coeffElemento,
		'stop':  $(this).offset().top + $(this).height(),
		'start': $(this).offset().top - $(window).height(),
		'type' : 'image'
	});
	plxFundo.parallax({ 
		'coeff': coeffFundo,
		'stop': $(this).offset().top + $(this).height(),
		'start': $(this).offset().top - $(window).height()
	});

});


$('.estatico').each(function(){
	var areaEstatico = $(this);
	areaEstatico.css('min-height', $(window).height() );	
	$(window).resize(function(){
		areaEstatico.css('min-height', $(window).height() );
	})
})


/*****************************************************************************/
/*********************************  PASSOS  **********************************/
/*****************************************************************************/
$('a','.passos').click(function(){
	var passoBox = $(this).parents('.passos'),
		passoParent = $(this).parent(),
		tam = $(this).width(),	
		tamMin = passoParent.css('min-width'),
		tempo = 500;
		
	passoParent.animate(
		{ width: tam },
		tempo,
		function(){ $(this).addClass('ativo'); }
	); 
	
	$('li',passoBox).not(passoParent).stop(true, true).animate(
		{ width: tamMin },
		tempo,
		function(){ $(this).removeClass('ativo'); }
	);
	
	this.blur();
	return false;
});



/*****************************************************************************/
/********************************  COPYRIGHT  ********************************/
/*****************************************************************************/
$('a','.copyright').click(function(){
	window.open(this.href,'copyright','width=370,height=435,status0,resizable=0,menubar=0,scrollbars=0,titlebar=0,toolbar=0');
	return false;
})
