$('body').removeClass('no-js').addClass('has-js');
	
$(function() {
	var gallery = $('#gallery-album');
	
	$('noscript', gallery).remove();
	
/*	$('img', gallery)
		.lazy_images()
		.removeClass('hide');		
*/		
	gallery.slideshow({
		beforeInit: function() {},
		init: function(slideshow){

			$(this).find('.description').hide();
			slideshow.__change_image__(0);
		/*	var items = slideshow.items,
				index_items = [];
			
			for (var i=0; i < items.length; i++) {
				index_items.push('<li title="Jump to #'+(i+1)+'">'+i+'</li>');};
						
			slideshow.item_index = $('<ol class="slideshow-index"></ol>')
				.appendTo($('.footer-inner'));			
			slideshow.item_index_items = $(index_items.join('')).appendTo(slideshow.item_index);
			
			slideshow.item_index_items.eq(0).addClass('active');
			
			slideshow.item_index.bind('click',function(e) {
				var target = $(e.target);				
				if (!target.is('li')) {return;}
				slideshow.__change_image__(parseInt(target.text(),10));
			});
			
			slideshow.wrapper.append('<span class="controls"><a class="next">Next</a><a class="previous">Previous</a></span>');
			
			slideshow.__change_image__(0);*/
		}/*,
		beforeChange: function(o,c,n){
			o.items.slice(n-1,n+2)
				.find('img').trigger('lazy_images.show');
			
			o.item_index_items.eq(c).removeClass('active');
			o.item_index_items.eq(n).addClass('active');
		},
		change: function(o,c,n){
			o.items.eq(n)
				.find('.description').slideDown(function() {
					o.items.eq(c)
						.find('.description').hide();					
				}).end()
				.find('img').trigger('lazy_images.show');		


		}*/
	});
	
});

jQuery.fn.slideshow = function(o) {
	var options = $.extend({
		container: '#gallery-container',
		items: '.gallery-item',
		class_names: {
			container: 'slideshow-container',
			item: 'slideshow-item',
			wrapper: 'slideshow-wrapper'
		},
		controls: {
			next: '.controls .next',
			previous: '.controls .previous'
		},
		scrollOptions: { duration: 500 },
		beforeInit: function(options) {},
		init: function(slideshow){},
		beforeChange: function(slideshow,current_item,next_item){},
		change: function(slideshow,current_item,next_item){}
	},o||{});
	
	return this.each(function() {
		var $this = $(this),
			slideshow = {};
					
		for(var o in options){
			$.isFunction(options[o])
				&& (slideshow[o] = options[o]); }
		
		slideshow.__change_image__ = function(item_index) {
			var	slideshow = this, scrollOptions = $.extend({},this.scrollOptions),
				con = true, sci = this.current_item;
			
			slideshow.beforeChange
				&& (con = slideshow.beforeChange.apply($this,[slideshow,sci,item_index]));

			if(con === false){ return;}
			
			var next = $(slideshow.controls.next,$this).removeClass('inactive'),
				previous = $(slideshow.controls.previous,$this).removeClass('inactive');
			
			item_index === 0 && previous.addClass('inactive');
			item_index === this.items.length - 1 && next.addClass('inactive');
			
			this.change
				&& (scrollOptions.onAfter = function() {
					return slideshow.change.apply($this,[slideshow,sci,item_index]); });
							
			slideshow.wrapper.scrollTo(this.items.eq(item_index), scrollOptions);			

			this.current_item = item_index;
		};
		
		slideshow.beforeInit
			&& slideshow.beforeInit.apply($this,[options]);
		
		slideshow.current_item = -1;
		slideshow.scrollOptions = options.scrollOptions;
		slideshow.items = $this.find(options.items).addClass(options.class_names.item);		
		slideshow.container = $this.find(options.container);
		slideshow.container.addClass(options.class_names.container);		
		slideshow.wrapper = $('<div class="'+options.class_names.wrapper+'"></div>')
								.insertBefore(slideshow.container);
		
		slideshow.container
			.css('width', '99999px' )
			.appendTo(slideshow.wrapper);		
		
		var controls = slideshow.controls = {
			next: options.controls.next,
			previous: options.controls.previous };
		
		$(controls.next,$this).live('click',function(e) {
			var slideshow = $this.data('slideshow'),
				sci = slideshow.current_item,
				sni = slideshow.current_item + 1;
			
			if(sni == slideshow.items.length){
				sni = 0; }
			
			slideshow.__change_image__(sni);
			
			return false;			
		})
		.live('mousedown',function() { $(this).addClass('focus'); })
		.live('mouseup',function() { $(this).removeClass('focus'); });
		
		$(controls.previous,$this).live('click',function(e) {
			var slideshow = $this.data('slideshow'),
				sci = slideshow.current_item,
				sni = slideshow.current_item - 1;
							
			if(sni === -1){
				sni = slideshow.items.length - 1; }
							
			slideshow.__change_image__(sni);
			
			return false;
		})
		.live('mousedown',function() { $(this).addClass('focus'); })
		.live('mouseup',function() { $(this).removeClass('focus'); });
		
		/*$this.mousewheel(function(event, delta) {
			var a = -((Math.floor(delta) === 0) ? 1 : Math.floor(delta)),
				current_item = slideshow.current_item,
				new_item = a + current_item,
				total = slideshow.items.length - 1;

			if( (current_item == total && delta < 0) || (current_item === 0 && delta > 0)){
				return false;
			}
			

			if(new_item >= total) {
				new_item = total;
			}
			
			if(new_item < 0){
				new_item = 0;
			}
			//console.info(new_item);
			slideshow.__change_image__(new_item);
			
			return false;
		});*/

		$this.data('slideshow',slideshow);
		
		slideshow.init && slideshow.init.apply($this,[slideshow]);
	});
};

