/* * Amasty Scroll * * @copyright Copyright (c) 2009-2012 Amasty (http://www.amasty.com) */ function amscroll() { this.frameHeight = null; this.initialized = false; /* * Button action was set */ this.loaderSet = false; /* * Ignore flat that loader was set */ this.ignoreLoaderSetFlag = false; /* * Current page */ this.page = 1; /* * Amount of pages to load within curretn search/filters. */ this.pagesCount = 0; /* * Top offset */ this.previousTop = 0; /* * Fetch */ this.pagesMultiplier = 3; /* * All pages loaded, now new pages to load */ this.pagesReached = false; /* * Navigation element */ this.navBarElement = null; /* * Holds amount of loaded pages */ this.loadedPagesNumbers = 1; /* * Url to make request */ this.url = ''; this.placeHolderElement = null; /* * Ajax request can be done: * - auto - automatically * - button - on button click */ this.actionMode = 'auto'; /* * Label on button */ this.loadingTextButton = ''; /* * Path to loading image */ this.loadingImage = ''; /* * Progress bar config */ this.progressbar = {}; /* * Display page numbers or not */ this.pageNumbers = 0; this.loadNextStyle = ''; this.init = function(params) { /* * Get total pages count */ this.pagesCount = this.getPagesCount(); if (this.pagesCount < 2) { return; } this.url = window.location.href.split('#')[0]; this.initialized = true; this.loaderSet = false; this.destroyNavBarElement(); if (params) { for (param in params) { this[param] = params[param]; } } /* * Remove bottom toolbar */ var toolbar = $$(amscroll_toolbar_bottom)[0]; if (toolbar) { $(toolbar).remove(); } /* * Hide pager */ $$(amscroll_pager).each(function(pager) { $(pager).hide(); }); var el = $$(amscroll_product_container_group)[0]; /* * Render page frames */ if (el && this.pagesCount > 1 && el.select('.amscroll-page').length == 0) { var i = 2; while(i <= this.pagesCount) { /* Page Number */ if (this.pageNumbers == 1) { var pageNumEl = new Element('div', { 'class': 'amscroll-page-num', 'id' : 'amscroll-page-num-' + i }); $(pageNumEl).update(this.page_text + i); el.insert(pageNumEl, {position: 'content' }); } /* Page Frame */ var id = 'amscroll-page-' + i; var element = new Element('div', { 'class': 'amscroll-page', 'id' : id, 'rel' : i }).update(''); el.insert(element, {position: 'content'}); i++; } } /* * Calculate frame height depending on loaded page */ this.getFrameHeight(); if (!this.page || this.page == 0) { /* * Get page number from hash */ var page = this.getUrlParam('page'); if (page) { this.page = page; var me = this; if (this.getFrameHeight()) { $$('div.amscroll-page').each(function(item) { var itemPage = parseInt($(item).readAttribute('rel')); if (itemPage <= me.page) { if (me.isLocalStorageSupported()) { var tmpHeight = localStorage.getItem('amscroll_height'); } if (typeof tmpHeight === 'undefined' || !tmpHeight) { tmpHeight = me.getFrameHeight(); } $(item).setStyle({height :tmpHeight + 'px'}); if (me.actionMode == 'button') { me.ignoreLoaderSetFlag = true; me.getLoadingPlaceholder(true, itemPage); } } }); } if (this.actionMode == 'button') { this.ignoreLoaderSetFlag = false; } /* * Scroll to previous position */ if (this.isLocalStorageSupported()) { window.scrollTo(0, localStorage.getItem('amscroll_top')); } if (this.shouldLoadNextPage(this.page)) { this.loadNextPage(this.page, true); } /* * Remove flag that page was in history */ this.setHashParam('external', null); } if (!this.page) { this.page = 2; } } Event.observe(window, "load", function(){ amscroll_correct_height(); setTimeout( function() { amscroll_correct_height(); } , 1000); if (typeof jQuery != 'undefined') { jQuery(window).resize(function(){ amscroll_correct_height(); }); } }); }; this.getFrameHeight = function() { if (this.frameHeight == null) { var el = $$(amscroll_product_container_group)[0]; if (el && $(el).getHeight()) { this.frameHeight = $(el).getHeight(); } } if (this.frameHeight <= 50) { this.frameHeight = 960; } return this.frameHeight - 50; // 50 for margins and paddings }; /* * All pages were loaded */ this.limitReached = function() { return (this.loadedPagesNumbers == this.pagesCount); }; /* * Check that page "page" should be loaded. * Order of blocks is imporant */ this.shouldLoadNextPage = function(page) { /* * If page is not loaded yet or not in loading - allow if(page > this.loadedPagesNumbers + 1){ this.loadNextPage(this.loadedPagesNumbers + 1) return false; } */ var ret = false; $$('div.amscroll-page').each(function(item) { if (parseInt($(item).readAttribute('rel')) == page && !$(item).hasClassName('loaded') && !$(item).hasClassName('loading')) { ret = true; } }); if (ret) { return true; } /* * Page should be loaded taking into account height and current postion */ if (document.viewport) { var top = document.viewport.getScrollOffsets().top; var height = document.viewport.getHeight(); var docHeight = Math.max( Math.max(document.body.scrollHeight, document.documentElement.scrollHeight), Math.max(document.body.clientHeight, document.documentElement.clientHeight), Math.max(document.body.offsetHeight, document.documentElement.offsetHeight) ); return (docHeight - top) <= (this.pagesMultiplier * height); } /* * Limit reached - loading should be stopped */ if (this.limitReached()) { return false; } return true; }; this.destroyNavBarElement = function() { if ($('amscroll-navbar')) { $('amscroll-navbar').remove(); } this.navBarElement = null; }; this.renderPaginator = function(current) { var element = this.getNavBarElement(); if (element) { if (current > 1 && current <= this.pagesCount && this.loadedPagesNumbers > 1) { element.style.display = 'block'; var html = '