var Switchy = new Class({
	
	Implements: [Options, Events],
	
	options: {
		
		optionsId: "photoLibrary",
		frameId: "mainPhoto",
		nextId: "aNextPic",
		prevId: "aPrevPic",
		
		effectTransition: Fx.Transitions.Sine.easeOut,
		effectDuration: 500,
		tileWidth: 122,
		allowClicks: false
		
	},
	
	initialize: function(options){
		
		this.setOptions(options);
		
		// Set Update Function
		if($(this.options.frameId)){
		
			$(this.options.frameId).update = function(image){
		
				if(this.innerHTML != "") this.innerHTML = "";
			
				this.innerHTML = "<img src='"+image+"' />";
			
			}
			
		}
		
		// Remap A Tags
		var buttons = $$("#"+this.options.optionsId+" a");
		
		if(buttons.length > 0){
			
			buttons.each(function(e){
			
				e.frame = this.options.frameId;
			
				e.addEvent('click', function(event){
				
					event.stop();
					$(this.frame).update(this.href);
				
				});
			
			}, this);
			
			// Map initial
			$(this.options.frameId).update(buttons[0].href);
			
		}
		
		// Map Buttons
		if($(this.options.nextId)){
			
			$(this.options.nextId).duration = this.options.effectDuration;
			$(this.options.nextId).transition = this.options.effectTransition;
			$(this.options.nextId).tilewidth = this.options.tileWidth;
			$(this.options.nextId).addEvent('click', function(event){
			
				var slider = $('ListingGallery');
			
				if(parseInt(slider.getStyle('margin-left')) > -(this.tilewidth * ($$('#'+slider.id+' a').length-3))){
			
					var targetleft = parseInt(slider.getStyle('margin-left')) - this.tilewidth;
			
					var slide = new Fx.Morph(slider, {duration: this.duration, transition: this.transition}, this);
			
					slide.start({
			
						'margin-left' : targetleft
				
					});
				
				}
			
			});
			
		}
		
		if($(this.options.prevId)){
			
			$(this.options.prevId).duration = this.options.effectDuration;
			$(this.options.prevId).transition = this.options.effectTransition;
			$(this.options.prevId).tilewidth = this.options.tileWidth;
			$(this.options.prevId).addEvent('click', function(event){
			
				var slider = $('ListingGallery');
			
				if(parseInt(slider.getStyle('margin-left')) < 0){
			
					var targetleft = parseInt(slider.getStyle('margin-left')) + this.tilewidth;
			
					var slide = new Fx.Morph(slider, {duration: this.duration, transition: this.transition}, this);
			
					slide.start({
			
						'margin-left' : targetleft
				
					});
				
				}
			
			});
			
		}
		
	}
	
});
