function goToUrl(url) {
	location.href=''+url+'';
}

function setRoom2(nrooms,label_adults,label_children,label_children_age,label_room){
	//cancello le righe e le ricreo
	for (cc=2;cc<=10;cc=cc+1) 
	{
		$('.r'+cc).remove();
		$('#childlabel'+cc).remove();
	}
	
	var roomsHtml = '';
	
	if(nrooms > 1)
	{
		for (cc=2;cc<=nrooms;cc=cc+1) 
		{
			var roomsHtml =  roomsHtml +
			'<tr class="r'+cc+'" >' +
			'<td colspan="2"><div class="roomlabel">'+label_room+' '+cc+'</div></td>' +
			'</tr>' +
			'<tr class="r'+cc+'" >' +
			'<th scope="row">'+label_adults+'</th>' +
			'<td><select name="bform[reqRooms]['+cc+'][adults]" >' +
			'<option value="1">1</option>' +
			'<option value="2" selected="selected" >2</option>' +
			'<option value="3">3</option>' +
			'<option value="4">4</option>' +
			'<option value="5">5</option>' +
			'<option value="6">6</option>' +
			'</select>' +
			'</td>' +
			' </tr>' +
			'<tr class="r'+cc+'" >' +
			'<th scope="row">'+label_children+'' +
			'</th>' +
			'<td><select name="bform[reqRooms]['+cc+'][child]" onchange="setChildAge2(this.value,\''+cc+'\')" >' +
			' <option value="0">0</option>' +
			' <option value="1">1</option>' +
			' <option value="2">2</option>' +
			' <option value="3">3</option>' +
			' <option value="4">4</option>' +
			' <option value="5">5</option>' +
			' <option value="6">6</option>' +
			'</select>' +
			'</td>' +
			' </tr>' +
			'<tr id="childlabel'+cc+'">' +
			'<th scope="row">'+label_children_age+'</th>' +
			'<td><div id="childage'+cc+'"></div></td>'
			'</tr>';
		}
		
		$('#lastroom').after( roomsHtml ).show();
		
		for (cx=2;cx<=nrooms;cx=cx+1) 
		{
			$('#childlabel'+cx).css( 'display', 'none' );
			$('#childage'+cx).html( '' );
		}
		
		$('div.roomlabel').css( 'display', 'block' );
		$('.spazio').css( 'height', '2px' );
	}
	else
	{
		$('div.roomlabel').css( 'display', 'none' );
		$('.spazio').css( 'height', '13px' );
	}
	
}

function setChildAge2(nchild,roomn){
	if(nchild > 0)
	{
		var selectHtml = '';
		var cc = 1;
		
		for (cc=1;cc<=nchild;cc=cc+1) 
		{
			var selectHtml =  selectHtml + '<select name="bform[reqRooms]['+roomn+'][childAge][' + cc + ']"  class="childage {validate:{required:true, messages:{required:\'\'}}}" >' +
			'<option value="">-</option>' +
			'<option value="0">&lt;1</option>' +
			'<option value="1">1</option>' +
			'<option value="2">2</option>' +
			'<option value="3">3</option>' +
			'<option value="4">4</option>' +
			'<option value="5">5</option>' +
			'<option value="6">6</option>' +
			'<option value="7">7</option>' +
			'<option value="8">8</option>' +
			'<option value="9">9</option>' +
			'<option value="10">10</option>' +
			'<option value="11">11</option>' +
			'<option value="12">12</option>' +
			'<option value="13">13</option>' +
			'<option value="14">14</option>' +
			'<option value="15">15</option>' +
			'<option value="16">16</option>' +
			'<option value="17">17</option>' +
			'<option value="18">18</option>' +
			'</select>';
		}
		
		$('#childage'+roomn).html( selectHtml ).show();
		$('#childlabel'+roomn).removeAttr( 'style' );
		//$('#search').validate();
	}
	else
	{
		$('#childage'+roomn).html( '' );
		$('#childlabel'+roomn).css( 'display', 'none' );
		//$('#search').validate();
	}
}

function showChildrenAge(n) {
	n = parseInt(n);
	if (n>0) {
		$('label[for=eta_bambino_1]').show();
	
		for(i=1;i<=n;i++) {
			$('select[name=eta_bambino_'+i+']').show();
		}
		for(j=n+1;j<=6;j++) {
			$('select[name=eta_bambino_'+j+']').hide();
		}
	} else {
		$('label[for=eta_bambino_1]').hide();
		$('select[name^=eta_bambino]').hide();
	}
}

