
document.ImageGallery = function() {

	this.get_gallery_offset = function() {
		return $( window ).width() / 2 - 380;
	};

	this.reset_gallery_offset = function() {
		$( '#image_gallery .images' ).css( 'margin-left' , document.image_gallery.get_gallery_offset() );
		$( '#image_gallery' ).css( 'width' , $( window ).width() );
		$( '#image_gallery' ).css( 'height' , $( window ).height() );
	};

	this.show = function() {
		$( '#image_gallery' ).fadeIn( 'fast' );
	};

	this.hide = function() {
		$( '#image_gallery' ).fadeOut( 'fast' );
	};
	
	this.display_next_image = function() {
		this.display_image( this.current_image + 1 );
	};

	this.display_previous_image = function() {
		this.display_image( this.current_image - 1 );
	};

	this.get_image_count = function() {
		return $( '#image_gallery .images' ).children( 'li' ).length;
	};

	this.display_image = function( image_id ) {

		image_id = parseInt( image_id );
		
		if( image_id == this.get_image_count() ) { image_id = 0; };
		if( image_id == 0 ) { $( '#previous_image' ).hide(); }else{ $( '#previous_image' ).show(); };

		this.current_image = image_id;

		$( '#image_gallery .images' ).animate( {
			'left' : 0 - $( '#gallery_' + image_id ).position().left
		} , 'fast' );
	};

	$( '#image_gallery' ).click( function() {
		document.image_gallery.hide();
	});

	$( window ).resize( function() {
		document.image_gallery.reset_gallery_offset();
	});

	this.current_image = 0;

};

$(document).ready(function() {

	$( '#swatch_magnifying_glass' ).hover(
		function() {
			$( '#swatch' ).slideToggle();
		},
		function() {
			$( '#swatch' ).slideToggle();
		}
	);

	document.image_gallery = new document.ImageGallery();
	document.image_gallery.reset_gallery_offset();		

	$( '#product_images li' ).click( function() {
		document.image_gallery.show();
		document.image_gallery.display_image( $( this ).attr( 'index' ) );
	});

	$( '#previous_image' ).click( function() {
		document.image_gallery.display_previous_image();
		return false;
	});

	$( '#next_image' ).click( function() {
		document.image_gallery.display_next_image();
		return false;
	});

	$('#add_to_shopping_bag_new').click(function() {
		add_product_to_shopping_bag(document.current_product_id);
	});

	$('#add_to_shopping_bag_reorder').click(function() {
		add_product_to_shopping_bag(document.current_product_id);
	});

});