jQuery.fn.lazy_images = function(o) {
	jQuery.lazy_images = jQuery.lazy_images || {};
	
	var debug = false,
		log = debug && typeof window.console !== 'undefined' && window.console.log;
	
	var options = $.extend({
		loadingClass: 'loading',
		loadedClass: 'loaded',
		init: function() {
			/*log && log('init',this);*/ 
		//	log
		//		&& log('Total: '+jQuery.lazy_images['.slideshow img'].images.length)
		
		
		},
		load: function() {// log && log('loaded',this);
		//	log	&& log('Loading: '+jQuery.lazy_images['.slideshow img'].loading.length);
		//	log	&& log('Loaded: '+jQuery.lazy_images['.slideshow img'].loaded.length);
		},		
		preLoad: function() {// log && log('loaded',this);
		//	log	&& log('Loading: '+jQuery.lazy_images['.slideshow img'].loading.length);
		//	log	&& log('Loaded: '+jQuery.lazy_images['.slideshow img'].loaded.length);
		}		
	},o||{});
	
	var inst = jQuery.lazy_images[this.selector] = jQuery.lazy_images[this.selector] || {};
	
	inst.images = this;
	inst.options = options;
	inst.loading = [];
	inst.loaded = [];
	
	var t = new Date().valueOf();
	
	this.each(function() {
		var $this = $(this),
			src = $this.attr('original') || this.src,
			is_image = $this.is('img');
		
		if (!is_image || !src){
			if (debug) {
				throw new Error('jQuery.lazy_images: Selector '+$this.selector+' did not return an image'); }
			else {
				return;	} }
		
		$this.removeAttr('src');
		
		options
			&& $.isFunction(options.init)
			&& options.init.apply(this);
		
		$this
			.bind('lazy_images.show',function() {
				var $this = $(this),
					options = this.lazy_load_options,
					status = this.lazy_load_status;
		
				if(status===1){ // already loaded
					log && log('Already loaded',this);
					return; }
				
				var inst_loading_index = $.inArray(this,inst.loading);				
				
				if( inst_loading_index !== -1) {
					log && log('Waiting to load',this);
					return;	}
				
				inst.loading.push(this);
				
				options
					&& $.isFunction(options.preLoad)
					&& options.preLoad.apply($this);				
				
				$('<img/>')
					.load(function() {
						$this
							.attr('src', this.src)
							[0].lazy_load_status = 1;
						
						inst.loading.splice(inst_loading_index,1);
						
						inst.loaded.push($this[0]);
		
						if(options) {
							options.loadingClass
								&& $this.removeClass(options.loadingClass);
								
							options.loadedClass
								&& $this.addClass(options.loadedClass);
								
							$.isFunction(options.load)
								&& options.load.apply($this);
						}
					})
					.attr('src', this.lazy_load_src);
			});
		
		this.lazy_load_options = options;
		this.lazy_load_src = src;
		this.lazy_load_status = 0;
		
		options
			&& options.loadingClass
			&& $this.addClass(options.loadingClass);		
	});
	
	//typeof console !== 'undefined' && console.log(new Date().valueOf()-t)
	
	return this;
};


/*! Copyright (c) 2010 Brandon Aaron (http://brandonaaron.net)
 * Licensed under the MIT License (LICENSE.txt).
 *
 * Thanks to: http://adomas.org/javascript-mouse-wheel/ for some pointers.
 * Thanks to: Mathias Bank(http://www.mathias-bank.de) for a scope bug fix.
 *
 * Version: 3.0.3-pre
 * 
 * Requires: 1.2.2+
 */

