function getQuerystring(key, default_)
{
  if (default_==null) default_=""; 
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return default_;
  else
    return qs[1];
} 

jQuery(function($) {
	
	$('.gallery_demo_unstyled').addClass('gallery_demo'); // adds new class name to maintain degradability
	
	$('ul.gallery_demo').galleria({
		history   : false, // activates the history object for bookmarking, back-button etc.
		clickNext : false, // 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.remove();
			
      //set image tooltip
      image.attr('title', 'Click to view bigger image');
      
			// fetch the thumbnail container
			var _li = thumb.parents('li');
			
			// fade out inactive thumbnail
			_li.siblings().children('img.selected').fadeTo(500,0.7);
			
			// fade in active thumbnail
			thumb.fadeTo('fast',1).addClass('selected');
			
			image
				.click(function(){
					var $this = $(this),
						$images = $this.parent().parent().parent().find('li img');
						imageurls = [],
						curr_index = 0;
						
						// Get all images url
						(function(el){ 
							el.each(function(){ imageurls.push( [$(this).attr('src')] );});
						})($images);
						
						// Get current image index in the imageurls
						var src = $this.attr('src').split('/'),
							src_length = src.length;
						for (i = 1; i < imageurls.length; i++){
							
							var url_arr = imageurls[i][0].split('/');
							
							// check if the image name matched
							if (url_arr[url_arr.length-1] == src[src_length-1]){
								curr_index = parseInt(i);
								break;
							}
						}
						
						$.slimbox(imageurls, curr_index, {loop: true, counterText: "Image {x} of {y}" });
				});
			
		},
		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.7';
			
			// 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.7); } // don't fade out if the parent is active
			)
		}
	});
	
	if($.browser.safari){
		/*
		** Google Chrome Bug fix, see url below:
		** http://code.google.com/p/galleria/issues/detail?id=78
		*/
		$.galleria.activate(
			$('ul.gallery_demo_unstyled li:first')
				.addClass('active')
				.find('img')
				.addClass('selected')
				.attr('src')
		);
	}
});
