SwipeWindow = function(options) {
    var config  = {
        container_target: "#swipe_container",
        container_id: "swipe_window_list",
        channel_name: "Preview",
        webservice: null,
        swipe_delay: 6000
    }
    this.config     = $.extend(config, options);
    this.webservice = config.webservice;
    this.channel_name = config.channel_name;
    this.offset = 0;
    
    var container = $("<div></div>");
    container.attr("id", this.config.container_id);
    container.addClass("list");
    
    $(this.config.container_target).html("");
    $(this.config.container_target).append(container);
         
    this._channel_uid = this.config.channel_uid;
    this.getClips();
};
SwipeWindow.prototype = {
    getClips: function(evt) {
        if (evt && typeof(evt.data) == "number")
            this._category_id = evt.data;
        else
            this._category_id = "";
            
        this.webservice.get_content(this._channel_uid, this._category_id, delegate(this, function(data) {
            var clip_list = $("#"+ this.config.container_id);
            clip_list.html("");
            
            if (data.contentList) {
                for (var i=0; i < data.contentList.length; i++) {
                    var clip = data.contentList[i];
                
                    var div  = $("<div></div>");
                    div.addClass("item");
                
                    var link = $("<a></a>");
                    //link.attr("href", BASE_URL + "?inventory="+ clip.inventoryUid + "&clip="+ clip.programUid);
                    //link.attr("href", BASE_URL + "program/"+ program_url(clip));
                    link.addClass("thumb");
                    link.bind('click', clip, delegate(this, this.playClip));
                
                    var img = $("<img></img>");
                    img.attr("width", 500);
                    img.attr("height", 300);
                    img.attr("src", selectThumbnail(clip.thumbnails, 500));
                
                    var md_div = $("<div></div>");
                    md_div.addClass("metadata");
                
                    var h3 = $("<h3></h3>");
                    h3.html(clip.title);
                    h3.addClass("title");
                
                    var p = $("<p></p>");
                    p.addClass("desc");
                    p.html(clip.titleBrief); // synopsis
                    
                    var a = $("<a></a>");
                    a.attr("id", "watch_now_button");
                
                    md_div.append(h3).append(p).append(a);
                    link.append(img).append(md_div);

                    div.append(link);

                    clip_list.append(div);
                }
                
                
                this.clip_count = data.contentList.length;
                $.log("SwipeWindow:")
                $.log(this.clip_count);
                this.startSwipe();
                
            } else {
                var div  = $("<div></div>");
                div.addClass("item");
                var h3 = $("<p></p>");
                h3.html("No clips found");
                h3.addClass("error");
                div.append(h3);
                clip_list.append(div);
            }
        }));
    },
    startSwipe: function() {
        this.offset = 0;
        $("#"+ this.config.container_id).css("left", 0);
        this.swipe_interval = setInterval(delegate(this, this.doSwipe), this.config.swipe_delay);
    },
    stopSwipe: function() {
        clearInterval(this.swipe_interval);
        this.offset = 0;
        $("#"+ this.config.container_id).css("left", 0);
    },
    doSwipe: function() {
        var offset = "";
        var max_offset = ((this.clip_count-1) * 500)
        if (this.offset == max_offset) {
            offset = "+="+max_offset;
            this.offset = 0;
        } else {
            offset = "-=500";
            this.offset += 500;
        }
        
        $("#"+ this.config.container_id).animate({"left": offset+"px"}, "slow");
    },
    playClip: function(evt) {
        this.stopSwipe();
        vidunia_play_clip(evt);
        return false;
    }
};
