// JavaScript Document
var width;
var height;
var boxAtual;
var boxAnterior;
var areaAtual = 'home';
var areaAnterior = ''
var timeOver = 300;
var init = false;

var deviceAgent;
var agentID;

// FUNÇÕES
$(function() {
	detectIPadOrientation = function(){
		onResize();	
	}
	
	onResize = function(){
		width = getWindowWidth();
		
		if (width > 1200){
			$('#background_menu').css('width',width);
			$('.quemsomos_background').css('width',width);
			$('.contato_background').css('width',width);
			$('#arte_menu').css('width',width);
			$('#menu').css({'left':'50%','marginLeft':-171});
			$('#container').css({'left':'50%','marginLeft':-600});
			$('body').css( {backgroundPosition: "center top"} );
			
			if (agentID) {
				$('#background_menu').css('width',"100%");
				$('.quemsomos_background').css('width',"100%");
				$('.contato_background').css('width',"100%");
			}
		} else {
			$('#background_menu').css('width',1200);
			$('.quemsomos_background').css('width',1200);
			$('.contato_background').css('width',1200);
			$('#arte_menu').css('width',1200);
			$('#menu').css({'left':600,'margin-left':-171});
			$('#container').css({'left':'0','marginLeft':0});
			$('body').css( {backgroundPosition: "0 top"} )
			
			if (agentID) {
				$('#background_menu').css('width',"100%");
				$('.quemsomos_background').css('width',"100%");
				$('.contato_background').css('width',"100%");
			}
		}
		
		$('#background_menu').css('height',$(document).height());
		$('.quemsomos_background').css('height',$(document).height());
		$('.contato_background').css('height',$(document).height());
	};
	
	
	fecharIframe = function() {
		
		// RESETA ADDRESS
		$.address.value('home');
		
		boxAtual.children('.iframe').uncorner();
		if(Number(boxAtual.css('left').replace('px','')) < 600){
			boxAtual.children('.iframe').css({'left':0, 'right':''});
		} else {
			boxAtual.children('.iframe').css({'right':0, 'left':''});
		}
		boxAtual.css('z-index', 8);
		boxAtual.children('.iframe').css('z-index', 8);
		boxAtual.children('.iframe').stop(true, true).animate({'height':0, 'width':0},300, function(){
			$(this).empty();
			
			var offsetBox = boxAtual.offset();
			var offsetContainer = $('#contentgrid').offset();
			var altura = offsetBox.top + boxAtual.children('.iframe').height();
			
			if (altura > offsetContainer.top + $('#contentgrid').height()){
				$('#footer').css({'position': 'absolute', 'top': altura});
			} else {
				$('#footer').css({'position': 'relative', 'top': ''});
			}
			
			boxAnterior = null;
			boxAtual = null;	
		});
	};
	
	alturaIframe = function(altura) {
		boxAtual.children('.iframe').children('.singleframe').stop(true, true).animate({'height':altura, 'width':590},300);
		boxAtual.children('.iframe').stop(true, true).animate({'height':altura, 'width':590},300, function(){
			
			
			var offsetBox = boxAtual.offset();
			var offsetContainer = $('#contentgrid').offset();
			var altura = offsetBox.top + boxAtual.children('.iframe').height();
			
			if (altura > offsetContainer.top + $('#contentgrid').height()){
				$('#footer').css({'position': 'absolute', 'top': altura});
			} else {
				$('#footer').css({'position': 'relative', 'top': ''});
			}
			
				
		});
		
		
		
		//alert(altura);
	}
});

