var MO = {						
	currIndex:0,
	materassoArray:[], 				
	objXML:null,						// the entire XML
	filter:''
};	

MO.loadSection = function(){
	MO.currIndex = 0;
	MO.materassoArray = [];
	if(PO.subSection == ''){
		PO.subSection = 'our_mattresses'
	}
	
	$('#mattresses .content').empty();
	$('#mattressStripContainer').css('width', $(document).width() - 65);
	//set timeout for scrollbars
	setTimeout(MO.checkScrollbars, 3000);

	
	$.ajax({
	  type: "GET",
		 url: 'content/' + lang + '/mattresses.xml',
		 context:$('#mattresses'),
		 dataType: "xml",
		 success: function(xml) {
			MO.objXML = xml;
			MO.createSecondaryMenu();
			MO.createMaterassoArray();			
			if(PO.subSection == 'our_mattresses'){
				$('#mattressInfoContainer').hide();
				$('#mattressStripContainer').show();
				MO.loadStrip();
			}else{
				MO.loadSubSection();	
			}
			$('#mattresses h1').html(langObject.materassi);
			Cufon.replace('h1');
			if(PO.newSection == 'mattresses' && PO.subSubSection != ''){
				MO.openZoom(PO.subSubSection);	
			}
			PO.sectionLoaded = true;
			if(BGO.imageLoaded && !PO.showingSplashPage){
				PO.animateSection();
				return;
			}
			
		 },
		 error:function(e){
			//
		}
	}); //close $.ajax
};

MO.createMaterassoArray = function(){
	MO.currIndex = 0;
	MO.materassoArray = [];
	var tip = '';
	$(MO.objXML).find('materasso').each(function(){		
		var i = {};
		i.id = $(this).attr('id');
		i.thumb = $(this).attr('thumb');
		i.title = $(this).attr('title');
		MO.materassoArray.push(i);			
	});
}

MO.loadSubSection = function(){
	if(PO.subSection == 'our_mattresses'){
		MO.loadStrip();
		$('#mattressInfoContainer').hide();
		$('#mattressStripContainer').show();
	}else{
		$('#mattressInfoContainer').show();
		$('#mattressStripContainer').hide();
		$(MO.objXML).find("section[id='" + PO.subSection + "']").each(function(){		
			$('#mattressInfoContainer').html($(this).text());		
		});
	}
}


MO.animateStrip = function(dir){
	if(dir=='out'){
		//$('.portfolioStrip').fadeOut(500, function(){
		$('#mattresses .portfolioStrip').hide();
			MO.loadStrip();			  
		//})	
	}else{
		//$('.portfolioStrip').fadeIn(500);
		$('#mattresses .portfolioStrip').show();
		
	}
}


MO.loadStrip = function(){
	
	var div = $('#mattresses .portfolioStrip');
	var prod = "";
	
	$(div).empty();
	
	MO.currIndex = 0;
	MO.materassoArray = [];
	MO.materassoArray.length = 0;

	$(MO.objXML).find('materasso').each(function(){
		var i = {};
		i.id = $(this).attr('id');
		i.thumb = $(this).attr('thumb');
		i.title = $(this).attr('title');
		MO.materassoArray.push(i);
	});	
	

	
	var arLen=MO.materassoArray.length;
	var counter = 0;
	for ( var i=0; i<arLen; ++i ){
	    var containerDiv = $('<div class="thumbnailContainer" data-id="' + MO.materassoArray[i].title + '"></div>');
		
		var img = new Image();
		var src = "img/" + MO.materassoArray[i].thumb;
		// wrap our new image in jQuery, then:
	  	$(img)
			.load(function () { 		 	
			 	counter ++;
			  	if(counter == arLen){
				  	fleXenv.fleXcrollMain("mattressStripContainer");
					if(document.getElementById('mattressStripContainer').fleXcroll){
						document.getElementById('mattressStripContainer').fleXcroll.setScrollPos(0,false);	
					}
					fleXenv.updateScrollBars();	
			  	}
				
		  	})		
		  	.error(function () {					// notify the user that the image could not be loaded
				//
		 	 })
		  	.attr('src', src)
		 	.addClass('thumbnail')
			
		 	.appendTo($(containerDiv));
		  	$(containerDiv).append('<div class="caption">' + MO.materassoArray[i].title + '</div>');
		  	var containerLink = $('<a href="" class="mattressLink" rel="' + MO.materassoArray[i].title + '"></a>');
			$(containerLink).append(containerDiv);
			$(div).append(containerLink);
			fleXenv.updateScrollBars();
	}
	
	MO.animateStrip('in');
	
	$('a.mattressLink').bind({
		click: function(e) {
			e.preventDefault();
		}		
		
	});
	
	 
}

MO.checkScrollbars = function(){
	if(!document.getElementById('mattressStripContainer').fleXcroll){
		fleXenv.fleXcrollMain("mattressStripContainer");
	}
	fleXenv.updateScrollBars();
}

MO.openZoom = function(prodotto){
	/*prodotto = prodotto.replace("+", "");*/
	$(MO.objXML).find("materasso[title='" + prodotto + "']").each(function(){
		var xmlNode = $(this);
		MZOOMER.init(xmlNode);
	});
	$('#mattressStripContainer').css('width', $(document).width() - 130);
}

MO.createSecondaryMenu = function(){
	$('#mattresses .secondaryMenuDiv .secondaryMenu').empty();
	$('#mattresses .searchDiv').empty();
	var list = $('<ul></ul>');
	var counter = 0;
	var numItems = $(MO.objXML).find('section').length;
	
		
	$(MO.objXML).find('section').each(function(){
		counter++;
		$('<li><a href="/' + lang + '/mattresses/' + $(this).attr("id") + '.html" id="' +  $(this).attr('id') + '-link">' + $(this).attr('title') + '</a></li>').appendTo(list);
		if(counter < numItems){
			$('<li class="divider"></li>').appendTo(list);		
		}
	});
	
	$('#mattresses .secondaryMenuDiv .secondaryMenu').append(list);
	
	
		
	// need to set timeout to add the actions etc - otherwise the elements aren't ready and nothing happens
	setTimeout(function(){				
		$('nav.secondaryMenu a').each(function() {	
			if($(this).attr('id') == PO.subSection+'-link'){
				$(this).parent().siblings().find("a").removeClass('selected');
				$(this).addClass('selected');
			}
		});	
		/*if(MO.filter==''){
			$('nav.secondaryMenu a#all-link').addClass('selected');	
		}*/
	}, 400);
	
};