function initialize() {
	var myLatlng = new google.maps.LatLng(46.5656351,12.6832999);
	var myCenterLatlng = new google.maps.LatLng(46.524000,12.680315);
	var myOptions = {
	  zoom: 15,
	  center: myCenterLatlng,
	  mapTypeId: google.maps.MapTypeId.HYBRID 
	}

	var map = new google.maps.Map(document.getElementById("map"), myOptions);

	var contentString = '<div id="info_window">'+
		'<a href="http://www.sappadadolomiti.com/" class="title">Sappada Dolomiti</a>'+
		'<div id="info_window_content">' +
		'<p>Borgata Bach, 9<br />' +
		'32047 - Sappada (BL)<br />'+
		'Tel 0435 428343<br />'+
		'<a href="http://www.sappadadolomiti.com/">www.sappadadolomiti.com</a><br /><br />'+
		'<a href="http://maps.google.it/maps?f=d&hl=it&geocode=18388385182670618452,46.5656351,12.6832999&saddr=&daddr=Borgata+Bach,+9,+32047+Sappada+(BL)+(Sappada)&mra=pe&mrcr=0&sll=46.5656351,12.6832999&sspn=0.337028,0.6427&ie=UTF8&z=16" target="_blank">Calcola il percorso</a>'+
		'</p>'+
		'</div>'+
		'</div>';
		
	var infowindow = new google.maps.InfoWindow({
		content: contentString,
		size: new google.maps.Size(50,50),
		position: myLatlng
	});
	
	var marker = new google.maps.Marker({
		position: myLatlng,
		map: map,
		title: 'Sappada'
	});
	google.maps.event.addListener(marker, 'click', function() {
	  infowindow.open(map,marker);
	});
	infowindow.open(map);
}
  