$(document).ready(function(){
	
	window.onorientationchange = detectIPadOrientation;
	deviceAgent = navigator.userAgent.toLowerCase();
	agentID = deviceAgent.match(/(iphone|ipod|ipad)/);

	width = getWindowWidth();
	
	$('.quemsomos_texto').columnize({ width : 225 });
	
	$('#background_menu').hide();
	$('.quemsomos_background').hide();
	$('.quemsomos_content').hide();
	$('.contato_background').hide();
	$('.contato_content').hide();
	
	// MENU
	$('.logo a').address(function() {
    	trocaArea('home');
		return $(this).attr('title');
    });  
	$('.botoes a').address(function() {
    	return $(this).attr('title');
    });  
	$('.botoes').children('a').click(function(){
		toggleMenu('fechar');
		
		switch($(this).attr('class')){
			case 'home':
				trocaArea('home');
			break;
			case 'quemsomos':
				trocaArea('quemsomos');
			break;
			case 'trabalho':
				trocaArea('trabalho');
			break;
			case 'contato':
				trocaArea('contato');
			break;
		}
	});
	
	
	$('#arte_menu').animate({height: "0px"}, 0);
	
	// BGs
	$('#background_menu').css('height',$(document).height() + 40);
	$('.quemsomos_background').css('height',$(document).height() + 40);
	$('.contato_background').css('height',$(document).height() + 40);
	
	$('body').css( {backgroundPosition: "center top"} )
	
	if (width > 1200){
		$('#background_menu').css('width',width);
		$('.quemsomos_background').css('width',width);
		$('.contato_background').css('width',width);
		$('#arte_menu').css('width',width);
		$('#menu').css({'left':'50%','marginLeft':-171});
		$('#container').css({'left':'50%','marginLeft':-600});
		$('body').css({backgroundPosition: "center top"}) ;
		
		if (agentID) {
			$('#background_menu').css('width',"100%");
			$('.quemsomos_background').css('width',"100%");
			$('.contato_background').css('width',"100%");
		}
		
	} else {
		$('#background_menu').css('width',1200);
		$('.quemsomos_background').css('width',1200);
		$('.contato_background').css('width',1200);
		$('#arte_menu').css('width',1200);
		$('#menu').css({'left':600,'margin-left':-171});
		$('#container').css({'left':'0','marginLeft':0});
		$('body').css({backgroundPosition: "0 top"});
		
		if (agentID) {
			$('#background_menu').css('width',"100%");
			$('.quemsomos_background').css('width',"100%");
			$('.contato_background').css('width',"100%");
		}
	}
		
	window.onresize = onResize;
	
	$('#container_categorias .titulo').hide(0);
	$('#container_categorias .lista').hide(0);
	$('#menu .botoes').children('a').each(function(){
		$(this).hide(0);
	});
	$('#menu').animate({height: "168px"}, 0);
	
	
	/* Get advert dimensions

    -------------------------------------------------------------- */
	
	var speed = 300;
	var $wall = $('#contentgrid');
	
	$wall.masonry({
		itemSelector: '.box:not(.invis)',
		columnWidth: 300,
		animate: true
	}, function(){
		$('#background_menu').css('height',$(document).height());
		$('.quemsomos_background').css('height',$(document).height());
		$('.contato_background').css('height',$(document).height());
	});
	
	
	
	
	
	$('#menu .botoes img').hover(
		function () {
			 $(this).animate({
				paddingLeft: "30px",
				paddingRight: "30px"
			  }, 100);
		}, 
		function () {
			$(this).animate({
				paddingLeft: "0px",
				paddingRight: "0px"
			  }, 100);
		}
	);
	$('.fechar').hide(0);
	
	
	$('#menu .togglemenu .fechar, #menu .togglemenu .abrir').click(function(){
		toggleMenu($(this).attr('class'));
	});
	
	
	$('#container_categorias .lista span:not(".divisoria")').click(function(){
		var colorClass = '.' + $(this).attr('class');
		if (colorClass == '.todos') {
			// mostra todos os vídeos
			$wall.children('.invis').toggleClass('invis').fadeIn(speed);
	  	} else {  
			// esconde videos visíveis
			$wall.children().not(colorClass).not('.invis').toggleClass('invis').fadeOut(speed);
			// mostra vídeos ocultos
			$wall.children(colorClass+'.invis').toggleClass('invis').fadeIn(speed);
	  	}
		
		// chama o grid novamente
	  	$wall.masonry({}, function(){
			setTimeout(function(){  $('#background_menu').css('height',$(document).height()); }, 600);
		});
		
	  	return false;
	});
	
	$('#container_categorias .lista span:not(".divisoria")').hover(function(){
		$(this).stop(true,true).animate({ color: "#aaaaaa" }, 'fast');
	}, function(){
		$(this).stop(true,true).animate({ color: "#ffffff" }, 'fast');
	});
	
	
	// CANTOS
	$('.cantos1').corner("bevel tl br");
	$('.cantos2').corner("bevel tr bl");
	
	$('.over').hide();
	
	$('div.box').hover(function(){
		$(this).children('.foto').children('img.pb').stop(true, true).fadeOut(300);
		
		var alturaBox = Number($(this).css('height').replace('px',''));
		var alturaTitulo = Number($(this).children('.over').css('height').replace('px',''));
		var posicao = Number(( alturaBox / 2 ) - (alturaTitulo / 2));
		$(this).children('.over').css('margin-top',posicao);
		$(this).children('.over').stop(true, true).fadeIn(300);
		
	}, function(){
		$(this).children('.foto').children('img.pb').stop(true, true).fadeIn(300);
		$(this).children('.over').stop(true, true).fadeOut(300);
	});
	
	// ABRE BOX
	$('div.box').click(function(){

		boxAnterior = boxAtual;
		boxAtual = $(this);
		
		// MUDA ADDRESS
		$.address.value($(this).children('.id_post').text());
		
		if(Number(boxAtual.css('left').replace('px','')) < 600){
			boxAtual.children('.iframe').css({'left':0, 'right':'', 'width':0,'height':0});
		} else {
			boxAtual.children('.iframe').css({'right':0, 'left':'', 'width':0,'height':0});
		}
		
		if ($.browser.msie && ($.browser.version == 7.0 || $.browser.version == 6.0)){
			boxAtual.css('z-index', 9);
			boxAtual.children('.iframe').css('z-index', 9);
		} else {
			boxAtual.css('z-index', 9);
			boxAtual.children('.iframe').css('z-index', 9);
		}
		
		boxAtual.children('.iframe').stop(true, true).animate({'height':990, 'width':590},300, function(){
			// Completou
			$('<iframe id="singleframe" scrolling="no" class="singleframe" src="single.php?id=' + $(this).parent().children('.id_post').text() + '" height="990" width="590" frameborder="0" style="width:590; height:990" />').appendTo($(this));
			
			if ($(this).parent().is('.cantos1')){
				$(this).corner("bevel tl cc:#fff");
			} else {
				$(this).corner("bevel tr cc:#fff");
			}
		});
		
		if (boxAnterior != undefined){
			boxAnterior.children('.iframe').uncorner();
			if(Number(boxAnterior.css('left').replace('px','')) < 600){
				boxAnterior.children('.iframe').css({'left':0, 'right':''});
			} else {
				boxAnterior.children('.iframe').css({'right':0, 'left':''});
			}
			
			if ($.browser.msie && ($.browser.version == 7.0 || $.browser.version == 6.0)){
				boxAnterior.css('z-index', 8);
				boxAnterior.children('.iframe').css('z-index', 8);
			} else {
				boxAnterior.css('z-index', 8);
				boxAnterior.children('.iframe').css('z-index', 8);
			}
			
			boxAnterior.children('.iframe').stop(true, true).animate({'height':0, 'width':0},300, function(){
				$(this).empty();		
			});
		}
	});
	
	$('.box').each(function(index){
		if(Number($(this).css('left').replace('px','')) < 600){
			$(this).children('.iframe').css({'left':0, 'width':0,'height':0});
		} else {
			$(this).children('.iframe').css({'right':0, 'width':0,'height':0});
		}
	});
	
	$('.iframe').animate({height: "0", width:"0"},0);	
	
	// CONTATO
	$('.contato_bt').hover(
		function() {
			$(this).children('.contato_btseta').stop(true, true).animate({"left": 4}, timeOver);
			$(this).children('.contato_bttexto').stop(true, true).animate({"color": "#fff5cf"}, timeOver);
		},
		function() {
			$(this).children('.contato_btseta').stop(true, true).animate({"left": 0}, timeOver);
			$(this).children('.contato_bttexto').stop(true, true).animate({"color": "#fff5cf"}, timeOver);
		}
	);
	
	$('#contato_btEnviar').click(function(){
		$('#contato_form').submit();
	});
	
	$('#contato_form').submit(function() {
		$("#contato_mensagem").html("Enviando Mensagem<br /><span>Aguarde...</span>");
		$("#contato_mensagem").fadeIn(300);
		
		$.post('php/mail_contato.php', $("#contato_form").serialize(), function(data) {
			if(data=="false"){
				$("#contato_mensagem").html("Erro no Envio<br /><span>Tente Novamente</span>");
				$("#contato_mensagem").delay(6000).fadeOut(300);
			}else{
				$("#contato_mensagem").html("Dados Enviados<br /><span>Obrigado Pelo Contato</span>");
				document.getElementById("nome").value = "";
				document.getElementById("email").value = "";
				document.getElementById("cidade").value = "";
				document.getElementById("estado").value = "";
				document.getElementById("mensagem").value = "";
				$("#contato_mensagem").delay(6000).fadeOut(300);
			}
		});
		return false;
	});
	
	// Eventos Address e Colorbox
	$.address.init(function(event) {
		init = true;
		window.scrollTo(0,0);
	}).change(function(event) {

		return false;
		
	}).externalChange(function(event){
		if (event.value != "" && event.value != "/"){
			if (init){
				switch(event.value){
					case 'home':
						areaAtual = 'home';
						
						$('.contato_background').hide();
						$('.contato_content').hide();
						
						$('.quemsomos_background').hide();
						$('.quemsomos_content').hide();
						
						$('#container_categorias .titulo').hide();
						$('#container_categorias .lista').hide();
						$('#container_grid').show();
						$('#footer').show();
					break;
					case 'quemsomos':
						areaAtual = 'quemsomos';
						$('#container_categorias .titulo').hide();
						$('#container_categorias .lista').hide();
						$('#container_grid').hide();
						$('#footer').hide();
						
						$('.contato_background').hide();
						$('.contato_content').hide();
						
						$('.quemsomos_background').show();
						$('.quemsomos_content').show();
					break;
					case 'trabalho':
						areaAtual = 'trabalho';
						$('.contato_background').hide();
						$('.contato_content').hide();
						
						$('.quemsomos_background').hide();
						$('.quemsomos_content').hide();
						
						$('#container_categorias .titulo').show();
						$('#container_categorias .lista').show();
						$('#container_grid').show();
						$('#footer').show();
					break;
					case 'contato':
						areaAtual = 'contato';
						$('#container_categorias .titulo').hide();
						$('#container_categorias .lista').hide();
						$('#container_grid').hide();
						$('#footer').hide();
						
						$('.quemsomos_background').hide();
						$('.quemsomos_content').hide();
						
						$('.contato_background').show();
						$('.contato_content').show();
						setTimeout(function(){
							initializeMap(-23.613866,-46.659824, 16);
							addMarker(-23.613866,-46.659824, null, "images/malabar_map.png", "Malabar Filmes");
						}, 500);
					break;
				}
			} else {
				window.scrollTo(0,0);
				switch(event.value){
					case 'home':
						trocaArea('home');
					break;
					case 'quemsomos':
						trocaArea('quemsomos');
					break;
					case 'trabalho':
						trocaArea('trabalho');
					break;
					case 'contato':
						trocaArea('contato');
					break;
				}
			}
		}
		init = false;
		
		return false;
	}).internalChange(function(event){
		return false;
	});
	
});

