var pagina_anterior = 'home';
var pagina = 'home';
var menu_aberto = false;
var colecao_menu_aberto = false;

var $imagePan;
var $imagePan_panning;
var $outer_container;
var $imagePan_container;
var functionMouseMove;

function trocaMenuColecao(acao) {

	if(acao=='fechar') {
		altura = 70;
	} else if (acao=='abrir') {
		altura = 230;
	} else {
		if(!colecao_menu_aberto) altura = 230;
		else altura = 70;	
	}
	
	$('#footer-container').animate({
		height: altura
	}, 500, function() {
		if(colecao_menu_aberto) colecao_menu_aberto = false;
		else colecao_menu_aberto = true;
	});
	
}

/* Limpa Coleção, Making ou Contato*/
function limpaTela(callbackFnk) {

	if(pagina=='makingof') {
		
		$('#makingof_conteudo').html('');
		$('#makingof_wrapper').animate({
			top: '-600px'
		}, 700, function() {
			if(typeof callbackFnk == 'function') callbackFnk.call(this);
		});
		$('#mask').fadeOut('slow');
		
	} else if(pagina=='contato') {
	
		$('#contato_conteudo').html('');
		$('#contato_wrapper').animate({
			top: '-600px'
		}, 700, function() {
			if(typeof callbackFnk == 'function') callbackFnk.call(this);
		});
		$('#mask').fadeOut('slow');
		
	} else if(pagina=='representantes') {
	
		$('#representante_conteudo').html('');
		$('#representante_wrapper').animate({
			top: '-600px'
		}, 700, function() {
			if(typeof callbackFnk == 'function') callbackFnk.call(this);
		});
		$('#mask').fadeOut('slow');
		
	} else {
		if(typeof callbackFnk == 'function'){
			callbackFnk.call(this);
		}
	}
	
}

