jQuery.preloadImages = function()
{
	for(var i = 0; i<arguments.length; i++)
	{
		jQuery("<img>").attr("src", arguments[i]);
	}
}

$.preloadImages(	"templates/hotels_.png"
					,"templates/hotels-sopra.png"
					,"templates/business_.png"
					,"templates/business-sopra.png"
					,"templates/yachts_.png"
					,"templates/yachts-sopra.png"
					,"templates/homes_.png"
					,"templates/homes-sopra.png"
					);

function HasVSCrollBar() {
	if ($("body").height() > $(window).height()) {
        return true;
    }
	return false;
}

function BackgroundPosition() {
	var w = 0;
	var cw = 0;
	var rw = 0;
	var sco = 0;
	
	w = $(window).width();
	cw = $('#pagecontent').outerWidth(false);
	if( $('#rightcontent').length ) {
		rw = $('#rightcontent').outerWidth(false);
		rw += 4;
	} else {
		rw = 174;
	}
	/*
	if( HasVSCrollBar() )  {
		sco = 10;
	}
	*/
	w = parseInt((w - cw) / 2);
	w += cw - rw - sco;
	$('body').css({backgroundPosition: w+'px 0px'});
}

$(document).ready(function() {
	BackgroundPosition();
	$(window).resize(function(){
		BackgroundPosition();
	});
	
	
	$(".imgrollover img").hover(
		function()
		{
			this.src = this.src.replace("_","-sopra");
		},
		function()
		{
			this.src = this.src.replace("-sopra","_");
		}
	);
	$("#menuitem0").click(function() {
		if( $("#submrealizzazioni").length ) {
			$("#submrealizzazioni").hide('slow');
		} else {
			$("#submrealizzazioniinterno").hide('slow');
		}
		return true;
	});
	$("#menuitem1").click(function() {
		if( $("#submrealizzazioni").length ) {
			$("#submrealizzazioni").show('slow');
		} else {
			$("#submrealizzazioniinterno").show('slow');
		}
		return false;
	});
	$("#menuitem2").click(function() {
		if( $("#submrealizzazioni").length ) {
			$("#submrealizzazioni").hide('slow');
		} else {
			$("#submrealizzazioniinterno").hide('slow');
		}
		//return false;
		return true;
	});
	$("#menuitem3").click(function() {
		if( $("#submrealizzazioni").length ) {
			$("#submrealizzazioni").hide('slow');
		} else {
			$("#submrealizzazioniinterno").hide('slow');
		}
		return true;
	});
	$("#menuitem4").click(function() {
		if( $("#submrealizzazioni").length ) {
			$("#submrealizzazioni").hide('slow');
		} else {
			$("#submrealizzazioniinterno").hide('slow');
		}
		return true;
	});
	$("#menuitem5").click(function() {
		if( $("#submrealizzazioni").length ) {
			$("#submrealizzazioni").hide('slow');
		} else {
			$("#submrealizzazioniinterno").hide('slow');
		}
		return true;
	});

	$("#previmg").click(function() { 
		var c = $("#scrollcontainer");
		var pos = c.position();  
		//alert(pos.top);
		if( pos.top < 0 ) {
			pos.top += 119;
			c.css({top: pos.top+'px'});
		}
		$(this).blur();
		return false;
	});
	$("#nextimg").click(function() { 
		var c = $("#scrollcontainer");
		var pos = c.position();  
		var h = c.height();
		var r;
		pos.top -= 119;
		r = h + pos.top;
		if( r >= (119*4) ) {
			c.css({top: pos.top+'px'});
		}
		$(this).blur();
		return false;
	});
	
	$('a[id*=page]').click(function() {
		var c = $("#scrollcontainer");
		var h = c.height();
		var page = $(this).attr('id');
		var pos;
		var r;
		
		page = page.split("page").pop();
		pos = (page - 1) * (119 * 4);
		r = h - pos;
		if( r < (119*4) ) {
			pos = h - (119*4);
		}
		pos *= -1;
		c.css({top: pos+'px'});
		
		return false;
	});
	
	$("#showhidepanel").click(function() {
		$("#leftcontentcurtain").toggle();
		$("#pannellogalleria").toggle();
		$(this).blur();
		return false;
	});
	
	$("#scrollcontainer a").click(function() { 
		var id = $(this).attr('id');
		var re = new RegExp('^imgid_([0-9]+)_([0-9]+)_([0-9]+)');
		var m = re.exec(id);

		if (m != null) {
			var galid= m[1];
			var imgid = m[2];
			var lngid = m[3];
			
			$("#imagetext").load( 'index.php?op=imgtext&galid='+galid+'&imgid='+imgid+'&lng='+lngid+'&rnd='+Math.floor(Math.random()*101),
				function (responseText, textStatus, XMLHttpRequest) {
						BackgroundPosition();

					//if( responseText.length > 0 ) {
					//	$("#imagetextt").show();
					//} else {
					//	$("#imagetextt").hide();
					//}
				}
			);
			
			$("#innerpannellogalleria").load( 'index.php?op=imgphotogal&galid='+galid+'&imgid='+imgid+'&lng='+lngid+'&rnd='+Math.floor(Math.random()*101) );
		}

		//var url = $(this).attr("src").replace("t1", "i");
		var url = $('img',this).attr("src").replace("t1", "i");
		var wrap = $("#image_wrap").fadeTo("medium", 0.5); 
		var img = new Image(); 
		img.onload = function() { 
			wrap.fadeTo("fast", 1); 
			wrap.find("img").attr("src", url); 
	 
		}; 
		img.src = url;
		return false;		
	}).filter(":first").click();	
});
