var href = location.href;
	
if(href.match(/\/#gaso=/)){
}else if (!href.match(/\/#\//)) {
	href = location.hostname.replace(/\//gi,'') + '/' + location.pathname.split('/')[1] +  '/#';
	if (location.pathname != '' && location.pathname != '/') {
			switch(location.pathname.replace(location.pathname.split('/')[1],'').replace(/\/\//,/\//).substr(1).split('/')[0]) {
				case 'parceiros': break;
				case 'nossas_unidades': break;
				case 'contato': break;	
				case 'parceiros': break;
				case '': break;
				default:
					href += '/'+href.split('/')[1].replace(/\//gi,'');
				break;
			}
		href += location.pathname.replace(location.pathname.split('/')[1],'') + location.search
	} else {
		href += '/' + location.search
	}
	location.href = location.protocol + '//' + href.replace(/\/\//gi,/\//);
}
	
	

keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

var ua = navigator.userAgent.toLowerCase();
if (ua.indexOf(" chrome/") >= 0 || ua.indexOf(" firefox/") >= 0 || ua.indexOf(' gecko/') >= 0) {
	var StringMaker = function () {
		this.str = "";
		this.length = 0;
		this.append = function (s) {
			this.str += s;
			this.length += s.length;
		}
		this.prepend = function (s) {
			this.str = s + this.str;
			this.length += s.length;
		}
		this.toString = function () {
			return this.str;
		}
	}
} else {
	var StringMaker = function () {
		this.parts = [];
		this.length = 0;
		this.append = function (s) {
			this.parts.push(s);
			this.length += s.length;
		}
		this.prepend = function (s) {
			this.parts.unshift(s);
			this.length += s.length;
		}
		this.toString = function () {
			return this.parts.join('');
		}
	}
}


function encode64(input) {
	var output = new StringMaker();
	var chr1, chr2, chr3;
	var enc1, enc2, enc3, enc4;
	var i = 0;
 
	while (i < input.length) {
		chr1 = input.charCodeAt(i++);
		chr2 = input.charCodeAt(i++);
		chr3 = input.charCodeAt(i++);
 
		enc1 = chr1 >> 2;
		enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
		enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
		enc4 = chr3 & 63;
 
		if (isNaN(chr2)) {
			enc3 = enc4 = 64;
		} else if (isNaN(chr3)) {
			enc4 = 64;
		}
 
		output.append(keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4));
   }
   
   return output.toString();
}
 
function decode64(input) {
	var output = new StringMaker();
	var chr1, chr2, chr3;
	var enc1, enc2, enc3, enc4;
	var i = 0;
 
	// remove all characters that are not A-Z, a-z, 0-9, +, /, or =
	input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
 
	while (i < input.length) {
		enc1 = keyStr.indexOf(input.charAt(i++));
		enc2 = keyStr.indexOf(input.charAt(i++));
		enc3 = keyStr.indexOf(input.charAt(i++));
		enc4 = keyStr.indexOf(input.charAt(i++));
 
		chr1 = (enc1 << 2) | (enc2 >> 4);
		chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
		chr3 = ((enc3 & 3) << 6) | enc4;
 
		output.append(String.fromCharCode(chr1));
 
		if (enc3 != 64) {
			output.append(String.fromCharCode(chr2));
		}
		if (enc4 != 64) {
			output.append(String.fromCharCode(chr3));
		}
	}
 
	return output.toString();
}


var ready = false;
//IMAGEM DE FUNDO DO SITE

function _tamanhoFundoWidth(obj_img) {
	
	if($(window).width() > $(obj_img).width() && $(obj_img).width() > 30) {		
		if($(window).height() > $(obj_img).height()) {
			return $(window).width() + ( ($(window).height() - $(obj_img).height()) * 1.6) ;
		}else{
			return $(window).width();
		}
	}else{
		$(obj_img).width($(window).width());
		if($(window).height() > $(obj_img).height()) {	
			return $(obj_img).width() + ( ($(window).height() - $(obj_img).height()) * 1.6) ;
		}else{						
			return $(window).width();
		}
	}
	/*
	width = width < 800 ? 800 : width ;
	height = height < 600 ? 600 : height ;
	
	var ratio = 768/1024;
	var dif = 1.5;
	
	var h = (height*dif);
	var w = (width*dif);
	
	if((width/height) > ratio){		
		w = (height*dif)/ratio;
	}else{
		h = (height*dif);
	}
	//alert($(window).width() > w);
	if($(window).width() > w || $(window).height() > h) {
		//return _tamanhoFundo((width*dif),(height*dif));
	}
	
	/*

	return Array(width,height);*/
}
var img_fundo_ori_w;
var img_fundo_ori_h;

function _fundo() {		
		if(!ready){
			$(".fundo")
			.hide().css("opacity",0);
		}
		
		$(".fundo")
			.css({
				position:'fixed',
				top:0,
				left:0,
				width:$(window).width(),
				height:$(window).height()//,
				//overflow:'none'
			})
			.show()
			.find("img")
			.attr("src","")
			.attr("src",img_bg)
			.load(function() {
				$(this)
					.css({
						width:_tamanhoFundoWidth(this)
					});
					
				$(".fundo")
					.animate({
						opacity:1		 
					},1000,function() { ready = true; });
				
			});
				
}


function _menuFundo() {
	$(".menu").hide();
	$(".menu > div > div")
		.css({
			background:'#FFFFFF',
			opacity:0.75,
			zIndex:1
		});
	$(".menu_fundo")
		.css({
			background:'#FFFFFF',
			opacity:0.8,
			zIndex:1
		})
		.animate(
			{ left:0 },
			'slow',
			function(){
				$(".menu").show('slow');
			}
		);
}



function _menuFundo2(type) {
	if(type){
		$(".menu2,.menu_fundo2").css('height',$(window).height());
	}else{
		$(".menu2").hide();
		$(".menu_fundo2")
			.css({
				background:'#FFFFFF',
				opacity:0.7,
				zIndex:1,
				height:0
			})
			.animate(
				{ height:$(window).height()},
				'slow',
				function(){
					$(".menu2")
						.css({
							 opacity:0,
							 display:''
						})
						.animate({
							opacity:1		 
						},'normal',function() {
							$(document.body)
								.append('<a href="/"><img id="logo_menu_img" src="'+$(this).find("a[id='logo_menu']").attr("img")+'" border="0"></a>');							
								_altA();
						});
						
				}
			);
	}
}

function _submenu(id) {
	$(".submenu[id='"+id+"']")
		.slideToggle();
}

function _logoHome() {
	$("#logo")
		.css({
			position:'relative'	 
		});
}


//Plano de fundo do Zoom
function _fundoZoom(){
	$("#fundo_zoom").remove();
	$(document.body)
		.append('<div id="fundo_zoom">&nbsp;Allan </div>');
	$("#fundo_zoom")
		.css({
			position:'fixed',
			background:'#000000',
			opacity:0,
			top:0,
			left:0,
			width:'100%',
			height:$(window).height(),
			zIndex:10000,
			cursor:'pointer'
		}).animate({opacity:0.8},300);
}


function _imgZommRight(speed){
	if(imgZoom_t - imgZoom_loc >= 3) {
		$(".div_centro")
			.find(".seta_esquerda")
			.css({
				opacity:1,
				cursor:'pointer'
			});
		imgZoom_loc+=3;
		$(".div_centro > #lista > div")
		.animate({
			left: -imgZoom_width*(imgZoom_loc-1)+'px'
		},(typeof speed!='undefined' ? speed : 'fast'),'linear');
		if(imgZoom_t - imgZoom_loc < 3){
			$(".div_centro")
				.find(".seta_direita")
				.css({
					opacity:0,
					cursor:'default'
				});		
		}
	}
}

function _imgZommLeft(speed){
	
	if(imgZoom_loc > 1) {
		imgZoom_loc-=3;
		$(".div_centro > #lista > div")
		.animate({
			left: -imgZoom_width*(imgZoom_loc-1)
		},(typeof speed!='undefined' ? speed : 'fast'),'linear');
		if(imgZoom_loc <= 1){
			$(".div_centro")
				.find(".seta_esquerda")
				.css({
					opacity:0,
					cursor:'default'
				});	
		}
		if(imgZoom_t - imgZoom_loc >= 3) {
		$(".div_centro")
			.find(".seta_direita")
			.css({
				opacity:1,
				cursor:'pointer'
			});
		}
	}
}

//Zom das imagens
var imgZoom_loc = 0;
var imgZoom_width = 0;
function _imgZoom() {
	imgZoom_width = 148;
	imgZoom_t = $(".div_centro > #lista").find("img").length;
	imgZoom_loc = 1;
	
	$(".div_centro > #lista > div").css("width",imgZoom_width*imgZoom_t);
	
	$(".div_centro > .seta_direita, .div_centro > .seta_esquerda")
			.css({
				position:'relative',
				top:'-79px',
				display:'block',
				opacity:0

			});
	$(".div_centro")
		.find(".seta_direita")
		.css({
			left:519,
			top:-121
		})
		.click(function() {
			_imgZommRight();			
		});
	$(".div_centro")
		.find(".seta_esquerda")
		.css({
			left:10	 
		})
		.click(function() {
			_imgZommLeft();			
		});
		
	if(imgZoom_width > 3){
		
		$(".div_centro")
			.find(".seta_direita")
			.css({
				opacity:1,
				cursor:'pointer'
			});
	}
		
	$(".div_centro > #lista")
		.find("img")
		.css({ cursor: 'pointer'})
		.click(function() {
			_fundoZoom();
			$(document.body).append('<div id="img_zoom"><img  src="' + this.src.split(".jpg")[0] +'_grande.jpg"></div>');
			$("#img_zoom")
				.css({
					padding:5,
					background:'#DDDDDD',
					position:'fixed',
					top: -2000,
					zIndex:100001,
					cursor:'pointer'
				 })
				.find("img")
				.load(function(){
					$("#img_zoom")
					.css({
						top: ($(window).height()-$(this).height())/2,
						left: ($(window).width()-$(this).width())/2					 
					})
					.hide()
					setTimeout('$("#img_zoom").show();',300);
				});
			$("#fundo_zoom,#img_zoom").click(function(){$("#fundo_zoom,#img_zoom").remove();});
			
		}).each(function() {
						   
		});
}

_before_fundo = false;
function _beforeFundo(){
	if(_before_fundo == false && ready==true) {
		_menuFundo(); // inicializa menu
		_menuFundo2(); // inicializa menu 2
		_before_fundo = true;
	}else if(_before_fundo == false){
		$(".menu").hide();
		$(".menu2").hide();
		$(".menu_fundo2").css({height:0});
	
		setTimeout(_beforeFundo,100);
	}
}

// Onload page
$(function() {
	_fundo(); // inicializa fundo
	_beforeFundo();
	_imgZoom(); //inicializa o zoom
	$(".menu2  .botao a")
		.click(function() {
			var obj = this;
			$(".menu2  .botao a")
				.each(function() {
					if($(this).is("[submenu]") && !$(".submenu[id='"+$(this).attr("submenu")+"']").is(":hidden") && this!=obj) {
						$(this)
							.toggleClass("menu_seta2 menu_seta",0);
						_submenu($(this).attr("submenu"));
					}
				});
			if($(this).is("[submenu]") && $(".submenu[id='"+$(this).attr("submenu")+"']").is(":hidden")) {
				$(this)
					.toggleClass("menu_seta2 menu_seta",0);
				_submenu($(this).attr("submenu"));
			}
		});
	$(".submenu a")
		.hover(function(){
			$(this).toggleClass("menu_seta menu_seta3",0);		
//			$(this).toggleClass("menu_seta3",0);		
		},function(){
			$(this).toggleClass("menu_seta menu_seta3",0);
		});
		
		
	_altA();
	
	if(window.location.href.split("#").length > 0) {
		_nav('td_centro');
	}
	setInterval("_navLocation();",100);
	
	$(".home_menu")
		.hover(function() {
			$(this)
				.html("Home")
				.css({
					 marginLeft:'30px',
					 color:'#6a6ba1',
					 textDecoration:'none',
					 fontWeight:'bold'
				});
		},function(){
			$(this).html("");									
		});
	
});
//RESIZE WINDOW
$(window)
	.resize(function() {
		_fundo();
		_menuFundo2(true);
	});
	
	
	
	
	/***************/
	
	
function _navLocation(){
	if(location.href!=href) {
		href = location.href
		_nav('td_centro');	
	}
}
	
function _aAction(_this,rlocation) {
	//pageTracker._trackPageview(_this.href.replace(location.protocol + '//' + location.hostname + '/',''));
	if(!$(_this).is("[disabledNav]")){
		var link="window.location='"+ href.split('#')[0]+'#/'+_this.href.replace(location.protocol + '//' + location.hostname + '/','')+"'";
		eval(link);	
		_nav(($(_this).attr("id")=='' ? 'td_centro' :$(_this).attr("id")));
		return false;
	}	
}	

function _altA(){
	$("a[target!='_BLANK']")
		.each(function() {
			if(!$(this).hasClass("isAltLink")) {
				$(this)
					.bind("click",function() {
						return _aAction(this);
					})
					.addClass("isAltLink");
			}
		});
}



function _nav(lnk) {
	var _url = window.location.href.replace(href.split('#')[0]+'#','');
	
	if(window.location.href.replace(href.split('#')[1]+'#','').replace(location.protocol + '//' + location.hostname,'')=='/concertina/'&& _url=='/'){
		_url = '/vendas_suspensas';
		//
	}
	if( _url!='/'){
		$.ajax({
			   type: "GET",
			   cache:false,
			   url:'/ajax/'+lnk+_url,
			   dataType: "script",
			   success:function(r)  {
				   $("#div[id='debug']").html('<textarea style="width:99%;" rows="5">aaa'+r+'</textarea>');//.html(r);
	
				   $(function(){
						_altA(); //Altera os links
						_imgZoom(); //inicializa o zoom
					});
			   }
		});
	}else{
		$("#td_centro").html('');
	}
}

function _contatoMsgError(msg){
	$(".contato_msg_error")
		.html(msg)
		.show()
		.css({
			backgroundColor:'#FFFFDD',
			border:'1px solid #CCCCCC',
			padding:7,
			textAlign:"center",
			marginBottom:5,
			fontWeight:'bold'
			
		})
		.animate({backgroundColor:'#F00000'},50,
			function(){
				_alinharConteudo();
				$(this)
				.animate({backgroundColor:'#FFFFDD'},50,
					function(){
						$(this)
						.animate({backgroundColor:'#F00000'},50,
							function(){
								$(this)
								.animate({backgroundColor:'#FFFFDD'});
							});
					});
			});
		return false;
}
function _contatoSubmit(_this) {
	var checkMail  = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
	var checkTelefone  = new RegExp(/^[0-9]{2} [0-9]{4}\-[0-9]{4}/);
	
	if($.trim(_this.nome.value)==''){
		_contatoMsgError("O nome não pode estar em branco.");							 
	}else if(!checkMail.test(_this.email.value)){
		_contatoMsgError("E-mail inválido.");
	}else if(!checkTelefone.test(_this.telefone.value)){
		_contatoMsgError("Telefone inválido.");
	}else if($.trim(_this.assunto.value)==''){
		_contatoMsgError("O Assunto não pode estar em branco.");							 
	}else if($.trim(_this.mensagem.value)==''){
		_contatoMsgError("A Mensagem não pode estar em branco.");							 
	}else{
		
		var _data = "";
		
		$(_this)
			.find("input[type='text'],textarea")
			.each(function(){
				_data+=this.name+"="+$(this).val()+"&";	
				$(this).val('');
			});
		
		$.ajax({
			cache:false,
			url:'/contato.php',
			data:_data,
			type:'POST',
			success:function(r){
				_contatoMsgError("Mensagem enviada com sucesso, em breve entraremos em contato.\n\n\n"+r);	
				$(_this)
					.find("input[type='text'],textarea")
					.each(function(){
						$(this).val('');
					});
			}
		});
	}
	return false;
}

function _alert(txt) {
	$("._alertFundo,._alertDiv").remove();
	$(document.body)
		.append('<div class="_alertFundo" style="display:none"></div><div class="_alertDiv" style="display:none;min-height:120px">'+txt+'</div>');
	var l = ($(window).width() - 500) /2;
	var t = ($(window).height()- $("._alertDiv").height()) /2;
	
	$("._alertFundo")
		.css({
			background:"#000000",
			opacity:0,
			width:'100%',
			height:$(window).height(),
			top:0,
			left:0,
			position:'fixed',
			zIndex:100000,
			cursor:"pointer"
		})
		.show()
		.animate({
			opacity:0.8
		},'normal',function() { 
			$("._alertDiv")
				.css({
					width:500,
					background:"#FFFFFF",
					border:"1px solid #CCCCCC",
					padding:"7px",
					textAlign:"center",
					position:'fixed',
					left:l,
					top:t,
					zIndex:100001,
					
					lineHeight:'120px',
					fontFamily:"tahoma",
					fontSize:'14px',
					fontWeight:"bold",
					cursor:"pointer"
					
				})
				.show()
				.click(function(){
					$("._alertFundo,._alertDiv").remove();
				});
		})
		.click(function(){
			$("._alertFundo,._alertDiv").remove();
		});	
}
