storis.prodImages = new function(){
	this.buttonLink = null;
	this.enlargedURL = null;		// gets populated from the active href attribute of the enlarged image link.
	
	this.init = function() {
		var self = storis.prodImages;
		storis.coreCode.addEventSimple(window,'load', self.loadImageScripts);
		
	};
	
	this.loadImageScripts = function(){
		var self = storis.prodImages;
		self.thumbSlider('.image-container');
	};
	
	this.thumbSlider = function(container){
		var self = storis.prodImages
		//Get our elements for faster access and set overlay width
		var div = $('div.sc_menu', $(container)),
			ul = $('ul.sc_menu'),
			ulPadding = 15;
			
			//Get menu width
			var divWidth = div.width();
		 
			//Remove scrollbars	
			div.css({overflow: 'hidden'});
			
			//Find last image container
			var lastLi = ul.find('li:last-child');
			
			//When user move mouse over menu
			div.mousemove(function(e){
				//As images are loaded ul width increases,
				//so we recalculate it each time
				var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;	
				var left = (e.pageX - div.offset().left) * (ulWidth-divWidth) / divWidth;
				div.scrollLeft(left);
			});		
	};
	
	this.setEnlargedTrigger = function(){
		var self = storis.prodImages;
		self.enlargedURL = $('#enlarged-view-link').attr("href") +  '&ajax=true';
		$('#enlarged-view-link').attr({
			'href': 'javascript:void(0);',
			'target': '_self'
		}).click(function(){
			self.loadEnlargedView();
		});
	};
	
	this.loadEnlargedView = function(){
		var self = storis.prodImages;		
		var GALLERY = {
			delay: 5000,
			attempts: 3,
			running: false,
			url: self.enlargedURL,
			load: function() {				
				if (this.running) { // Don't call if already running!
					return;
				}
				this.running = true;
				var _gallery = this;
				$.ajax({
					type: "GET",
					url: _gallery.url,
					dataType: 'html',
					cache: false,
					success: function(data){
						_gallery.display(data);
					}
				});
			},
			display: function(data) {	// Process the data
				var container = storis.coreCode.expandDiv(data, (screen.width- 100), (screen.height - 250));
				container.ready(function(){
					self.thumbSlider('.enlarged-image-container');
					self.setMultiImageTrigger('.enlarged-image-container');
				});
			},
			reset: function() {
				this.delay = 5000;
				this.attempts = 3;
			}
			
		};
		GALLERY.load();
	};
	
	this.debug = function(data){
		var self = storis.prodImages;
		var textarea = $(document.createElement('textarea'));
		$(textarea).text(data).css({
			'width': '300px',
			'height': '500px',
			'position': 'absolute',
			'top': '20px',
			'left': '20px',
			'z-index': '500'
		});
		$(textarea).appendTo('body');
	}
};

$(storis.prodImages.init());
