(function(window){
	var window = window;
	/**
	* Basic $R setup
	*/
	function defined(w,s) { 
		return typeof( (w||this)[s])!='undefined';
	}
	
	
	
	window.$R = window.$R || {
		author:			'Rhys Burnie',
		url:			'http://www.hybridgarden.com',
		defined:		defined,
		themes:			[]
	};
	/**
	 * =========================================================================
	 * =========================================================================
	 */
	window.$R.currentTheme =	'kiss';
	window.$R.themeVersion =	1;
	window.$R.themes.push(window.$R.currentTheme);
	
	window.$R.log = function(msg) {
		if($chk(this.logger)) {
			this.logger.appendText('\n'+msg);
		}else{
			this.logger = new Element('textarea').setStyles({
				position: 'fixed',
				top: 10,
				right: 10,
				zIndex: 0,
				width: 295,
				padding: 10,
				background: 'rgba(9,5,0,.8)',
				border: '1px dotted #000',
				font: '12px "Courier New",Courier,monospace',
				color: '#deff00'
			}).inject(document.body).appendText(msg);
		}
	}
	
	function makeScrollbar(content,scrollbar,handle,horizontal,ignoreMouse){
		var size = content.getSize(),
			scrollSize = content.getScrollSize(),
			steps = horizontal ? (scrollSize.x - size.x) : (scrollSize.y - size.y),
			percent = horizontal ? (steps/scrollSize.x*100) : (steps/scrollSize.y*100),
			knobSize = horizontal ? (scrollbar.getSize().x*percent/100) : (scrollbar.getSize().y*percent/100)
			;
		scrollbar.setStyles({
			height: size.y
		});
		handle.setStyle('height',(100-percent)+'%');
		
		
		var slider = new Slider(scrollbar, handle, {	
			steps: steps,
			mode: (horizontal?'horizontal':'vertical'),
			onChange: function(step){
				// Scrolls the content element in x or y direction.
				var x = (horizontal?step:0);
				var y = (horizontal?0:step);
				content.scrollTo(x,y);
			},
			offset: -10
		}).set(0);
		if( !(ignoreMouse) ){
			// Scroll the content element when the mousewheel is used within the 
			// content or the scrollbar element.
			$$(content, scrollbar).addEvent('mousewheel', function(e){	
				e = new Event(e).stop();
				var step = slider.step - e.wheel * 30;	
				slider.set(step);					
			});
		}
		// Stops the handle dragging process when the mouse leaves the document body.
		$(document.body).addEvent('mouseleave',function(){slider.drag.stop()});
	}
	
	/**
	 *	At the moment I'm gonna use MooTools for speed etc.
	 *	
	 */
	
	window.addEvent('domready',function(){
		//return;
		/**
		 * Setup Scrolling
		// */
		/*$(document.body).setStyle('overflow','hidden');
		
		var scroll		= new Fx.Scroll(window),
			scrollbar	= new Element('div').set('id','faux_scrollbar').inject(document.body),
			knob		= new Element('div').addClass('knob').inject(scrollbar),
			slider		= makeScrollbar($(document.body),scrollbar,knob,false,false);
			;*/
			/*slider		= new Slider($(document.body),knob,{
			    range: [-50, 50],
			    wheel: true,
			    snap: true,
			    onTick: function(pos){
			        console.log(pos);
			    },
			    onComplete: function(){
			       
			    }
			})
			;*/
		
		content	= $('content');
		//content.setStyle('z-index', 100);
		
		//var log = window.$R.log;
		
		//log('enhance Hybrid Garden theme "kiss"');
		
		/**
		 * Postcards
		 * 
		 * In development 
		 * 
		 */
		var postcardsFile = 'Postcards.obf.js';//'Postcards.obf.js'
		Asset.javascript('3p/extendmoot/Fx.Push.js',{
			onload: function() {
				Asset.javascript('3p/extendmoot/Drag.Flick.js',{
					onload: function() {
						Asset.javascript('/assets/scripts/Postcards/'+postcardsFile,{
							onload: function() {
								//log('Postcards class loaded');
								new Postcards('.postcard',{
									avoid: content
								});
							}
						});
					}
				});
			}
		});
		
		
		/**  section
		 * = EXHIBITIONS = =====================================================
		 * 
		 */
		
		$$('section.exhibitions').each(function(el,i){
			var yrs = el.getElements('.year'),
				ins = el.getElements('.inner'),
				hs	= el.getElements('h3')
				chn = new Chain()
				;
			
			yrs.each(function(yr,i){
				
				hs[i].store('styleOpen',hs[i].getStyles('color','fontSize'));
				yr.addClass('closed').setStyle('cursor','pointer');
				
				hs[i].store('styleClosed',hs[i].getStyles('color','fontSize'));
				hs[i].set('morph',{
					duration: 'short'
				});
				
				//yr.removeClass('closed');
			});
			
			var A = new Fx.Accordion(yrs,ins,{
				onActive: function(yr,inr) {
					var h	= yr.getFirst('h3'),
						os	= h.retrieve('styleOpen')
						;
					yr.removeClass('closed').setStyle('cursor','auto');
					h.fade(.15).morph({
						color:		os.color,
						fontSize:	os.fontSize
					});
				},
				onBackground: function(yr,inr) {
					var h	= yr.getFirst('h3'),
						cs	= h.retrieve('styleClosed')
						;
					yr.addClass('closed').setStyle('cursor','pointer');
					h.fade(1).morph({
						color:		cs.color,
						fontSize:	cs.fontSize
					});
				}
			});
		});
	});
	
/* = end closure = ========================================================== */	
})(window);
/* ========================================================================== */
