var PIECES_PER_PAGE = 6;
var NUM_THUMBS = 0;
var THUMB_WIDTH = 116+10;
var _pager;
var _currentIndex = 0;

$(window).load(function() {
	$("a.group").fancybox({
		'zoomOpacity'			: true,
		'overlayShow'			: false,
		'zoomSpeedIn'			: 450,
		'zoomSpeedOut'			: 450,
		'hideOnContentClick': false,
		'imageScale':true,
		'imageSwap':false
	});
	
	var onMouseOutOpacity = 0.70;
    $('#gallery img').css('opacity', onMouseOutOpacity)
        .hover(
	        function () {
		        $(this).not('.selected').fadeTo('fast', 1.0);
	        }, 
	        function () {
		        $(this).not('.selected').fadeTo('fast', onMouseOutOpacity);
	        }
        );
});

/*paging init
$(window).load(function() {

    NUM_THUMBS = $("#gallery").find("a").length;
    initPager();
         
    $(window).bind('resize', function() 
    {   resizeHandler();
    });

 });
 */
//paging init
$(function(){
    NUM_THUMBS = $("#gallery").find("a").length;
    initPager();
         
    $(window).bind('resize', function() 
    {   resizeHandler();
    });
    resizeHandler();
});

function SelectCurrentNavLink()
{
}

var resizeTimer = null;
function resizeHandler()
{
   if (resizeTimer) clearTimeout(resizeTimer);    
        resizeTimer = setTimeout(adjustPager, 200);
}

//paging adjust on resize
function adjustPager() 
{    
    var borderPad = 0;
    var galleryWidth = $("#gallery").width() - borderPad;
    PIECES_PER_PAGE = Math.floor(galleryWidth / THUMB_WIDTH) * 3;
    if (PIECES_PER_PAGE > NUM_THUMBS)
        PIECES_PER_PAGE = NUM_THUMBS;
        
    initPager();
} 

function initPager()
{
    _pager =new virtualpaginate({
        piececlass: "group",
        piececontainer: 'a',
        pieces_per_page: PIECES_PER_PAGE,
        defaultpage: 0,
        persist: true
    })
    
    _pager.buildpagination(["pager"]);
    
}

function GallerySetCurrentIndexPrevious()
{
    _currentIndex = _currentIndex-1;
    
    //currentPage and _currentIndex are 0 based
    var lastIndexOnPreviousPage = PIECES_PER_PAGE * (_pager.currentpage) -1
    if (_currentIndex <= lastIndexOnPreviousPage)
	    _pager.navigate("previous"); 
}

function GallerySetCurrentIndexNext()
{
    _currentIndex = _currentIndex+1;
    
    //currentPage and _currentIndex are 0 based
    var lastIndexOnThisPage = PIECES_PER_PAGE * (_pager.currentpage+1) -1
    
    if (_currentIndex > lastIndexOnThisPage)
	    _pager.navigate("next"); 
}

function GallerySetCurrentIndex(index)
{
    _currentIndex = index;
}
 