$.tablesorter.addWidget({
    id:"paginator",
    format: function(table) {
        /* run once */
        if (!this.init) {
            this.table = table;
            this.pagenum = 1;
            this.startcol = 5;
            this.showcols = 4;
            this.numcols = $("thead th",table).length - this.startcol;
            this.a_next = null;
            this.a_prev = null;

            this.maxpages = Math.floor(this.numcols/this.showcols);
            if(this.numcols%this.showcols!=0)this.maxpages+=1;

            var self = this;

            showNext = function(){
                colnum = self.startcol+self.pagenum*self.showcols-1;

                /* hide cols */
                $("thead th:gt("+(self.startcol-1)+")",self.table).hide();
                $("tbody tr",self.table).find("td:gt("+(self.startcol-1)+")").hide();

                /* show cols */
                $("thead th:gt("+colnum+"):lt("+self.showcols+")",self.table).fadeIn(200);
                $("tbody tr",self.table).find("td:gt("+colnum+"):lt("+self.showcols+")").fadeIn(200);

                self.pagenum += 1;
                if(self.pagenum>1) self.a_prev.show();
                else self.a_prev.hide();
                if(self.pagenum<self.maxpages) self.a_next.show();
                else self.a_next.hide();
            };

            showPrev = function(){
                self.pagenum -= 1;

                colnum = self.startcol+self.pagenum*self.showcols-1;
                /* hide cols */
                $("thead th:gt("+(self.startcol-1)+")",self.table).hide();
                $("tbody tr",self.table).find("td:gt("+(self.startcol-1)+")").hide();

                /* show cols */
                $("thead th:gt("+(colnum-self.showcols)+"):lt("+self.showcols+")",self.table).fadeIn(200);
                $("tbody tr",self.table).find("td:gt("+(colnum-self.showcols)+"):lt("+self.showcols+")").fadeIn(200);

                if(self.pagenum>1) self.a_prev.show();
                else self.a_prev.hide();
                if(self.pagenum<self.maxpages) self.a_next.show();
                else self.a_next.hide();
            };

            if ($("thead th",table).length > this.startcol+this.showcols) {
                colnum = this.startcol+this.showcols-1;

                /*hide cols*/
                $("thead th:gt("+colnum+")",table).css('display','none');
                $(table).find("tr").find("td:gt("+colnum+")").css('display','none');

		var link_wrapper = $('<div/>').insertBefore(table);

                /*add next button*/
                pos = $("thead th:eq("+colnum+")",table).position();
                pos.left+=10;pos.top-=18;
                a = this.a_next = $('<a class="g_unvisited" style="text-decoration:underline;cursor:pointer;"><b>Next &raquo;</b></a>');
		a.css({'float':'right','cursor':'pointer','margin-left':'20px'});
                a.click(showNext);
		link_wrapper.append(a);

                /* add prev button */
                pos = $("thead th:eq("+this.startcol+")",table).position();
                pos.top-=18;
                a = this.a_prev = $('<a class="g_unvisited" style="text-decoration:underline;cursor:pointer;"><b>&laquo; Prev</b></a>');
		a.css({'float':'right','cursor':'pointer'}).css('display','none');
                a.click(showPrev);
		link_wrapper.append(a);

		link_wrapper.append('<div class="g_clear"/>');
            }
            this.init = true;
        }
    }
});


// setup image viewer
$(document).ready(function(){
    $('div.thumbwrapper:first').addClass('selected');
    $('div.thumbwrapper').hover(function(){
	$('div.thumbwrapper').removeClass('selected');

	var self = $(this);
	var credit_url     = self.attr('credit_url');
	var credit_domain  = self.attr('credit_domain');
	var image_url_90px = self.attr('url_90px');
	var image_url      = $('a',self).attr('href');

	self.addClass('selected');
	$('div.imagewrapper img').attr('src',image_url_90px);
	$('div.imagewrapper a.image').attr('href',image_url);
	$('div.imagewrapper a.credit').attr('href',credit_url).text(credit_domain);
    });
});
