window.addEvent('domready', function(){
	
	var count_swatches = $$('.colour_swatch').length;
	
	var count_textures = $$('.colour_swatch .texture').length;
	
	//console.log('Moving textures... ('+count_swatches+' Swatches, '+count_textures+' Textures)');
	
	$$('.colour_swatch').each(function(item,index){
		
		var texture;
		
		if($chk(texture = item.getElement('.texture'))){

			texture.inject($('textures'));
			
			//console.log('Texture '+index+' Moved');
		
		}		

	});
	
	
	var index_offset = 0;

	var is_textures_open = false;
	
	$$('.colour_swatch').each(function(item, index){

		if( item.getElement('.swatch') && item.getElement('.swatch').get('html') != '' ) {

			var texture_index = index - index_offset;
		
			/*
				Debugging
			
			var texture_found = 'texture found';
			if(!$chk($$('#textures .texture')[texture_index])){
				var texture_found = 'texture not found';
			}			
			//console.log('Texture '+texture_index+' (Real Index: '+index+') of '+$$('#textures .texture').length+' ('+texture_found+')');
			*/

			//var colour_texture = $$('#textures .texture')[texture_index];
			
			/*
			colour_texture.set('slide', {
				duration:500, 
				transition: Fx.Transitions.Cubic.easeIn
			}).slide('hide');
			*/
			
			var colour_info = item.getElements('.swatch_details').hide();
	
			item.addEvents({
				'mouseenter': function(){
					colour_info.show();
					if(is_textures_open == false){
					
						is_textures_open = true;
					}
					//colour_texture.slide('in');
				},
				'mouseleave' : function(){
					colour_info.hide();
					//colour_texture.slide('out');
				}
			});

		}else{
			index_offset++;
		}
	
	});


	//$$('#textures .texture').setStyle('overflow','show');
	var myAccordion = new Fx.Accordion($$('.colour_swatch'), $$('#textures .texture'), {
		display : -1,
		trigger : 'mouseenter',
		onActive: function(toggler, element) { element.setStyle('height', 'auto'); }, 
		onBackground: function(toggler, element) { element.setStyle('height', 0); } 
	});		

	
});