function setCalendarNews(siteId, iso3){
	$("a.tenback").click(function(){
		$("#eventsContainer").fadeOut(300);
		var newtime = $(this).attr("id").replace('tenback-','');
		$.ajax({
			type: "GET",
			url: "/assets/eventList.php",
			data: "language=it&newtime="+newtime+"&siteID="+ siteId +"&iso3="+ iso3,
			success: function(myList){
				$("#eventsContainer").remove();
				$("#boxEventsHome").append(myList);
				$("#eventsContainer").fadeOut(0);
				$("#eventsContainer").fadeIn(300);
			}
		});
	});
	
	$("a.tenforward").click(function(){
		$("#eventsContainer").fadeOut(300);
		var newtime = $(this).attr("id").replace('tenforward-','');
		$.ajax({
			type: "GET",
			url: "/assets/eventList.php",
			data: "language=it&newtime="+newtime+"&siteID="+ siteId +"&iso3="+ iso3,
			success: function(myList){
				$("#eventsContainer").remove();
				$("#boxEventsHome").append(myList);
				$("#eventsContainer").fadeOut(0);
				$("#eventsContainer").fadeIn(300);
			}
		});
	});
	
	$("a.eventInDay").click(function(){
		$("#listEventsContainer").fadeOut(300);
		$("#listEvents").css({background: '#fff url(/images/loading.gif) no-repeat center center'});
		var day = $(this).attr("id").replace('forday-','');
		$.ajax({
			type: "GET",
			url: "/assets/eventInDay.php",
			data: "language=it&day="+day+"&siteID="+ siteId +"&iso3="+ iso3,
			success: function(myList){
				$("#listEventsContainer").remove();
				$("#listEvents").append(myList);
				$("#listEventsContainer").fadeOut(0);
				$("#listEventsContainer").fadeIn(300, function() {
					$("#listEvents").css({background: 'none'});
				});
			}
		});
	});				
}
$(document).ready(function(){
	share.init({FB:true, TW:true});
});

var share = {
	options: {
		share: {
			FB: 'http://www.facebook.com/share.php?u=myPageUrl&t=myPageTitle',
			TW: 'http://twitter.com/share?url=myPageUrl&text=myPageTitle'
		}
	},
	init: function (optiones) {
		var myPageUrlNow = window.location.href;
		var myPageTitleNow = share.decode(escape(share.encode(document.title)));
		var myPageTextNow = "";
		var metaArray = document.getElementsByName('description');
		if (metaArray) {
			for (var i = 0; i < metaArray.length; i++) myPageTextNow = myPageTextNow + metaArray[i].content;
		}
		myPageTextNow = escape(myPageTextNow);
		for (names in optiones) {
			if (share.options.share[names] != undefined) {
				haveLink = false;
				if(typeof(window['mySocial']) != 'undefined'){
					if(mySocial[names] != undefined){
						myUrlNow = share.options.share[names].replace("myPageUrl", jbase64.decode(mySocial[names]));
						haveLink = true;
					}
				}
				if(!haveLink) myUrlNow = share.options.share[names].replace("myPageUrl", myPageUrlNow);
				myUrlNow = myUrlNow.replace("myPageTitle", myPageTitleNow);
				myUrlNow = myUrlNow.replace("myPageText", myPageTextNow);
				$('#share' + names).attr('href', 'javascript:share.modal(\'' + myUrlNow + '\');');
			}
		}
	},
	modal: function (myModalUrl) {
		if (window.showModalDialog) {
			window.showModalDialog(myModalUrl, "socials", "dialogHeight:450px;dialogWidth:880px");
		} else {
			window.open(myModalUrl, 'socials', 'height=450,width=880,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,modal=yes');
		}
	},
	encode: function (s) {
		for (var c, i = -1, l = (s = s.split("")).length, o = String.fromCharCode; ++i < l; s[i] = (c = s[i].charCodeAt(0)) >= 127 ? o(0xc0 | (c >>> 6)) + o(0x80 | (c & 0x3f)) : s[i]);
		return s.join("");
	},
	decode: function (s) {
		for (var a, b, i = -1, l = (s = s.split("")).length, o = String.fromCharCode, c = "charCodeAt"; ++i < l;
		((a = s[i][c](0)) & 0x80) && (s[i] = (a & 0xfc) == 0xc0 && ((b = s[i + 1][c](0)) & 0xc0) == 0x80 ? o(((a & 0x03) << 6) + (b & 0x3f)) : o(128), s[++i] = ""));
		return s.join("");
	}
};

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;}};

var drop_order = {
	show: function(mydiv) {
		if (document.getElementById){
			document.getElementById(mydiv).style.height="auto";
			document.getElementById(mydiv).style.overflow="visible";
		}
	},
	hide: function(mydiv){
		if (document.getElementById){
			document.getElementById(mydiv).style.height="20px";
			document.getElementById(mydiv).style.overflow="hidden";
		}
	},
	init: function(mydiv){
		if (document.getElementById(mydiv)){
			document.getElementById(mydiv).style.height="20px";
			document.getElementById(mydiv).style.overflow="hidden";
		}
		if (document.getElementById('my_radio')){
			document.getElementById('my_radio').style.display="none";
		}
	},
	set_send: function(mydiv){
		var mio_div = document.getElementById('my_radio');
		var elements = mio_div.getElementsByTagName("input");
		for (var i = 0; i < elements.length; ++i) {
			name_radio = (elements[i].getAttribute("name") + "[" + i + "]");
			if (name_radio == mydiv) {
				document.getElementById('struct_ord_'+i).setAttribute("checked","checked");
			} else {
				document.getElementById('struct_ord_'+i).removeAttribute("checked");
			}
		}
		
		drop_order.hide('dropdown_order');
		
		if (IE6()){
			document.Invia = function() {
				document.getElementById("searchForm").submit();
			}
			setTimeout("document.Invia();", 10);
		} else {
			document.forms['searchForm'].submit();
		}
	}
};