function urldecode (str) {
    // Decodes URL-encoded string  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/urldecode
    // +   original by: Philip Peterson
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: AJ
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // +      input by: travc
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Lars Fischer
    // +      input by: Ratheous
    // +   improved by: Orlando
    // %        note 1: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // *     example 1: urldecode('Kevin+van+Zonneveld%21');
    // *     returns 1: 'Kevin van Zonneveld!'
    // *     example 2: urldecode('http%3A%2F%2Fkevin.vanzonneveld.net%2F');
    // *     returns 2: 'http://kevin.vanzonneveld.net/'
    // *     example 3: urldecode('http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a');
    // *     returns 3: 'http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a'
    
    var hash_map = {}, ret = str.toString(), unicodeStr='', hexEscStr='';
    
    var replacer = function (search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The hash_map is identical to the one in urlencode.
    hash_map["'"]   = '%27';
    hash_map['(']   = '%28';
    hash_map[')']   = '%29';
    hash_map['*']   = '%2A';
    hash_map['~']   = '%7E';
    hash_map['!']   = '%21';
    hash_map['%20'] = '+';
    hash_map['\u00DC'] = '%DC';
    hash_map['\u00FC'] = '%FC';
    hash_map['\u00C4'] = '%D4';
    hash_map['\u00E4'] = '%E4';
    hash_map['\u00D6'] = '%D6';
    hash_map['\u00F6'] = '%F6';
    hash_map['\u00DF'] = '%DF';
    hash_map['\u20AC'] = '%80';
    hash_map['\u0081'] = '%81';
    hash_map['\u201A'] = '%82';
    hash_map['\u0192'] = '%83';
    hash_map['\u201E'] = '%84';
    hash_map['\u2026'] = '%85';
    hash_map['\u2020'] = '%86';
    hash_map['\u2021'] = '%87';
    hash_map['\u02C6'] = '%88';
    hash_map['\u2030'] = '%89';
    hash_map['\u0160'] = '%8A';
    hash_map['\u2039'] = '%8B';
    hash_map['\u0152'] = '%8C';
    hash_map['\u008D'] = '%8D';
    hash_map['\u017D'] = '%8E';
    hash_map['\u008F'] = '%8F';
    hash_map['\u0090'] = '%90';
    hash_map['\u2018'] = '%91';
    hash_map['\u2019'] = '%92';
    hash_map['\u201C'] = '%93';
    hash_map['\u201D'] = '%94';
    hash_map['\u2022'] = '%95';
    hash_map['\u2013'] = '%96';
    hash_map['\u2014'] = '%97';
    hash_map['\u02DC'] = '%98';
    hash_map['\u2122'] = '%99';
    hash_map['\u0161'] = '%9A';
    hash_map['\u203A'] = '%9B';
    hash_map['\u0153'] = '%9C';
    hash_map['\u009D'] = '%9D';
    hash_map['\u017E'] = '%9E';
    hash_map['\u0178'] = '%9F';
    hash_map['\u00C6'] = '%C3%86';
    hash_map['\u00D8'] = '%C3%98';
    hash_map['\u00C5'] = '%C3%85';

    for (unicodeStr in hash_map) {
        hexEscStr = hash_map[unicodeStr]; // Switch order when decoding
        ret = replacer(hexEscStr, unicodeStr, ret); // Custom replace. No regexing
    }
    
    // End with decodeURIComponent, which most resembles PHP's encoding functions
    ret = decodeURIComponent(ret);

    return ret;
}

////////////////////////////////////////////////////////////////////////////////
function attachInfoBox(marker, text, myMap, url, offsetTop) {
	google.maps.event.addListener(marker, "mouseover", function() {
		infoBox = new InfoBox({latlng:this.position, map: maps, html: text, offsetV: offsetTop, offsetH: -110, height: 90, width: 220});
		this.setZIndex(markerZIndex + 1);
		markerZIndex++;
	});
	google.maps.event.addListener(marker, "mouseout", function() { infoBox.setMap(null); });
	google.maps.event.addListener(marker, "click", function() { parent.location.href = url; });
}

function attachURL(marker, url) {
	google.maps.event.addListener(marker, "click", function() { parent.location.href = url; });
}

function loadStructure() {
	var markerBounds = new google.maps.LatLngBounds();
	infoBox = new BFInfoWindow();
	var myOptions = { mapTypeId: google.maps.MapTypeId.SATELLITE }
	maps = new google.maps.Map(document.getElementById("mapStructures"), myOptions);
	var fluster = new Fluster2(maps);
	
	for (var i=0; i<HotelPoint.length; i++){
		var marker = new google.maps.Marker({
			position: new google.maps.LatLng(parseFloat(HotelPoint[i]['lat']), parseFloat(HotelPoint[i]['lon'])),
			icon: HotelPoint[i]['icon'],
			title: HotelPoint[i]['title'],
			zIndex: markerZIndex
		});
		if(HotelPoint[i]['description'] != "" || HotelPoint[i]['photo'] != "") {
			var infoBoxText = "";
			infoBoxText = '<div class="hotel"><h2>'+ HotelPoint[i]['title'] +'</h3><p><image src="'+ HotelPoint[i]['photo'] +'" border="0" alt="" />'+ HotelPoint[i]['description'] +'</p></div>';
			attachInfoBox(marker, infoBoxText, maps, HotelPoint[i]['href'], -120);
		} else if(HotelPoint[i]['href'] != "" && HotelPoint[i]['href'].substr(0,4) != "java") {
			attachURL(marker, HotelPoint[i]['href']);
		}
		markerZIndex++;
		markerBounds.extend(new google.maps.LatLng(parseFloat(HotelPoint[i]['lat']), parseFloat(HotelPoint[i]['lon'])));
		fluster.addMarker(marker);
	}
	fluster.initialize();
	maps.fitBounds(markerBounds);
	google.maps.event.addListener(maps, 'zoom_changed', function() {
		if(changeZoom == false){
			changeZoom = true;
			if(HotelPoint.length == 1 && maps.getZoom()>11) maps.setZoom(11);
			//if(HotelPoint.length > 3) maps.setZoom(maps.getZoom()-1);
		} 
	});
	fluster.styles = {
		0: {
			image: '/images/iconMinisite.png',
			imageHover: '/images/iconMinisite.png',
			textColor: '#FFFFFF',
			width: 23,
			paddingRight: 2,
			height: 25,
			paddingTop: 2,
			lineHeight: 16
		}
	};
}

/*******************************************************   TABS */

$(function () {
	var tabContainers = $('div#tabs > form');
	tabContainers.hide().filter(':first').show();
	
	$('div#tabs ul#top a').click(function () {
		tabContainers.hide();
		tabContainers.filter(this.hash).show();
		$('div#tabs ul#top a').removeClass('selected');
		$(this).addClass('selected');
		return false;
	}).filter(':first').click();
});

/*******************************************************   COMFORT SELECT */
$('#comfortHotel').stop(true).animate({
	width:"134px",
	height:"12px",
	opacity: 0
}, 0);
$('#comfortHotel').css({"display":"none", "overflow":"hidden"});

var comfort = {
	options:{
		heightComfort: 0,
		myText: ""
	},
	init: function(opts) {
		for (name in opts) comfort.options[name] = opts[name];
		if((/MSIE (\d+\.\d+);/.test(navigator.userAgent))) $('#comfortHotel').css('background', 'none');
		$('#comfortHotel').stop(true).animate({
			width:"134px",
			height:"12px",
			opacity: 0
		}, 0);
		$('#comfortHotel').css({"display":"none", "overflow":"hidden"});
		$('#comfortHotelCheck li input').click(function() {
			if(!$(this).attr("checked") && $(this).attr("id") != "checkAll"){
				$('#checkAll').removeAttr("checked");
			} else if($(this).attr("checked") && $(this).attr("id") != "checkAll"){
				var selAll = true;
				$('#comfortHotelCheck li input').each(function() {
					if(!$(this).attr("checked")){
						if($(this).attr("id") != "checkAll") selAll = false;
					};
				});
				if(selAll) $('#checkAll').attr("checked", "checked");
			}
		});
		$('#checkAll').click(function() {
			if($(this).attr("checked")){
				comfort.checkAll();
			} else {
				comfort.uncheckAll();
			}
		});
		$('#selComfort').click(function() {
			$('#selComfort').blur();
			$('#comfortHotel').css({"display":"block", "overflow":"hidden"});
			$('#comfortHotel').stop(true).animate({
				width:"270px",
				height:$('#comfortHotel ul').height()+11,
				opacity: 1
			}, 500);
		});
		$('#selComfort').focus(function() {
			$('#selComfort').blur();
			$('#comfortHotel').css({"display":"block", "overflow":"hidden"});
			$('#comfortHotel').stop(true).animate({
				width:"270px",
				height:$('#comfortHotel ul').height()+11,
				opacity: 1
			}, 500);
		});
		$('#selComfort').focus(function() {$('#selComfort').blur(); $('#comfortHotel').css({"display":"block"}); });
	},
	apply: function(){
		var myAttributeChecked = "";
		$('#comfortHotelCheck li').each(function() {
			if($(this).children("input").attr("checked") && $(this).children("input").attr("id") != "checkAll"){
				if(myAttributeChecked != "") myAttributeChecked = myAttributeChecked + ", ";
				myAttributeChecked = myAttributeChecked + $(this).children("label").html();
			};
		});
		if(myAttributeChecked != "") {
			$('#selComfort').attr("value", myAttributeChecked);
		} else {
			$('#selComfort').attr("value", comfort.options.myText);
		}
		$('#comfortHotel').animate({
			width:"134px",
			height:"12px",
			opacity: 0
		}, 500, function(){
			$(this).css({"display":"none"});
		});
	},
	checkAll: function(){
		$('#comfortHotelCheck input').each(function() {
			$(this).attr("checked", "checked");
		});
	},
	uncheckAll: function(){
		$('#comfortHotelCheck input').each(function() {
			$(this).removeAttr("checked");
		});
	}
}

// Funzione per l'inserimento della shadowbox per webcam
function shadowRelWebcam(){
	$('a.shadowBoxWebcam').each(function() {
		$(this).attr('rel', 'shadowbox['+ $(this).attr('class').replace(/ /g, '_') +']width=640;height=480');
	});
}

// Funzione per slideShow immagini nel visual
function slideShow() {

  	var active = $('#contGalJs img:first');
	
	active
		.remove()
		.insertAfter('#contGalJs img:last')
		.animate({opacity: 1.0}, 1000, function() {
			$(this).siblings().removeAttr('style');
		});
		
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////

$(document).ready(function(){
	
	shadowRelWebcam();
	
	// Init slideShow 
	slideShow();
	$("#contGalJs").css("background-image", "none");
	setInterval( "slideShow()", 5000 );
	
	/*================================== ACCORDION */
	$("#accordion").accordion({ header: 'li a.title', active:false, navigation:true, autoHeight:false });
	$("#sectionsExcursion").accordion({ header: 'span.excursionTitle', active:false, navigation:true, autoHeight:false });
	
	// Funzione Slide webcam ////////////////
	var lista = $('#webcamCont ul');
	var lista_li = $('#webcamCont ul li img').length-1;
	var move = 0;
	
	$('#nextCam').click(function(){ 
		if ((move < lista_li) && (!lista.is(':animated'))) {
			lista.stop(true).animate({left:'+=-137px'});
			move++;
		}
	});
	
	$('#prevCam').click(function(){
		if ((move > 0) && (!lista.is(':animated'))) {
			lista.stop(true).animate({left:'+=137px'});
			move--;
		}
		
	});	
});
