$(document).ready(function() {
  
// Set the two columns to equal height, compensating for leftcol's padding
  var $leftcol    = $('#leftcol'),
      $rightcol   = $('#rightcol'),
      leftpad     = parseInt($leftcol.css('padding-bottom'), 10),
      leftheight  = $leftcol.height(),
      rightheight = $rightcol.height();
  
  if (leftheight+leftpad < rightheight ) {
    $leftcol.height(rightheight - leftpad);  
  }
  
// Homepage gallery switcher
  var $gallery = $('#gallery');
  $gallery
    .find("img:first")
      .wrap('<div class="galleryimages"/>')
    .parent()
      .load('/_generated/gallery-images.inc', '', function(){
        $(this).innerfade({
          speed: 'slow',
          timeout: 4000,
          type: 'sequence'
        });
      // .innerfade({
      //   speed: 'slow',
      //   timeout: 4000,
      //   type: 'sequence'
      });
      
// Wedding Galleries

var $wedding = $('#wedding_gallery');

if ($wedding.length > 0) {
  $wedding.wrap('<div class="gallerydiv"/>');
  $wedding.find('li:first').addClass('active');
  $wedding.after('<br clear="all"/>');
  var $wrapper = $('.gallerydiv');
  
  $wedding.galleria({
    history:false,
    onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes

    				// fade in the image & caption
					image.css('display','none');	
    				caption.css('display','none');

					if (image.height() > image.width()) {
						image.css({
							left: '27%'
						});
					}

					var oldimg = $wrapper.find('.oldreplaced');
					
					if (oldimg.length > 0) {
						oldimg.stop().fadeOut(200, function(){
							image.fadeIn(500, function(){
								oldimg.remove();
							});	
		    				caption.fadeIn(500);
						});
					} else {
						image.fadeIn(500);
						caption.fadeIn(500);
					}

    				// 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.stop().fadeTo('fast',1); },
    					function() { _li.not('.active').children('img').stop().fadeTo('fast',0.3); } // don't fade out if the parent is active
    				);
    			}
    
  });
}

});

