/**
 * Image gallery
 * See page: Bikes & equipment > Equipment
 */
jQuery.fn.equipmentGallery = function (){
	var preloader = jQuery(new Image());

	var collections = jQuery(this);
	collections.each(function(n, me){
		me = jQuery(me);
		jQuery(me)
			// Create the code for the image gallery
			.before(
					jQuery('<div class="collection-parsed w51 float-left"><\/div>')
						.append(
								jQuery('<a href="javascript:;" class="collection-title active"><\/a>')
									.html(jQuery('h1:first', me).html())
									.click(function(){
											// Show the men's collection
											jQuery('.collection-gallery.men a:first').trigger('click');
											jQuery('.collection-gallery.women').slideUp();
											jQuery('.collection-gallery.men').slideDown();
											jQuery('.collection-title:first').addClass('active');
											jQuery('.collection-title:last').removeClass('active');
										})
							)
						.append(
								jQuery('<div id="bigpicture"><img src="images\/blank.gif" alt=""\/><\/div>')
							)
						.append('<a class="addthis_button" href="http://www.addthis.com/bookmark.php?v=250&amp;username=xa-4b5f0de520aeba4e"><img src="http://s7.addthis.com/static/btn/sm-share-en.gif" width="83" height="16" alt="Bookmark and Share" style="border:0"\/></a>')
						.append('<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#username=xa-4b5f0de520aeba4e"><\/script>')
						.append('<div class="clear"><!-- IE --><\/div>')
						.append('<div class="description"><\/div>')
						.append('<div class="clear"><!-- IE --><\/div>')
				);
		
		me
			.before(
					jQuery('<div class="collection-parsed w48 float-right"><\/div>')
						.append(
								jQuery('<a href="javascript:;" class="collection-title"><\/a>')
									.html( jQuery('h1:first', me.next()).html() )
									.click(function(){
											// Show the women's collection
											jQuery('.collection-gallery.women a:first').trigger('click');
											jQuery('.collection-gallery.men').slideUp();
											jQuery('.collection-gallery.women').slideDown();
											jQuery('.collection-title:first').removeClass('active');
											jQuery('.collection-title:last').addClass('active');
										})
							)
				)
			.after('<div class="clear"><!-- IE --><\/div>');

		// Create the collections
		jQuery('.collection h2').each(function(m, el){
				el = jQuery(el);
				me.prev()
					.append(
							jQuery('<div class="collection-gallery"><\/div>')
								.append(el.clone())
								.addClass( el.parent().index(collections)==0 ? 'men' : 'women' )
						);

				jQuery('.women').hide();

				var container = jQuery('.collection-gallery:last', me.prev());
				jQuery('a', jQuery(el).next()).each (function(o, te){
						jQuery(te)
							.clone()
							.appendTo(container)
							.addClass( o%4==3 && o>0 ? 'modulus4' : '')
							.click(function(){
								var a = jQuery(this);
								jQuery('#bigpicture img').fadeOut('fast', function(){
										preloader
											.load(function(){
													jQuery('#bigpicture img')
														.attr('src', preloader.attr('src'))
														.fadeIn();
												})
											.attr('src', a.attr('href'));
									});

								var collectionIndex = a.parent().hasClass('men') ? '0' : '1';
								var tierIndex = jQuery(collectionIndex=='0' ? '.men' : '.women').index(a.parent());
								var liIndex = jQuery('a', a.parent()).index(a);
								jQuery('.description').html(jQuery('.collection:eq('+collectionIndex+') ul:eq('+tierIndex+') li:eq('+liIndex+')').html());

								return false;
							});
						jQuery(te).hide();
					});

				container.append('<div class="clear"><!-- IE --><\/div>');
			});

		jQuery('.collection').hide();
		var li = jQuery('.collection:eq(0) li:first');
		jQuery('#bigpicture img').attr('src', jQuery('a', li).attr('href'));
		jQuery('.description').html(li.html());

		return false;
	});
};

jQuery(document).ready(function(){
	// Initialize the gallery
	jQuery('.collection').equipmentGallery();
});