(function($) {

var types = ['DOMMouseScroll', 'mousewheel'];

$.event.special.mousewheel = {
    setup: function() {
        if ( this.addEventListener )
            for ( var i=types.length; i; )
                this.addEventListener( types[--i], handler, false );
        else
            this.onmousewheel = handler;
    },
    
    teardown: function() {
        if ( this.removeEventListener )
            for ( var i=types.length; i; )
                this.removeEventListener( types[--i], handler, false );
        else
            this.onmousewheel = null;
    }
};

$.fn.extend({
    mousewheel: function(fn) {
        return fn ? this.bind("mousewheel", fn) : this.trigger("mousewheel");
    },
    
    unmousewheel: function(fn) {
        return this.unbind("mousewheel", fn);
    }
});


function handler(event) {
    var args = [].slice.call( arguments, 1 ), delta = 0, returnValue = true;
    
    event = $.event.fix(event || window.event);
    event.type = "mousewheel";
    
    if ( event.wheelDelta ) delta = event.wheelDelta/120;
    if ( event.detail     ) delta = -event.detail/3;
    
    // Add event and delta to the front of the arguments
    args.unshift(event, delta);

    return $.event.handle.apply(this, args);
}

})(jQuery);

/*! jQuery.ScrollTo - Easy element scrolling using jQuery.
 * Copyright (c) 2007-2009 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 5/25/2009
 * @author Ariel Flesler
 * @version 1.4.2
 *
 * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
 */
;(function(d){var k=d.scrollTo=function(a,i,e){d(window).scrollTo(a,i,e)};k.defaults={axis:'xy',duration:parseFloat(d.fn.jquery)>=1.3?0:1};k.window=function(a){return d(window)._scrollable()};d.fn._scrollable=function(){return this.map(function(){var a=this,i=!a.nodeName||d.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!i)return a;var e=(a.contentWindow||a).document||a.ownerDocument||a;return d.browser.safari||e.compatMode=='BackCompat'?e.body:e.documentElement})};d.fn.scrollTo=function(n,j,b){if(typeof j=='object'){b=j;j=0}if(typeof b=='function')b={onAfter:b};if(n=='max')n=9e9;b=d.extend({},k.defaults,b);j=j||b.speed||b.duration;b.queue=b.queue&&b.axis.length>1;if(b.queue)j/=2;b.offset=p(b.offset);b.over=p(b.over);return this._scrollable().each(function(){var q=this,r=d(q),f=n,s,g={},u=r.is('html,body');switch(typeof f){case'number':case'string':if(/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(f)){f=p(f);break}f=d(f,this);case'object':if(f.is||f.style)s=(f=d(f)).offset()}d.each(b.axis.split(''),function(a,i){var e=i=='x'?'Left':'Top',h=e.toLowerCase(),c='scroll'+e,l=q[c],m=k.max(q,i);if(s){g[c]=s[h]+(u?0:l-r.offset()[h]);if(b.margin){g[c]-=parseInt(f.css('margin'+e))||0;g[c]-=parseInt(f.css('border'+e+'Width'))||0}g[c]+=b.offset[h]||0;if(b.over[h])g[c]+=f[i=='x'?'width':'height']()*b.over[h]}else{var o=f[h];g[c]=o.slice&&o.slice(-1)=='%'?parseFloat(o)/100*m:o}if(/^\d+$/.test(g[c]))g[c]=g[c]<=0?0:Math.min(g[c],m);if(!a&&b.queue){if(l!=g[c])t(b.onAfterFirst);delete g[c]}});t(b.onAfter);function t(a){r.animate(g,j,b.easing,a&&function(){a.call(this,n,b)})}}).end()};k.max=function(a,i){var e=i=='x'?'Width':'Height',h='scroll'+e;if(!d(a).is('html,body'))return a[h]-d(a)[e.toLowerCase()]();var c='client'+e,l=a.ownerDocument.documentElement,m=a.ownerDocument.body;return Math.max(l[h],m[h])-Math.min(l[c],m[c])};function p(a){return typeof a=='object'?a:{top:a,left:a}}})(jQuery);
(function() {
	var body = $('body'),
		slideshow = $('#bg-slideshow'),
		images = slideshow.find('img').hide();
	
	function go_next(num){		
		var current = go_next.current = (num) ? num-1 : go_next.current || 0;
		var next = num || current;
		
		next = (next >= images.length - 1) ? 0 : current+1;
		
		var current_image = images.eq(current),
			next_image = images.eq(next);
		
		if(next_image.width()) { // has loaded
			body.css('background-image','none');
			createCookie('slideshow_last_image', next);
			current_image.fadeOut(1000,function() {
				next_image.fadeIn(800,function() {
					go_next.current = next;
					setTimeout(function() {
						go_next();
					}, 8000);					
				});
			});			
		}
		else { // keep previous image, wait for this one to load and then show it
		//	console.info('not loaded', next );
			setTimeout(function() {
				go_next(next);	
			}, 500);
			
		}
		
		
	}
	
	function start(){
		var next = 0,
			last_showing = images.eq( next );
		
		function check(){			
			if(last_showing.width()){									
				last_showing.show();
				setTimeout(function() {go_next(next+1);}, 8000); }
				//go_next(next); }
			else {
				setTimeout(function() {check();}, 500); }			
		}
		
		slideshow.show();
		check();
	}
	
	$(window).ready(function() {
		start();	
	})
	
	
})();
