
/*******************************************************    FUNZIONE BASE64 PER CONTENUTI JAVASCRIPT */

var jbase64 = {
 _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
 encode : function (input) {
  var output = "";
  var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
  var i = 0;
  input = jbase64._utf8_encode(input);
  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 = output +
   this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
   this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
  }
  return output;
 },
 decode : function (input) {
  var output = "";
  var chr1, chr2, chr3;
  var enc1, enc2, enc3, enc4;
  var i = 0;
  input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  while (i < input.length) {
   enc1 = this._keyStr.indexOf(input.charAt(i++));
   enc2 = this._keyStr.indexOf(input.charAt(i++));
   enc3 = this._keyStr.indexOf(input.charAt(i++));
   enc4 = this._keyStr.indexOf(input.charAt(i++));
   chr1 = (enc1 << 2) | (enc2 >> 4);
   chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
   chr3 = ((enc3 & 3) << 6) | enc4;
   output = output + String.fromCharCode(chr1);
   if (enc3 != 64) {
    output = output + String.fromCharCode(chr2);
   }
   if (enc4 != 64) {
    output = output + String.fromCharCode(chr3);
   }
  }
  output = jbase64._utf8_decode(output);
  return output;
 },
 _utf8_encode : function (string) {
  string = string.replace(/\r\n/g,"\n");
  var utftext = "";
  for (var n = 0; n < string.length; n++) {
   var c = string.charCodeAt(n);
   if (c < 128) utftext += String.fromCharCode(c);
   else if((c > 127) && (c < 2048)) {
    utftext += String.fromCharCode((c >> 6) | 192);
    utftext += String.fromCharCode((c & 63) | 128);
   } else {
    utftext += String.fromCharCode((c >> 12) | 224);
    utftext += String.fromCharCode(((c >> 6) & 63) | 128);
    utftext += String.fromCharCode((c & 63) | 128);
   }
  }
  return utftext;
 },
 _utf8_decode : function (utftext) {
  var string = "";
  var i = 0;
  var c = c1 = c2 = 0;
  while ( i < utftext.length ) {
   c = utftext.charCodeAt(i);
   if (c < 128) {
    string += String.fromCharCode(c);
    i++;
   } else if((c > 191) && (c < 224)) {
    c2 = utftext.charCodeAt(i+1);
    string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
    i += 2;
   } else {
    c2 = utftext.charCodeAt(i+1);
    c3 = utftext.charCodeAt(i+2);
    string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
    i += 3;
   }
  }
  return string;
 }
}






/*******************************************************    */
function setChildAge(nchild, chdid, roomn, label_children_age){
	//alert("ok " + nchild + " " + chdid + " " +roomn);
	var selectHtml = "";
	if(nchild > 0){
		selectHtml = '<label class="age">'+ label_children_age +'</label>';
		for (var cc=1; cc<=nchild; cc++) {
			selectHtml =  selectHtml + '<select name="bform[reqRooms]['+roomn+'][childAge][' + cc + ']">' +
                '<option value="0">&lsaquo;1 </option>';
				for(var k=1; k<=18; k++){
					selectHtml =  selectHtml + "\n" + '<option value="'+ k +'">'+ k +'</option>';
				}
				selectHtml =  selectHtml + '</select>';
		}
	}
	if(document.getElementById(chdid)){
		document.getElementById(chdid).innerHTML = selectHtml;
	}

}

function createSelect(num, init, label, selected){
	if(label!=""){
		label = " "+ label;
	}
	var mySel = "";
	for(var k=init; k<=num; k++){
		if (selected != 0 && selected == k){
			mySel = mySel + "\n" + '<option value="'+ k +'" selected="selected">'+ k + label +'</option>';
		} else {
			mySel = mySel + "\n" + '<option value="'+ k +'">'+ k + label +'</option>';
		}
	}
	return mySel;
}

