$(function(){
      
   	$('#galleryThumbs li a').click(function() { return false; });
   
	var time = 5000;
	var timeout = setTimeout(function() { showNextImage(); }, time);
	var paused = false;
	
	var numCols = $('#galleryThumbs li').length;
	
	if(typeof currentCol == 'undefined') var currentCol = 6;
	var step = 154;
	var leftCalc = 0;
	var leftDisabled = true;
	var rightDisabled = false;
	var animating = false;
		
	$('#outerThumbsWrapper .prevPage:not(.disabled)').live('click', function() {	
		rightDisabled = false;
		if(!leftDisabled && !animating) {
			var newLeft = $('#roomsetGallery .thumbs').position().left + step;
			leftCalc += step;
			currentCol --;
			$('#outerThumbsWrapper .prevPage, #outerThumbsWrapper .nextPage').removeClass('disabled');
			if(leftCalc >= 0)
			{	
				$(this).addClass('disabled');		
				leftDisabled = true;
			}
			else {
				leftDisabled = false;	
			}
			$('#galleryThumbs').stop().animate({'left': newLeft+'px'}, 200, 'linear', function() { animating = false; });		
		}
		return false;
	});
	
	$('#outerThumbsWrapper .nextPage:not(.disabled)').live('click', function() {
		leftDisabled = false;
		if(!rightDisabled && !animating) {
			var newLeft = $('#roomsetGallery .thumbs').position().left - step;
			leftCalc -= step;
			currentCol ++;
			$('#outerThumbsWrapper .prevPage, #outerThumbsWrapper .nextPage').removeClass('disabled');
			if(currentCol >= numCols) {				
				$(this).addClass('disabled');	
				rightDisabled = true;
			}
			else {
				rightDisabled = false;	
			}
			animating = true;
			$('#galleryThumbs').animate({'left': newLeft+'px'}, 200, 'linear', function() { animating = false; });		
		}
		return false;
	});
     
   $('#roomsetGallery .thumbs  li').each(function(idx) {
        $(this).data('index', (++idx));
    });
    
	$('#roomsetGallery .thumbs li img').bind('click', function() { pauseIt($('.heroPause')); });

	$('#roomsetGallery .thumbs li img').bind('load',function(){ $(this).css('opacity', 0).animate({'opacity': 1}, 300) })

    $('#roomsetGallery .thumbs ').galleria({
        // #img is the empty div which holds full size images
        insert: '#roomsetGalleryLargeImage',
		
		clickNext: false,
        
        // enable history plugin
        history: false,
        
        // function fired when the image is displayed
        onImage: function(image, caption, thumb) 
		{
					
			// fade in the image
			image.hide().fadeIn(500);
			image.css({'width':'970px', 'height': '489px'});
          
			$roomsetLink = thumb.parent('li').attr('title');			
			caption.wrapInner('<span class="innerCaption"></span>');
			// caption.append('<a class="fc" href="'+$roomsetLink+'">More Details <span>&#187;</span></a>');
				
			// animate active thumbnail's opacity to 1, other list elements to 0.6
			thumb.parent().fadeTo(200, 1).siblings().fadeTo(200, 0.6);
			
			clearTimeout(timeout);
			
			if(!paused) {
				timeout = setTimeout(function() { $.galleria.next(); }, time);
			}

			$('#roomsetGalleryLargeImage').trigger('image-loaded');
                
        },
        
        // function similar to onImage, but fired when thumbnail is displayed
        onThumb: function(thumb) {
              
			  var $li = thumb.parent(),
                opacity = $li.is('.active') ? 1 : 0.6;
            
            // hover effects for list elements
            $li.hover(
                function() { $li.fadeTo(200, 1); },
                function() { $li.not('.active').fadeTo(200, opacity); }
            )
        }        
    }).find('li:first').addClass('active') // display first image when Galleria is loaded
    	
	//////////////////////////////////////////////////////
	
	$('.heroNext').click(function(evt) {
		$.galleria.next();			  
	});
	
	$('.heroPrev').click(function(evt) {
		$.galleria.prev();								  
	});
	
	$('.heroPause').click(function(evt) {
		if($(this).hasClass('paused')) {
			playIt($(this));
		}
		else {		
			pauseIt($(this));
		}							  
	});
	
	function playIt(heroPause) {
		clearTimeout(timeout);
		heroPause.removeClass('paused').attr('title','Pause');
		timeout = setTimeout(function() { $.galleria.next(); }, time);
		paused = false;
	}
	
	function pauseIt(heroPause) {
		clearTimeout(timeout);
		heroPause.addClass('paused').attr('title','Play');
		paused = true;
	}
	
	$('img.replaced').live('click', function(evt) { 
		var $loc = $(this).parent().siblings('.caption').find('.fc');
		if($loc.length) {
			window.location = $loc.attr('href');
		}
		return false;
	});
		
	//////////////////////////////////////////////////////

    	
});


