//arrow key functionality

document.onkeydown = nav;

function nav(e) {
	
	if (!e) var e = window.event
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	if (code==37) {
		$.galleria.prev();
		return false;
	}
	if (code==39) {
		$.galleria.next();
		return false;
	}
}

//correct tab display

var current;

function load() {
	
	var url = String(current.attr('src'));
	var backslash = url.lastIndexOf('/');
	var number = (current.attr('src').substr(backslash+1,2))*1;
	if(number<13) {
		TabbedPanels1.showPanel(0);
	}
	else if(number<25) {
		TabbedPanels1.showPanel(1);
	}
	else if(number<37) {
		TabbedPanels1.showPanel(2);
	}
	else if(number<49) {
		TabbedPanels1.showPanel(3);
	}
	else if(number<61) {
		TabbedPanels1.showPanel(4);
	}
	else if(number<73) {
		TabbedPanels1.showPanel(5);
	}
}

jQuery(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
				current = thumb;
				image.css('display','none').fadeIn(400);
				//}
				if (!$.browser.msie) {
					caption.css('display','none').fadeIn(400);
				}
				
				// fetch the thumbnail container
				var _li = thumb.parents('li');
				
				// fade out inactive thumbnail
				_li.siblings().children('img.selected').fadeTo(400,0.6);
				
				// fade in active thumbnail
				thumb.fadeTo('fast',1).addClass('selected');
				
				// add a title for the clickable image
				image.attr('title',thumb.attr('title'));
				
				//activate correct tab
				load();

			},
			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.6';
				
				// fade in the thumbnail when finnished loading
				thumb.css({display:'none',opacity:_fadeTo}).fadeIn(400);
				
				// hover effects
				thumb.hover(
					function() { thumb.fadeTo('fast',1); },
					function() { _li.not('.active').children('img').fadeTo('fast',0.6); } // don't fade out if the parent is active
				)
			}
		});
});
