var Pager = Class.create ();

Pager.prototype.initialize = function ()
{
    this.json = playlistJson.evalJSON ();

    if (this.json.videos.length == 0) { return; }

    this.getPager ();
};

Pager.prototype.getPager = function (pnum)
{
    if (pnum === undefined) {var page = this.json.startPage;} else { var page = pnum; }

    var div = this.getPage (page);

    $("sr").update (div);
};

Pager.prototype.addItem = function (index)
{
    if (index > this.json.videos.length - 1) { return; }

    var div = new Element ("div", {
       "class" : "stripThumb"
    });

    var that = Object.clone (this);

    Event.observe (div, "click", function () {
        window.location = ("v" + that.json.videos [index].videoId + "-dlife.html?" + that.getQueryString ());
    });

    var img = new Element ("img", {
       "src" : this.json.videos [index].thumbnailURL
    });

    Event.observe (img, "click", function () {
        window.location = ("v" + that.json.videos [index].videoId + "-dlife.html?" + that.getQueryString ());
    });

    div.insert (img);

    var d = new Element ("div", {}).insert (this.json.videos [index].name.substr (0, 20));

    Event.observe (d, "click", function () {
        window.location = ("v" + that.json.videos [index].videoId + "-dlife.html?" + that.getQueryString ());
    });

    div.insert (d);

    img = new Element ("img", {
        "src" : "/dlifeTv/images/play.gif",
        "style" : "border: none; clear: both; margin-top: 5px;"
    });

    Event.observe (img, "click", function () {
        window.location = ("v" + that.json.videos [index].videoId + "-dlife.html?" + that.getQueryString ());
    });

    div.insert (img);

    return div;
};

Pager.prototype.getPage = function (pnum)
{
   // setCookie ("startPage", pnum);

    var stop = (this.json.perPage * pnum);

    var i = stop - this.json.perPage;

    var div = new Element ("div");

    var that = Object.clone (this);

    if (pnum > 1) {

        var left = new Element ("img", {
            "src"    : "/widgetBroker/stripResults/images/fsleft.gif",
            "style" : "float: left;"
        });

        Event.observe (left, "click", function () {
           that.getPager (parseInt (pnum, 10) - 1);
        });

        div.insert (left);
    }

    for (var x = i; x < stop; x ++) {
        div.insert (this.addItem (x));
    }

    if (pnum < this.json.totalPages) {

        var right = new Element ("img", {
            "src" : "/widgetBroker/stripResults/images/fsright.gif",
            "style" : "float: right;"
        });

        Event.observe (right, "click", function () {
            that.getPager (parseInt (pnum, 10) + 1); 
        });

        div.insert (right);
    }

    return div;
};

Pager.prototype.getQueryString = function ()
{
    var l = document.location.href.toString ();

    return l.split (/\?/) [1];
};