function toggleMenu(botao){
	
	var cont = 0;
	var tdelay = 0;
	var bt = "";
	
	if (botao == 'abrir'){
		$('.abrir').fadeOut(300);
		$('.fechar').delay(300).fadeIn(300);
		
		$('#background_menu').fadeIn(300);
		
		$('#arte_menu').animate({height: "1217px"}, 1000);
		
		setTimeout(function(){ $('#background_menu').css('height',$(document).height()); },1000);
		
		$('#menu').animate({height: "598px"}, 500);
		$('#menu .botoes').children('a').each(function(i){
			$(this).delay(200 + (i * 100)).fadeIn(300);
		});
	} else {
		$('.fechar').fadeOut(300);
		$('.abrir').delay(300).fadeIn(300);
		
		$('#background_menu').fadeOut(300);
		
		$('#arte_menu').animate({height: "0px"}, 1000);
		
		setTimeout(function(){ $('#background_menu').css('height',$(document).height()); },1000);
		
		$('#menu').animate({height: "168px"}, 500);
		$($('#menu .botoes a').get().reverse()).each(function(i){
			$("#bt" + (3-i)).delay(i * 50).fadeOut(100);
		});
	}
}

function getWindowWidth() {
	var myWidth = 0;  
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth - 17;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
	}
	return myWidth;
}

