var FA = {						
	currIndex:0,
	collezioneArray:[], 				// gets refilled each time the collezione is filtered.
	objXML:null,						// the entire XML
	filter:''
};	

FA.loadSection = function(){
	FA.currIndex = 0;
	FA.collezioneArray = [];
	if(PO.subSection != '' && PO.subSection != 'all'){
		FA.filter = PO.subSection;
	}else{
		FA.filter = '';
	}
	
	$('#fabrics .content').empty();
	$('#fabricStripContainer').css('width', $(document).width() - 65);
	//set timeout for scrollbars
	setTimeout(FA.checkScrollbars, 3000);

	
	$.ajax({
	  type: "GET",
		 url: 'content/fabrics.xml',
		 context:$('#fabrics'),
		 dataType: "xml",
		 success: function(xml) {
			FA.objXML = xml;
			FA.createSecondaryMenu();
			FA.createCollezioneArray();			
			FA.loadStrip();
			$('#fabrics h1').html(langObject.tessuti);
			Cufon.replace('h1');
			if(PO.newSection == 'fabrics' && PO.subSubSection != ''){
				FA.openZoom(PO.subSubSection);	
			}
			PO.sectionLoaded = true;
			if(BGO.imageLoaded && !PO.showingSplashPage){
				PO.animateSection();
				return;
			}
			
		 },
		 error:function(e){
			//
		}
	}); //close $.ajax
};

FA.createCollezioneArray = function(){
	FA.currIndex = 0;
	FA.collezioneArray = [];
	var cat = '';
	$(FA.objXML).find('categoria').each(function(){
		cat = $(this).attr('id');
		
		
		$(this).find('tessutoGruppo').each(function(){
			var i = {};
			i.nome = $(this).attr('nome');
			i.thumb = $(this).attr('thumb');
			i.categoria = cat;
			i.index = $(this).attr('id');
			if(cat!='uk'){
				FA.collezioneArray.push(i);
			}	
			
		});	
	});
	
	
}

FA.filterCollection = function(){
	FA.currIndex = 0;
	FA.filter = PO.subSection;
	if(FA.filter=='all'){
		FA.filter = '';	
	}
	FA.animateStrip('out');
}

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


FA.loadStrip = function(){
	var div = $('#fabrics .portfolioStrip');
	var prod = "";
	
	
	$(div).empty();
	
	FA.currIndex = 0;
	FA.collezioneArray = [];
	FA.collezioneArray.length = 0;
	if(FA.filter!= '' && FA.filter != undefined && FA.filter.indexOf('filter') == -1 && FA.filter != 'uk'){
	 $(FA.objXML).find("categoria[id='" + FA.filter.toUpperCase() + "']").each(function(){
		  
		  $(this).find('tessutoGruppo').each(function(){
			  var i = {};
			  i.nome = $(this).attr('nome');
			  i.thumb = $(this).attr('thumb');
			  i.categoria = $(this).parent().attr('id');
			  i.index = $(this).attr('id');
			  if(prod == '' || (i.nome.toLowerCase().indexOf(prod.toLowerCase()) != -1) && i.categoria!='uk'){
			  	FA.collezioneArray.push(i);
			  }
		  });
	  });
	}else{
		if(FA.filter.indexOf('filter') != -1 && (FA.filter.indexOf('new') == -1 && FA.filter.indexOf('uk') == -1)){
			// filtering by name
			$('nav.secondaryMenu a#all-link').parent().siblings().find("a").removeClass('selected')
			$('nav.secondaryMenu a#all-link').addClass('selected');
			if(FA.filter.indexOf('new') == -1 && FA.filter.indexOf('uk') == -1){
				prod = FA.filter.split("filter_").join("");
			}
			
			 $(FA.objXML).find('tessutoGruppo').each(function(){
				var i = {};
				i.nome = $(this).attr('nome');
			  	i.thumb = $(this).attr('thumb');
			  	i.categoria = $(this).parent().attr('id');
				i.index = $(this).attr('id');
			  	if(prod == '' || (i.nome.toLowerCase().indexOf(prod.toLowerCase()) != -1)  && i.categoria!='uk'){
					FA.collezioneArray.push(i);					
				}
		 	 });
		}
		
		if(FA.filter.toLowerCase() == 'uk'){
			$('nav.secondaryMenu a#UK-link').parent().siblings().find("a").removeClass('selected')
			$('nav.secondaryMenu a#UK-link').addClass('selected');
			
			 $(FA.objXML).find("categoria[id='uk']").each(function(){
		  
		  		$(this).find('tessutoGruppo').each(function(){
					var i = {};
					i.nome = $(this).attr('nome');
			  		i.thumb = $(this).attr('thumb');
			  		i.categoria = $(this).parent().attr('id');
					i.index = $(this).attr('id');
			  		if(prod == '' || (i.nome.toLowerCase().indexOf(prod.toLowerCase()) != -1) ){
						FA.collezioneArray.push(i);					
					}
		 	 	});
		   	});
		}
		
		if(FA.filter==''){
			// show the lot
		  $(FA.objXML).find('tessutoGruppo').each(function(){
			  var i = {};
			  i.nome = $(this).attr('nome');
			  i.thumb = $(this).attr('thumb');
			  i.categoria = $(this).parent().attr('id');
			  i.index = $(this).attr('id');
			  if(i.categoria!='uk'){
			 	 FA.collezioneArray.push(i);
			  }
		  });	
		}
		
		
	}
	
	var arLen=FA.collezioneArray.length;
	var counter = 0;
	for ( var i=0; i<arLen; ++i ){
	    var containerDiv = $('<div class="thumbnailContainer" data-id="' + FA.collezioneArray[i].nome + '"></div>');
		
		var img = new Image();
		var src = "img/tessuti/" + FA.collezioneArray[i].thumb;
		// wrap our new image in jQuery, then:
	  	$(img)
			.load(function () { 		 	
			 	counter ++;
			  	if(counter == arLen){
				  	fleXenv.fleXcrollMain("fabricStripContainer");
					if(document.getElementById('fabricStripContainer').fleXcroll){
						document.getElementById('fabricStripContainer').fleXcroll.setScrollPos(0,false);	
					}
					fleXenv.updateScrollBars();	
			  	}
				
		  	})		
		  	.error(function () {					// notify the user that the image could not be loaded
				//
		 	 })
		  	.attr('src', src)
		 	.addClass('fabricThumbnail')
			
		 	.appendTo($(containerDiv));
			var colNome = FA.collezioneArray[i].nome
			if(lang == 'it'){
				colNome = colNome.replace("PVC", "Ecopelle Ignifuga");	
			}
		  	$(containerDiv).append('<div class="fabricCaption">' + colNome + '</div>');
		  	var containerLink = $('<a href="" class="fabricLink" rel="' + FA.collezioneArray[i].index + '"></a>');
			$(containerLink).append(containerDiv);
			$(div).append(containerLink);
			fleXenv.updateScrollBars();
	}
	
	FA.animateStrip('in');
	
	$('a.fabricLink').bind({
		click: function(e) {
			e.preventDefault();
		},	
	});
	 
	$('nav.secondaryMenu a').each(function() {	 
		if($(this).attr('id') == PO.subSection.toUpperCase()+'-link'){
			$(this).parent().siblings().find("a").removeClass('selected');
			$(this).addClass('selected');
		}
	});
	 
}

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