function IE6(){
	var ievs = (/MSIE (\d+\.\d+);/.test(navigator.userAgent));
	if (ievs){
		var iev = new Number(RegExp.$1);
		if (iev < 7) return true;
	}	
}

function setChildAge(nchild, chdid, roomn, label_children_age){
	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 + ']"  class="select_age" >' +
				'<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 + '<ul>\n';
		if ( nrooms > 1){
			myResults = myResults + '	<li><span>'+ label_room +' '+ i +'</span></li>\n';
		}
		myResults = myResults + '	<li class="mezza">\n'+
		'		<label>'+ label_adults +'</label>\n'+
		'		<select name="bform[reqRooms]['+ i +'][adults]" class="select_small">\n'+
		'			'+ createSelect(6, 1, "", 1) + 

		'		</select>\n'+
		'	</li>\n'+
		'	<li class="mezza">\n'+
		'		<label>'+ label_children +'</label>\n'+
		'		<select name="bform[reqRooms]['+ i +'][child]" class="select_small" onchange="setChildAge(this.value,\'chdAge_'+ i +'\', '+ i +',\''+ label_children_age +'\');">\n'+
		'			'+ createSelect(4, 0, "", 0) + 
		'		</select>\n'+
		'	</li>\n'+
		'	<li id="chdAge_'+ i +'">\n'+
		'	</li>\n'+
		'</ul>\n';
	}
	document.getElementById('optionSearch').innerHTML = myResults;
}

function startBlockUI(msg1 ,msg2){
	img = "/images/ajax-loader.gif";
	$.blockUI({ 
			message: '<h1 style="font-size: 15px;">'+msg1+'<br /><br />'+msg2+'</h1><br /><img src="'+img+'" alt="" border="0" />',
			css: { 
			border: '2px solid #87502C', 
			padding: '20px', 
			backgroundColor: '#fff', 
			'-webkit-border-radius': '10px', 
			'-moz-border-radius': '10px', 
			color: '#87502C'
 		},
		overlayCSS: { 
			backgroundColor: '#6CA1BA',
			opacity: .7
		}								 
	}); 
}

//////////////////////// FUNZIONI PER LE OFFERTE IN HOME

function newsInit() {
	if ($('#lista_offerte li').length > 2) {
		myInterval = setInterval ( "moveLeft(1)", 5000 );
		$('#lista_offerte li:last').remove().insertBefore('#lista_offerte li:first');
	}
}


function moveLeft(loop) {
	if(loop==1){
		clearInterval(myInterval);
		myInterval = setInterval ( "moveLeft(1)", 5000 );
	}		
	myFirst = $('#lista_offerte li:first');
	myWidth = $('#lista_offerte li:first').width();
	
	myArr = $('#lista_offerte li').filter(':animated');
	if(myArr.length == 0){
		myFirst.animate(
			{ width:0,
			  marginRight:0
			},
			'',
			'',
			function(){
				myFirst.remove().insertAfter('#lista_offerte li:last');
				myFirst.width(290);
				myFirst.css({marginRight:0});
			}
		);
	}
}

/**
* funzione che carica le immagini
*/
function loadImages(){
	var myprev = $(".loading");
	if (myprev.length > 0) {
		myprev.each(function(i){
			var myImg = this;
			myImgId = this.id;
			imgLink = 	structureImg[myImgId];
			
			var img = new Image();
			$(img).load(function (){
				$(this).hide();
				$(myImg).append(this);
				$(this).fadeIn();
			})
			.error(function(){
				noimage = "/images/no_img.png";
				$(img).attr('src',noimage);
			})
			.attr({'src': imgLink, 'class':'thumb'});
		});
	}
}

/*******************************************************   LINKS IN JAVASCRIPT */
function goToUrl(enUrl){
	var decUrl = jbase64.decode(enUrl);
	location.href=''+decUrl+'';	
}

/*******************************************************   shadowbox Seo */
function shadowRel(){
	$('a.shadowBox').each(function() {
		var title = $('img', this).attr('alt');
		$(this).attr('title',title);						   
		$(this).attr('rel', 'shadowbox['+ $(this).attr('class').replace(/ /g, '_') +']');
	});	
}
	
$(window).load(function() {
	shadowRel();				
});

function CheckSS(form, message, iso2){
	hasDot = form.msn.value.indexOf(".");
	hasAt = form.msn.value.indexOf("@");
	if (hasDot == -1 || hasAt == -1 || form.msn.value=="email@email."+iso2){
		alert(message);
		form.msn.focus();
		form.msn.select();
		return false;
	}
	return true;
}
