YAHOO.CMA.albumTab = {
    json:null
    , pag:null
    , msgElm:null
    , sortSelector:null
    , sortElm:null
    , pagElm:null
    , contentElm:null
    , totalRecords:0
    , rowsPerPage:10
    , message:""
    , CountTag:"%count%"
    , AlbumLabelTag:"%album label%"
    , init:function(elements, json, message, rowsPerPage) {
        if (YAHOO.lang.isObject(elements)) {
            if (YAHOO.lang.isString(elements.msgElm)) {
                this.msgElm = $(elements.msgElm);
            } else {
                this.msgElm = elements.msgElm;
            }
            if (YAHOO.lang.isString(elements.sortElm)) {
                this.sortElm = $(elements.sortElm);
            } else {
                this.sortElm = elements.sortElm;
            }
            if (!YAHOO.lang.isString(elements.pagElm)) {
                this.pagElm = elements.pagElm;
            } else {
                this.pagElm = $(elements.pagElm);
            }
            if (YAHOO.lang.isString(elements.contentElm)) {
                this.contentElm = $(elements.contentElm);
            } else {
                this.contentElm = elements.contentElm;
            }
        } else {
            throw "Invalid Parameter: elements";
        }
        if (YAHOO.lang.isObject(json)) {
            this.json = json;
            if (YAHOO.lang.isObject(json.data)) {
                for(var key in this.json.data) {
                    this.totalRecords++;
                }
                if (!YAHOO.lang.isObject(json.sort) && this.totalRecords > 0) {
                    throw "Invalid Data: Object Required";
                }
            } else {
                throw "Invalid Data: Object Required";
            }
        } else {
            throw "Invalid JSON: Object Required";
        }
        this.message = message;
        if (YAHOO.lang.isNumber(rowsPerPage)) {
            this.rowsPerPage = rowsPerPage;
        }
        this.pag = new YAHOO.widget.Paginator({
            'rowsPerPage':this.rowsPerPage
            , 'totalRecords':this.totalRecords
            , 'containers':this.pagElm
            , 'pageLinks' : 10
        });
        this.updateMsg();
        this.renderSort();
        if(this.totalRecords > this.rowsPerPage) {
            this.pag.subscribe('changeRequest', this.handlePagination, this, true);
            this.pag.render();
            if(YAHOO.lang.isArray(this.pagElm)) {
                for(var idx = 0; idx < this.pagElm.length ; idx++) {
                    $(this.pagElm[idx]).style.display = 'block';
                }
            } else {
                this.pagElm.style.display = 'block';
            }
        }
        this.updateContent();
    }
    , updateMsg:function() {
        var tmpMsg = this.message.replace(this.CountTag, this.totalRecords);
        tmpMsg = tmpMsg.replace(this.AlbumLabelTag, this.totalRecords == 1 ? "Album" : "Albums");
        this.msgElm.innerHTML = tmpMsg;
    }
    , updateSortHandler:function() {
        this.pag.setPage(1);
        this.updateContent();
    }
    , renderSort:function() {
        if (!YAHOO.lang.isNull(this.sortElm) && YAHOO.lang.isObject(this.json.sort)) {
            this.sortSelector = new YAHOO.widget.Selector(this.sortElm, {
                "message":"Sort By:"
                , "selections":[
                    {
                        "id":"price"
                        , "label":"Price"
                        , "title":"Sort albums by price; lowest to highest."
                    }
                    , {
                        "id":"date"
                        , "label":"Release Date"
                        , "title":"Sort albums by release date in descending order."
                    }
                ]
                , "selection":"date"
            });
            this.sortSelector.subscribe('rendered', this.updateSortHandler, this, true);
            this.sortSelector.subscribe('selectionChanged', this.updateSortHandler, this, true);
            this.sortSelector.render();
        }
    }
    , getPageData:function(page) {
        var pageData = [];
        var pageRowBottom = (this.rowsPerPage * (page-1));
        var pageRowTop = (this.rowsPerPage * (page-1)) + this.rowsPerPage;
        if (pageRowTop > this.totalRecords) {
            pageRowTop = this.totalRecords;
        }
        for (var i = pageRowBottom; i < pageRowTop; i++) {
            pageData[i-pageRowBottom] = this.json.sort[this.sortSelector.getState().selection][i];
        }
        return pageData;
    }
    , updateContent:function(newState) {
        var buf = "";
        var page = 1;
        var recordOffset = 0;
        var pageSortedID = null;
        if (YAHOO.lang.isObject(newState)) {
            page = newState.page;
            recordOffset = newState.recordOffset;
        }
        pageSortedID = this.getPageData(page);

        var prevRowNum = 0;
        var limit = pageSortedID.length;
        for (var i = 0; i < limit; i++) {
            var album = this.json.data["id_"+pageSortedID[i]];
            if (YAHOO.lang.isObject(album)) {
                var colNum = ((i)%2==0) ? 1 : 2;
                var rowNum = Math.ceil((i+1)/2);

                if (prevRowNum != rowNum) {
                    if (prevRowNum != 0) {
                        buf += '<div class="nrHR"></div>';
                        buf += '</div>';
                    }
                        buf += '<div class="ATCItem Clear">';
                }

                buf += '<div style="width:290px;float:' + ((colNum%2==0) ? "right" : "left") + ';">';

                buf += '<a href="/album/' + album.upc + '.html"';
                buf += ' style="position:absolute;left:' + ((colNum%2==0) ? 300 : 0) + 'px;top:0px;"><img';
                if (album.imagePath) {
                    buf += ' src="' + album.imagePath + '" width="' + album.imageWidth + '" height="' + album.imageHeight + '"';
                } else {
                    buf += ' src="/images/album_cover_not_available.gif" width="100" height="100"';
                }
                buf += ' id="apg_mo_' + album.upc + '"';
                buf += ' onmouseover="qaa.show(this.id,\'/ajax/qai/' + album.upc + '.json\',null,\'tl:tl\', {x:-120,y:0});"';
                buf += ' class="ca ca3D" alt="View Album Details"/>';
                buf += '<div class="ATCNum Clear">- ' + (i + recordOffset + 1) + ' -</div>';
                buf += '</a>\n';

                buf += '<div class="atcInfo" style="position:absolute;left:' + ((colNum%2==0) ? 414 : 114) + 'px;top:0px;">';
                buf += '<strong>' + this.adjStringLen(album.title,48) + '</strong><br/>\n';
                if (!YAHOO.lang.isUndefined(album.performer) && album.performer.length>0) {
                    buf += this.adjStringLen(album.performer,24) + '<br/>\n';
                }
                buf += '<br/><em>'+ album.label + '</em><br/>\n';
                if (YAHOO.lang.isString(album.releaseDate) && album.releaseDate.length > 0) {
                    buf += 'Rel. ' + album.releaseDate + '<br/>\n';
                }
                buf += '</div>';

                buf += '<div class=\"albPrice\" style="position:absolute;left:' + ((colNum%2==0) ? 414 : 114) + 'px;bottom:41px;font-weight:bold;">';
                if (album.displayPrice > 0) {
                    buf += '$' + dollarFormat(album.displayPrice) + '<br/>\n';
                } else {
                    buf += 'FREE<br/>\n';
                }
                buf += '</div>';

                buf += '<div style="position:absolute;left:' + ((colNum%2==0) ? 485 : 185) + 'px;bottom:40px;">';
                buf += '<div class="btnDetails"><a href="/album/' + album.upc + '.html"><img alt="View Album Details" src="/images/b_details.gif"/></a></div>';
                buf += '</div>';

                buf += '</div>';
                prevRowNum = rowNum;
            }
        }
        if (prevRowNum != 0) { // CLOSE OFF OPEN DIV
            buf += '</div>';
        }
        this.contentElm.innerHTML = buf;
    }
    , adjStringLen:function(str, len) {
        var tmpStr = str;
        if (tmpStr.length > len) {
            tmpStr = tmpStr.substr(0, len-3) + "...";
        }
        return tmpStr;
    }
    , handlePagination:function(newState) {
        this.updateContent(newState);
        this.pag.setState(newState);
    }
}
