Dynamic content generation breaks image slider.

Dynamic content generation breaks image slider.

Hi,

I'm very new to this, I have been modifying a wordpress theme and I have come across an issue.
The page I am working on is a portfolio page, It uses dynamic content generation.
It pulls up all portfolio posts. I have added an image slider to each portfolio post.
My original problem was that only the first portfolio items slider would work and the rest would not load.
So I changed the script for the page to get it to call up the new slider for each project.
My problem now is that if I click back onto a project I have already viewed it calls up the slider again and doubles the amount of slides in the post and breaks the slider.
Does anyone have any suggestions on how I can fix this?
Any help would be greatly appreciated.

The page can be viewed here

This is what I added to the script to make it call up the slider for each post:

  1. $( '.thethe_image_slider' ).each( function (){
             var theTheImageSlider = new thetheImageSlider();
             theTheImageSlider.init( $( this ).attr( 'id' ) );
         });

This is the whole script:

  1. /**
  2.  * Portfolio previewer-  displays previews of the portfolio
  3.  * items separated by pages and adds a functionality for displaying a post after
  4.  * clicking on the smaller preview
  5.  * 
  6.  * @author Pexeto http://pexeto.com
  7.  */

  8. (function($){
  9. $.fn.portfolioPreviewer=function(options){
  10. // main default settings
  11. var defaults={
  12. itemnum : 6,
  13. pageWidth : 270,
  14. speed : 500, // will be overwritten by the XML data
  15. catId : -1,
  16. autoThumbnail:'on'
  17. };

  18. var options=$.extend(defaults, options);
  19. // data containers
  20. var items = [], divContainer, firstColumn, wrapper, pageWrappers = [], currentShown = 0, ie=$.browser.msie, maxWrapperHeight=0,images=[], inAnimation=false;
  21. var root=$(this);

  22. /**
  23. * Parses the data from the XML file, creates objects from the data and
  24. * saves the objects into arrays.
  25. */
  26. var parseData = function() {
  27. $('.portfolio-showcase-item').each(
  28. function(i) {
  29. var current = $(this),
  30. post = {};
  31. // create the object for the post
  32. post.obj = current.find('.preview-item:first');
  33. post.html = post.obj.html();
  34. post.smallObj = current.find('.showcase-item:first');

  35. items.push(post);
  36. });
  37. if (items.length > 0) {
  38. displayContent();
  39. bindEventHandlers();
  40. }
  41. };


  42. /**
  43. * Displays the main content of the items container.
  44. */
  45. var displayContent = function() {
  46. firstColumn = $('<div id="portfolio-preview"><div class="loading"></div></div>');
  47. var secondColumn = $('<div class="portfolio-sidebar"><h4>'+options.more+'</h4><div class="double-line"></div><div id="portfolio-wrapper"></div></div>');
  48. $('.preview-items').detach();
  49. root.html('').append(firstColumn).append(secondColumn);
  50. wrapper = secondColumn.find('#portfolio-wrapper');

  51. // show the first post
  52. showFirstPost();

  53. // show the other posts from the right
  54. displayPostList();
  55. pexetoSite.loadCufon();
  56. };

  57. var showFirstPost = function(post) {
  58. firstColumn.html(items[0].obj);
  59. };

  60. /**
  61. * Displays all the smaller post previews to the right, separated by pages.
  62. */
  63. var displayPostList = function() {

  64. for ( var i = 0; i < items.length; i++) {
  65. if (i % options.itemnum === 0) {
  66. var pageWrapper = $('<div class="portfolio-items"></div>');
  67. pageWrappers.push(pageWrapper);
  68. wrapper.width(wrapper.width() + 500);
  69. wrapper.append(pageWrapper);
  70. }
  71. var post = items[i].smallObj;
  72. pageWrappers[pageWrappers.length - 1]
  73. .append(post);
  74. }

  75. if (items.length > options.itemnum) {
  76. setWrapperHeight();
  77. showNavigation();
  78. }

  79. setPostClickHandlers();
  80. };
  81. var setWrapperHeight = function(){
  82. for(var i=0,length=pageWrappers.length; i<length; i++){
  83. if(pageWrappers[i].height()>maxWrapperHeight){
  84. maxWrapperHeight=pageWrappers[i].height();
  85. }
  86. wrapper.height(maxWrapperHeight);
  87. }
  88. }
  89. /**
  90. * Binds a change slide event handler to the wrapper so that when a navigation button has been clicked,
  91. * to change the slide according to the index of the slide to be displayed.
  92. */
  93. var bindEventHandlers=function(){
  94. wrapper.bind('changeSlide', function(e, index){
  95. if(!inAnimation){
  96. //it is not currently animated, do the animation now
  97. inAnimation=true;
  98. $('.portf-navigation').trigger('slideChanged', [index]);
  99. var margin = - index * (options.pageWidth + 31);
  100. wrapper.animate( {
  101. marginLeft :  margin
  102. }, options.speed, function(){
  103. currentShown=index;
  104. inAnimation=false;
  105. });
  106. }
  107. });
  108. }


  109. /**
  110. * Shows the pagination below the smaller post previews.
  111. */
  112. var showNavigation = function() {
  113. var paginationHolder=$('<div id="portfolio-big-pagination" class="hover"></div>').appendTo(wrapper.parent()),
  114. ul=null,
  115. pageNumber=pageWrappers.length,
  116. selectedClass='selected';
  117. // set the next button click handler
  118. $('<a />', {'class':'alignright', 'id':'next-item'})
  119. .click(function(event) {
  120. event.preventDefault();
  121. if (currentShown < pageWrappers.length - 1) {
  122. wrapper.trigger('changeSlide', [currentShown+1]);
  123. }
  124. }).appendTo(paginationHolder);

  125. // set the previous button click handler
  126. $('<a />', {'class':'alignright', 'id':'prev-item'})
  127. .click(function(event) {
  128. event.preventDefault();
  129. if (currentShown !== 0) {
  130. wrapper.trigger('changeSlide', [currentShown-1]);
  131. }
  132. }).appendTo(paginationHolder);
  133. //build the navigation buttons
  134. ul=$('<ul />', {'class':'portf-navigation'}).appendTo(paginationHolder);
  135. var html='';
  136. while(pageNumber--){
  137. html+='<li></li>';
  138. }
  139. ul.bind('slideChanged', function(event, index){
  140. $(this).find('li').removeClass(selectedClass).eq(index).addClass(selectedClass);
  141. })
  142. .append(html).find('li').click(function(){
  143. if(currentShown!=$(this).index()){
  144. wrapper.trigger('changeSlide', [$(this).index()]);
  145. }
  146. }).eq(0).addClass(selectedClass);
  147. };

  148. /**
  149. * Sets post click handlers to the smaller previews. When a small preview post is clicked
  150. * the whole content of the post is shown to the left.
  151. */
  152. var setPostClickHandlers = function() {
  153. var itemsNumber = items.length;
  154. for ( var i = 0; i < itemsNumber; i++)
  155. (function(i) {
  156. items[i].smallObj.each(function(j) {
  157. $(this).bind(
  158. {
  159. 'click' : function() {
  160. var current = items[i];
  161. firstColumn.html(current.obj);
  162. $('.thethe_image_slider').each(function(){
  163.         var theTheImageSlider = new thetheImageSlider();
  164.         theTheImageSlider.init( $(this).attr('id') );
  165.     });
  166. pexetoSite.loadCufon();
  167. pexetoSite.setLightbox(jQuery("a[rel^='lightbox'],a[rel^='lightbox[group]']"));
  168. },
  169. 'mouseover' : function() {
  170. $(this).css( {
  171. cursor : 'pointer'
  172. });
  173. }
  174. });
  175. });
  176. })(i);

  177. };
  178. if(root.length){
  179. parseData();
  180. }

  181. };
  182. }(jQuery));