function trocaArea(area){
	
	if (areaAtual != area){
		areaAnterior = areaAtual;
		areaAtual = area;
		
		// Espera o Menu Fechar
		setTimeout(function(){
			// Fecha Area Anterior
			switch(areaAnterior){
				case 'home':
					if (areaAtual == 'trabalho'){
						delay = 0;
					} else {
						delay = 500;
						$('#container_categorias').fadeOut(500);
						$('#container_grid').fadeOut(500);
						$('#footer').fadeOut(500);
						
						// FECHA CATEGORIAS
						$('#container_categorias .titulo').delay(500).slideUp(100);
						$('#container_categorias .lista').delay(500).slideUp(100, function(){
							$('#background_menu').css('height',$(document).height());	
							$('.quemsomos_background').css('height',$(document).height());
							$('.contato_background').css('height',$(document).height());
						});
					}
				break;
				case 'contato':
					delay = 1000;
					$('.contato_content').fadeOut(500);
					$('.contato_background').delay(500).fadeOut(500);
				break;
				case 'quemsomos':
					delay = 1000;
					$('.quemsomos_content').fadeOut(500);
					$('.quemsomos_background').delay(500).fadeOut(500);
				break;
				case 'trabalho':
					if (areaAtual == 'home'){
						delay = 0;
					} else {
						delay = 500;
						$('#container_categorias').fadeOut(500);
						$('#container_grid').fadeOut(500);
						$('#footer').fadeOut(500);
						
						// FECHA CATEGORIAS
						$('#container_categorias .titulo').delay(500).slideUp(100);
						$('#container_categorias .lista').delay(500).slideUp(100, function(){
							$('#background_menu').css('height',$(document).height());	
							$('.quemsomos_background').css('height',$(document).height());
							$('.contato_background').css('height',$(document).height());
						});
					}
					
					
					
				break;
			}
			
			// Espera Fechar areaAnterior e Abre areaAtual
			setTimeout(function(){
				switch(areaAtual){
					case 'home':
						if (areaAnterior != 'trabalho'){
							$('#container_categorias').fadeIn(500);
							$('#container_grid').fadeIn(500);
							$('#footer').fadeIn(500);
							
							// FECHA CATEGORIAS
							$('#container_categorias .titulo').delay(500).slideUp(100);
							$('#container_categorias .lista').delay(500).slideUp(100, function(){
								if ($.browser.msie && jQuery.browser.version == 7.0){
									$('#contentgrid').masonry({}, function(){
										setTimeout(function(){  $('#background_menu').css('height',$(document).height()); }, 600);
									});
								}
								$('#background_menu').css('height',$(document).height());	
								$('.quemsomos_background').css('height',$(document).height());
								$('.contato_background').css('height',$(document).height());
							});
						} else {
							// FECHA CATEGORIAS
							$('#container_categorias .titulo').slideUp(300);
							$('#container_categorias .lista').delay(150).slideUp(300, function(){
								$('#background_menu').css('height',$(document).height());	
								$('.quemsomos_background').css('height',$(document).height());
								$('.contato_background').css('height',$(document).height());
							});
						}
					break;
					case 'contato':
						$('.contato_background').fadeIn(500);
						$('.contato_content').delay(500).fadeIn(500, function(){
							setTimeout(function(){
								initializeMap(-23.613866,-46.659824, 16);
								addMarker(-23.613866,-46.659824, null, "images/malabar_map.png", "Malabar Filmes");
							}, 500);
						});
					break;
					case 'quemsomos':
						$('.quemsomos_background').fadeIn(500);
						$('.quemsomos_content').delay(500).fadeIn(500);
					break;
					case 'trabalho':
						if (areaAnterior == 'home'){							
							// ABRE CATEGORIAS
							$('#container_categorias .titulo').slideDown(300);
							$('#container_categorias .lista').delay(150).slideDown(300, function(){
								$('#background_menu').css('height',$(document).height());	
								$('.quemsomos_background').css('height',$(document).height());
								$('.contato_background').css('height',$(document).height());
							});
						} else {
							$('#container_categorias').show();
							$('#container_categorias .titulo').slideUp(0);
							$('#container_categorias .lista').slideUp(0);
							
							$('#container_grid').fadeIn(500);
							$('#footer').fadeIn(500);
							
							// ABRE CATEGORIAS
							$('#container_categorias .titulo').delay(500).slideDown(300);
							$('#container_categorias .lista').delay(650).slideDown(300, function(){
								if ($.browser.msie && jQuery.browser.version == 7.0){
									$('#contentgrid').masonry({}, function(){
										setTimeout(function(){  $('#background_menu').css('height',$(document).height()); }, 600);
									});
								}
								$('#background_menu').css('height',$(document).height());	
								$('.quemsomos_background').css('height',$(document).height());
								$('.contato_background').css('height',$(document).height());
							});
						}
					break;
				}
			}, delay);
		}, 1000);
	}
}

