Advertisement
Guest User

Untitled

a guest
Sep 11th, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. tgs.classifieds = {
  2.     init: function () {
  3.         if ($("[data-listing]")) {
  4.             tgs.classifieds.listings(0.66);
  5.         };
  6.         if ($(".classifieds.single")) {
  7.             tgs.classifieds.single.photos(0.66);
  8.         }
  9.         if ($(".classifieds-search .search-bar").length > 0) {
  10.             tgs.classifieds.searchBar();
  11.         };
  12.         if ($("[name=\"view\"]")) {
  13.             tgs.classifieds.view.init();
  14.         };
  15.     },
  16.     searchBar: function () {
  17.         $(".classifieds-search .search-bar").do(function () {
  18.             $(this).css({
  19.                 "position": "absolute",
  20.                 "top": "50%",
  21.                 "margin-top": -($(this).get(0).offsetHeight / 2)
  22.             });
  23.         });
  24.     },
  25.     listings: function (masterRatio) {
  26.  
  27.         // temp code to show difference between premium/non
  28.         var listings = document.querySelectorAll("[data-listing]");
  29.         if (listings.length) {
  30.             for (var i = 0; i < 10; i++) {
  31.                 listings[i].setAttribute("data-listing", "premium");
  32.             }; // end temp code
  33.         };
  34.  
  35.         $("[data-listing]").each(function () {
  36.  
  37.             //$(this).click(function () {
  38.             //  window.location.href += "/view/" + $(this).attr("data-id");
  39.             //});
  40.  
  41.             //var masterRatio = 0.66; // Parameter of 'tgs.classifieds.listings()'
  42.  
  43.             // Find the photo within the listing
  44.             var photo = $(this).find(".photo");
  45.             var width = photo.width();
  46.             photo.width(width);
  47.             photo.height(width * masterRatio);
  48.  
  49.             // Find the anchor tag
  50.             var a = photo.find("a");
  51.             a.attr("href", window.location.href + "/view/" + $(this).attr("data-id"));
  52.             a.width(photo.width());
  53.             a.height(photo.height());
  54.  
  55.             // Find the actual photo
  56.             var img = a.find("img");
  57.             img.load(function () {
  58.                 // Detect image dimensions
  59.                 if (img.width() > img.height()) {
  60.                     var ratio = img.height() / img.width();
  61.                     if (ratio > masterRatio) { // How wide is the image?
  62.                         img.width(photo.width());
  63.                         img.css({ // Vertically center photo inside of link
  64.                             "position": "relative",
  65.                             "top": (-img.height() / 2) + (photo.height() / 2) + "px",
  66.                         });
  67.                     } else {
  68.                         img.height(photo.height());
  69.                         img.css({ // Horizontally center photo inside of link
  70.                             "position": "relative",
  71.                             "left": (-img.width() / 2) + (photo.width() / 2) + "px",
  72.                         });
  73.                     };
  74.                 } else {
  75.                     var ratio = img.width() / img.height();
  76.                     img.width(photo.width());
  77.                     img.css({ // Vertically center photo inside of link
  78.                         "position": "relative",
  79.                         "top": (-img.height() / 2) + (photo.height() / 2) + "px",
  80.                     });
  81.                 };
  82.             });
  83.  
  84.  
  85.         });
  86.     },
  87.     single: {
  88.         photos: function (masterRatio) {
  89.             $(".classifieds.single").find(".photo").each(function (i) {
  90.  
  91.                 var photo = $(this);
  92.                 photo.addClass("grid");
  93.  
  94.                 // Create "grid" depending on which image this is
  95.                 if (i === 0) {
  96.                     photo.addClass("full");
  97.                 } else if (i >= 1 && i <= 2) {
  98.                     photo.addClass("half");
  99.                 } else if (i >= 3 && i <= 5) {
  100.                     photo.addClass("third");
  101.                 } else if (i >= 6) {
  102.                     photo.addClass("fourth");
  103.                 };
  104.  
  105.                 photo.height(photo.width() * masterRatio);
  106.  
  107.                 var a = photo.find("a");
  108.                 a.click(function (e) {
  109.                     e.preventDefault();
  110.                 });
  111.                
  112.                 var img = a.find("> img");
  113.  
  114.                 img.load(function () {
  115.  
  116.                     tgs.classifieds.single.viewer.each(img, i);
  117.  
  118.                     // Detect image dimensions
  119.                     if (img.width() > img.height()) {
  120.                         var ratio = img.height() / img.width();
  121.                         if (ratio > masterRatio) { // How wide is the image?
  122.                             img.width(photo.width());
  123.                             img.css({ // Vertically center photo inside of link
  124.                                 "position": "relative",
  125.                                 "top": (-img.height() / 2) + (photo.height() / 2) + "px",
  126.                             });
  127.                         } else {
  128.                             img.height(photo.height());
  129.                             img.css({ // Horizontally center photo inside of link
  130.                                 "position": "relative",
  131.                                 "left": (-img.width() / 2) + (photo.width() / 2) + "px",
  132.                             });
  133.                         };
  134.                     } else {
  135.                         var ratio = img.width() / img.height();
  136.                         img.width(photo.width());
  137.                         img.css({ // Vertically center photo inside of link
  138.                             "position": "relative",
  139.                             "top": (-img.height() / 2) + (photo.height() / 2) + "px",
  140.                         });
  141.                     };
  142.                 });
  143.  
  144.                 img.promise().done(function () {
  145.  
  146.                     img.siblings().click(function () {
  147.                         tgs.classifieds.single.viewer.lightbox();
  148.                         tgs.classifieds.single.viewer.nav(i);
  149.                     });
  150.  
  151.                 });
  152.  
  153.             });
  154.  
  155.         },
  156.         viewer: {
  157.             each: function (img, i) {
  158.  
  159.                 var collection = $("#photoCollection");
  160.  
  161.                 var caption = $("<div>");
  162.                 caption.addClass("caption");
  163.                 caption.html(img.attr("alt"));
  164.                 caption.hide();
  165.  
  166.                 collection.find(".captions").append(caption);
  167.  
  168.                 collection.find(".viewer").do(function () {
  169.  
  170.                     imgClone = img.clone();
  171.                     imgClone.width(img.width());
  172.                     imgClone.height(img.height());
  173.  
  174.                     img = imgClone;
  175.  
  176.                     $(this).append(img);
  177.                     img.hide();
  178.  
  179.                     img.load(function () {
  180.  
  181.                         img = $(this);
  182.  
  183.                         if (img.width() > img.height()) {
  184.  
  185.                             var ratio = img.height() / img.width();
  186.                             if (img.width() > img.parent().width()) {
  187.                                 img.width(img.parent().width());
  188.                                 img.height(img.width() * ratio);
  189.                             };
  190.                             img.css({ // Vertically center photo inside of link
  191.                                 "position": "relative",
  192.                                 "top": (-img.height() / 2) + (img.parent().height() / 2) + "px",
  193.                                 "left": (-img.width() / 2) + (img.parent().width() / 2) + "px"
  194.                             });
  195.  
  196.                         } else {
  197.  
  198.                             var ratio = img.width() / img.height();
  199.                             if (img.height() > img.parent().height()) {
  200.                                 img.height(img.parent().height());
  201.                                 img.width(img.height() * ratio);
  202.                             };
  203.                             img.css({ // Horizontally center photo inside of link
  204.                                 "position": "relative",
  205.                                 "left": (-img.width() / 2) + (img.parent().width() / 2) + "px",
  206.                                 "top": (-img.height() / 2) + (img.parent().height() / 2) + "px"
  207.                             });
  208.  
  209.                         };
  210.  
  211.                     });
  212.  
  213.                 });
  214.             },
  215.             nav: function (i) {
  216.                 $("#photoCollection .viewer").find("img").do(function () {
  217.  
  218.                     var captions = $("#photoCollection").find(".caption");
  219.  
  220.                     var clicked = $(this)[i];
  221.                     $(clicked).show();
  222.                    
  223.                     if ($(captions[i]).html()) {
  224.                         $(captions[i]).show();
  225.                     };
  226.  
  227.                     var count = $(this).length;
  228.                     var first = $(this).first();
  229.                     var last = $(this).last();
  230.  
  231.                     $(".which").text(i + 1 + " / " + count);
  232.  
  233.                     var images = $(".viewer").find("img");
  234.  
  235.                     // Next:
  236.                     $("#photoCollection .next").click(function () {
  237.                         $(images[i]).hide();
  238.                         $(captions[i]).hide();
  239.                         if (i < count - 1) {
  240.                             i++;
  241.                         } else {
  242.                             i = 0;
  243.                         };
  244.                         $(".which").text(i + 1 + " / " + count);
  245.                         $(images[i]).show();
  246.                         if ($(captions[i]).html()) {
  247.                             $(captions[i]).show();
  248.                         };
  249.                     });
  250.  
  251.                     // Previous:
  252.                     $("#photoCollection .previous").click(function () {
  253.                         $(images[i]).hide();
  254.                         $(captions[i]).hide();
  255.                         if (i === 0) {
  256.                             i = count - 1;
  257.                         } else {
  258.                             i--;
  259.                         };
  260.                         $(".which").text(i + 1 + " / " + count);
  261.                         $(images[i]).show();
  262.                         if ($(captions[i]).html()) {
  263.                             $(captions[i]).show();
  264.                         };
  265.                     });
  266.  
  267.                 });
  268.             },
  269.             lightbox: function (i) {
  270.  
  271.                 var collection = $("#photoCollection");
  272.                 var cover = $("#photoCover");
  273.  
  274.                 collection.show();
  275.                 cover.show();
  276.  
  277.                 collection.find(".close").click(function () {
  278.  
  279.                     collection.hide();
  280.                     cover.hide();
  281.  
  282.                     var captions = collection.find(".captions");
  283.                     captions.find(".caption").each(function () {
  284.                         $(this).hide();
  285.                     });
  286.  
  287.                     var viewer = collection.find(".viewer");
  288.                     viewer.find("img").each(function () {
  289.                         $(this).hide();
  290.                     });
  291.  
  292.                 });
  293.  
  294.             }
  295.         }
  296.     },
  297.     view: {
  298.         init: function () {
  299.             // TODO:
  300.             // first get value stored in cookie (call it: classifiedsView)
  301.             // then check correct radio button, accordingly
  302.             $("[name=\"view\"]").each(function () {
  303.                 var radio = $(this).get(0);
  304.                 $(this).parent().click(function () {
  305.                     if (!$(this).hasClass("checked")) {
  306.                         radio.checked = true;
  307.                         $(this).parent().get(0).submit();
  308.                     };
  309.                 });
  310.             });
  311.         }
  312.     }
  313. }
  314. tgs.classifieds.init();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement