function MM_findObj(n, d)
{ //v4.01
	var p, i, x;
	if (!d) d = document;
	if ((p = n.indexOf("?")) > 0 && parent.frames.length) 
	{
		d = parent.frames[n.substring(p + 1)].document;
		n = n.substring(0, p);
	}
	if (!(x = d[n]) && d.all) x = d.all[n];
	for (i = 0; !x && i < d.forms.length; i++) 
		x = d.forms[i][n];
	for (i = 0; !x && d.layers && i < d.layers.length; i++) 
		x = MM_findObj(n, d.layers[i].document);
	if (!x && d.getElementById) x = d.getElementById(n);
	return x;
}


/**
 * Compatibilità vecchia versione
 * @param {String} url
 * @param {String} name
 * @param {Object} params
 */
function ApriPopup(url, name, params)
{
	return fx.openPopup(url, name, params);
}

/**
 *
 */
function fx()
{
	this.langAni = false;
}


fx.prototype.getWindowWidth = function()
{
	if (document.documentElement) 
		return document.documentElement.offsetWidth;
	else 
		if (window.innerWidth) 			
			return window.innerWidth;
	return screen.width;
};

/**
 * @param {String} lang
 */
fx.prototype.startClock = function(lang)
{
	var settings = 
	{
		format: '%I:%M %p',
		am_pm: true
	};
	
	$('#clock').jclock(settings);
	
	var self = this;
	$(window).resize(function(e)
	{
		if (self.getWindowWidth() < 1024) 
		{
			$("#clockSun").hide();
			$('#topContainer').width('967px');
		}
		else 
		{
			$("#clockSun").show();
			$('#topContainer').width('1200px');
		}
	});
	$(window).resize();
};


fx.prototype.activateButtonsRollOver = function()
{
	$(".formButton, .formButtonW").hover(function()
	{
		$(this).toggleClass("buttonOn");
	}, function()
	{
		$(this).toggleClass("buttonOn");
	});
}



fx.prototype.activateLangRollOver = function()
{
	var self = this;
	
	$("#lang").hoverIntent(function()
	{
		self.showHideLang("show");
		
	}, function(e)
	{
		if (!$(e.relatedTarget).parents('#changeLang').length && !$(e.relatedTarget).is("#changeLang")) 
		{
			self.showHideLang("hide");
		}
		//e.stopPropagation();
	}).mouseout(function(e)
	{
		if (!$(e.relatedTarget).parents('#changeLang').length && !$(e.relatedTarget).is("#changeLang")) 
		{
			self.showHideLang("hide");
		}
		//e.stopPropagation();
	}).click(function()
	{
		self.showHideLang("show");
	});
	
	$("#changeLang").mouseout(function(e)
	{
		if (!$(e.relatedTarget).parents('#lang').length && !$(e.relatedTarget).parents('#changeLang').length && !$(e.relatedTarget).is("#lang")) 
		{
			self.showHideLang("hide");
		}
		//e.stopPropagation();
	});
};


fx.prototype.showHideLang = function(showHide)
{
	var speed = "fast";
	var self = this;
	if (!this.langAni) 
	{
		this.langAni = true;
		if (showHide == "show") 
		{
			var myOffset = $("#lang").css("left").replace(/px/, "");
			var myHalfLang = $("#lang").width() / 2;
			var myHalfChangeLang = ($("#changeLang").width() / 2);
			var myLeft = Math.round(parseInt(myOffset) + parseInt(myHalfLang) - parseInt(myHalfChangeLang));
			$("#changeLang").css("left", myLeft + "px").show(speed, function()
			{
				self.langAni = false;
			})
		}
		else 
		{
			$("#changeLang").hide(speed, function()
			{
				self.langAni = false;
			});
		}
	}
};


fx.prototype.activateLightBox = function(el)
{
	$(el).lightBox(
	{
		fixedNavigation: true,
		
		//@sergio
		imageLoading: 'galleria/lightbox-ico-loading.gif',
		imageBtnPrev: 'galleria/lightbox-btn-prev.gif',
		imageBtnNext: 'galleria/lightbox-btn-next.gif',
		imageBtnClose: 'galleria/lightbox-btn-close.gif',
		imageBlank: 'galleria/lightbox-blank.gif',
		loopMode: true,
		
		txtImage: "",
		txtOf: "/"
	});
};


