$(document).ready(function() {
  thumbViewer1 = new thumbViewerObject(".PostThumbnailsWrapper");
  thumbViewer1.init();

  //Fancybox stuff
	$("a.iframe").fancybox({
  	'width'	:	720,
  	'height'	:	480,
  	'overlayColor' : "#222"
  });

	$("a.zoom").fancybox({
  	'overlayColor' : "#222",
  	'autoScale' : false
  });

	$("a.pop_slingshot").fancybox({
     'width'  : 420,
     'height' : 840,
  	'type' : "iframe",
  	'overlayColor' : "#222"
  });

	$("a.vibes_login").fancybox({
     'width'  : 650,
     'height' : 780,
  	 'type' : "iframe",
  	 'overlayColor' : "#222"
  });




})

var thumbViewerObject = function(id) {
  
  thumbViewer = {
    viewer : id,
    thumbsPerPage : 12,
    showingPage : 0,

    init : function(){

      fillingPage = 0;
      thumbNumber = 0; 

      $(this.viewer + " div.TempPost").each(function(post){

        thumbNumber++;
        fillingPage = Math.ceil(thumbNumber/thumbViewer.thumbsPerPage);

        //Clones image and link and nukes the temporary post
        var img = $(this).find("img:first").addClass("ThumbnailImage").clone();
        var link = $(this).find(".PostLink").clone();
        $(this).remove();

        //If there's no page div created for it, make one
        if ($("#page_" + fillingPage).length == 0) {
          var page  = document.createElement("div");
          $(page).attr("id" , "page_" + fillingPage).addClass("Page");
          $(".PostThumbnails").append(page);
        }
 
       //Append link and img into the page div

       if (img.attr("src")) {
         $("#page_" + fillingPage).append(link);
         $(link).append(img);

         //Adds a class if this is the post being shown
         //currentPostID is set via PHP on the thumbnails.php file
         //Goes to the page with the currently viewed post
         if ($(link).attr("post_id") == currentPostID) {
           $(link).addClass("Same");
           thumbViewer.gotoPage(fillingPage, "jump");
         }
        }

      });
      
      
      
      $(this.viewer + " .ThumbNav a").click(function() {
         if ($(this).attr("id") == "next") {
            thumbViewer.gotoPage(thumbViewer.showingPage + 1,"slide");
         } else {
            thumbViewer.gotoPage(thumbViewer.showingPage - 1,"slide");
         }

         return false;

       });
       
              thumbViewer.hideNav();
   
   
    },
    
    hideNav : function() {
        $("#previous").show();
        $("#next").show();
        
        if (thumbViewer.showingPage == 1) {
          $("#previous").hide();
        }
        
        numberPages = $(".Page").size();
        
        if (thumbViewer.showingPage == numberPages) {
          $("#next").hide();
        }
    },

    gotoPage : function (page,style) {
      gotoPosition = $("#page_" + page).position()

      if(gotoPosition) {

        if (style == "jump") {
          $('.PostThumbnails').css("left", gotoPosition.left * -1);
        } else {
          $(".PostThumbnails").animate({
            left: gotoPosition.left * -1
          }, 350, function() {});
        }
        thumbViewer.showingPage = page;
        thumbViewer.hideNav();
      }
    }
    
    }
    
    return thumbViewer; 
    
  }
