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

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

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

CO.createCollezioneArray = function(){
	CO.currIndex = 0;
	CO.collezioneArray = [];
	var tip = '';
	$(CO.objXML).find('tipologia').each(function(){
		tip = $(this).attr('id');
		$(this).find('prodotto').each(function(){
			var i = {};
			i.nome = $(this).attr('nome');
			i.thumb = $(this).attr('thumb');
			i.tipologia = tip;
			i.path = $(this).parent().attr('path');
			i.isNew = $(this).attr('news');
			i.isFireproof = $(this).attr('ignifugo');
			CO.collezioneArray.push(i);
		});	
	});
}

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

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


CO.loadStrip = function(){
	var div = $('#collections .portfolioStrip');
	var prod = "";
	
	$(div).empty();
	
	CO.currIndex = 0;
	CO.collezioneArray = [];
	CO.collezioneArray.length = 0;
	
			
	if(CO.filter!= '' && CO.filter != undefined && CO.filter.indexOf('filter') == -1){
		
	  $(CO.objXML).find("tipologia[id='" + CO.filter + "']").each(function(){
		  $(this).find('prodotto').each(function(){
			  var i = {};
			  i.nome = $(this).attr('nome');
			  i.thumb = $(this).attr('thumb');
			  i.path = $(this).parent().attr('path');
			  if(prod == '' || (i.nome.toLowerCase().indexOf(prod.toLowerCase()) != -1)){
			  	CO.collezioneArray.push(i);
			  }
		  });
	  });
	}else{
		if(CO.filter.indexOf('filter') != -1 && (CO.filter.indexOf('new') == -1 && CO.filter.indexOf('fireproof') == -1)){
			// filtering by name
			$('nav.secondaryMenu a#all-link').parent().siblings().find("a").removeClass('selected')
			$('nav.secondaryMenu a#all-link').addClass('selected');
			if(CO.filter.indexOf('new') == -1 && CO.filter.indexOf('fireproof') == -1){
				prod = CO.filter.split("filter_").join("");
			}
			 $(CO.objXML).find('prodotto').each(function(){
				var i = {};
				i.nome = $(this).attr('nome');
				i.thumb = $(this).attr('thumb');
				i.path = $(this).parent().attr('path');
				if(prod == '' || (i.nome.toLowerCase().indexOf(prod.toLowerCase()) != -1)){
					CO.collezioneArray.push(i);
				}
		 	 });
		}
		if(CO.filter.indexOf('new') != -1){
			//filter by news
			$(CO.objXML).find("prodotto[news='yes']").each(function(){
				var i = {};
				i.nome = $(this).attr('nome');
				i.thumb = $(this).attr('thumb');
				i.path = $(this).parent().attr('path');
				CO.collezioneArray.push(i);				
		 	 });
		}
		if(CO.filter.indexOf('fireproof') != -1){
			// filter by fireproof material
			$(CO.objXML).find("prodotto[ignifugo='yes']").each(function(){
				var i = {};
				i.nome = $(this).attr('nome');
				i.thumb = $(this).attr('thumb');
				i.path = $(this).parent().attr('path');
				CO.collezioneArray.push(i);				
		 	 });
		}
		
		if(CO.filter==''){
			// show the lot
		  $(CO.objXML).find('prodotto').each(function(){
			  var i = {};
			  i.nome = $(this).attr('nome');
			  i.thumb = $(this).attr('thumb');
			  i.path = $(this).parent().attr('path');
			  CO.collezioneArray.push(i);
		  });	
		}
	}
	
	var arLen=CO.collezioneArray.length;
	var counter = 0;
	for ( var i=0; i<arLen; ++i ){
	    var containerDiv = $('<div class="thumbnailContainer" data-id="' + CO.collezioneArray[i].nome + '"></div>');
		
		var img = new Image();
		var src = "img/collezioni/" + CO.collezioneArray[i].thumb;
		// wrap our new image in jQuery, then:
	  	$(img)
			.load(function () { 		 	
			 	counter ++;
			  	if(counter == arLen){
				  	fleXenv.fleXcrollMain("portfolioStripContainer");
					if(document.getElementById('portfolioStripContainer').fleXcroll){
						document.getElementById('portfolioStripContainer').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">' + CO.collezioneArray[i].nome + '</div>');
		  	var containerLink = $('<a href="" class="galleryLink" rel="' + CO.collezioneArray[i].nome + '"></a>');
			$(containerLink).append(containerDiv);
			$(div).append(containerLink);
			fleXenv.updateScrollBars();
	}
	
	CO.animateStrip('in');
	
	 $('a.galleryLink').bind({
		click: function(e) {
			e.preventDefault();
		},
		
	  });
	
	 
}

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

CO.openZoom = function(prodotto){
	prodotto = prodotto.replace("+", "");
	$(CO.objXML).find("prodotto[nome='" + prodotto + "']").each(function(){
		var xmlNode = $(this);
		ZOOMER.init(xmlNode);
	});
	//$('#portfolioStripContainer').css('width', $(document).width() - 130);
}

CO.createSecondaryMenu = function(){
	$('#collections .secondaryMenuDiv .secondaryMenu').empty();
	$('#collections .searchDiv').empty();
	var list = $('<ul></ul>');
	var counter = 0;
	var numItems = $(CO.objXML).find('tipologia').length;
	
	$('<li><a href="/' + lang + '/collections/all.html" id="all-link">' + langObject.tutti_prodotti + '</a></li>').appendTo(list);
	$('<li class="divider"></li>').appendTo(list);
		
	$(CO.objXML).find('tipologia').each(function(){
		counter++;
		$('<li><a href="/' + lang + '/collections/' + $(this).attr("id") + '.html" id="' +  $(this).attr('id') + '-link">' + $(this).attr('id') + '</a></li>').appendTo(list);
		if(counter < numItems){
			$('<li class="divider"></li>').appendTo(list);		
		}
	});
	
	$('<li style="width:60px">&nbsp;</li>').appendTo(list);
	$('<li><a href="/' + lang + '/collections/filter_new.html" id="filter_new-link">' + langObject.news + ' 2011</a></li>').appendTo(list);
	$('<li class="divider"></li>').appendTo(list);		
	$('<li><a href="/' + lang + '/collections/filter_fireproof.html" id="filter_fireproof-link">' + langObject.fireproof + ' 1IM</a></li>').appendTo(list);
		
	
	$('#collections .secondaryMenuDiv .secondaryMenu').append(list);
	
	
	$('<input name="search" class="darkInput" id="search" type="text" value=""><a href="" class="notDefaultNav searchProdotto">' + langObject.cerca + '</a>').appendTo($('#collections .searchDiv'));
	$('#search').attr('value', langObject.cercaPerNome);
	
	$('#search').live("focus", function(){	
		if($(this).attr('value') == langObject.cercaPerNome){
			$(this).attr('value', '');		
		}
	})
	$('#search').live("blur", function(){
		if($(this).attr("value") == ""){
			$(this).attr('value', langObject.cercaPerNome);
		}
	})
	
	//$('.searchProdotto').html(langObject.cerca);
	//$('#search').attr('placeholder', langObject.cercaPerNome);
	//$('<a href="/' + lang + '/collections/all/" rel="address:/' + lang + '/collections/all/" id="close_spotlight-link"><img src="img/chiudi.png" id="close_spotlight"></a>').appendTo($('#collections .searchDiv'));
		
		
	// 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');
			}
		});	
		$('.searchProdotto').click(function(e){
			e.preventDefault();
			if(document.getElementById("search").value == "" || document.getElementById("search").value == langObject.cercaPerNome){
				return;	
			}
			$.address.value('/' + lang + '/collections/filter_'+document.getElementById("search").value);
		});
		$('#search').keypress(function(e){
      		if(e.which == 13){
				e.preventDefault();
      			$.address.value('/' + lang + '/collections/filter_'+document.getElementById("search").value);
				return false;
       		}
      	});
		$('#close_spotlight-link').click(function(e){
			$('#search').val('');								  
		})
		if(CO.filter==''){
			$('nav.secondaryMenu a#all-link').addClass('selected');	
		}
	}, 400);
	
};



