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 collapseMenu(menuId){
	var myMenuId = menuId;
	var mySpace = jq("#"+menuId).width();
	var myWidth = 0;
	var myChild = 0;
	jq("#"+menuId+" > li > a").each(function() { myWidth += jq(this).width(); myChild++; });
	if(myWidth > 0){
		var myPadding = Math.floor(((mySpace-myWidth)/myChild)/2);
		if(myWidth+(myPadding*2*myChild) <= mySpace){ jq("#"+menuId+" > li").each(function() {jq(this).css({"padding-left":myPadding, "padding-right":myPadding});});}
		else {setTimeout("collapseMenu('"+myMenuId+"')", 400);}
	} else {setTimeout("collapseMenu('"+myMenuId+"')", 400);}
}

var dropLanguage = {
	init: function(mydiv){
		jq('#'+ mydiv).css({"height":"20px", "overflow":"hidden"});
		jq('#'+ mydiv).hover( function(){jq(this).css({"height":"auto"});}, function(){jq(this).css({"height":"20px"});});
	}
}

var resetBox = {
	options:{ myBlock: "boxSearch", myHeight: 0, myLiHeight: 0, speed: 1000, rooms:1 },
	init: function(opts) {
		for (name in opts) resetBox.options[name] = opts[name];
		if(jq('#'+ resetBox.options.myBlock).length){
			resetBox.options.myHeight = jq('#'+ resetBox.options.myBlock).height();
			//resetBox.options.myLiHeight = jq('#contOption').innerHeight();
			resetBox.options.myLiHeight = 55;
			
			if(resetBox.options.rooms > 1){
				resetBox.options.myLiHeight = jq('#contOption').innerHeight()/resetBox.options.rooms;
				var newHeight = resetBox.options.myHeight + (resetBox.options.myLiHeight*(resetBox.options.rooms*1));
				jq('#'+ resetBox.options.myBlock).animate({height: newHeight}, 0);
			}
			jq('#numList').change(function(){ resetBox.set(jq(this).val()); });
		} else alert("attenzione: "+ resetBox.options.myBlock +" non esiste.");
	},
	set: function(myValue){
		myValuePass = parseInt(myValue);
		if(myValue > 1) myValue++;
		var heightB = resetBox.options.myHeight + (resetBox.options.myLiHeight*(myValue*1-1));
		var onFinish = true;
		if(jq('#'+ resetBox.options.myBlock).height() < heightB){ setRoom(myValuePass, name_adults, name_children, name_childage, name_room, name_rooms); onFinish = false; }
		if(onFinish){ jq('#'+ resetBox.options.myBlock).animate({height: heightB}, resetBox.options.speed, function(){
			setRoom(myValuePass, name_adults, name_children, name_childage, name_room, name_rooms);});
		} else { jq('#'+ resetBox.options.myBlock).animate({height: heightB}, resetBox.options.speed); }
	},
	add: function(myValue){
		myValuePass = parseInt(myValue);
		var heightB = resetBox.options.myHeight + (resetBox.options.myLiHeight*(myValue*1));
		jq('#'+ resetBox.options.myBlock).animate({height: heightB}, 400);
	}
}

var resetBoxOff = {
	options:{ myBlock: "search2", myHeight: 0, myLiHeight: 0, speed: 1000, rooms:1 },
	init: function(opts) {
		for (name in opts) resetBoxOff.options[name] = opts[name];
		if(jq('#'+ resetBoxOff.options.myBlock).length){
			resetBoxOff.options.myHeight = jq('#'+ resetBoxOff.options.myBlock).height();
			resetBoxOff.options.myLiHeight = jq('#liOption2').innerHeight();
			
			if(resetBoxOff.options.rooms > 1){
				resetBoxOff.options.myLiHeight = jq('#liOption2').innerHeight()/resetBoxOff.options.rooms;
				var newHeight = resetBoxOff.options.myHeight + (resetBoxOff.options.myLiHeight*(resetBoxOff.options.rooms*1));
				jq('#'+ resetBoxOff.options.myBlock).animate({height: newHeight}, 0);
			}
			jq('#numList2').change(function(){ resetBoxOff.set(jq(this).val()); });
		} else alert("attenzione: "+ resetBoxOff.options.myBlock +" non esiste.");
	},
	set: function(myValue){
		myValuePass = parseInt(myValue);
		if(myValuePass > 1){
			var heightB = resetBoxOff.options.myHeight + (resetBoxOff.options.myLiHeight*(myValue*1));
		} else {
			var heightB = resetBoxOff.options.myHeight;
		}
		var onFinish = true;
		if(jq('#'+ resetBoxOff.options.myBlock).height() < heightB){ setRoom(myValuePass, name_adults, name_children, name_childage, name_room, name_rooms, 'liOption2'); onFinish = false; }
		if(onFinish){ jq('#'+ resetBoxOff.options.myBlock).animate({height: heightB}, resetBoxOff.options.speed, function(){
			setRoom(myValuePass, name_adults, name_children, name_childage, name_room, name_rooms, 'liOption2');});
		} else { jq('#'+ resetBoxOff.options.myBlock).animate({height: heightB}, resetBoxOff.options.speed); }
	},
	add: function(myValue){
		myValuePass = parseInt(myValue);
		var heightB = resetBoxOff.options.myHeight + (resetBoxOff.options.myLiHeight*(myValue*1));
		jq('#'+ resetBoxOff.options.myBlock).animate({height: heightB}, 0);
	}
}

