var blank = new Image();
blank.src = '/imgs/pix.gif';
function fixPng(png) {
	// get src
	var src = png.src;
	// set width and height
	if (!png.style.width) { png.style.width = $(png).width(); }
	if (!png.style.height) { png.style.height = $(png).height(); }
	// replace by blank image
	png.onload = function() { };
	png.src = blank.src;
	// set filter (display original image)
	png.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
}

$(document).ready(function() {
	//máscaras de formulário
	$.mask.options = {
		attr: 'alt', // an attr to look for the mask name or the mask itself
		mask: null, // the mask to be used on the input
		type: 'fixed', // the mask of this mask
		maxLength: 5000, // the maxLength of the mask
		defaultValue: '', // the default value for this input
		textAlign: true, // to use or not to use textAlign on the input
		selectCharsOnFocus: true, //selects characters on focus of the input
		setSize: true, // sets the input size based on the length of the mask (work with fixed and reverse masks only)
		autoTab: false, // auto focus the next form element
		fixedChars : '[(),.:/ -]', // fixed chars to be used on the masks.
		onInvalid : function(){},
		onValid : function(){},
		onOverflow : function(){}
	};
	$.mask.masks = {
		'ddd'     : { mask : '99' },
		'fone'     : { mask : '9999-9999' },
		'foneddd'     : { mask : '(99) 9999-9999' },
		'cpf'       : { mask : '999.999.999-99' },
		'data'      : { mask : '39/19/2999' },
		'datahora'      : { mask : '39/19/2999 29:59' },
		'cpf'       : { mask : '999.999.999-99' },
		'cep'       : { mask : '99999-999' },
		'ano'      : { mask : '2999' }
	};
	$('input:text').setMask();
	
	var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
	if (badBrowser) $('img[src$=.png]').each(function() {
		if (!this.complete) this.onload = function() { fixPng(this) };
		else fixPng(this);
	});
});


Shadowbox.init({
	language: 'pt-br',
	handleOversize: 'drag',
	continuous: true,
	players: ['img', 'html', 'iframe', 'qt', 'wmp', 'swf', 'flv'],
	onFinish: function() {
		$('#sb-wrapper').show();
		if($('img',Shadowbox.getCurrent().el).attr('alt') != '' && $('img',Shadowbox.getCurrent().el).attr('alt') != undefined) {
			if($('#sb-caption').html() == null) $('#sb-body').after('<div id="sb-caption"><div id="sb-caption-inner"></div></div>');
			$('#sb-caption-inner').html($('img',Shadowbox.getCurrent().el).attr('alt'));
			$('#sb-caption').css({
					position: 'absolute',
					textAlign: 'center',
					color: '#FFFFFF',
					background: 'url(/legba/site/imgs/legendas.png) top',
					top: ($('#sb-body').position().top+$('#sb-body').height()-$('#sb-caption').height()+1) + 'px', //1 da borda
					width: ($('#sb-body').width()) + 1 + 'px'
				}).hide().slideDown('fast');
		}else{
			$('#sb-caption').hide('fast');
		}
	},
	onClose: function() {
		$('#sb-caption').hide();
	}


});

$(function() {
	/* Mala direta */
	$('#malaDireta form').validate({
		errorContainer: $('#malaDireta div.error'),
		errorLabelContainer: $("ul", $('#malaDireta div.error')),
		wrapper: 'li',
		submitHandler: function(form) {
			textoAnterior = $(form).html();
			$.ajax({
				type: "POST", cache: false,
				url: form.action, //ATENÇÃO, Para acabar de vez com problemas de acentuação, basta colocar Response.Charset="ISO-8859-1" nos asps e o jQuery navega lindamente.
				data: 'nome=' + escape($('#malaDireta input[name="nome"]').val()) + '&email=' + escape($('#malaDireta input[name="email"]').val()) + '&pasta=' + escape($('#malaDireta input[name="pasta"]').val()),
				success: function(html){
					$(form).removeClass('carregando').html($('#malaDiretaRetorno',html))
					.fadeTo(5000,1).queue(function() { $(this).html(textoAnterior); $(this).dequeue(); }); // O fadeTo é de mentira. Só pra contar o tempo.
				},
				error: function(){
					$(form).removeClass('carregando').html('<p class="fonte1 fVermelho">Erro no envio do cadastro!</p>')
					.fadeTo(5000,1).queue(function() { $(this).html(textoAnterior); $(this).dequeue(); }); // O fadeTo é de mentira. Só pra contar o tempo.
				}
			});
			$(form).html('').addClass('carregando');
		}
	});
});

function googleMap(obj) {
	var map = new GMap2(document.getElementById(obj.div));
	//Ponto
	var ponto = new GLatLng(obj.lat, obj.lng);

	var zoom = obj.zoom ? obj.zoom : 18;
	//centralizando o mapa
	map.setCenter(ponto,zoom);
	
	// controles no mapa
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());

	if (obj.marker != undefined) {
		var objIcon = new GIcon(G_DEFAULT_ICON);
		objIcon.image = obj.marker;
		objIcon.iconSize = new GSize(obj.mw, obj.mh);
		objIcon.shadow = obj.shadow;
		objIcon.shadowSize = new GSize(obj.sw, obj.sh);
		objIcon.iconAnchor = new GPoint(obj.iax, obj.iay);
		markerOptions = { icon: objIcon };
		var marker = new GMarker(ponto, markerOptions);
		map.addOverlay(marker);
	}
}