/**
 * @param {String} what
 * @return {Bool}
 */
fx.prototype.exists = function(what)
{
	return (typeof what != 'undefined');
};


fx.prototype.toggleMapLegend = function()
{
	if ($('#mapLegend table:visible').length) 
	{
		$('#mapLegend table').slideUp();
		$('#mapLegend .title img').attr('src', '/images/arrow_dn.gif');
	}
	else 
	{
		$('#mapLegend table').slideDown();
		$('#mapLegend .title img').attr('src', '/images/arrow_up.gif');
	}
};


/**
 * @param {String} url
 * @param {String} name
 * @param {Object} params
 * @return {Bool}
 */
fx.prototype.openPopup = function(url, name, params)
{
	var w = window.open(url, name, params);
	w.focus();
	return false;
};


fx.prototype.showLoadingBox = function()
{
	$('#searchLoadingBoxBg').height();
	$('#searchLoadingBoxBg, #searchLoadingBox').show();
};


fx.prototype.activateScrollable = function(expr, size)
{
	if (!fx.isIE6()) 
	{
		$(expr).scrollable(
		{
			size: size,
			clickable: false
		});
		$(expr).each(function()
		{
			var $this = $(this);
			if ($this.find("img").length > size) 
			{
				$this.next("a.nextPage").removeClass("disabled");
			}
		});
	}
};


fx.prototype.preloadImages = function(images, baseDir)
{
	if (!baseDir) 
	{
		baseDir = '/img/';
	}
	for (var i in images) 
	{
		$("<img>").attr('src', baseDir + images[i]);
	}
};


fx.prototype.isNight = function(bool)
{
	if (bool) 
	{
		$("body").addClass('night');
		$("#sunMoon img").attr('src', '/img/moon.png');
	}
	else 
	{
		$("body").removeClass('night');
		$("#sunMoon img").attr('src', '/img/sun.png');
	}
};


fx.prototype.initNightAndDay = function(sunInfo)
{
	var d = new Date();
	var now = parseInt(d.getTime() / 1000);
	
	if (now < parseInt(sunInfo.sunrise) || now > parseInt(sunInfo.sunset)) 
	{
		this.isNight(true);
	}
	else 
	{
		this.isNight(false);
	}
};


fx.prototype.fixPngImages = function(items)
{
	for (var i in items) 
	{
		$(items[i]).pngFix();
	}
};


fx.prototype.hasTooltip = function(el, text)
{
	var tooltip = $('<div class="tooltip">' + text + '</div>');
	$(el).hover(function(e)
	{
		tooltip.css(
		{
			left: e.clientX + 5 - 260,
			top: e.clientY + 5
		});
		$(this).after(tooltip);
	}, function()
	{
		tooltip.remove();
	}).css("cursor", "help");
};


fx.prototype.isIE6 = function()
{
	return ($.browser.msie && /6\..+/.test($.browser.version));
}


fx.prototype.getMapCookie = function(filter)
{
	var name = 'hc_map';
	var cookie = $.cookie(name);
	
	if (cookie == null) 
	{
		cookie = [1, 1];
		fx.setMapCookie(
		{
			city: cookie[0],
			hostel: cookie[1]
		});
	}
	else 
	{
		cookie = cookie.split("|");
	}
	
	cookie = {
		city: cookie[0],
		hostel: cookie[1]
	};
	
	if (filter && (typeof cookie[filter] != 'undefined'))
	{
		return cookie[filter];
	}
	return cookie;
}


fx.prototype.setMapCookie = function(value, filter)
{
	var name = 'hc_map';
	var cookie = [];
	
	if (typeof value == 'object') 
	{
		if (typeof value.city != 'undefined') 
		{
			cookie[0] = value.city;
		}
		if (typeof value.hostel != 'undefined') 
		{
			cookie[1] = value.hostel;
		}
		$.cookie(name, cookie.join("|"));
	}
	else 
	{
		var cookie = fx.getMapCookie();
		cookie[filter] = value;
		fx.setMapCookie(cookie);
	}
}


Date.prototype.getMonthTotalDays = function()
{
	return 32 - new Date(this.getFullYear(), this.getMonth(), 32).getDate();
};


window.fx = new fx();






