

$(function () { gallery.initiate(); });

var gallery = function () {
	
	var imgObj = new Image();
	
	var loadedImgs = {};
	
	var config = {
		  images : 'IMG'
		, mainItem : '#gallery_main_item'
		, thumbItem : '.gallery_item_thumb'
	};
	
	var params = {
		baseUrl : ''
	};
	
	function setImageObj(newImg,callback){

	    imgObj.src = newImg;
		imgObj.onload = callback;
		imgObj.onerror = function(){ }; 
	};
	
	
		
	function setGalleryItem ( path )  {
		
		$('.product_image_table').addClass('image_loading');
		
		$(config.mainItem).parent().stop().animate({opacity : 0}, 400, function () {
			
			
			if($.browser.msie && loadedImgs[path]) {
				setImageObj(path,null);
				
				$(config.mainItem).attr('src', path);
				$('.product_image_table').removeClass('image_loading');
				$(config.mainItem).parent().animate({opacity : 1},'fast',function(){ $(this).css('opacity',null) });
			}
			else {

				setImageObj(path,function(){
					loadedImgs[path] = true;
					$(config.mainItem).attr('src', path);
					$('.product_image_table').removeClass('image_loading');
					$(config.mainItem).parent().animate({opacity : 1},'fast',function(){ $(this).css('opacity',null) });
				});
			}
		});
	}
	
	return {
		
		preloadImages : function (imgs) {
			if ( document.images ) {
				var imgArr = [];
				for ( var i = 0, n = imgs.length; i < n; i++ ) {
					imgArr[i] = new Image();
					imgArr[i].src = imgs[i];
				};
			};
		},
		
		showImage : setGalleryItem,
		
		initiate : function () {
			
		}
	};
	
}();