$(function(){
	
	$("a.outside").attr('target','_blank');
	
	
	/** ABRE MENU **/
	$('#abrir-menu').click(function(event) {
		event.preventDefault();
		if(!menu_aberto) {
			$('div.header > div.placa').animate({
				top: '-220px'
			}, 700, function() {
				menu_aberto = true;
				$('li.abrir').fadeOut('fast',function(){
					$('li.fechar').fadeIn();	
				})
			});
		}
	});
	
	/** FECHAR MENU **/
	$('#fechar-menu').click(function(event) {
		event.preventDefault();
		if(menu_aberto) {
			$('div.header > div.placa').animate({
				top: '-410px'
			}, 700, function() {
				menu_aberto = false;
				$('li.fechar').fadeOut('fast',function(){
					$('li.abrir').fadeIn();	
				})
			});
		}
	});
	
	/** VOLTAR MENU NAV **/
	$('#fechar-menu-nav').click(function(event) {
		event.preventDefault();
		if(menu_aberto) {
			
			if(pagina=='makingof') {
				$('#makingof').fadeOut(300,function(){
					$('li.fechar').hide();
					$('li.abrir').hide();
					$('li.voltar').hide();
					$('div.header > div.placa').animate({
						top: '-220px'
					}, 700, function() {
						menu_aberto = true;
						pagina = 'home';
						pagina_anterior = 'makingof';
						$('div.header div.nav .menu').show();
						$('div.header div.nav').fadeIn('fast',function(){
							$('li.fechar').fadeIn();
							
						})
					});
				})
			} else if(pagina=='contato') {
				$('#contato_wrapper').fadeOut(300,function(){
					$('li.fechar').hide();
					$('li.abrir').hide();
					$('li.voltar').hide();
					$('div.header > div.placa').animate({
						top: '-220px'
					}, 700, function() {
						menu_aberto = true;
						pagina = 'home';
						pagina_anterior = 'contato';
						$('div.header div.nav .menu').show();
						$('div.header div.nav').fadeIn('fast',function(){
							$('li.fechar').fadeIn();
							
						})
					});
				})
				
			} else if(pagina=='representantes') {
				$('#representante_wrapper').fadeOut(300,function(){
					$('li.fechar').hide();
					$('li.abrir').hide();
					$('li.voltar').hide();
					$('div.header > div.placa').animate({
						top: '-220px'
					}, 700, function() {
						menu_aberto = true;
						pagina = 'home';
						pagina_anterior = 'representantes';
						$('div.header div.nav .menu').show();
						$('div.header div.nav').fadeIn('fast',function(){
							$('li.fechar').fadeIn();
							
						})
					});
				})
				
			}
			
			
		}
	});
	
	/** VOLTAR MENU **/
	$('#voltar-menu').click(function(event) {
		event.preventDefault();
		if(menu_aberto) {
			
			$('#amarca_conteudo').fadeOut(300,function(){
				$('li.fechar').hide();
				$('li.abrir').hide();
				$('li.voltar').hide();
				$('div.header > div.placa').animate({
					top: '-220px'
				}, 700, function() {
					menu_aberto = true;
					pagina = 'home';
					pagina_anterior = 'amarca';
					$('div.header div.nav').fadeIn('fast',function(){
						$('li.fechar').fadeIn();
						
					})
				});
				
			})
			
			
		}
	});

	/** MENU MARCA **/
	$('li.amarca a').click(function(event) {
		event.preventDefault();
		if(pagina!='amarca') {
			trocaMenuColecao('fechar');
			limpaTela(function(){
				pagina_anterior = pagina;
				pagina = 'amarca';
				$('div.header div.nav').fadeOut('fast');
				$('div.header > div.placa').animate({
					top: '-100px'
				}, 700, function() {
					menu_aberto = true;
					$('li.abrir').fadeOut('fast',function(){
						$('#amarca_conteudo').fadeIn('fast',function(){
							$('#amarca').jScrollPane();
						});	
						$('li.voltar').fadeIn();
					})
				});
			});
		}
		
	});
	
	/* MENU COLECAO */
	$('li.colecao a').click(function(event) {
		event.preventDefault();
		if(pagina!='makingof') {
			trocaMenuColecao();
		} else {
			limpaTela(function(){
				
			});
			trocaMenuColecao();
		}
	});
	
	/* MENU COLECAO FOOTER */
	$('div.footer h3').click(function(event) {
		$('.nav li.colecao a').click();
	});
	
	
	
	/** MENU MAKING **/
	$('li.makingof a').click(function(event) {
		event.preventDefault();
		if(pagina!='makingof') {
			trocaMenuColecao('fechar');
			limpaTela(function(){
				pagina_anterior = pagina;
				pagina = 'makingof';
				$('div.header div.nav .menu').fadeOut('fast');
				$('ul.botoes li').fadeOut('fast');
				$('ul.botoes .voltar').fadeIn('slow');
				$('div.header > div.placa').animate({
					top: '-0px'
				}, 700, function() {
					menu_aberto = true;
					$('li.abrir').fadeOut('fast',function(){
						$('#makingof').fadeIn('fast',function(){
							
						});	
						$('li.voltar').fadeIn();
					})
				});
			});
		}
		
	});
	
	
	
	/* MENU CONTATO */
	$('li.contato a').click(function(event) {
		
		if(pagina!='contato') {
			trocaMenuColecao('fechar');
			limpaTela(function(){
				pagina_anterior = pagina;
				pagina = 'contato';
				$('div.header div.nav .menu').fadeOut('fast');
				$('ul.botoes li').fadeOut('fast');
				$('ul.botoes .voltar').fadeIn('slow');
				$('div.header > div.placa').animate({
					top: '-0px'
				}, 700, function() {
					menu_aberto = true;
					$('li.abrir').fadeOut('fast',function(){
						$("#contato_conteudo").load(baseurl+'/index.php/contato', function(response, status, xhr) {
							$('#contato_wrapper').fadeIn('slow');
						});
						$('li.voltar').fadeIn();
					})
				});
			});
		}
		
	});
	
	
	/* MENU REPRESENTANTES */
	$('li.representantes a').click(function(event) {
		
		if(pagina!='representantes') {
			trocaMenuColecao('fechar');
			limpaTela(function(){
				pagina_anterior = pagina;
				pagina = 'representantes';
				$('div.header div.nav .menu').fadeOut('fast');
				$('ul.botoes li').fadeOut('fast');
				$('ul.botoes .voltar').fadeIn('slow');
				$('div.header > div.placa').animate({
					top: '-0px'
				}, 700, function() {
					menu_aberto = true;
					$('li.abrir').fadeOut('fast',function(){
						$("#representante_conteudo").load(baseurl+'/index.php/representante', function(response, status, xhr) {
							$('#representante_wrapper').fadeIn('slow');
						});
						$('li.voltar').fadeIn();
					})
				});
			});
		}
		
	});
	
	
	jQuery('body').undelegate('#submit-botao','click').delegate('#submit-botao','click',function(){jQuery.ajax({'type':'POST','success':function(data) {
	$("#contato-form").fadeOut("fast",function() {
		$("#contato_conteudo").html(data);		
	})},'url':'/index.php/contato','cache':false,'data':jQuery(this).parents("form").serialize()});return false;});
	
	jQuery('body').undelegate('#submit-botao-representante','click').delegate('#submit-botao-representante','click',function(){jQuery.ajax({'type':'POST','success':function(data) {
	$("#representante-form").fadeOut("fast",function() {
		$("#representante_conteudo").html(data);		
	})},'url':'/index.php/representante','cache':false,'data':jQuery(this).parents("form").serialize()});return false;});
	
	
	
	
	
	$('#contato_wrapper a.fechar').live('click', function() {
		pagina = 'home';
		pagina_anterior = 'contato';
		$('#contato_wrapper').fadeOut('fast');
	});
	
	
	
	
	
	
});	
	
	
/* NÃO EDITAR */
$(function(){
	
	$("#galeria_colecao li a").fancybox({'overlayColor':'#000'});
	
	/*$('#galeria_colecao li.jcarousel-item a').live('click', function(event) {
		event.preventDefault();
		clearTimeout(intervalo);
		limpaTela();
		pagina_anterior = pagina;		
		pagina = 'colecao';
		li = $(this).parent();
		li.prepend('<div class="li-loading"><div><div></div>');
		li.find('.li-loading').fadeIn('fast');
		url = $(this).attr('href');
		var pic_real_width, pic_real_height;
		$("<img/>")
			.attr("src", url)
			.load(function() {
				pic_real_width = this.width;
				pic_real_height = this.height;
				
				$('div.show > img').removeAttr('style');
				$('div.show > img').addClass('background');
				jQuery.fn.fullscreenr({ width: pic_real_width, height: pic_real_height, bgID: '.background' });
				
				obj1 = $('#background1');
				obj2 = $('#background2');
				obj2.attr('src', url);
				if (agentID!=null) {
					obj2.show();
					obj1.attr('src', url);
					li.find('.li-loading').fadeOut('slow',function(){
						$(this).remove();	
						trocaMenuColecao();
					});
					
				} else {
					obj2.fadeIn('fast',function(){
						obj1.attr('src', url);
						obj2.fadeOut(50);
						
						$('div.show > img').addClass('panning');
						$('div.show > img').removeClass('background');
						li.find('.li-loading').fadeOut('slow',function(){
							$(this).remove();	
							trocaMenuColecao();
						});
						startPan();
					});
				}
			});
	});	*/
});