FA.openZoom = function(prodotto_id){
	//prodotto = prodotto.replace("+", "");
	$(FA.objXML).find("tessutoGruppo[id='" + prodotto_id + "']").each(function(){
		var xmlNode = $(this);
		var cat = $(this).parent().attr("title");
		FZOOMER.init(xmlNode, cat, FA.collezioneArray);
	});
	//$('#fabricStripContainer').css('width', $(document).width() - 130);
}

FA.createSecondaryMenu = function(){
	$('#fabrics .secondaryMenuDiv .secondaryMenu').empty();
	var list = $('<ul></ul>');
	var counter = 0;
	var numItems = $(FA.objXML).find('categoria').length;
	
	$('<li><a href="/' + lang + '/fabrics/all.html" id="all-link">' + langObject.tutti_tessuti + '</a></li>').appendTo(list);
	$('<li class="divider"></li>').appendTo(list);
		
	$(FA.objXML).find('categoria').each(function(){
		if($(this).attr('id') == 'uk'){
			return;	
		}
		counter++;
		var title = $(this).attr('title');
		if(lang == "de"){
			title = title.replace("cat", "Kat");				
		}
		title = title.replace("ignifughi", langObject.fireproof_plural);
		
		if($(this).attr('id')=='I'){
			$('<li style="margin-left:50px"><a href="/' + lang + '/fabrics/' + $(this).attr("id") + '.html" id="' +  $(this).attr('id') + '-link">' + title + '</a></li>').appendTo(list);
			$('<li class="divider"></li>').appendTo(list);
		}else{
			$('<li><a href="/' + lang + '/fabrics/' + $(this).attr("id") + '.html" id="' +  $(this).attr('id') + '-link">' + title + '</a></li>').appendTo(list);
			if(counter < numItems-2){
				$('<li class="divider"></li>').appendTo(list);		
			}
		}
	});
	
	$('<li><a href="/' + lang + '/fabrics/UK.html" id="UK-link">UK fabrics</a></li>').appendTo(list);
	
		
	$('#fabrics .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.toUpperCase()+'-link'){
				$(this).parent().siblings().find("a").removeClass('selected');
				$(this).addClass('selected');
			}
		});	
		
		if(FA.filter==''){
			$('nav.secondaryMenu a#all-link').addClass('selected');	
		}
	}, 400);
	
};



