var Dom = YAHOO.util.Dom;
var thumbURL = "/gallery/img.x?sz=gallery_thumb&id=";
var selfURL = "/gallery/index.x?";

if(!Array.indexOf){
    Array.prototype.indexOf = function(obj){
        for(var i=0; i<this.length; i++){
            if(this[i]==obj){
                return i;
            }
        }
        return -1;
    }
}

function filmStrip(id, size, files, selected) {
    this.id = id;
    this.max_thumbs = size;
    this.init = init;
    this.files = files;
    this.selected = selected;
    this.num_windows;
    this.cur_window;
    
    this.go_left = go_left;
    this.go_right = go_right;
    this.fill_strip = fill_strip;
    this.stripClear = stripClear;
    
    function go_left(e, obj) {
        YAHOO.util.Event.preventDefault(e);
        if ( this.cur_window >0 ) {
            this.stripClear();
            this.cur_window--;
            this.fill_strip();
        }
    }
    
    function go_right(e, obj) {
        YAHOO.util.Event.preventDefault(e);
        if ( this.cur_window < (this.num_windows-1) ) {
            this.stripClear();
            this.cur_window++;
            this.fill_strip();
        }
    }

    function fill_strip() {
        var first = this.cur_window*this.max_thumbs;
        for(var i=first; (i<files.length) && (i< (first+this.max_thumbs)) ; i++) {
            var a = document.createElement("a");
            a.setAttribute("href", selfURL+"id="+this.id+"&image="+this.files[i]);
            if ( this.files[i] == this.selected ) { Dom.addClass(a, "selected"); }
            var img = document.createElement("img");
            //Dom.setStyle(img, "border", "none");
            img.setAttribute("src", thumbURL + this.files[i]);
            a.appendChild(img);
            this.strip.appendChild(a);
        }
        
        //are we at the beginning?
        if ( this.cur_window == 0 ) {
            //inactivate the left arrow
            if ( Dom.hasClass(this.left, "leftarrow_hl") ) {
                Dom.replaceClass(this.left, "leftarrow_hl", "leftarrow") }
        } else {
        //no
            //activate the left arrow
            if ( Dom.hasClass(this.left, "leftarrow") ) {
                Dom.replaceClass(this.left, "leftarrow", "leftarrow_hl") }
        }
        
        //are we at the end?
        if ( this.cur_window >= ( this.num_windows-1)  ) {
            //inactivate the right arrow
            if ( Dom.hasClass(this.right, "rightarrow_hl") ) {
                Dom.replaceClass(this.right, "rightarrow_hl", "rightarrow") }
        } else {
        //no
            //activate the right arrow
            if ( Dom.hasClass(this.right, "rightarrow") ) {
                Dom.replaceClass(this.right, "rightarrow", "rightarrow_hl") }
        }
    }

    function stripClear() {
        while ( this.strip.childNodes.length >0 ) {
            var node = this.strip.childNodes[0]
            this.strip.removeChild(node);
        }
    }

    function init(){
        this.num_windows = Math.ceil(this.files.length/this.max_thumbs);
        if (this.selected > 0 ) {
            this.cur_window = Math.floor( this.files.indexOf(this.selected)/this.max_thumbs );
        } else {
            this.cur_window = 0;
        }

        this.left = Dom.get("gallery_left_" + id);
        this.right = Dom.get("gallery_right_" + id);
        this.strip = Dom.get("filmstrip_"+id);
        YAHOO.util.Event.addListener("gallery_left_" + id, "click", this.go_left, this, true);
        YAHOO.util.Event.addListener("gallery_right_" + id, "click", this.go_right, this, true);
        
        this.fill_strip();
    }
}

