/* Magic.js
* By : Drew Hornbein
* Version 0.01
* 8/6/2009
*/

$(document).ready(function() {
						   
	 var viewportwidth;
	 var viewportheight;
	 
	 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	 
	 if (typeof window.innerWidth != 'undefined')
	 {
		  viewportwidth = window.innerWidth,
		  viewportheight = window.innerHeight
	 }
	 
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	
	 else if (typeof document.documentElement != 'undefined'
		 && typeof document.documentElement.clientWidth !=
		 'undefined' && document.documentElement.clientWidth != 0)
	 {
		   viewportwidth = document.documentElement.clientWidth,
		   viewportheight = document.documentElement.clientHeight
	 }
	 
	 // older versions of IE
	 
	 else
	 {
		   viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
		   viewportheight = document.getElementsByTagName('body')[0].clientHeight
	 }	
		
	//$('ul.gallery').addClass('gallery'); // adds new class name to maintain degradability
	
	$('ul.gallery').galleria({
		history   : 0, // activates the history object for bookmarking, back-button etc.
		clickNext : true, // helper for making the image clickable
		onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
		
			//thumbnail size
			var thumb_height = $('.galleria li').height();
			
			// mesure the footer height + header height + thumbnail + 1/2 of a thumbnail + 40 misc pixels
			var extra_height = $('#footer').height() + $('#header').height() + thumb_height + (thumb_height / 2) + 40;
			
			//caption height
			var cap_height = $('.galleria_container .caption').height() + 20;
			
			// viewing area height - then set .galleria_container height
			var win_height = (viewportheight - extra_height);
			$('.galleria_container').css('height',(win_height + cap_height));
			
			// max height, picture can't be greater than this
			var max_height = win_height;
			var cur_height = image.height();
			
			//if the picture's height is MORE than the max, the images's height is set DOWN to max
			if(cur_height > max_height){
				image.attr('height',max_height);
			} 
			
			//if the picture's height is LESS than the max, top margin is set to half the remainder
			if (cur_height < max_height) {
				var marginTop = ((max_height - cur_height)/2);
				image.css('marginTop',marginTop);
			}
			
			if(! ($.browser.safari)){
				// 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.5);
				
				// fade in active thumbnail
				thumb.fadeTo('fast',1).addClass('selected');
			} else {image.css('display','none').fadeIn(1000);}
			// add a title for the clickable image
			image.attr('title','Next image >>');
			
			
		},
		onThumb : function(thumb) { // thumbnail effects goes here
			if(! ($.browser.safari)){
				// 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
				)
			}
		}
	});

$('.gallery').removeClass('gallery');
$('#loader').remove();



});

$(document).ready(function() {
	$('#magicbox').slideDown(200);
});