function decodeHTML(e){
	var clean = e;
	var entities=[
				['&amp;','&'],
				['&nbsp;',' ']
			];
	for( var i=0, limit=entities.length; i < limit; ++i){
		clean = clean.replace( new RegExp(entities[i][0],"ig"), entities[i][1]);
	}
	return clean;
}


function carregaConteudo(url){
	var $ = jQuery.noConflict();
	url = document.location.toString().replace(/^.*#/, '');
	// exclui quando é feito um comentario
	if(url.indexOf('comment')<0 && url.indexOf('FSContact')<0) {
		$('#mascara').addClass('loader')
		$('#linha2').animate({ left: '-'+($('#linha2').width() + 30) },1000);
		$('#linha1').animate({ left: '-'+($('#linha1').width() + 30) },2000, function() {
			$.ajax({
				url: url,
				cache: true,
				success: function(html){
					title = html.substring(html.indexOf('<title>')+7,html.indexOf('</title>'));
					if (title != ""){
						document.title = decodeHTML(title);
					}
					$('#linha1').html($('#linha1',html).html());
					$('#linha2').html($('#linha2',html).html());
					executaScripts();
					urchinTracker(url);
					$('#linha1').animate({ left: '0' },2000);
					$('#linha2').animate({ left: '0' },1000);
					$('#mascara').removeClass('loader');
					Shadowbox.setup();
				}
			});
		});
	}
}

jQuery(function($) {
	// Inicializa o plugin history. A função CB é chamada uma vez com o location.hash atual
	$.historyInit(carregaConteudo, "/");
	executaScripts();

	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);
	});

});

function executaScripts(){
	var $ = jQuery.noConflict();
	$('#linha2 img').attr('alt','')
	$('#linha2 img').attr('title','')
	$('.menu-header a, .entry-title a, a.marca., .aviso a, a.link, #conteudoTitulo a, .internaClubinhodoJacare a, .internaClubinhodoJacareKidsClub a').unbind('click').click(function(){
		var url = $(this).attr('href').replace('http://' + document.location.host,'');
		$.historyLoad(url);
		return false;
	});
	$('#newsletter p:eq(0)').addClass("newsNome");
	$('#newsletter p:eq(1)').addClass("newsEmail");
	$('#newsletter p:eq(2)').addClass("newsEnviar");
	$('#newsletter').css('display','block');
	
	$('input[type=submit]').addClass("btEnviar");  
	$('input[type=text]').addClass("input");
	$('input[type=file]').addClass("inputArquivo");  

	/* Colorbox para todos os links de imagem e youtube */
//	$('a[href$="jpg"]').colorbox();
//	$('a[href*="youtube.com"]').colorbox({iframe:true, innerWidth:425, innerHeight:344});
	/** :hover effect on main menu**/
	$('.menu li:has("ul")')
		.addClass("parent-item")
		.mouseover( function(){ $(this).children('ul').css({ 'display' : 'block' }); })
		.mouseout( function(){ $(this).children('ul').css({ 'display' : 'none' }); });

	/* mapa na página de localização */
	if ($('#map').length > 0) mapaLocalizacao();
	
	/** Slideshow **/
	$(".carrosel .mask").scrollable({
		items: '.trilho',
		size: 4,
		speed: 400,
		clickable: false,
		keyboard: false,
		nextPage: '#btProximo',
		prevPage: '#btAnterior'
	});
	
	// animações
	$('#reservas img').bind("mouseenter mouseleave",function(event) {
		$(this).stop(true).animate({ backgroundPosition: (event.type=='mouseenter'?'-'+$(this).width():0)+'px' }, 300);
	});
	timerBG = setTimeout("trocaBG()",30000);
}

var timerBG = null;
var fotos = 11;
var fotoAtual = 1;
function trocaBG(){
	var $ = jQuery.noConflict();
	clearTimeout(timerBG)
	fotoAtual++;
	if(fotoAtual > fotos){
		fotoAtual = 1;
	}
	$('#cFoto .imagensFundo').append('<img width="1062" height="825" alt="" src="'+jsPath.replace('js/','')+'imgs/fotosBG/cFoto'+fotoAtual+'.jpg" style="display:none;">');
	$('#cFoto .imagensFundo img:last').load(function(){
		$('#cFoto .imagensFundo img:last').fadeIn('2000',function(){
			$('#cFoto .imagensFundo img:first').remove();
			timerBG = setTimeout("trocaBG()",30000);
		});
	});
}

function initialize() {
	var myLatlng = new google.maps.LatLng(-9.641796,-35.699393);
	var myOptions = {
		zoom: 16,
		center: myLatlng,
		marker: templatePath + "/imgs/marker.png",
		mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	var map = new google.maps.Map(document.getElementById("map"), myOptions);
	var marker = new google.maps.Marker({
		position: myLatlng, 
		map: map,
		icon: templatePath + "/imgs/marker.png",
		title:"Jatiúca Hotéis & Resort"
	}); 
}

function mapaLocalizacao() {
	var script = document.createElement("script");
	script.type = "text/javascript";
	script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=initialize";
	document.body.appendChild(script);
}


var blank = new Image();
blank.src = templatePath + '/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')";
}