function startPan(pic_real_width,pic_real_height) {
	
	$outer_container=$("#outer_container");
	$imagePan_panning=$("#imagePan .panning");
	$imagePan=$("#imagePan");
	$imagePan_container=$("#imagePan .container");

	$outer_container.css("top", ($(window).height()-($outer_container.outerHeight()))/2);
	$imagePan_panning.css("margin-top",($imagePan.height()-$imagePan_panning.height())/2+"px");
	containerWidth=$imagePan.width();
	containerHeight=$imagePan.height();
	totalContentW=$imagePan_panning.width();
	totalContentH=$imagePan_panning.height();
	$imagePan_container.css("width",totalContentW).css("height",totalContentH);
	
	
	functionMouseMove = function MouseMove(e){
		var mouseCoordsX=(e.pageX - $imagePan.offset().left);
		var mouseCoordsY=(e.pageY - $imagePan.offset().top);
		
		var mousePercentX=mouseCoordsX/containerWidth;
		var mousePercentY=mouseCoordsY/containerHeight;
		var destX=-(((totalContentW-(containerWidth))-containerWidth)*(mousePercentX));
		var destY=-(((totalContentH-(containerHeight))-containerHeight)*(mousePercentY));
		var thePosA=mouseCoordsX-destX;
		var thePosB=destX-mouseCoordsX;
		var thePosC=mouseCoordsY-destY;
		var thePosD=destY-mouseCoordsY;
		var marginL=$imagePan_panning.css("marginLeft").replace("px", "");
		var marginT=$imagePan_panning.css("marginTop").replace("px", "");
		var animSpeed=2000; //ease amount
		var easeType="easeOutCirc";
		var con = 500;
		var metade = containerHeight/2;
		if(mouseCoordsX>destX || mouseCoordsY>destY){
			//$imagePan_container.css("left",-thePosA-marginL); $imagePan_container.css("top",-thePosC-marginT); //without easing
			$imagePan_container.stop().animate({left: -thePosA-marginL, top: -thePosC-marginT}, animSpeed,easeType); //with easing
		} else if(mouseCoordsX<destX || mouseCoordsY<destY){
			//$imagePan_container.css("left",thePosB-marginL); $imagePan_container.css("top",thePosD-marginT); //without easing
			$('#debug').html('mouseCoordsY<destY');
			$imagePan_container.stop().animate({left: thePosB-marginL, top: thePosD-marginT}, animSpeed,easeType); //with easing
		} else {
			$imagePan_container.stop();
		}
	}

	$imagePan_panning.css("margin-left",($imagePan.width()-$imagePan_panning.width())/2).css("margin-top",($imagePan.height()-$imagePan_panning.height())/2);
	$('body').bind("mousemove",functionMouseMove);	

}

