var QuickGallery = new Class({

	//implements
	Implements: [Options],

	//origColor: null,
	positionsArray: new Array(),		//to store item positions
	itemNum: null,
	totalSlides: 0,					
	curIndex: null,  				
	prevIndex: null,				
	curPic: null,							//index of current pic (from mini-thumbs)
	prevPic: null,						//index of prev. pic (from mini-thumbs)
	activeThumb: null,

	//options
	options: {
		stage: null,
		thumbs: null,
		thumbsHolder: null,
		items: new Array(),
		thumbsDir: null,			
		transitionTime: 1000
	},
	
	
	
	//initialization
	initialize: function(options) {		
		//set options
		this.setOptions(options);
		
		var self = this;
		var tempCount = 0;
		//var tempSlide = null;
		
		
		self.options.stage.setStyles({
			'overflow': 'hidden',
			'position': 'relative'
			
		});
		
		self.options.thumbsHolder.set('tween', {
			'duration' : 1000,
			'ease' : 'cubic:out',
			'link' : 'ignore'
		});
		
		
		self.options.items.each(function(el, k){
			
			var cloned = el.clone();
			var theSrc = el.getElement('a').get('href');
			var destroyed = cloned.getElement('a').destroy();
		    
			var newImg = new Element('img');
			var uri = new URI(theSrc);
			var filename = uri.get('file');
			var mainImg = $(self.options.stage);
			var colorway = $(el.getElement('a')).get('rel');
			
			var thumbSrc = self.options.thumbsDir + filename;
			
			cloned.setProperty('rel', colorway);
			newImg.setProperty('src', thumbSrc);
			newImg.setStyles({ 'width': 40, 'height': 40, 'cursor': 'pointer'});
			cloned.grab(newImg);
			
			$('minithumbs').grab(cloned);
			
			//ditch the original (text) link
			var destroyed = el.getElement('a').destroy();
			
			
			//add click events to each 'cloned' li
			cloned.addEvents({
				'click': function(e){
					var newPic = new Element('img');
					newPic.setProperty('src', theSrc);
					//newPic.set('opacity',.001);
					$('colorway').set('text', this.getProperty('rel'));
					if(self.activeThumb != null) {
						self.activeThumb.fade('in');	
					}
					
					self.activeThumb = this;
					this.set('opacity', .6);
					
					mainImg.fade('hide');
					//mainImg.fade('hide');					
					mainImg.getElement('img').dispose();
					
					self.options.stage.grab(newPic);
					
					
					//mainImg.fade('hide');
					//mainImg.set('opacity',.0001)
					//mainImg.setProperty('src', imgSrc);
					mainImg.fade('in');
					
				}
			});
			
			//fire off first thumb, to ensure new product view starts on 1st img
			if(k == 0){
				cloned.fireEvent('click');
			}
				
		});
		
		
		
		
		
	
	},

	//my post-initialization (startup) function
	start: function() {
		
		var self = this;

		//alert('test');
		
	},
	
	
	
	
	
	showMiniThumbs: function(item_id) {
		
		var self = this;
		var myMenu = $('minithumbs');
		
		//hide #mini_thumbs, empty li's
		myMenu.fade('hide');
		myMenu.getElements('li').each(function(el, k) {
			el.destroy();
		});
		
		
		//repopulate with new li's (which are stored inside each 'item' element)
		var newThumbs = self.options.items[item_id].retrieve('miniThumbs');
		newThumbs.each(function(el, k) {
			var cloned = el.clone();
			var newImg = new Element('img');
			var imgSrc = $(cloned.getElement('a')).get('href');
			var uri = new URI(imgSrc);
			var filename = uri.get('file');
			var mainImg = $(self.options.items[item_id].getElement('.box.item'));
			var colorway = $(cloned.getElement('a')).get('rel');
			
			var thumbSrc = self.options.thumbsDir + filename;
			
			cloned.setProperty('rel', colorway);
			newImg.setProperty('src', thumbSrc);
			newImg.setStyles({ 'width': 25, 'height': 25, 'cursor': 'pointer'});
			cloned.grab(newImg);
			
			$('minithumbs').grab(cloned);
			
			//ditch the original (text) link
			var destroyed = cloned.getElement('a').destroy();
			
			
			//add click events to each 'cloned' li
			cloned.addEvents({
				'click': function(e){
					var newPic = new Element('img');
					newPic.setProperty('src', imgSrc);
					//newPic.set('opacity',.001);
					
					
					mainImg.fade('hide');
					//mainImg.fade('hide');					
					mainImg.getElement('img').dispose();
					
					self.options.items[item_id].getElement('.box.item').grab(newPic);
					
					
					//mainImg.fade('hide');
					//mainImg.set('opacity',.0001)
					//mainImg.setProperty('src', imgSrc);
					mainImg.fade('in');
					
				}
			});
			
			//fire off first thumb, to ensure new product view starts on 1st img
			if(k == 0){
				cloned.fireEvent('click');
			}
		})
		
		
		
		
		//show $mini_thumbs again
		$('minithumbs').fade('in');
		
		
		//alert("item_id:" + item_id);
		
	},
	
	
	showTearsheet: function(item_id) {
		
		var self = this;
		var myBtn = $('lowerbar').getElement('.dltearsheet');
		
		//hide download button, destroy it
		myBtn.fade('hide');
		myBtn.destroy();
		
		
		//repopulate with new element
		var newBtn = self.options.items[item_id].retrieve('tearsheet');
		var cloned = newBtn.clone();
		

		$('lowerbar').grab(cloned);		
		cloned.set('tween', { 'duration' : 1000, 'ease' : 'cubic:out' });
		cloned.set('opacity', .0001);
		
		//show btn
		var tempHref = cloned.getElement('a').get('href');
		var tempLength = tempHref.length;
		var lastChar = tempHref.charAt(tempLength - 1);
		
		if(lastChar != '#') cloned.fade('in');
		
	}
	
	
	
});