function onPressKey(e){
	if(e.keyCode==13){
		makeRoute();
		return false;	
	}
}

function makeRoute(){
	var address = document.getElementById("procura").value;
	Route(address, new google.maps.LatLng(-23.613866,-46.659824));
}

$(window).load(function(){
	//alert('COMPLETOU');
	window.scrollTo(0,0);
	
	var addDelay = 0;
	
	if (areaAtual != 'trabalho'){
		addDelay = 1000;
		$('#container_categorias .titulo').delay(0).slideDown(100);
		$('#container_categorias .lista').delay(0).slideDown(100);
		$('#container_categorias .titulo').delay(500).slideUp(100);
		$('#container_categorias .lista').delay(500).slideUp(100);
	}
	
	if (areaAtual != 'contato'){
		addDelay = 1000;		
		$('.contato_content').delay(0).fadeIn(50);
		$('.contato_content').delay(0).fadeIn(50);
		$('.contato_content').delay(300).fadeOut(50);
		$('.contato_content').delay(300).fadeOut(50);
	}
	
	
	$('#loading img').delay(300 + addDelay).fadeOut(300);
	$('#loading').delay(600 + addDelay).animate({height: "0px"}, 500);
	setTimeout(function(){
		$("body").css("overflow", "auto");
		if ($("#dadoget").text() != ""){
			$("#bloco" + $("#dadoget").text()).click();
		}
	}, 600 + addDelay);
});
