$(window).ready(function(){
	resizeBk();
	var w_c = $('#cont #footer .copyright').width();
	var w_n = $('#cont #footer .newsletter').width();
	var w_l = $('#cont #footer .lang').width();
	var diff = Math.floor((960 - (w_c + w_n + w_l+18+38+12))/2); 
	$('#cont #footer .lang').css('margin-left',diff);
	$('#cont #footer .lang').css('margin-right',(960 - (w_c + w_n + w_l+18+38+12))-diff);
	
	$('#bk').cycle({ 
		fx:    'fade', 
		pause:  1 
	});
});


$(window).resize(function(){
	resizeBk();
});

//return the height of the window
function getDimWindow(){
	var totalWidth=Math.max($(window).width(),$('#bd').width() );
	var totalHeight=Math.max($(window).height(),$('#bd').height());
	
	var array = new Array(totalWidth, totalHeight);
	return array;
}

/**
 * Ritorna le dimensioni proporzionate in base ad un'area massima definita in modo da riempire sempre l'area definita.
 */
function getMinAreaResize(w, h, wMax, hMax) {
	var dim;
	var newW;
	var newH;
	var rapDest;
	var rapOrig;
	if(w == h) {
		// la sorgente è quadrata
		if(wMax == hMax) {
			// l'area di destinazione è quadrata
			newW = wMax;
			newH = hMax;
			dim = [newW,newH];
		} else if(wMax > hMax) {
			// l'area di destinazione è più LARGA che alta
			dim = setHeightProp(w, h, wMax);
		} else {
			// l'area di destinazione è più ALTA che larga
			dim = setWidthProp(w, h, hMax);
		}
	} else if(w > h) {
		// la sorgente è LARGA
		if(wMax == hMax) {
			// l'area di destinazione è quadrata
			dim = setWidthProp(w, h, hMax);
		} else if(wMax > hMax) {
			// l'area di destinazione è più LARGA che alta
			rapDest = hMax / wMax;
			rapOrig = h / w;
			if(rapDest == rapOrig) {
				// l'area e l'immagine hanno la stessa proporzione
				dim = setSameProp(w, h, hMax);
			} else if(rapDest > rapOrig) {
				// l'area è più Alta in proporzione all'immagine
				dim = setWidthProp(w, h, hMax);
			} else {
				// l'area è più LARGA in proporzione all'immagine
				dim = setHeightProp(w, h, wMax);
			}
		} else {
			// l'area di destinazione è più ALTA che larga
			dim = setWidthProp(w, h, hMax);
		}
	} else {
		// la sorgente è ALTA
		if(wMax == hMax) {
			// l'area di destinazione è quadrata
			dim = setHeightProp(w, h, wMax);
		} else if(wMax > hMax) {
			// l'area di destinazione è più LARGA che alta
			dim = setHeightProp(w, h, wMax);
		} else {
			// l'area di destinazione è più ALTA che larga
			rapDest = hMax / wMax;
			rapOrig = h / w;
			if(rapDest == rapOrig) {
				// l'area e l'immagine hanno la stessa proporzione
				dim = setSameProp(w, h, hMax);
			} else if(rapDest > rapOrig) {
				// l'area è più Alta in proporzione all'immagine
				dim = setWidthProp(w, h, hMax);
			} else {
				// l'area è più LARGA in proporzione all'immagine
				dim = setHeightProp(w, h, wMax);
			}
		}
	}
	return dim;
}

function setWidthProp(w, h, hMax) {
	var dim = [];
	var newW = (hMax / h) * w;
	var newH = hMax;
	dim = [newW, newH];
	return dim;
}

function setHeightProp(w, h, wMax) {
	var dim = [];
	var newW = wMax;
	var newH = (newW / w) * h;
	dim = [newW, newH];
	return dim;
}

function setSameProp(w, h, hMax) {
	var dim = [];
	var newW = (hMax / h) * w;
	var newH = (newW / w) * h;
	dim = [newW, newH];
	return dim;
}



/**************SPLASH PAGE*****************/
function resizeBk(){
	var arr_dim = getDimWindow();
	var _ht = arr_dim[1];
	var _wt = arr_dim[0];
	//alert(arr_dim);
	$('#cont').height(_ht);
	
	//$('#img_bk').css('width',);	
	var w_img = parseInt($('.img_bk').attr('_w'));
	var h_img = parseInt($('.img_bk').attr('_h'));
	

	if(_wt < 960 && _ht < 800){
		_wt = 960;
		_ht = 800;
	}
	
	var dimensions = getMinAreaResize(w_img ,h_img, _wt, _ht);

	
	var newH = dimensions[1];
	var newW = dimensions[0];
	var rapOrig = w_img / h_img;
	
	
	$('.img_bk').css('height',Math.floor(dimensions[1]));	
	$('.img_bk').css('width',Math.floor(dimensions[0]));
}


