var
Ferrante = {
	init: function()
	{
		Ferrante.initMenu();
		Ferrante.salao.init();
		Ferrante.historia.init();
		
		if( $('#titulo_destaque').length )
			Ferrante.destaques.init();
		
		if( $('#mapa').length )
			Ferrante.localizacao.init();	
		
		$(window).load(function(){
			Ferrante.scroller();				
		});
		Ferrante.scroller();
		
		if($('#fale_conosco').length)
		{
			$('#nome').focus();
			$('#telefone').mask('(99) 9999-9999');			
		}
		
		if($('#imagem_produto_cb').length)
		{
			$('#imagem_produto_cb').colorbox();
		}
		
		$('.showCores').click(Ferrante.showCores);
		$('.cores_disponiveis .close').click(Ferrante.closeCores);
	},
	
	scroller: function()
	{
		var
		SC = $('#saloes_container'),
		PC = $('.produtos_container'),
		PC2 = $('#produtos_container'),
		S = $('.scrollpane');
		
		if( $.fn.jScrollPane )
		{
			if(SC.length)
			{
				SC.jScrollPane({animateTo:false, animateInterval:50, animateStep:2, showArrows: true});
			}		
			if(PC.length)
			{
				PC.jScrollPane({animateTo:false, animateInterval:50, animateStep:2, showArrows: true}).data('jsp');
			}		
			if(PC2.length)
			{
				PC2.jScrollPane({animateTo:false, animateInterval:50, animateStep:2, showArrows: true}).data('jsp');
			}		
			if(S.length)
			{
				S.each(function(){
					$(this).jScrollPane({animateTo:false, animateInterval:50, animateStep:2, showArrows: true}).data('jsp');
				});
			}
		}			
	},
	
	showCores: function()
	{
		$('#cores_disponiveis').animate({marginTop: 395});
	},
	
	closeCores: function()
	{
		$('#cores_disponiveis').animate({marginTop: -230});
	},
	
	contato: function()
	{
		var 
		DADOS = {
			'#nome': $('#nome').val(),
			'#email': $('#email').val(),
			'#endereco': $('#endereco').val(),
			'#estado': $('#estado').val(),
			'#numero': $('#numero').val(),
			'#bairro': $('#bairro').val(),
			'#cidade': $('#cidade').val(),
			'#cep': $('#cep').val(),
			'#telefone': $('#telefone').val(),
			'#assunto': $('#assunto').val(),
			'#descricao': $('#descricao').val()			
		};
		
		for(var i in DADOS)
		{
			if(DADOS[i].length < 1 || (i === '#email' && ! DADOS[i].match(/.+@.+\..+/)))
			{
				$(i).addClass('erro').focus();
				return false;
			}
			else
				$(i).removeClass('erro');			
		}
	},
	
	salao: { // Ferrante.salao
		init: function() // Ferrante.salao.init
		{
			Ferrante.salao.trocar(1);
			//$('#lista_salao li > a').click(function(){
			$('.lista_salao a[data-foto]').hover(function(){
				Ferrante.salao.trocar($(this).attr('data-id'));
			})
			.each(function(){
				var
				img = new Image();
				img.src = $(this).attr('data-foto');
			});
		},
		
		trocar: function(id) // Ferrante.salao.trocar
		{
			var
			SP = $('#salao_principal'),
			IM = $('#salao_img'),			
			Foto = $('.lista_salao').find('a[data-id="'+ id +'"]').attr('data-foto');
			
			IM.attr('src', Foto);
			SP.show();
		}
	},
	
	historia: { // Ferrante.historia
		
		init: function() // Ferrante.historia.init
		{
			var
			Nav = $('#HistoriaNav');
			
			if(Nav.length)
			{
				var 
				Primeiro = Nav.find('a').eq(0).attr('nohref');
				
				$(Primeiro).fadeIn('fast');
				
				Nav.find('a').click(function(){					
					var
					Todos = $('div.histDiv');
					Item = $($(this).attr('nohref'));
					
					Todos.hide();
					Item.fadeIn('fast');
					
					
					return false;
				});
			}
		}	
	},
	
	setBg: function(imagem)
	{
		if(imagem)
			$('div.root').css({'background': "#fff url('"+ imagem +"') no-repeat center"});
	},
	
	setProdutoBg: function(imagem)
	{
		if(imagem)
			$('div.direita.produto').css({'background': "#fff url('"+ imagem +"') no-repeat center"});
	},
		
	destaques: {
		
		tempo: 5 * 1e3, // 5 segundos
		timer: null,
		atual: -1,
		total: 0,
		
		def: function(destaques)
		{
			if(destaques instanceof Array)
			{
				this.dados = destaques;
				this.total = destaques.length - 1;
			}
		},
		
		//TODO: Image Preloader
		init: function()
		{					
			var
			_this = Ferrante.destaques,
			d = _this.dados;
			
			clearTimeout(_this.timer);
			
			if(d instanceof Array)
			{
				var n;
				
				if(_this.atual >= _this.total)
					n = 0;
				else
					n = _this.atual + 1;				
							
				if(d[n])
				{
					Ferrante.setBg(d[n]['background']);				
					$('#titulo_destaque').text(d[n]['nome_linha']);
				}

				_this.atual = n;				
				_this.timer = setTimeout(_this.init, _this.tempo);
			}						
		}
		
	},
	
	menuTime: null,
	lastOpen: null,
	lastOpenA: null,
	menuOpen: 0,	
	
	initMenu: function()
	{
/*		
		$('.nav li a').click(function(){
			var 
			UL = $(this).parent().find('ul').eq(0);
			
			if( UL.length )
			{
				if(UL.is(':visible'))
				{
					UL.hide();
					$(this).animate({'margin-right': 0}, 150);
				}
				else
				{
					UL.fadeIn(300);
					$(this).animate({'margin-right': '-20px'}, 150);
				}					
				
				return false;
			}			
		});
*/
		$('.nav li').hover(function(){
			$(this).find('a').eq(0).animate({'margin-right': '-20px'}, 150);
		}, function(){
			$(this).find('a').eq(0).animate({'margin-right': 0}, 150);
		});
		
		$('.nav li').hover(function(){
			var 
			UL = $(this).find('ul').eq(0),
			Opened = $(this).attr('data-opened');
			
			if(UL.length && ! Opened)
			{
				UL.show();
				$(this).attr('data-opened', true);
			}
			
			$(this).find('a').eq(0).animate({'margin-right': '-20px'}, 150)
			;		
		}, function(){
			var 
			UL = $(this).find('ul').eq(0),
			Opened = $(this).attr('data-opened');

			if(UL.length && Opened)
			{
				UL.hide();
				$(this).removeAttr('data-opened');
			}
			
			$(this).find('a').eq(0).animate({'margin-right': 0}, 150);
		});
		
		/*
		$('.nav li').hover(
			function(){
				var 
				UL = $(this).find('ul').eq(0),
				A = $(this).find('a').eq(0);
					
				if( UL.length )
					UL.fadeIn(300);
				
				A.animate({'margin-right': '-20px'}, 150);
			},
			function(){
				var 
				UL = $(this).find('ul').eq(0),
				A = $(this).find('a').eq(0);					
				
				if( UL.length )
					UL.hide();
				
				A.animate({'margin-right': 0}, 150);
			}
		);
		*/
	},
	
	localizacao: {
		
		title: 'Cadeiras Ferrante',
		icon: '',
		div: 'mapa',
		
		defIcon: function(icon)
		{
			this.icon = icon;
		},
		
		init: function()
		{
			var
			_this = Ferrante.localizacao,
			lojaFerrante = new google.maps.LatLng(-23.566978, -46.616086),
			config = {
				zoom: 15,
				center: lojaFerrante,
				mapTypeId: google.maps.MapTypeId.ROADMAP,
				mapTypeControl: false,
				navigationControl: true,
				navigationControlOptions: {
					style: google.maps.NavigationControlStyle.SMALL,
					position: google.maps.ControlPosition.TOP_RIGHT
				},
				streetViewControl: false
			},
			mapaFerrante = new google.maps.Map(document.getElementById( _this.div ), config),
			logotipo = new google.maps.Marker({
		        position: lojaFerrante,
		        map: mapaFerrante,
		        title: _this.title,
		        icon: _this.icon
		    });			
			/*
			janela = new google.maps.InfoWindow({
		        position: lojaFerrante,
		        content: _this.title
		    });
			google.maps.event.addListener(logotipo, 'click', function(){
				janela.open(mapaFerrante, logotipo);
		    });
			//janela.open(mapaFerrante, logotipo);
			*/		
		}
	}		
};

