//<![CDATA

var widthDoc=0;
var heightDoc=0;
var imgHeight=0;
var imgWidth=0;
var welcomeHeight=0;
var welcomeWidth=0;
var logoHeight=0;
var logoWidth=0;
var versionIE;
var lightboxAttiva = true;

var sonoNellaHome = false;

window.addEvent('domready', function() {
	
	var arVersion = navigator.appVersion.split("MSIE");
	versionIE = parseFloat(arVersion[1]);
	
	if(versionIE < 7) pngfix();
	
	for(var i=0; i<document.links.length; i++) {
		if (document.links[i].rel == "external") document.links[i].target = "_blank";
		if (document.links[i].rel.substr(0,7) == "milkbox") document.links[i].rel = "lightbox[gallery]";
	}
	
	var heightWindow=document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
	var heightBody = $('wrapper').offsetHeight;
	
	//alert(heightWindow+" - "+heightBody);
	
	if(heightWindow > heightBody) {
		heightDoc = heightWindow;
	} else {
		heightDoc = heightBody;
	}
	
	var widthWindow = document.body.offsetWidth;
	var widthBody = $('wrapper').offsetWidth;
	
	if(widthWindow > widthBody) {
		widthDoc = widthWindow;
	} else {
		widthDoc = widthBody;
	}
	
	$(document.body).setStyle('height', heightDoc+'px');
	$('contBg').setStyle('height', heightDoc+'px');
	
	var imgLoading = new Asset.image('templates/main/immagini/loadingWhite.gif', { id: 'imgLoading'});
	document.body.appendChild(imgLoading);

	$('imgLoading').setStyle('margin','0 auto');
	$('imgLoading').setStyle('margin-top',(heightDoc/2)-20);
	
	$('wrapperFix').setStyle('opacity','0');
	
	var imgBackground = new Asset.image('templates/main/immagini/bgPagina.jpg', { id: 'imgBackground', onload:function(img){
		
		$('contBg').appendChild(img);
		document.body.removeChild(imgLoading);
		resizeImage();
		
		window.onresize = resizeImage;
		
		$('wrapperFix').fade('in');
		
	}.bindWithEvent(this)});
	
	if(typeof(do_init)=='function') do_init();
	
	if(lightboxAttiva == true) Mediabox.scanPage();
	
});

function resizeImage(){
	
	var newHeightWindow=document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
	var newHeightBody = $('wrapper').offsetHeight;
	
	if(newHeightWindow > newHeightBody) {
		var newHeightDoc = newHeightWindow;
	} else {
		var newHeightDoc = newHeightBody;
	}
	
	var newWidthWindow = document.body.offsetWidth;
	var newWidthBody = $('wrapper').offsetWidth;
	
	if(newWidthWindow > newWidthBody) {
		var newWidthDoc = newWidthWindow;
	} else {
		var newWidthDoc = newWidthBody;
	}
	
	$(document.body).setStyle('height', newHeightDoc+'px');
	$('contBg').setStyle('height', newHeightDoc+'px');
	
	imgHeight=document.getElementById('imgBackground').offsetHeight;
	imgWidth=document.getElementById('imgBackground').offsetWidth;
	
	
	if(newHeightWindow < newHeightBody) {
		
		$(document.body).setStyle('overflow-y','auto');
		
	} else {
		
		window.scroll(0,0);
		$(document.body).setStyle('overflow-y','hidden');
		var widthScrollBar = getScrollerWidth();
		newWidthDoc = newWidthDoc + widthScrollBar;
		
	}
					
	if(newWidthDoc>newHeightDoc) {
	
		document.getElementById('imgBackground').height=newHeightDoc;
		document.getElementById('imgBackground').width=((newHeightDoc*imgWidth)/imgHeight);


		if (newWidthDoc>(document.getElementById('imgBackground').offsetWidth)) {
			
			document.getElementById('imgBackground').width=newWidthDoc;
			document.getElementById('imgBackground').height=((newWidthDoc*imgHeight)/imgWidth);
			
		}

	
	}else{

		document.getElementById('imgBackground').width=newWidthDoc;
		document.getElementById('imgBackground').height=((newWidthDoc*imgHeight)/imgWidth);

		if (newHeightDoc>(document.getElementById('imgBackground').offsetHeight)) {
			
			document.getElementById('imgBackground').height=newHeightDoc;
			document.getElementById('imgBackground').width=((newHeightDoc*imgWidth)/imgHeight);
			
		}

	}
	
	//alert(document.getElementById('imgBackground').height);
	
}

function getScrollerWidth() {
    var scr = null;
    var inn = null;
    var wNoScroll = 0;
    var wScroll = 0;

    // Outer scrolling div
    scr = document.createElement('div');
    scr.style.position = 'absolute';
    scr.style.top = '-1000px';
    scr.style.left = '-1000px';
    scr.style.width = '100px';
    scr.style.height = '50px';
    // Start with no scrollbar
    scr.style.overflow = 'hidden';

    // Inner content div
    inn = document.createElement('div');
    inn.style.width = '100%';
    inn.style.height = '200px';

    // Put the inner div in the scrolling div
    scr.appendChild(inn);
    // Append the scrolling div to the doc

    document.body.appendChild(scr);

    // Width of the inner div sans scrollbar
    wNoScroll = inn.offsetWidth;
    // Add the scrollbar
    scr.style.overflow = 'auto';
    // Width of the inner div width scrollbar
    wScroll = inn.offsetWidth;

    // Remove the scrolling div from the doc
    document.body.removeChild(
    document.body.lastChild);

    // Pixel width of the scroller
    return (wNoScroll - wScroll);
}

//]]>
