var Core = (function($){
	var my = {},
		_p = {};
	
	_p.initMyApp = function(){
		window.addEvent('load', function(){
			//imgSizer.collate();
			
			// Set a timeout...
			setTimeout(function(){
			  // Hide the address bar!
			  window.scrollTo(0, 1);
			}, 0);
		});
	}();
	
	my.gallery = function(){
		var _p = {};
			
		_p.galleries = $$('.gallery .inner[data-images]');
		
		if ( ! _p.galleries.length || Browser.ie6) {
			return;
		}
		
		_p.galleries.each(function(gallery){
			gallery.images = gallery.get('data-images').split('|');
			gallery.parent = gallery.getParent();
			
			_p.next = function(){
				var active = _p.wrapper.getElement('img.active');
				
				if (active.getNext('img')) {
					_p.scrollAndSetActive(active.getNext('img'));
				}	
			};
			
			_p.prev = function(){
				var active = _p.wrapper.getElement('img.active');
				
				if (active.getPrevious('img')) {
					_p.scrollAndSetActive(active.getPrevious('img'));
				}	
			};
			
			_p.scrollAndSetActive = function(img) {
				_p.wrapper.getElements('img').removeClass('active');
				_p.myScroll.toElement(img);
				img.addClass('active');
			};
			
			if (gallery.images.length > 1) {
				_p.buildControls = function(){
					gallery.controls = {
						prev: new Element('a', {
							'href': 'javascript:void(0);',
							'class': 'control previous',
							'events': {
								click: _p.prev
							}
						}),
						next: new Element('a', {
							'href': 'javascript:void(0);',
							'class': 'control next',
							'events': {
								click: _p.next
							}
						})
					};
					
					gallery.controls.next.inject(gallery, 'after');
					gallery.controls.prev.inject(gallery, 'after');
				}();
				
				_p.buildContainer = function(){
					var	dimension = gallery.getChildren('img')[0].getCoordinates();
					
					_p.scrollWrapper = new Element('div', {
						'class': 'scrollWrapper',
						'styles': {
							width: dimension.width + 'px',
							height: dimension.height + 'px',
							overflow: 'hidden',
							position: 'relative'
						}
					});
					
					_p.wrapper = new Element('div', {
						'class': 'wrapper',
						'styles': {
							float: 'left',
							width: '10000px',
							height: dimension.height + 'px'
						}
					});
					
					_p.myScroll = new Fx.Scroll(_p.scrollWrapper, {
						duration: 250
					});
					
					var myImages = [];
						
					gallery.images.each(function(src, index){
						var image = new Image();
						
						myImages.push(image);
						
						new Element('img', {
							'src': src,
							'width': dimension.width,
							'height': dimension.height,
							'class': index < 1 ? 'active' : ''
						}).inject(_p.wrapper);
					});
					
					gallery.images = myImages;
					
					gallery.empty();
					
					_p.scrollWrapper.inject(gallery, 'top');
					_p.wrapper.inject(_p.scrollWrapper);
					
					gallery.setStyles({
						paddingBottom: 9
					});
				}();
				
				_p.resize = false;
				
				_p.updateContainer = function(){
					var parent = _p.scrollWrapper.getParent(),
						dim = parent.getCoordinates(),
						width = dim.width - 20;
						height = dim.height;
						
					_p.wrapper.getElements('img').each(function(el){
						el.setStyles({
							width: width + 'px',
							height: 'auto'
						});					
					});
					
					_p.scrollWrapper.setStyles({
						width: width + 'px',
						height: _p.wrapper.getElements('img')[0].height + 'px'
					});
					
					_p.scrollAndSetActive(_p.wrapper.getElement('img:first-child'));
				};
				
				window.addEvent('resize', function(){
					window.clearTimeout(_p.resize);
					_p.resize = window.setTimeout(_p.updateContainer, Browser.ie ? 100 : 0);
				});
			}
		});
	};
	
	window.addEvent('load', my.gallery);
	
	return my;
})(document.id);
