var Feature = Class.create();
Feature.prototype = {
    initialize: function(primary_object_id, flip, data, onclick) {
        Object.extend(this, data);
        this.polymorphism = null;
        this.flip = flip
        this.primary_object_id = primary_object_id
        this.image = Builder.node('img', {src:featureBrowser.warName()+'/images/' + this.color() + ".gif",
            id: 'feature_image_' + this.object_id, 
            className:'feature_image_' + this.feature_type.gsub(' ', '_') });

        if(this.object_id == parseInt(this.object_id)) {
            this.image.onmouseover=this.showText.bind(this);
        }


        Event.observe(this.image, 'click', onclick || this.popup.bind(this));
    },

warName: function(){
    var path=location.pathname;
    var a=path.split("/");
    return '/'+a[1];
},
  
toString: function() {
    return this.feature_id + " - " + this.feature_type + " [" + this.start_position + ", " + this.end_position + "]";
},

draw: function(container, ratio, begin_index, end_index) {
    var start_position = this.start_position;
      
    if(this.object_id!=-1){
        //the polymorphism start stop actually spans where the polymorphism actually is.  Draw the middle only (length one instead of 2)
        if(this.object_id == this.primary_object_id) {
            start_position += 1
            this.image.style.width='2px'
            this.image.style.height=(15*1.5)+'px'
        }
        //isPoint
        else if(this.feature_type=='polymorphism') {
            start_position += 1
            this.image.style.width='1px'
        }
        else {
            //+1, next to each other weren't lining up
            this.image.style.width=Math.ceil(ratio * (Math.min(this.end_position+1, end_index)-Math.max(start_position, begin_index))) + 'px';
        }

        if(this.flip) {
            this.image.style.right=Math.max(0,ratio * (start_position-begin_index)) + 'px';
        }
        else {
            this.image.style.left=Math.max(0,ratio * (start_position-begin_index)) + 'px';
        }
    }else{
        this.image.style.width=Math.ceil(ratio * (Math.min(this.end_position+1, end_index)-Math.max(start_position, begin_index))) + 'px';
        this.image.style.left=Math.max(0,ratio * (start_position-begin_index)) + 'px';
        this.image.style.height='15px';
    }
    // + 1 for the orientation bar
    var offset = this.offset+1;
    
   
    //
    // Make the primary feature larger
    //
    if((this.object_id == this.primary_object_id)&&(this.object_id!=-1)) {
        this.image.style.top=(offset * 40 - (7.5*1.5) ) + 'px';
    }
    else if(this.feature_type == 'polymorphism') {
        this.image.style.top=(offset * 40 - 7.5 ) + 'px';
    }
    //alternate above and below the scaffolding
    else if(this.feature_type == 'PCR amplicon') {
        if(this.up) {
            this.image.style.top=(offset * 40 - 11 ) + 'px';
        }
        else {
            this.image.style.top=(offset * 40 + 8 ) + 'px';
        }
    }
    // UTR, CDS, etc...
    else {
       this.image.style.top=(offset * 40 - 5 ) + 'px';     
    }
    
    container.appendChild(this.image);

    if(this.feature_type == 'PCR amplicon' && this.object_id == parseInt(this.object_id)) {
        Element.addClassName('feature_image_' + this.object_id, 'features')
        new Draggable('feature_image_' + this.object_id, {revert:true, tag:'img', ghosting:true});
    }

},

popup: function () {
    alert(this.toString());
},

showText: function () {
    $('info').innerHTML = this.toString();
    
    var offsets = this.getPosition();
    $('pointer').style.top = offsets[1] + 'px';
    $('pointer').style.left = offsets[0]-15 + 'px';
    Element.show($('pointer'));

},

length: function() {return this.end_position - this.start_position},

isPoint: function() {return this.length() == 2},

color: function() {
    if((this.object_id == this.primary_object_id)&&(this.object_id!=-1)) {
        return 'gold'
    }
    switch(this.feature_type)
    {
        case 'polymorphism': return 'black';
        case 'CDS': return 'red';
        case 'UTR': return 'green';
        case 'Repeat': return 'blue';
        case 'PCR amplicon':
            switch(this.validated)
            {
                case 'Pass': return 'green';
                case 'Fail': return 'red';
                //          case 'Pending': return 'yellow';
            default: return 'yellow';
        }
        case 'GenomicDNASpan': return 'green';

}
},

showPolymorphismInfo: function(e) {
if(this.polymorphism) {
    this.stopClick();
    new Effect.Appear(this.row().nextSibling);
}
else {
    //if(this.genotypCnt > 0) {
    new Ajax.ULRequest(this.warName()+'/gene/polymorphism/' + this.object_id, {
        method: 'get',
        onSuccess: this.renderPolymorphismInfo.bind(this)
    });
    //This was removed to allow polyconf info to be visible even if there are no genotypes...
    //}
    //else {
    //  this.polymorphism = new Polymorphism(null, this);
    //  this.polymorphism.render();
    //   this.stopClick();
    // }
}
Event.stop(e);
},

renderPolymorphismInfo: function(transport) {
var data = eval("(" + transport.responseText + ")");
this.polymorphism = new Polymorphism(data, this);
this.polymorphism.render();
this.stopClick();    
},

hidePolymorphismInfo: function(e) {
this.link().innerHTML = '+';
new Effect.Fade(this.row().nextSibling);
this.handleClick(this.link());
Event.stop(e);
},

row: function() {
return $(''+this.feature_id);
},

link: function() {
return this.row().firstChild.firstChild;
},

inRange: function(begin,end) {
return (this.start_position >= begin && this.end_position <= end) ||
    (this.start_position <= begin && this.end_position >= begin) ||
    (this.end_position   >= end   && this.start_position <= end)
},

handleClick: function(link) {
Event.stopObserving(link, "click", this.hidePolymorphism, false);
this.openPolymorphism = this.showPolymorphismInfo.bindAsEventListener(this);
Event.observe(link, "click", this.openPolymorphism, false);
},

stopClick: function() {
var link = this.link();
Event.stopObserving(link, "click", this.openPolymorphism, false);
link.innerHTML = '-';
this.hidePolymorphism = this.hidePolymorphismInfo.bindAsEventListener(this);
Event.observe(link, "click", this.hidePolymorphism, false);
},

/*
    Use the table cell instead of the row to support safari 2.x: http://jacob.peargrove.com/blog/2006/technical/table-row-offsettop-bug-in-safari/
*/
getPosition: function() {
return Position.cumulativeOffset($(''+this.feature_id).firstChild);
}
}
