$(document).ready(function() {
	
	$('.gallery_demo_unstyled').addClass('gallery_demo'); // adds new class name to maintain degradability
	
	$('ul.gallery_demo').galleria({
		history   : true, // activates the history object for bookmarking, back-button etc.
		clickNext : true, // helper for making the image clickable
		insert    : '#main_image', // the containing selector for our main image
		onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
			
			// fade in the image & caption
			if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
				image.css('display','none').fadeIn(1000);
			}
			caption.css('display','none').fadeIn(1000);
			
			// fetch the thumbnail container
			var _li = thumb.parents('li');
			
			// fade out inactive thumbnail
			_li.siblings().children('img.selected').fadeTo(500,0.3);
			
			// fade in active thumbnail
			thumb.fadeTo('fast',1).addClass('selected');
			
			// add a title for the clickable image
			image.attr('title','Next image >>');
		},
		onThumb : function(thumb) { // thumbnail effects goes here
			
			// fetch the thumbnail container
			var _li = thumb.parents('li');
			
			// if thumbnail is active, fade all the way.
			var _fadeTo = _li.is('.active') ? '1' : '0.3';
			
			// fade in the thumbnail when finnished loading
			thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
			
			// hover effects
			thumb.hover(
				function() { thumb.fadeTo('fast',1); },
				function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
			)
		}
	});
	
	$('a#photos').click(function() {
		$('.gallery_box').css('position', 'absolute');
		var doc_width = $(document).width();
		var doc_height = $(document).height();
		$('.gallery_box').width = doc_width + 'px';
		$('.gallery_box').height = doc_height + 'px'; 
		$('.gallery_box .gallery_bg').css('width', doc_width);
		$('.gallery_box .gallery_bg').css('height', doc_height);
		
		// Positioning the gallery
		$('.gallery_box').css('top', '0px');
		$('.gallery_box').css('left', '0px');
		var win_width = $(window).width();
		var win_height = $(window).height();
		var book_top_padding = $('.gallery_box .gallery').css('padding-top').replace('px', '');
		var book_left_padding = $('.gallery_box .gallery').css('padding-left').replace('px', '');
		var book_width = 630;
		var book_height = 620;
		$('.gallery_box .gallery').css('width', book_width);
		$('.gallery_box .gallery').css('height', book_height);
		var yPos = (win_height - book_height - (book_top_padding * 2)) / 2;
		var xPos = (win_width - book_width - (book_left_padding * 2)) / 2;
		var yOffset = (window.pageYOffset) ? window.pageYOffset : (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
		$('.gallery_box .gallery').css('left', xPos);
		$('.gallery_box .gallery').css('top', yPos + yOffset);
		$('.gallery_box').fadeIn(500);
		$('.gallery_box .gallery_bg').delay(500).fadeIn(500);
		$('.gallery_box .gallery').delay(500).fadeIn(500);
		
		// Attaching Close Action
		$('.gallery_box .gallery_bg').click(function() {
			$('.gallery_box .gallery').fadeOut(500);
			$('.gallery_box .gallery_bg').delay(500).fadeOut(500);
			$('.gallery_box').delay(500).fadeOut(500);
			$('.gallery_box').delay(500).css('top', -999999);
			$('.gallery_box').delay(500).css('left', -999999);
		});
		
		$('.gallery_box .gallery a.close').click(function() {
			$('.gallery_box .gallery').fadeOut(500);
			$('.gallery_box .gallery_bg').delay(500).fadeOut(500);
			$('.gallery_box').delay(500).fadeOut(500);
			$('.gallery_box').delay(500).css('top', -999999);
			$('.gallery_box').delay(500).css('left', -999999);
			return false;
		});
		return false;
	});
});
