// page object namespace - contains global vars and functions
		var PO = {						
			resizeTimeout:null,	
			newSection:'',						// next section to open
			oldSection:'',						// last section to close
			subSection:'',
			oldSubSection:'',
			subSubSection:'',
			sectionCalled:false,				// false after address event received, true when bg etc. are loaded
			sectionLoaded:false,			    // whether the section has been loaded (ie the text)
			navArray:[],
			showingSplashPage:true
		};	
				
		/************************************ xml for menu **************************************/
		
		PO.resetVars = function(){
			this.resizeTimeout = null;	
			//this.newSection = '';						// next section to open
			this.subSection = '';
			this.subSubSection = '';
			this.sectionCalled = false;				// false after address event received, true when bg etc. are loaded
			this.sectionLoaded = false;			    // whether the section has been loaded (ie the text)
		}
		
		// read xml and create the main menu
		PO.readXML = function(){
			$.ajax({
			  type: "GET",
				 url: "content/struttura.xml",
				 dataType: "xml",
				 success: function(xml) {
					$("#mainMenu ul").empty();
					$(xml).find('localization').each(function(){
						if($(this).attr('lang') == lang){
							//create menu
							var numSections = $(this).children().length;
							var counter = 0;
							$(this).find('section').each(function(){
								$("#mainMenu ul").append('<li><a href="/' + lang + '/' + $(this).attr("id") + '.html"  id="' + $(this).attr("id") + '-link">' + $(this).attr("title").toUpperCase() + '</a></li>');
								counter ++;
								if(counter < numSections){
									$("#mainMenu ul").append('<li class="divider"></li>');
								}								
							});
						}
					});
					$('nav#mainMenu a').each(function() {											  
						if($(this).attr('id') == PO.newSection+'-link' ){
							$(this).parent().siblings().find("a").removeClass('selected');
							$(this).addClass('selected');
						}
					});
				 },
				 error:function(e){
					 //console.log(e);
				}
			}); //close $.ajax(	
		}
		
		PO.doLangChange = function(){
			$('#areaRiservata_link').html(langObject.areaRiservata);	
			$('#language_link').html(langObject.lingua);	
			$('#social_link').html(langObject.socialize);
			$('#percorso').html(langObject.calcola_percorso);
			$('#invia_email').html(langObject.invia_percorso);
			$('#downloadArea_link').html(langObject.download_area);
		}
		
		/************************************* sections *****************************************/
		
		PO.loadNewSection = function(){
			PO.hasBgImg = false;
			PO.sectionLoaded = false;
			clearTimeout(BGO.t);					// stop animating the background
			
			//load bg img
			$.each(BGO.bgImgArray, function(key, value) {
			 	if(key == PO.newSection){
					PO.hasBgImg = true;	
					if(PO.newSection == "collections" || PO.newSection == "distribution" || PO.newSection == "mattresses" || PO.newSection == "fabrics"){						// subsections of collections (i.e. letti, divani) don't have their own background images
						BGO.setUpArray(key, "");
					}else{
						BGO.setUpArray(key, PO.subSection);					// pass the value of the key to the setUpArray function
					}
				}
			});
			
			//load text
			var contentWrapper = $('#' + PO.newSection).find('.contentWrapper');
			switch(PO.newSection){
				case "company":
					AZ.createSecondaryMenu();
					AZ.loadSection();
					break;
				case "news":
					NZ.loadSection();
					break;
				case "home":
					PO.sectionLoaded = true;
					break;
				case "designers":
					DZ.loadSection();
					break;
				case "collections":
					CO.loadSection();
					break;
				case "mattresses":
					MO.loadSection();
					break;
				case "fabrics":
					FA.loadSection();
					break;
				case "distribution":
					DIST.loadSection();
					break;
				case "reserved_area":
					AREA_RIS.loadSection();
					break;
				default:
					$.ajax({
					  type: "GET",
						 url: 'content/' + lang + '/' + PO.newSection+'.html',
						 context:$('#' + PO.newSection),
						 success: function(h) {
							 $(this).find('.content').html($(h).filter('.pageContent').html());	
							 $(this).find('h1').html($(h).filter('h1').html());
							  Cufon.replace('h1');
							 PO.sectionLoaded = true;
							 if(BGO.imageLoaded && !PO.showingSplashPage){
				 				 PO.animateSection();
								 return;
							 }							 
						 }
					}); //close $.ajax(
					break;
					
			}
				
			if((!PO.hasBgImg || BGO.imageLoaded)  && !PO.showingSplashPage){			// no img to wait for, load section straight away
				PO.animateSection();						
			}
		}
		
		PO.loadNewSubSection = function(){
			//load bg img
			clearTimeout(BGO.t);										// stop animating the background
			
			$.each(BGO.bgImgArray, function(key, value) {
			 	if(key == PO.newSection){
					if(PO.newSection == "collections" || PO.newSection == "distribution" || PO.newSection == "mattresses" || PO.newSection == "fabrics"){						// subsections of collections (i.e. letti, divani) don't have their own background images
						BGO.setUpArray(key, "");
					}else{
						BGO.setUpArray(key, PO.subSection);					// pass the value of the key to the setUpArray function
					}
				}
			});
			
			switch(PO.newSection){
				case "company":
					AZ.loadSubSection();
					break;
				case "collections":
					CO.filterCollection();
					break;
				case "distribution":
					DIST.loadBlock1();
					break;
				case "components":
					COMP.loadSubSection();
					break;
				case "designers":
					DZ.loadSubSection();
					break;
				case "mattresses":
					MO.loadSubSection();
					break;
				case "fabrics":
					FA.filterCollection();
					break;
				default:
					break;
			}
		}
		
		PO.animateSection = function(){
			
			if(this.newSection == this.oldSection){
				return;	
			}
			var contentWrapper = $('#' + this.newSection).find('.contentWrapper');
			if(this.oldSection != ''){
				$('#' + this.oldSection).css("zIndex", 99);
				$('#' + this.newSection).css("zIndex", 100);					// put the new section over the old one
			  //	$('#' + this.oldSection).fadeOut(1000, function(){
					$('#' + this.oldSection).hide();
					$(this).find('img.fullscreen').remove();
			  	//});	
			}
			
			switch (this.newSection){
				case "news":
					// don't do anything - news.js handles it itself.
					//break;
				default:			
					$('#' + this.newSection).show();
					//$('#' + this.newSection).fadeIn(1000, function(){
						if(PO.newSection == 'collections'){
							fleXenv.fleXcrollMain("portfolioStripContainer");	
						}
						if(document.getElementById('portfolioStripContainer').fleXcroll){
							document.getElementById('portfolioStripContainer').fleXcroll.setScrollPos(0,false);	
						}
						fleXenv.updateScrollBars();												   
					//});
					//contentWrapper.show();
					//('#' + this.newSection + ' .contentWrapper').show();
					break;
			}			
			this.oldSection = this.newSection;			
			this.doResize();
		}
			
		PO.animateMenu = function(dir){
			if(dir=="up"){
				$('#mainMenu').animate({top:7}, 300);
				$('#'+this.newSection+'Menu').css('top',67).show().animate({top:37}, 300);
			}else{
				$('#mainMenu').animate({top:21}, 300);
				$('#'+this.oldSection+'Menu').animate({top:67}, 300, function(){
					$(this).hide();													  
				});
			}
		}	
				
		/********************************* resize *****************************************/
		
		PO.doResize = function(){
			PO.resizeElement($('.container'));	
			PO.resizeElement($('.splashContainer'));	
		
			PO.resizeSplashImg();
			
			if(PO.newSection == "distribution"){
				$('#block1Wrapper').css('height', $(document).height() - ($(document).height()/3 + 120));
				$('#block2Wrapper').css('height', $(document).height() - ($(document).height()/3 + 90));
			}
			if(PO.newSection == "components"){
				$('#scrollWrapper').css('height', $(document).height() - ($(document).height()/3 + 130));
			}
			if(PO.newSection == 'company'){
				$('#companyContent').css('height', $(document).height()-(($(document).height()/3)+100));	
			}
			if(PO.newSection == 'collections'){
				$('#zoomer .infoHolder').css({'maxHeight':$(document).height() - (($(document).height()/3) + 200)});
				//$('#zoomer .infoHolder').css({'height':$('#zoomer .description').height()});
				if($('#zoomer .description').height() > 0){
					$('#zoomer #infoHolderDiv').css({'height':$('#zoomer .description').height()});	
				}
				if($('#zoomer .tech').height() > 0){
					$('#zoomer #techHolderDiv').css({'height':$('#zoomer .tech').height()});	
				}
				$('#galleryStripContainer').css({'width':$(document).width() - 150});
				if(PO.subSubSection == ''){
					$('#portfolioStripContainer').css('width', $(document).width() - 65);
				}
			}
			if(PO.newSection == 'mattresses'){
				$('#mattressInfoHolderDiv').css('height', $(document).height()-(($(document).height()/3)+100));
				$('#mattressStripContainer').css({'width':$(document).width() - 65});
			}
			
			if(PO.newSection == 'fabrics'){
				$('#fabricInfoHolderDiv').css('height', $(document).height()-(($(document).height()/3)+60));
				$('#fabricStripContainer').css({'width':$(document).width() - 65});
			}
			
			$('.home_footer').css('left', '50%');
			if($('.home_footer').offset().left < $('nav#helperMenu').width()){
				$('.home_footer').css('left', $('nav#helperMenu').width() + 100);	
			}
			fleXenv.updateScrollBars();	
		}
		
		PO.resizeElement = function(elem){
			
			var imgWidth = 1500;				// original dimensions of img
			var imgHeight = 1000;
			var minHeight = 500;			// min display dimensions
			var minWidth = 980;	
			var iw = elem.width();
			var ih = elem.height();
			var targetHeight = $(window).height() ;
			var targetWidth = $(window).width();
			var ratio = 0;
			var targetImgHeight;
			var targetImgWidth;
			
			if(targetHeight < minHeight){
				targetHeight = minHeight;	
			}
			if(targetWidth < minWidth){
				targetWidth = minWidth;
			}
			
			targetImgHeight = targetHeight;
			targetImgWidth = targetWidth;
			
			if((targetHeight > targetWidth)){		// height commands
				ratio = imgWidth/imgHeight;					
				targetWidth = targetHeight*ratio;
				targetImgWidth = targetWidth;
			}else{
				ratio = imgHeight/imgWidth;
				targetImgHeight = targetWidth*ratio;
				if(targetImgHeight <  $(window).height()){
					ratio = imgWidth/imgHeight;
					targetImgHeight = $(window).height();
					targetImgWidth = targetImgHeight*ratio;
				}
			}
			
			// if section text longer than page, add to it
			if(this.newSection != ''){
				var contentWrapper = $('#' + PO.newSection).find('.contentWrapper');
				if(targetHeight < contentWrapper.height()+20){
					targetHeight = contentWrapper.height()+20;
				}
			}
			
			elem.css("height",targetHeight+ "px", "width",targetWidth+"px");
			var imgMarginTop = -(targetImgHeight - targetHeight)/2
			var imgMarginLeft = -(targetImgWidth - targetWidth)/2;
			elem.find("img.fullscreen").css({'height' : targetImgHeight, 'width' : targetImgWidth, 'marginTop' : imgMarginTop, 'marginLeft' : imgMarginLeft}); 
						  
		 }
		 
		PO.resizeSplashImg = function(){
			var targetHeight = $(window).height() -100;
			var targetWidth = $(window).width() - 100;
			var ratio = 700 / 900;
			var invRatio = 900 / 700;
			
			var h = targetHeight;
			var w = targetHeight*ratio;
			
			if(w > targetWidth){
				w = targetWidth;
				h = targetWidth*invRatio;
			}
			var l = targetWidth/2  - ((w/2) - 50);
			var t = (targetHeight/2) - ((h/2) - 20);
			
			$('.splashTemporaryStore').css({'position':'absolute', 'height':h, 'width':w, 'top':t, 'left':l}); 
			$('#splash .splashLink').css({'marginTop':(h/2)-20});
		}
