var pageLength, pageBack, pageFor, mini, miniIndex;
$(document).ready(function () {

    // hover for www page
        $("#grid li").css("opacity", ".7").hover(function() {
            $(this).animate({opacity: 1}, 300);    
        },
        
        function () {
            $(this).animate({opacity: .7}, 1000);
        });
        
        //Shop pager
        
            //remove paypal 1px images
                $("img[src='https://www.paypal.com/en_GB/i/scr/pixel.gif']").remove();
        
        
            //init
                pageLength = $("ul.pager li").length;
                

                //read from querystring or set page to 1
                if(document.location.hash=="") {
                    thisPage=1;
                } else {
                    thisPage=parseInt(document.location.hash.substring(1, document.location.hash.length)); 
                }
                pageBack = (thisPage==1) ? pageLength : thisPage - 1;
                pageFor = (thisPage==pageLength) ? 1 : thisPage + 1;
                
                
                
                //console.log(thisPage + " " + pageBack + " " + pageFor + " " + pageLength)

            //build pagers
                bigPager = '<div class="paging"><ul>';
                for(a=1; a<=pageLength; a++) {
                    bigPager += '<li><a href="#' + a + '">' + a + '</a></li>';
                }
                bigPager += '</ul></div>';
                
                //only build minipager if we have to
                
                    if($(".pager li:eq(" + (parseInt(thisPage)-1) + ") img").size() > 1) {
                        //FminiPager();
                    }
    
                    $("ul.pager").before(bigPager);
                
            //clicks
                $("div.paging a").click(function() {
                th = $(this).attr("href");
                   goPage(th.substring(th.indexOf("#")+1, th.length));
                   $(".paging li:eq(1)").slideDown();
                });
                
                $("#page-back").click(function() {
                    back = (mini==1) ? $(".pager li:eq(" + (thisPage-1) + ") img").size() : (mini-1);   
                });
                
            //init first page
                $("ul.pager > li:not(eq(" + thisPage + ")").hide();
                goPage(thisPage);
                
                
            //minipager controls
                $("#page-back").click(function() {
                    miniBack = ((miniIndex-1) < 1) ? $("ul.pager li:visible img").size() : miniIndex-1;
                    goMini(miniBack); 
                    return false;
                });
                
                $("#page-for").click(function() {
                    miniFor = ((miniIndex+1) > $("ul.pager li:visible img").size()) ? 1 : miniIndex+1;
                    goMini(miniFor); 
                    return false;
                });
        
            
                
                
    
 
});

function FminiPager() {
    /* miniIndex = 1;
    if($(".pager li:eq(" + (parseInt(thisPage)-1) + ") img").size() > 1) {
        $(".pager li:eq(" + (thisPage-1) + ") img").wrapAll('<div class="image-wrap"></div>');
        $(".pager li:eq(" + (thisPage-1) + ") img:not(:first)").hide();
        if($(".minipager").size()>0) {
            $(".minipager").show();
        } else {
            miniPager = '<div class="minipager"><ul><li><a href="#" id="page-back">&laquo;</a></li><li><a id="page-for" href="#">&raquo;</a></li></ul></div>';
            $("ul.pager h3").after(miniPager);
        }
    } */

}

function goPage(x) {  
    x=parseInt(x);
    
    thisPage = x;
     //show minipager if needed
        if($(".pager li:eq(" + (x-1) + ") img").size()>1) {FminiPager()} else {  $(".minipager").hide() };

    //de-select open page
        $(".pager > li:visible").slideUp();
        $(".paging li.selected").removeClass("selected"); 
    //select new page
        $(".paging ul > li:nth-child(" + x + ")").addClass("selected");
        $(".pager > li:nth-child(" + x + ")").slideDown(); 

    //change URL
         
        document.location.hash = x;  
}

function goMini(x) {  /*
    $("ul.pager > li:visible img:eq(" + (miniIndex-1) + ")").fadeOut();
    $("ul.pager > li:visible img:eq(" + (x-1) + ")").fadeIn();
    //console.log(x + " " + miniIndex + " " + x); 
    miniIndex = x;    */
}

