/* =========================================================================

NAME: page-sort.js

AUTHOR: Istvan Siposs , Y-Times Publications, LLC
DATE  : 4/13/2005

REVISED: 1/5/2007

COMMENT: Advanced Yahoo! Store pagination and sorting.

============================================================================ */

// CONFIGURATION VARIABLES //

var SHOWALL = true;         // set to False if VIEW ALL should not be displayed.
var PAGENEXT = "NEXT >";
var PAGEPREV = "< PREVIOUS";

function PageObject(sortfields,src)
{
    this.sortfields = sortfields;
    this.src = src;
}

if (queryString('page')=='false')
  CurrentPage = 1;
else
  CurrentPage = queryString('page') * 1;

function PageSortFunction(a,b)
{
    var sf = queryString('sf');
    if (sf =='false')
        return 0;
    if (typeof(a) == 'undefined' || typeof(b) == 'undefined')
        return 0;
        
    var sd = queryString('sd');
    if (sd == 'false' || sd == '')
        sd = 'a';

    if (sd == 'a')
    {
        if (! isNaN(a.sortfields[sf]) && !isNaN(b.sortfields[sf]))
            return (a.sortfields[sf] - b.sortfields[sf]);
        else
        {
            if (a.sortfields[sf] > b.sortfields[sf])
                return 1;
            else if (a.sortfields[sf] < b.sortfields[sf])
                return -1;
            else
                return 0;
        }
    }
    else
    {
        if (! isNaN(a.sortfields[sf]) && !isNaN(b.sortfields[sf]))
            return (b.sortfields[sf] - a.sortfields[sf]);
        else
        {
            if (a.sortfields[sf] < b.sortfields[sf])
                return 1;
            else if (a.sortfields[sf] > b.sortfields[sf])
                return -1;
            else
                return 0;
        }
    }
}

function newSort(dropdown)
{
  var sf = "";
  var sd = "";
  if (dropdown.options[dropdown.selectedIndex].value == 0 || dropdown.options[dropdown.selectedIndex].value == 1)
    sf = 1;
  if (dropdown.options[dropdown.selectedIndex].value == 2)
    sf = 0;
  if (dropdown.options[dropdown.selectedIndex].value == 1)
    sd = "d";
  var href = PageID + ".html?sf=" + sf + "&sd=" + sd + "#sortblock";
  document.location.href = href;
}

function newPageSize(dropdown,params)
{
    var ps = dropdown.options[dropdown.selectedIndex].value;
    var sf = queryString('sf');
    if (sf == 'false')
        sf = '';
    var sd = queryString('sd');
    if (sd == 'false')
        sd = '';
    href = PageID + ".html?sf=" + sf + "&sd=" + sd + "&ps=" + ps + "#sortblock";
    document.location.href = href;
}