function setChildAge(nchild, chdid, roomn, label_children_age){
	var selectHtml = "";
	if(nchild > 0){
		if(jq('#numList2').val() == 1){
			resetBoxOff.add(1);
		}
		if(jq('#numList').val() == 1){
			resetBox.add(1);
		}
		selectHtml = '<label>'+ label_children_age +'</label>';
		for (var cc=1; cc<=nchild; cc++) {
			selectHtml =  selectHtml + '<select name="bform[reqRooms]['+roomn+'][childAge][' + cc + ']" class="selectSmall">' + '<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>';
		}
	} else {
		if(jq('#numList2').val() == 1){
			resetBoxOff.add(0);
		}
		if(jq('#numList').val() == 1){
			resetBox.add(0);
		}
	}
	if(document.getElementById(chdid)){
		document.getElementById(chdid).innerHTML = selectHtml;
	}
}

function createSelect(num, init, label, labels, selected){
	if(label != "")	label = " "+ label;
	labels = (labels != "")? " "+ 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>';
		} else {
			mySel = mySel + "\n" + '<option value="'+ k +'">'+ k + label +'</option>';
		}
	}
	return mySel;
}

function setRoom(nrooms, label_adults, label_children, label_children_age, label_room, label_rooms, myCont){
	if(myCont == undefined) myCont = 'contOption';
	var myPrefix = '';
	if(myCont == 'liOption2') myPrefix = 'off_';
	var myResults = "";
	myResults = myResults + '<ul>\n';
	for (var i=1; i<=nrooms; i++){
		if ( nrooms > 1){
			myResults = myResults + '	<li><strong>'+ label_room +' '+ i +'</strong>\n';
		} else {
			myResults = myResults + '	<li>\n';
		}
		myResults = myResults + '		<span>\n'+
		'			<label>'+ label_adults +'</label>\n'+
		'			<select name="bform[reqRooms]['+ i +'][adults]" class="selectSmall">\n'+
		'			'+ createSelect(6, 1, "", "", 2) + 
		'			</select>\n'+
		'		</span>\n'+
		'		<span>\n'+
		'			<label>'+ label_children +'</label>\n'+
		'			<select name="bform[reqRooms]['+ i +'][child]" class="selectSmall" onchange="setChildAge(this.value,\''+ myPrefix +'chdAge_'+ i +'\', '+ i +',\''+ label_children_age +'\');">\n'+
		'			'+ createSelect(4, 0, "", "", 0) + 
		'			</select>\n'+
		'		</span>\n'+
		'		<span class="childAge" id="'+ myPrefix +'chdAge_'+ i +'"></span>\n'+
		'	</li>\n';
	}
	myResults = myResults + '</ul>\n';
	if (document.getElementById(myCont)){
		document.getElementById(myCont).innerHTML = myResults;
	} else {
		alert(myCont +" => blocco non valido");
	}
}

function getHostname(str) {
	var re = new RegExp('^(?:f|ht)tp(?:s)?\://([^/]+)', 'im');
	return str.match(re)[1].toString();
}

/*******************************************************   shadowbox Seo */

function shadowRel(){
    jq('a.shadowBox').each(function() {
        jq(this).attr('rel', 'shadowbox['+ jq(this).attr('class').replace(/ /g, '_') +']');
    });
}

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

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

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