/** 
 * TV Scroller object
 */
Abstract.XMLHttpCatch = Class.create();  
Abstract.XMLHttpCatch.prototype =  
    {  
        initialize: function(opts) { },  
        receive: function(response, scope) {  
            scope.processReponse(response);  
        }  
};
TVClips = Class.create();
Object.extend(Object.extend(TVClips.prototype, Abstract.prototype), {
    initialize: function(options) {
            this.options = Object.extend({
                clipboxId : 'tvclips', 
                binId : 1337,
                type : 'CLIPS'
            }, options || {});
            this.XMLHttpCatch = new Abstract.XMLHttpCatch();  
            
            this.wrapper = $(this.options.clipboxId).down('div.clip-wrapper > div.scroller > div.content');
            this.gliderElement = $(this.options.clipboxId).down('div.clip-wrapper');
            this.gliderElementId = this.gliderElement.identify();
 
            this.populate(this.options.clipboxId, this.wrapper.identify());
            
        }, 
    getUrl: function() {
            var clipbinURL = '/tvclips/tvclipsrss.aspx';    
            var packagebinURL = '/tvclips/alternative_tvclipsrss.aspx';
            var featuredBinURL = '/tvclips/featured_tvclipsrss.aspx';
            var dataUrl = clipbinURL;
            if (this.options.type == 'TODAY') {
                dataUrl = featuredBinURL;
            } else if (this.options.type == 'PKG') {
                dataUrl = packagebinURL;
            }
            return dataUrl;
        },
    next: function() {
            this.glider.next();
        },
    previous: function() {
            this.glider.previous();
        },
    setSource: function(binId, type, pubset) {
            this.options.binId = binId;
            this.options.type = type;
            if (pubset != undefined && pubset != "") {
                this.options.pubset = pubset
            } else {
                pubset = ""
            }
            this.populate(this.options.clipboxId, this.wrapper.identify());
        },
    processReponse: function(transport) {
            var tvclips = transport.responseText.evalJSON();
            var target_id = this.wrapper.identify();
            var clipbox_id = this.options.clipboxId;
            
            $(target_id).innerHTML = "";
            var count = 1;
            /*
            To link to a Bin (usually a show):
            http://watch.bnn.ca/Redirect/?ShowId=xxx
            To link to an PubSet (usually a season)
            http://watch.bnn.ca/Redirect/?SeasonId=xxx
            To link to a Video Package (usually an episode)
            http://watch.bnn.ca/Redirect/?EpisodeId=xxx
            To link to a Clip
            http://watch.bnn.ca/Redirect/?ClipId=xxx
            */
            if (tvclips.tvclipsdata != null && tvclips.tvclipsdata.length > 0) {
                for(var i in tvclips.tvclipsdata) {
                    var url = "";
                    if(tvclips.tvclipsdata[i].type == "CLIP" || tvclips.tvclipsdata[i].type == "2")
                        url = "http://watch.bnn.ca/Redirect/?ClipId=" + tvclips.tvclipsdata[i].id ;
                    if(tvclips.tvclipsdata[i].type == "VIDEO")
                        url = "http://watch.bnn.ca/Redirect/?EpisodeId=" + tvclips.tvclipsdata[i].id ;
                    if(tvclips.tvclipsdata[i].title != null)
                        $(target_id).innerHTML += "<div class=\"tvClipsVids\" id=\"" + target_id + "0"+count+"\"><a target=\"_blank\" href=\""+url+"\"><img src=\""+tvclips.tvclipsdata[i].imgUrl+"\" width=\"128\" height=\"80\" alt=\"\" /><div>"+tvclips.tvclipsdata[i].title+"</div></a></div>";
                    count++;                
                }
                $(target_id).innerHTML += "<br clear=\"all\" /><img src=\"/images/spacer.gif\" width=\"568\" height=\"5\" alt=\"\" />";
                this.glider = new Glider(this.gliderElementId, {sectionClass: 'div.tvClipsVids', displayedElements:4, displayedElements:1});
//                this.glider.start();
                
                $(clipbox_id).show();
            } else {
                $(clipbox_id).hide();
            }
        },
    populate: function(clipbox_id, target_id) {
        
        var XMLHttpCatch = this.XMLHttpCatch;  
        var scope = this;
        
        var jsonrequest = "";
        if (this.options.pubset != undefined && this.options.pubset != "") {
            var jsonrequest = "{\"bid\": \""+ this.options.binId +"\", \"pubsetServiceId\": \"" + this.options.pubset + "\"}";
        } else {
            var jsonrequest = "{\"bid\": \""+ this.options.binId +"\"}";
        }
        new Ajax.Request(this.getUrl(), {
            method:'post',
            postBody:jsonrequest,
            requestHeaders: {Accept: 'application/json'},
            onSuccess: function(transport) {
                XMLHttpCatch.receive(transport, scope)
            }
        });
    }
});