﻿
//create an jQuery plug in
(function($)
    {
        $.fn.pager = function(options)
                        {
                            var defaults = {
                                            Display:3,
                                            PageCount:0,
                                            currentPage:0,
                                            outputNextPrevLinks:true,
                                            outputFirstAndLastLinks:true,
                                            ellipsisText:"..."
                                            };
                                            
                            var options = $.extend(defaults, options); 
                            
                            var links;
                            var pager;
                            
                            if(options.PageCount <= 0)
                            {
                                $(this).empty();
                                return;
                            }
                            
                            if(options.currentPage >= options.PageCount)
                            {
                                options.currentPage = 0;
                            }
                            pager = '';
                            options.Display = 3;
                            
                            if (options.PageCount <= 1)
                                options.Display = -1;

                            links = new Array();
                            links.length = 0;
                            links[0] = 0;

                            if (options.Display == -1)
                            {
	                            //links = new int[options.PageCount];
	                            for (i = 0; i < links.length; i++)
		                            links[i] = i + 1;
                            }
                            else
                            {
                                //links = new int[options.Display];
                                links = new Array();
                                links.length = options.Display;
                                        
                                // current page in the middle of our range
                                var middle = Math.floor(options.Display / 2.0) - 1;
                                links[middle] = parseInt(options.currentPage);

                                // pages preceding current page
                                for (i = middle - 1; i >= 0; i--)
	                                links[i] = links[i + 1] - 1;

                                // pages following current page
                                for (i = middle + 1; i < links.length; i++)
	                                links[i] = links[i - 1] + 1;


                                // Get rid of page numbers exceeding options.PageCount ("shift" page numbers to the right)
                                while (links[links.length - 1] > options.PageCount)
                                {
	                                for (i = 0; i < links.length; i++)
		                                links[i]--;
                                }

                                // Get rid of 0 or negative pages ("shift" page numbers to the left)
                                while (links[0] <= 0)
                                {
	                                for (i = 0; i < links.length; i++)
		                                links[i]++;
                                }

                                // assign -1 to pages over options.PageCount
                                for (i = links.length - 1; i >= 0; i--)
                                {
	                                if (links[i] > options.PageCount)
		                                links[i] = -1;
	                                else
		                                break;
                                }
                            }
                            
                            pager += "<ul id='pager'>";
                            
                            if(options.outputNextPrevLinks)
                            {
                                if((options.currentPage + 1) > 1)
                                    pager += "<li id='" + (options.currentPage-1) + "' class='pagePrev' onclick='goToPage(" + (options.currentPage - 1) +")'><< Prec</li>";
                            }
                            
                            if(links[0] != 1)
                            {   
                                if(options.outputFirstAndLastLinks)
                                {
                                    if((options.currentPage + 1) == 1)
                                    {
                                        pager += "<li id='1'";
                                        pager += "class='currentPage' onclick='goToPage("+ (0) + ")'>" + 1 + "</li>";
                                    }
                                    else
                                    {
                                        pager += "<li id='1'";
                                        pager += "class='page'onclick='goToPage("+ (0) + ")'>" + 1 + "</li>";
                                    }
                                }
                                
                                if (links[0] != 2)
		                        {
                                    pager += options.ellipsisText;
		                        }
                            }
                            
                            for ( i = 0; i < links.length; i++)
	                        {
		                        if (links[i] == -1)
			                        break;

                                pager += "<li id='" + links[i] + "'";
                                
                                if((options.currentPage + 1) == links[i])
                                    pager += "class='currentPage'"
                                 else
                                    pager += "class='page'"
                                    
                                pager += " onclick='goToPage("+ (links[i] - 1) + ")'>" + links[i] + "</li>"

		                        if (options.Display == -1)
			                        pager += " "; // write a space so page numbers can wrap if displaying ALL page links
	                        }
                        	
	                        if (links[links.length - 1] != -1 && links[links.length - 1] != options.PageCount)
	                        {
		                        if (links[links.Length - 1] != options.PageCount - 1)
		                        {
                                    pager += options.ellipsisText;
		                        }

                                // output link to last page number
		                        if (options.outputFirstAndLastLinks)
			                        pager += "<li id='" + options.PageCount + "' class='page'";
                        			
			                        pager += " onclick='goToPage("+ (options.PageCount - 1) + ")'>" + options.PageCount + "</li>"
	                        }
                        	
	                        if(options.outputNextPrevLinks)
                            {
                                if((options.currentPage + 1) < options.PageCount)
                                    pager += "<li id='" + (options.currentPage + 1) + "' class='pageNext' onclick='goToPage("+ (options.currentPage + 1) +")'>Suiv >></li>"
                            }
                            
                            pager + "</ul>";
                            
                            $(this).empty();
                            $(pager).appendTo(this);
                            
                        };
    }
)(jQuery);

var scroll;
function goToPage(pageInd)
{
    isSliderInitialized = false;
    
    pageIndex = pageInd;
    
    var div = '<div><img src="images/load2.gif"/></div>';
    $.facebox(div);
                                    
    $.get("extractproduct.aspx",{
                                    group:groupId,
                                    refresh:fetchData,
                                    sort:sortOrder, 
                                    size:pageSize, 
                                    page:pageIndex, 
                                    rand: Math.random(),
                                    priceStartValue:priceStart, 
                                    priceStopValue:priceStop }, 
                                    function(response) 
                                    {
                                        $(document).trigger('close.facebox');
                                        
                                        displayResult(response);
                                        
                                        makeGAnalyticsFunctionCall();
                                        
                                        initializeProductDisplayAndPrepareProductMenuForEachProductBox();
                                        
                                        isSliderInitialized = true;
                                        
                                        createPager();
                                        
                                        scroll = $(window).scrollTop();
                                        window.setTimeout("scrollWindow(scroll)",30);
                                        
                                    });
}
function scrollWindow(scrollTop)
{
    scroll = scrollTop;
    if(scroll >= 0)
    {
        scroll -= 120;
        $(window).scrollTop(scroll);
        window.setTimeout("scrollWindow(scroll)",30);
    }
}