/**
 * downloadspage extends page.
 *
 * @author geraldyeo
 */
var downloadsPage = function(opts){
    var that = htmlPage(opts), super_init = that.init;
    
    
    function loadDownloadDetails(dlid){
        var $dl = $("#download-listing a[href$="+dlid+"]");
        if ($dl.length === 1){
            $dl.click();
        }
    }
    
    
    function prepDownloadThumbs(){
        $("dl.download-thumb").each(function(i, item){
            var $dl = $(item);
            // set ellipsis to titles
            $dlTitle = $dl.find("dt.title");
            $dlTitle.text(ellipsis($.trim($dlTitle.text()), 35));
        });
    }
    
    
    function ellipsis(str, len){
        if (str && str.length > len) {
            return str.substring(0, len - 3) + "...";
        }
        return str;
    }
    
    
    // override public init
    var init = function(){
        super_init.apply(that);
        
        
    };
    that.init = init;
    
    
    var load = function(path){
        $("#listing-content").load(path, "", function(){
            prepDownloadThumbs();
            // add events
            $(this).find("div.scrollable").scrollable({
                clickable: false,
                items: '#download-listing',
                size: 5,
                speed: 800
            });
            
            $("a[class=downloadmodal]").modal({
                name: "downloadmodal-",
                target: "#main_content",
                height: "505px",
                width: "918px",
                pathToCloseBtn: "/cineplexes/media/images/close_ie6.png"
            });
            // if there's a specified download, open it
            var dlid = (typeof that.parsedUrl.param === 'function') ? that.parsedUrl.param("dlid") : undefined;
            if (dlid) {
                that.parsedUrl = ""; // load once
                delegate.callLater(500, this, loadDownloadDetails, dlid);
            }
        });
    };
    that.load = load;
    
    
    return that;
};
