var Gallery = new Class({
	
	initialize: function( mainImageArray ) {

        this.mainImageArray = mainImageArray;
        this.prepareSplash();
		this.processThumbs();
		this.activateNav();
		this.currentImage = 0;
		this.changeImage(0);
	},

	fetchThumbs: function() {
		var imageArray = $$('#nav_gallery li');
		return imageArray;
	},

	getThumbById: function(imageId) {
		var thumbArray = this.fetchThumbs();
		return thumbArray[imageId]; 
	},

	getThumbImage: function(listItem) {
		var thumbImg = listItem.getElementsByTagName('img');
		return $(thumbImg[0]);
	},

	getImageCount: function() {
		var thumbArray = this.fetchThumbs();
		return thumbArray.length;
	},

	processThumbs: function() {
		var imageArray = this.fetchThumbs();
		var thisObj = this;
		imageArray.each(function(li, i) {
			li.setProperty('image_id', i);
			var configureFunction = thisObj.configureThumb.bind(thisObj);
			configureFunction(li);
		});
	},

	configureThumb: function(element) {
		element.getFirst().removeProperty('href');
		element.addEvent('click', this.thumbClick.bind(this, element));
	},

	thumbClick: function(element) {
		this.changeImage(element.getProperty('image_id'));
	},

	selectThumb: function(thumbId) {
		var thisObj = this;
		var imageArray = this.fetchThumbs();
		imageArray.each(function(li, i) {
			thisObj.thumbInactive(li);
		});
		this.thumbActive(this.getThumbById(thumbId));
	},

	thumbActive: function(element) {
		element.addClass('active');
	},

	thumbInactive: function(element) {
		element.removeClass('active');
	},

	getFilePath: function(thumbId) {
		var element = this.getThumbImage(this.getThumbById(thumbId));
		var imagePath = element.getProperty('src');
		var filePath = imagePath.substring(0, imagePath.lastIndexOf('/')+1);
		return filePath;
	},

	makeImageUrl: function(thumbId, size) {
		var element = this.getThumbImage(this.getThumbById(thumbId));
		
		if (element.getProperty("src"+size)) {
			var imageUrl = element.getProperty("src"+size);
		} else {
			var filePath = this.getFilePath(thumbId);
			var imagePath = element.getProperty('src');
			var imageName = imagePath.substring(imagePath.lastIndexOf('/')+1, imagePath.length);
			var imageNameSplit = imageName.split("_");
            var imageUrl = filePath + imageNameSplit[0] + "_" + imageNameSplit[1] + "_" + imageNameSplit[2] + '_' + size + '.jpg';
        }
		return imageUrl;
		
	},

    selectMainImage : function (count) {

        var urlString = this.mainImageArray[count];
        return urlString;


    },

    // Splash

	changeImage: function(imageId) {
		if (this.currentImage != imageId) {
			this.currentImage = imageId;
            var imageUrl = this.selectMainImage(imageId);
            //var imageUrl = this.makeImageUrl(imageId, 'lge');
			this.swapImage(imageUrl); 
		}
		this.selectThumb(imageId);
		this.setImageInfo(imageId);
		this.hideNav();
	},
	
	prepareSplash: function() {
		var splashElement = $('gallery_splash');
		$$('#gallery_splash img').addClass('active');
		var newImg = new Element('img', {'styles': {'opacity': 0}});
		newImg.injectTop(splashElement);
		
		$$('#gallery_splash ul').setStyle("z-index", 10000);
	},
	
	getSplashImages: function() {
		var splashElements = $$('#gallery_splash img');
		if (splashElements[0].hasClass('active')) {
			return [splashElements[1], splashElements[0]];
		} else if (splashElements[1].hasClass('active')) {
			return [splashElements[0], splashElements[1]];
		}
	},

	swapImage: function(url) {
		var splashElements = this.getSplashImages();
		splashElements[0].setProperty('src', url);
		splashElements[0].addClass('active');
		splashElements[1].removeClass('active');
		
		splashElements[0].fade('in');
		splashElements[1].fade('out');
	},
	
	// Change image info
	
	setImageInfo: function(imageId) {
		var infoArray = this.getImageInfo(imageId);
		$$('#gallery_info h3').set('text', infoArray[0]);
		$$('#gallery_info p span').set('text', infoArray[1]);
		$$('#gallery_info p a').setProperty("href", this.makeImageUrl(imageId, 'download'));
	},
	
	getImageInfo: function(imageId) {
		var thumbImg = this.getThumbImage(this.getThumbById(imageId));
		if (thumbImg.getProperty("title")) {
			var imageAttribute = thumbImg.getProperty("title");
			var imageTitle = imageAttribute.substring(0, imageAttribute.lastIndexOf(' -- '));
			var imageDesc = imageAttribute.substring(imageAttribute.lastIndexOf(' -- ')+4, imageAttribute.length);
		}
		else {
			var imageAttribute = "";
			var imageTitle = "";
			var imageDesc = "";
		}
		return [imageTitle, imageDesc];
	},

	// Navigation

	activateNav: function() {
		var navLinks = $$('#gallery_splash ul a');
		if (navLinks.length > 0) {
			navLinks[0].removeProperty('href');
			navLinks[1].removeProperty('href');
			navLinks[0].addEvent('click', this.previousImage.bindAsEventListener(this, navLinks[0]));
			navLinks[1].addEvent('click', this.nextImage.bindAsEventListener(this, navLinks[1]));
		}
	},


	previousImage: function() {
		if (this.currentImage > 0) {
			this.changeImage(this.currentImage-1);
		}
	},
	
	nextImage: function() {
		var imageCount = parseInt(this.getImageCount());
		if (this.currentImage+1 < imageCount) {
			this.changeImage(this.currentImage+1);
		}
	},
	
	hideNav: function() {
		var navLinks = $$('#gallery_splash ul a');
		if (navLinks.length > 0) {
			if (this.currentImage == 0) {
				navLinks[0].setStyle("display", "none");
			} else {
				navLinks[0].setStyle("display", "block");
			}
			if (this.currentImage+1 == this.getImageCount()) {
				navLinks[1].setStyle("display", "none");
			} else {
				navLinks[1].setStyle("display", "block");
			}
		}
	}
	
});

window.addEvent('domready', function() { 
	var myGallery = new Gallery(mainImageArray);
});