function Sela(o){o.select();return false}
function inClass(ned,arr){
  var achou=null;
  arr=arr.split(' ');
  for(i=0;i<arr.length;i++){
    if(ned==arr[i])achou=true
  }
  return achou
}
function Mais(e){
    var P = $("#item_"+e),
        q = $("#quant_"+e),
        n = parseInt(q.val()) + parseInt(1);
    if(n > 99){return false}
    if(n > 0){
      P.css({
        "background-color" : "#dddddd",
        "border-left-color" : "#1d398d"
      });
    }
    q.val(n);
}
function Menos(e){
    var P = $("#item_"+e),
        q = $("#quant_"+e),
        n = parseInt(q.val()) - parseInt(1);
    if(n < 0){return false}
    if(n == 0){
      P.css({
        "background-color" : "",
        "border-left-color" : ""
      });
    }
    q.val(n);
}
function Cor(e){
  var P = $("#item_"+e),
      q = $("#quant_"+e),
      n = q.val();
  if(n > 0){
    P.css({
      "background-color" : "#ddd",
      "border-left-color" : "#1d398d"
    });
  }
  if(n == 0){
    P.css({
      "background-color" : "",
      "border-left-color" : ""
    });
  }
  if(n == null || n == ""){P.css("background-color", "")}
  Sela(q);
}
function Vaaai()
{
	if($("div.titulo").length)
	{
		var 
			Tits = $("div.titulo"),
			Prods = $("div.produtos"),
			Itens = $("div.potitulo"),
			Itens2 = $("span.title"),
			Res = $(".resultado");
		
		Tits.click(function(){
		    var
		      i = Tits.index(this), 
		      Linha = $(Prods[i]);
		    
		    Linha.slideToggle("fast", function(){
			      if($('.produtos_container').length)
			      {
				      $('.produtos_container').jScrollPane({animateTo:false, animateInterval:50, animateStep:2, showArrows: true});
			      }
		    });
		});
	
		Itens2.each(function(n){
			var
			      L = $(Itens[n]),
			      foto = L.attr('data-foto');
	
			      L.easyTooltip({
				      tooltipId: "preview",
				      content: '<img width="100" height="90" src="'+ foto +'" />'
			      });
		});
	}
}

$(Vaaai);
$(Ferrante.init);

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-7774906-1']);
_gaq.push(['_trackPageview']);

(function() {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