function setRoom(nrooms, label_adults, label_children, label_children_age, label_room){
	var myResults = "";
	for (var i=1; i<=nrooms; i++){
		myResults = myResults + '';
		if ( nrooms >= 1){
			myResults = myResults + '<div id="optionSearch_'+i+'">';
		}
		myResults = myResults + 
		'		<div class="titCamera">'+label_room+' '+i+'</div>\n'+
		'		<div class="select">\n'+		
		'		<label>'+ label_adults +'</label>\n'+
		'		<select name="bform[reqRooms]['+ i +'][adults]" id="bform[reqRooms]['+ i +'][adults]">\n'+
		'			'+ createSelect(6, 1, "", 1) + 

		'		</select>\n'+
		'		</div>'+
		'		<div class="select">\n'+
		'		<label>'+ label_children +'</label>\n'+
		'		<select name="bform[reqRooms]['+ i +'][child]" id="bform[reqRooms]['+ i +'][child]" onchange="setChildAge(this.value,\'chdAge_'+ i +'\', '+ i +',\''+ label_children_age +'\');">\n'+
		'			'+ createSelect(3, 0, "", 0) + 
		'		</select>\n'+
		'		</div>'+
		'		<div id="chdAge_'+ i +'" class="child">\n'+
		'		</div>\n'+
		'		</div>\n';
	}
	document.getElementById('optionSearch').innerHTML = myResults;
	$('.spacerBooking').css('marginTop', 90*nrooms+'px');
	
	if(nrooms == 1) {
		$('.titCamera').remove();				// rimuovo il titolo della camera 1
	}
}




/******************************************************** FUNZIONI FORM OFFERTA SINGOLA */

function setRoomHomeOffer(nrooms, label_adults, label_children, label_children_age, label_room, element) {
	var form = element.parents('form');
	var myResults = '';
	
	if (nrooms > 1)	// se viene scelta pił di una camera...
	{
		for (var i=2; i<=nrooms; i++)	// preparo la stringa da accodare alla prima camera, contenente i campi necessari
		{
			myResults = myResults + '<li class="room'+ i +' roomTitle">'+ label_room +' '+ i +'</li>';
			
			myResults = myResults +	'<li class="room'+ i +' adults">'+
										'<label>'+ label_adults +'</label>'+
										'<select name="bform[reqRooms]['+ i +'][adults]">'+
											createSelectOffer(6, 1, label_adults, "", 2)+
										'</select>'+
									'</li>'+
									'<li class="room'+ i +' children">'+
										'<label>'+ label_children +'</label>'+
										'<select name="bform[reqRooms]['+ i +'][child]" onchange="setChildAgeOffer(this.value,\'chdAge_'+ i +'\', '+ i +',\''+ label_children_age +'\', $(this));">'+
											createSelectOffer(4, 0, label_children, "", 0)+
										'</select>'+
									'</li>'+
									'<li class="room'+ i +' childAge chdAge_'+ i +'"></li>';
		}
		
		$('li.room1.roomTitle', form).remove();				// rimuovo il titolo della camera 1 (per evitare duplicazioni nel caso sia gią presente: vedi istruzione successiva)
		$('li.room1:first', form).before('<li class="room1 roomTitle">'+ label_room +' 1</li>');		// aggiungo il titolo della camera 1, prima del primo campo della camera 1
		$('li.room1:last', form).nextAll().remove();		// rimuovo tutti gli elementi dopo l'ultimo elemento della camera 1
		$('li.room1:last', form).after(myResults);			// aggiungo dopo l'ultimo elemento della camera 1, la stringa preparata in precedenza
	}
	else			// se viene scelta una sola camera...
	{
		$('li.room1.roomTitle', form).remove();				// rimuovo il titolo della camera 1
		$('li.room1:last', form).nextAll().remove();		// rimuovo tutti gli elementi dopo l'ultimo elemento della camera 1
	}
}


function setChildAgeOffer(nchild, chdid, roomn, label_children_age, element){
	var form = element.parents('form');
	var selectHtml = "";
	
	if(nchild > 0){
		selectHtml = '<label>'+ label_children_age +'</label>';
		for (var cc=1; cc<=nchild; cc++) {
			selectHtml =  selectHtml + '<select name="bform[reqRooms]['+roomn+'][childAge][' + cc + ']">' +
                '<option value="0">&lsaquo; 1</option>';
				for(var k=1; k<=18; k++){
					k_opt = k<10 ? "&nbsp;"+ k : k;
					selectHtml =  selectHtml + "\n" + '<option value="'+ k +'">'+ k_opt +'</option>';
				}
				selectHtml =  selectHtml + '</select>';
		}
	}

	if($('.'+chdid, form)){
		$('.'+chdid, form).html(selectHtml);
	}
}


