$(function($){ var pageOut = 12, default_page = 1, show_on_section = 'no', use_cookies = 'yes', use_to_top = 'no', show_paging_links = 'both', show_sort = 'yes', container = '.horizontal-seperator', PlaceCounter = '#contents', sort_name = '.name', sort_price = '.price-bold', sort_sale_price = '.sale-price-bold'; /* do not change variables */ var containerCount = $(container).length, pages = Math.ceil(containerCount / pageOut), total_pages = Math.ceil(containerCount / pageOut), current_page = location.href; // cookie object function bake_cookie(c_name,value,exdays){ this.set = function(c_name,value,exdays){ var exdate=new Date(); exdate.setDate(exdate.getDate() + exdays); var c_value=escape(value) + ((exdays===null) ? "" : "; expires="+exdate.toUTCString()); document.cookie=c_name + "=" + c_value; }; this.get = function(c_name){ var i,x,y,ARRcookies=document.cookie.split(";"); for (i=0;i'+x+''; }else{ build_html += ''+x+''; } } build_html += ''; if(show_sort === 'yes'){ build_html += '
'; build_html += '
'; } build_html += ''; if(show_paging_links === 'both' || show_paging_links === 'top'){ $(PlaceCounter).before(''+build_html); } if(show_paging_links === 'both' || show_paging_links === 'bottom'){ $(PlaceCounter).after(build_html); } } // this is to help with price and name to the parent. $(container).each(function(){ $(this).attr('data-name',$(this).find(sort_name).text()); $(this).attr('data-price',$(this).find(sort_sale_price).text().replace(/[^\d.]/g,'')); if($(this).attr('data-price') === ''){ $(this).attr('data-price',$(this).find(sort_price).text().replace(/[^\d.]/g,'')); } }); // function to sort by the options built above. function sort_options(sort_option){ $('.sort_options').val(sort_option); switch(sort_option){ case 'a': $(container).sortElements(function(a, b){ return $(a).attr('data-name') > $(b).attr('data-name') ? 1 : -1; }); break; case 'z': $(container).sortElements(function(a, b){ return $(a).attr('data-name') < $(b).attr('data-name') ? 1 : -1; }); break; case 'high': $(container).sortElements(function(a, b){ return $(b).attr('data-price') - $(a).attr('data-price'); }); break; case 'low': $(container).sortElements(function(a, b){ return $(a).attr('data-price') - $(b).attr('data-price'); }); break; } Page_it(default_page); } function Page_it(links){ var high = containerCount; var low = 0; if(use_cookies === 'yes'){ bakecookie.set(current_page,links,1); } if(links !== 'all'){ high = parseInt(links) * pageOut; low = high - pageOut; var currents = 'a[data="'+links+'"]'; $(currents).addClass('current'); } $(container).each(function(i){ if(i < high && i >= low){ $(this).show(); }else{ $(this).hide(); } }); } show = (show_on_section === 'no' && $(sort_sale_price).length === 0 && $(sort_price).length === 0) ? false : true; // this is where the magic starts if(location.pathname !== '/' && show === true){ var bakecookie = new bake_cookie(); if(containerCount > pageOut){ if(use_cookies === 'yes' && bakecookie.get(current_page) != null){ default_page = bakecookie.get(current_page); } build_links(default_page); Page_it(default_page); if(use_cookies === 'yes' && bakecookie.get('ecomm_pagination') != null){ var page_sort = bakecookie.get('ecomm_pagination'); sort_options(page_sort); } } } $('.pagingLinks').data('page',default_page); // end magic $('.page_links a').click(function(e){ e.preventDefault(); if(use_to_top === 'yes'){ location.hash = 'pagination_anchor'; } var links = $(this).attr('data'); $('.page_links a').removeClass('current'); if(links === 'prev'){ links = parseInt($('.pagingLinks').data('page')) - 1; } if(links === 'next'){ links = parseInt($('.pagingLinks').data('page')) + 1; } if(links < 1){ links = 1; } if(links > total_pages){ links = total_pages; } if(links === 'all'){ $('.pagingLinks').data('page',1); }else{ $('.pagingLinks').data('page',links); } Page_it(links); }); $('.sort_options').change(function(){ var sort_option = $(this).val(); bakecookie.set('ecomm_pagination',sort_option,30); sort_options(sort_option); }); });