function ShowPageControls(idx)
{
    var sf = queryString('sf');
    if (sf == 'false')
        sf = '';
    var sd = queryString('sd');
    if (sd == 'false')
        sd = '';
    var ps = queryString('ps');
    if (ps == 'false')
    {
        ps = GetCookie('pagesize');
        if (!ps)
            ps = pagesize;
    }
    SetCookie('pagesize',ps);

    pagesize = parseInt(GetCookie('pagesize'));
    
    var numpages = Math.ceil(PageObjects.length / ps);
        
    document.write("<a name=sortblock></a><div class='paginate" + idx + "'>");
    
    // stats
    document.write ("<table border=0 width=100% cellpadding=0 cellspacing=0><tr valign=top>");
    if (CurrentPage > 0)
    {
        var ItemsFrom = (CurrentPage - 1) * pagesize + 1;
        var ItemsTo = ItemsFrom + pagesize - 1;
        if (ItemsTo > PageObjects.length)
            ItemsTo = PageObjects.length;
        document.write ("<td nowrap><b>" + PageObjects.length + " items found.</b>" );
        document.write ("<br>Page " + CurrentPage + " of " + numpages + " pages" );
        document.write ("</td>");
    }

    document.write("<td align=right>");
    if (SortFields.length > 0)
    {
        document.write ("Sort by ");
        document.write ("<select id=sfdd style='font-size: 11px' onchange='newSort(this)'>");
        document.write ("<option value=''>Name or Price</option>");
        document.write ("<option value=0>Price Low to High</option>");
        document.write ("<option value=1>Price High to Low</option>");
        document.write ("<option value=2>Name</option>");
        document.write ("</select>");
        if (sf == 1)
        {
          if (sd == "d")
             document.getElementById("sfdd").selectedIndex = 2;
          else
             document.getElementById("sfdd").selectedIndex = sf;
        }
        else
          document.getElementById("sfdd").selectedIndex = sf;
        document.write("&nbsp;|&nbsp;");
    }
    document.write ("View <select name=ps style='font-size: 11px' onchange='document.href=newPageSize(this)'>");
    for (var ps = 0; ps < pageSizes.length; ps++)
    {
        document.write ("<option value=" + pageSizes[ps] + " ");
        if (parseInt(pageSizes[ps]) == parseInt(pagesize))
        {
            document.write ("selected");
        }
        document.write (">" + pageSizes[ps] + "</option>");
    }
    document.write ("</select> items per page");
    if (SHOWALL && numpages > 1)
    {
        document.write("&nbsp;|&nbsp;");
        document.write("<a href=" + PageID + ".html?page=0&sf=" + sf + "&sd=" + sd + "#sortblock>VIEW ALL</a> ");
    }
    document.write("<br>");
    if (CurrentPage > 1)
        document.write ("<a href=" + PageID + ".html?page=" + (CurrentPage - 1) + "&sf=" + sf + "&sd=" + sd + "#sortblock>" + PAGEPREV + "</a>&nbsp; ");
        
    for (var i = 1; i <= numpages && numpages > 1; i++)
    {
        if (CurrentPage != i)
            document.write("<a href=" + PageID + ".html?page=" + i + "&sf=" + sf + "&sd=" + sd + "#sortblock>" + i + "</a>&nbsp; ");
        else
            document.write("<b>" + i + "</b>&nbsp;&nbsp;");
    }
    if (CurrentPage < numpages && CurrentPage > 0)
        document.write ("<a href=" + PageID + ".html?page=" + (CurrentPage + 1) + "&sf=" + sf + "&sd=" + sd + "#sortblock>" + PAGENEXT + "</a>");
    
    document.write("</td></tr></table>");
    document.write("</div>");
}

function ShowPage()
{
    var colcnt = 0;

    var ps = queryString('ps');
    if (ps == 'false')
    {
        ps = GetCookie('pagesize');
        if (!ps)
            ps = pagesize;
    }
    pagesize = parseInt(ps);
    
    if (CurrentPage == 0)
    {
        var idx0 = 0;
        var idx1 = PageObjects.length - 1;
    }
    else
    {
        var idx0 = (CurrentPage - 1) * pagesize;
        var idx1 = idx0 + pagesize - 1;
        if (idx1 >= PageObjects.length)
            idx1 = PageObjects.length - 1;
    }

    var sf = queryString('sf');
    if (sf != 'false' && sf != '')
        PageObjects.sort(PageSortFunction);    
    var idx = idx0;
    ShowPageControls(' controlstop');
    document.write ("<table border=0 width=" + wid + " cellpaddign=0 cellspacing=0>");
    document.write ("<tr>");
    while (idx <= idx1)
    {
        if (colcnt == cols)
        {
            document.write ("</tr><tr>");
            colcnt = 0;
        }
        document.write("<td>" + "<br>" + PageObjects[idx].src.replace(/&#96;/gi, "'") + "</td>");
        colcnt ++;
        idx ++;
    }
    document.write ("</tr></table>");
    ShowPageControls(' controlsbottom');
}


function PageQuery(q) {
if(q.length > 1) this.q = q.substring(1, q.length);
else this.q = null;
this.keyValuePairs = new Array();
if(q) {
for(var i=0; i < this.q.split("&").length; i++) {
this.keyValuePairs[i] = this.q.split("&")[i];
}
}
this.getKeyValuePairs = function() { return this.keyValuePairs; }
this.getValue = function(s) {
for(var j=0; j < this.keyValuePairs.length; j++) {
if(this.keyValuePairs[j].split("=")[0] == s)
return this.keyValuePairs[j].split("=")[1];
}
return false;
}
this.getParameters = function() {
var a = new Array(this.getLength());
for(var j=0; j < this.keyValuePairs.length; j++) {
a[j] = this.keyValuePairs[j].split("=")[0];
}
return a;
}
this.getLength = function() { return this.keyValuePairs.length; } 
}
function queryString(key){
var page = new PageQuery(window.location.search); 
return unescape(page.getValue(key)); 
}