function createSelectOffer(num, init, label, labels, selected){
	if(label != ""){
		label = " "+ label;
	}
	if(labels != ""){
		labels = " "+ labels;
	} else {
		labels = label;
	}
	var mySel = "";
	for(var k=init; k<=num; k++){
		if(k!=init) label = labels;
		if (selected != 0 && selected == k){
			//mySel = mySel + "\n" + '<option value="'+ k +'" selected="selected">'+ k + label +'</option>';	// numero + nome
			mySel = mySel + "\n" + '<option value="'+ k +'" selected="selected">'+ k + '</option>';				// solo numero
		} else {
			//mySel = mySel + "\n" + '<option value="'+ k +'">'+ k + label +'</option>';	// numero + nome
			mySel = mySel + "\n" + '<option value="'+ k +'">'+ k + '</option>';				// solo numero
		}
	}
	return mySel;
}


/******************************************************* BOX OFFERTE HOMEPAGE */


function newsInit(newsToDisplay,newsListId) {
	
	

	
	newsToDisplay = (typeof(newsToDisplay) != 'undefined')? newsToDisplay : 1;
	
	var list = $('li','#'+newsListId);

	if(list.length > newsToDisplay) {
		setInterval ( "newsScroll('"+newsListId+"')", 6000);
	}
}

function newsScroll(container) {
	
	if(container=='eventsListHome') { /*******************************************************  Scroll box offerte  */
		
		var myHeight = $('#eventsListHome > li:first').outerHeight();
		var myItems = $('> li:first','#eventsListHome');
		//console.log(myHeight);
		
		myItems.clone().insertAfter('#eventsListHome > li:last');
		myItems.animate(			
			{opacity:0,
			marginTop: '-'+myHeight+'px'
			},
			700,
			'',
			function(){				
				$(this).remove();
				$('#eventsListHome  > li').removeAttr('style');
			});
		
	} 

}

/*******************************************************   LINKS IN JAVASCRIPT */

function goToUrl(enUrl){
	
	var decUrl = jbase64.decode(enUrl);
	location.href=''+decUrl+'';	
}

/*******************************************************  SCROLL EVENTI IN HOME */
function initScrollzEventi() {
	
	var pausecontent2=new Array();
    var boxEventi = document.getElementById('tickerEventiTemp');
    var pEvento =  boxEventi.getElementsByTagName('span');
    var counter = 0;

    for(var j=0;j<pEvento.length;j+=2) {
        pausecontent2[counter] = '<a href="javascript:;">'+pEvento[j].innerHTML+'</a><small>' + pEvento[j+1].innerHTML.substr(0,80) + '&hellip;</small>';
        counter++;
    }

	boxEventi.style.display = 'none';
    
    new createListEvents(pausecontent2, "tickerEventi", "someclass");

}

function createListEvents(content, divId, divClass){
	this.content=content //message array content
	this.tickerid=divId //ID of ticker div to display information
	//this.delay=delay //Delay between msg change, in miliseconds.
	this.mouseoverBol=0 //Boolean to indicate whether mouse is currently over scroller (and pause it if it is)
	this.hiddendivpointer=1//index of message array for hidden div
	document.write('<div id="'+divId+'" >');
	document.write('<div id="wrapEventi" >');
	document.write('<ul id="eventsListHome">');
	for(var j=0;j<content.length;j+=2) {
		document.write('<li onclick="openEvents(); return false;">'+content[j]+'<span class="sep"></span></li>');
	}
	document.write('</ul></div></div>');
}

function openEvents() {
	Shadowbox.open({
        content:    '/assets/eventi.php?lg='+myLang+'',
        player:     "iframe",
		title:		"Jesolo Events",
        height:     800,
        width:      600
    });

}


/*******************************************************    */
/*******************************************************    */
/*******************************************************    */

$(window).load(function() {
		newsInit(2,'eventsListHome');				
});

$(document).ready(function() {
if($('img','#slideShow').length > 1) { // Visual Homepage
		$('#slideShow').cycle({ 
			fx:      'fade', 
			speed:   500,
			timeout:  2000 
		});
	}

});



