Advertisement
imoda

j_v_gallery.js

Aug 24th, 2011
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /***************************************************\
  2. |                                                   |  
  3. |   JavaScript Document [j_v_gallery.js]            |
  4. |   Requirements: jQuery [core: current release]    |
  5. |   Original Creation Date: 03.20.2011              |
  6. |   Last Update On: 08.24.2011                      |
  7. |                                                   |
  8. \***************************************************/
  9.    
  10.  
  11. /******************** Constants **********************/
  12.  
  13.    var _PREVIEW_OBJECT                  = "#vGalleryPreview > img";
  14.    var _CONTROLS_OBJECT                 = "#vGalleryPreviewControls > *";
  15.    var _AUTO_CYCLE_DELAY                = 5000;
  16.    var _PREVIEW_CONTROL_ANIMATION_SPEED = "slow";
  17.    var _RUNNER_SLIDE_ANIMATION_SPEED    = "fast";
  18.    var _PREVIEW_FADE_ANIMATION_SPEED    = 200;
  19.    var _GLOW_MOVE_ANIMATION_SPEED       = 100;
  20.    var _THUMBS_PER_SLIDE                = 4;
  21.    var _NUMBER_OF_GALLERIES             = 3;
  22.    
  23.    galleryArray = createArray(_NUMBER_OF_GALLERIES);     // Array creation
  24.    
  25.    galleryArray[0][0]                   = "gold";        // class name
  26.    galleryArray[0][1]                   = "#98f50d";     // hex color
  27.    galleryArray[1][0]                   = "platinum";    // class name
  28.    galleryArray[1][1]                   = "#47bce8";     // hex color
  29.    galleryArray[2][0]                   = "supremacy";   // class name
  30.    galleryArray[2][1]                   = "#FFF";        // hex color
  31.    
  32.    // Default Color
  33.    galleryArray[_NUMBER_OF_GALLERIES][0]= "default";
  34.    galleryArray[_NUMBER_OF_GALLERIES][1]= "#404041";     // hex color
  35.  
  36. /******************************************************/
  37.  
  38. // Create our variables
  39. var runnerStartPosition = 0;
  40. var thumbPosition       = 1;
  41. var moveMeter           = true;
  42. var thumbTotalCount;
  43. var runnerCurrentPosition;
  44. var glowCorrection;
  45. var thumbDistance;
  46. var previewIndex;
  47. var galleryClass;
  48. var currentClass;
  49. var cycleTimer;
  50.  
  51. // Runs keyControl when a key is pressed
  52. document.onkeyup = keyControl;
  53.  
  54. $(document).ready(function() {
  55.    
  56.     // Get the number of thumbs in the vGalleryThumbsRunner
  57.     thumbTotalCount = $("#vGalleryThumbsRunner > .vGalleryThumb").size();
  58.    
  59.     // Get the offset of the thumbnail glow
  60.     glowCorrection = parseInt($("#vGalleryGlow").css("top")) * -(1);
  61.    
  62.     // Get the distance between two thumbnails (top to top)
  63.     thumbDistance = parseInt($(".vGalleryThumb:nth-child(2)").offset().top) - parseInt($(".vGalleryThumb:first-child").offset().top)
  64.    
  65.     runnerCurrentPosition = runnerStartPosition;
  66.    
  67.     // No one likes IE
  68.     $("#vGalleryGlow").css("position", "absolute");
  69.    
  70.     // Hide preview controls on page load
  71.     $(_CONTROLS_OBJECT).animate({opacity: 0.0}, 1);
  72.    
  73.     // Bold and color the gallery that the preview on page load belongs to
  74.     setGallery();
  75.    
  76.     // Auto cycling
  77.     setTimer();
  78.    
  79.     $("#vGallery").hover(function() {
  80.        
  81.         moveMeter = false;
  82.        
  83.         stopTimer();
  84.     }, function() {
  85.        
  86.         moveMeter = true;
  87.        
  88.         setTimer();
  89.     });
  90.    
  91.     // Let's get started, shall we?
  92.     animateMeter();
  93.     thumbNavigation();
  94.     thumbClick();
  95.     galleryClick();
  96.     previewControlsVisible();
  97.     previewControl();
  98.     browseWindowOpen();
  99. });
  100.  
  101. /********************
  102.  * Start: Functions *
  103.  ********************/
  104.  
  105.     // This function creates the empty multidimensional galleryArray
  106.     function createArray(_NUMBER_OF_GALLERIES) {
  107.        
  108.         var galleryArray = new Array(_NUMBER_OF_GALLERIES + 1);
  109.    
  110.         for (var i = 0; i < _NUMBER_OF_GALLERIES + 1; i++) {
  111.            
  112.            galleryArray[i] = new Array(2);
  113.            
  114.            for (var j = 0; j < 2; j++) {
  115.                
  116.                galleryArray[i][j] = "";
  117.            }
  118.         }
  119.        
  120.         return galleryArray;
  121.     }
  122.    
  123.     // This function allows you to traverse through the thumbnails inside the vGalleryThumbsRunner
  124.     function thumbNavigation() {
  125.        
  126.         $("#vGalleryControlB").click(function(){
  127.            
  128.             if (thumbPosition + _THUMBS_PER_SLIDE <= thumbTotalCount) {
  129.            
  130.                 positionIncrease();
  131.                 runnerAnimation();
  132.             }
  133.         });
  134.        
  135.         $("#vGalleryControlT").click(function(){
  136.            
  137.             if (thumbPosition > 1) {
  138.            
  139.                 positionDecrease();
  140.                 runnerAnimation();
  141.             }
  142.         });
  143.     }
  144.    
  145.     // This function will display the preview associated with the thumbnail that is clicked
  146.     function thumbClick() {
  147.        
  148.         $(".vGalleryThumb").click(function(){
  149.            
  150.             previewIndex = $(this).index();
  151.            
  152.             if (previewIndex != $(_PREVIEW_OBJECT + ":visible").index()) {
  153.                
  154.                 $(_PREVIEW_OBJECT + ":visible").hidePreview();
  155.                
  156.                 $(_PREVIEW_OBJECT).each(function(){
  157.                
  158.                     if ($(this).index() == previewIndex) {
  159.                        
  160.                         $(this).showPreview();
  161.                     }
  162.                 });
  163.             }
  164.         });
  165.     }
  166.    
  167.     // This function will display the first preview it finds in the vGalleryThumbsRunner
  168.     // that corresponds with the plan type clicked on
  169.     function galleryClick() {
  170.            
  171.         $("#vGalleryTitle").children("p").click(function() {
  172.        
  173.             currentClass = $(_PREVIEW_OBJECT + ":visible").attr("class");
  174.                                                      
  175.             galleryClass = $(this).attr("class");
  176.        
  177.             if (galleryClass != currentClass) {
  178.            
  179.                 $(_PREVIEW_OBJECT + ":visible").hidePreview();
  180.                
  181.                 $(_PREVIEW_OBJECT).each(function(){
  182.                
  183.                     if (galleryClass == $(this).attr("class")) {
  184.                        
  185.                         $(this).showPreview();
  186.                        
  187.                         // If we got here, we found the droids we're looking for.  Let's leave...
  188.                         exit;
  189.                     }
  190.                 });
  191.             }
  192.         });
  193.     }
  194.    
  195.     // This function will display the controls in the preview area that let
  196.     // you move forward and back, as well as browsing the preview's site
  197.     function previewControlsVisible() {
  198.        
  199.         $("#vGalleryColumnL").hover(function(){
  200.                                                  
  201.             $(_CONTROLS_OBJECT).stop().animate({opacity: 0.85}, _PREVIEW_CONTROL_ANIMATION_SPEED);
  202.         }, function(){
  203.                                                  
  204.             $(_CONTROLS_OBJECT).stop().animate({opacity: 0.0}, _PREVIEW_CONTROL_ANIMATION_SPEED);
  205.         });
  206.     }
  207.    
  208.     // This function will cycle the preview left or right according to which button you click on
  209.     function previewControl() {
  210.        
  211.         $(_CONTROLS_OBJECT + "[alt='Left']").click(lastPreview);
  212.        
  213.         $(_CONTROLS_OBJECT + "[alt='Right']").click(nextPreview);
  214.     }
  215.    
  216.     // This function will open the site in a new window when the "Browse" area is clicked
  217.     function browseWindowOpen() {
  218.        
  219.         $("#vGalleryPreviewControls > p").click(function(){
  220.                                                          
  221.             window.open("http://patientacquisitionsystem.com/portfolio/" + $(_PREVIEW_OBJECT + ":visible").attr("id"), "Browse");
  222.         });
  223.     }
  224.    
  225.     // This function cycles through the previews when you press the left or right arrow on your keyboard
  226.     function keyControl(e) {
  227.        
  228.         // Firefox fix
  229.         var keyID = (typeof event !="undefined") ? window.event.keyCode : e.keyCode;
  230.    
  231.         switch(keyID) {
  232.    
  233.             // Left Arrow
  234.             case 37:
  235.        
  236.                 lastPreview();
  237.                
  238.                 stopTimer();
  239.                
  240.                 setTimer();
  241.                
  242.                 break;
  243.            
  244.             // Right Arrow
  245.             case 39:
  246.                
  247.                 nextPreview();
  248.                
  249.                 stopTimer();
  250.                
  251.                 setTimer();
  252.                
  253.                 break;
  254.        }
  255.     }
  256.    
  257.     // This function runs other functions necessary after the preview is changed.
  258.     // The functions ran inside here are reliant on the visible preview
  259.     function setAll() {
  260.        
  261.         setGallery();
  262.         setGlow();
  263.     }
  264.    
  265.     // This function will bold and color the gallery plan type according to which preview plan type is visible
  266.     function setGallery() {
  267.        
  268.         currentClass = $(_PREVIEW_OBJECT + ":visible").attr("class");
  269.        
  270.         $("#vGalleryTitle > p").each(function(){
  271.            
  272.             // Do we have a plan?
  273.             if (currentClass == $(this).attr("class")) {
  274.                
  275.                 for (var i in galleryArray) {
  276.                
  277.                     for (var j in galleryArray) {
  278.                        
  279.                         // What is the plan?
  280.                         if ($(this).attr("class") ==  galleryArray[i][0]) {
  281.                        
  282.                             $(this).attr("style", "font-weight: bold; color: " + galleryArray[i][1] + ";");
  283.                         }
  284.                     }
  285.                 }
  286.             }
  287.             // Set it back to normal
  288.             else {
  289.                
  290.                 $(this).attr("style", "font-weight: normal; color: " + galleryArray[_NUMBER_OF_GALLERIES][1] + ";");
  291.             }
  292.         });
  293.     }
  294.    
  295.     // This function will move the glow around the thumb container according to which preview is visible
  296.     function setGlow() {
  297.        
  298.         previewIndex = $(_PREVIEW_OBJECT + ":visible").index();
  299.        
  300.         $(".vGalleryThumb").each(function(){
  301.        
  302.             if (previewIndex == $(this).index()) {
  303.                
  304.                 $("#vGalleryGlow").animate({top: (previewIndex * thumbDistance) - glowCorrection + "px"}, _GLOW_MOVE_ANIMATION_SPEED, forceSlide);
  305.             }
  306.         });
  307.     }
  308.    
  309.     // This function cycles to the preview before the visible one, according to its index in the HTML code
  310.     function lastPreview() {
  311.        
  312.         previewIndex = $(_PREVIEW_OBJECT + ":visible").index();
  313.            
  314.         $(_PREVIEW_OBJECT + ":visible").hidePreview();
  315.        
  316.         // Check if we can go back
  317.         if (previewIndex > 0) {
  318.            
  319.             $(_PREVIEW_OBJECT).each(function(){
  320.            
  321.                 if ($(this).index() == previewIndex - 1) {
  322.                    
  323.                     $(this).showPreview();
  324.                 }
  325.             });
  326.         }
  327.         // If we can't, go down to the last index
  328.         else {
  329.            
  330.             $(_PREVIEW_OBJECT).each(function(){
  331.            
  332.                 if ($(this).index() == thumbTotalCount - 1) {
  333.                    
  334.                     $(this).showPreview();
  335.                 }
  336.             });
  337.         }
  338.     }
  339.    
  340.     // This function cycles to the preview after the visible one, according to its index in the HTML code
  341.     function nextPreview() {
  342.        
  343.         previewIndex = $(_PREVIEW_OBJECT + ":visible").index();
  344.            
  345.         $(_PREVIEW_OBJECT + ":visible").hidePreview();
  346.        
  347.         // Check if we can go forward
  348.         if (previewIndex < thumbTotalCount - 1) {
  349.            
  350.             $(_PREVIEW_OBJECT).each(function(){
  351.            
  352.                 if ($(this).index() == previewIndex + 1) {
  353.                    
  354.                     $(this).showPreview();
  355.                 }
  356.             });
  357.         }
  358.         // If we can't, go up to the first index
  359.         else {
  360.            
  361.             $(_PREVIEW_OBJECT).each(function(){
  362.            
  363.                 if ($(this).index() == 0) {
  364.                    
  365.                     $(this).showPreview();
  366.                 }
  367.             });
  368.         }
  369.     }
  370.    
  371.     // This function forces the vGalleryThumbsRunner to move so that the thumb corresponding to the visible preview is showing
  372.     function forceSlide() {
  373.        
  374.         // If the glow moves below vGalleryThumbsRunner's view...
  375.         if (Math.round($("#vGalleryGlow").css("top").replace("px", "") / thumbDistance) + 1 >= (parseInt(thumbPosition) + parseInt(_THUMBS_PER_SLIDE))) {
  376.            
  377.             positionIncrease();
  378.             runnerAnimation();
  379.             forceSlide();
  380.         }
  381.        
  382.         // If the glow moves above vGalleryThumbsRunner's view...
  383.         if ((Math.round($("#vGalleryGlow").css("top").replace("px", "") / thumbDistance) + 1) < (parseInt(thumbPosition))) {
  384.            
  385.             positionDecrease();
  386.             runnerAnimation();
  387.             forceSlide();
  388.         }
  389.     }
  390.    
  391.     // This function controls how the vGalleryThumbsRunner behaves when moving
  392.     function runnerAnimation() {
  393.        
  394.             $("#vGalleryThumbsRunner").animate({top: -(runnerCurrentPosition * (thumbDistance * _THUMBS_PER_SLIDE)) + glowCorrection + "px"}, _RUNNER_SLIDE_ANIMATION_SPEED);
  395.     }
  396.    
  397.     // This function increases counters
  398.     function positionIncrease() {
  399.            
  400.             runnerCurrentPosition++;
  401.            
  402.             thumbPosition = thumbPosition + _THUMBS_PER_SLIDE;
  403.     }
  404.    
  405.     // This function decreases counters
  406.     function positionDecrease() {
  407.            
  408.             runnerCurrentPosition--;
  409.            
  410.             thumbPosition = thumbPosition - _THUMBS_PER_SLIDE;
  411.     }
  412.    
  413.     function stopTimer() {
  414.        
  415.         $(".jMeter").stop().animate({"width": ["0", "linear"]}, 1);
  416.        
  417.         clearInterval(cycleTimer);
  418.     }
  419.    
  420.     function setTimer() {
  421.                
  422.         cycleTimer = setInterval(nextPreview, _AUTO_CYCLE_DELAY);
  423.        
  424.         if (moveMeter) {
  425.        
  426.             $(".jMeter").stop().animate({"width": ["100%", "linear"]}, _AUTO_CYCLE_DELAY);
  427.         }
  428.     }
  429.    
  430.     function animateMeter() {
  431.        
  432.         $(".jMeter").animate({"width": ["0", "linear"]}, 1);
  433.        
  434.         if (moveMeter) {
  435.        
  436.             $(".jMeter").stop().animate({"width": ["100%", "linear"]}, _AUTO_CYCLE_DELAY);
  437.         }
  438.     }
  439.    
  440.     // This function controls the behavior of a preview when hiding
  441.     $.fn.hidePreview = function() {
  442.        
  443.         this.css("z-index", "100");
  444.        
  445.         this.animate({opacity: ["toggle", "linear"]}, _PREVIEW_FADE_ANIMATION_SPEED, setAll);
  446.     };
  447.    
  448.     // This function controls the behavior of a preview when showing
  449.     $.fn.showPreview = function() {
  450.        
  451.         this.css("z-index", "50");
  452.        
  453.         this.show();
  454.        
  455.         animateMeter();
  456.     };
  457.  
  458. /******************
  459.  * End: Functions *
  460.  ******************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement