
//
//   This bit of code was taken from the fantastic MB over at FullyEmpty.
//   Check out his talent! http://www.fullempty.co.uk/js/index.htm




cssforMSIE = function() {
// Change the css for IE because it breaks the position randomly
 if ($.browser.msie) {
	$('.topgal').css('top', '20px');
	$('.topgal').css('marginBottom', '20px ');
	}
}




executepage = function() {

		
		// Galarial Prepare
		$('.gallery_side').addClass('gallery_home'); // adds new class name to maintain degradability
		$('.nav').css('display','none'); // hides the nav initially
 
		// MB addition - extra text display
		$('.img_d').addClass('hidden_text').css('display','none'); // Hide the text for the images
		$('#pic_info').empty().addClass('picture_description'); // Clear the more informaion tag and add a class
		
		$('ul.gallery_home').galleria({
			history   : false, // deactivates the history object for bookmarking, back-button etc.
			clickNext : false, // helper for making the image clickable. Let's not have that in this example.
			insert    : undefined, // the containing selector for our main image. 
		   // If not found or undefined (like here), galleria will create a container 
		   // before the ul with the class .galleria_container (see CSS)
		
		 onImage   : function(image,caption,thumb) {
	 	            $('.nav').css('display','block'); // shows the nav when the image is showing
				 	
		             // fade in the image & caption
	
		             image.css('display','none').fadeIn(600);
		             caption.css('display','none').fadeIn(200);
	
				// 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
				)
	
	        } 
		
		});

                // Fix for IE
		cssforMSIE();
	}



///////////////// -- This is where the action starts
	$(document).ready(function(){
 
		// MB run the galleria page once
		executepage();
		
		$('.ajaxLoad').click(function() {
			 
		        var _grab = 'http://pauliwerks.com/'+($(this).attr('rel'));
			//var _grab = ($(this).attr('rel'));
			var _toplace = $('.topgal');
			
			//Clear the current gallery and rename
			_toplace.empty().addClass('oldgal').removeClass('topgal');
			var _wasgal = $('.oldgal');
			//Add the loading message
			_wasgal.text('Loading...');
			
			_wasgal.after('<div class="topgal"></div>');
			// re find the topgal div
			var _toplace = $('.topgal');
			_toplace.css('display','none');
			
			// get the page
			$.ajax({
				type:'get',
				url:_grab,
				dataType:'html',
				timeout:10000, // 10 secs
				success: function(_grabbed){
					//what to do if it loads correctly;
					// data is in _grabbed
					if (_grabbed) {
						_toplace.html(_grabbed);
					} else {
						_toplace.html('<p>Sorry, that gallery cannot be found. Please try a different one</p>');
					}
					
					// apply the gallery style in the background
					
					//again IE hack as wont display thumbnails properly - need to check galleria code maybe original?
					if ($.browser.msie) { $('.gallery_side').children().children('img').addClass('noscale'); }
					executepage();
					// Show the new gallery
					_toplace.css('display','block');
					// Apply the IE hack for my style
					cssforMSIE();
					// Remove the previous div as no longer needed
					_wasgal.remove();
 
				},
				error: function() {
					_toplace.html('<p>Sorry, that gallery cannot be found. Please try a different one.</p>');
					_toplace.css('display','block');
					cssforMSIE();
					_wasgal.remove();
				}
			}); // End of ajax
 
				
		}); // end of ajaxLoad



// ---------------------------------------- Change gallery list ---------------
		
		// Show a gallery description on hover over links
		var _wanttochange = $('.ajaxLoad').children('.LinkDetail');
		_wanttochange.css('display','none');
		
		// When hover show the text in the span
		$('.ajaxLoad').hover(function(){
			// add the text to the end of the thing
			var _changetext = $(this).find('span.LinkDetail').text();
			$('.gal_pages ul').append('<li style="margin-left:30px; color:#38d">'+_changetext+'</li>');
		}, function() {
			$('.gal_pages').find("li:last").remove();
		});
		
	});
	
