/*
Variables used in the context of the calling page
*/

var _imageRibo = null;
var _selDiv = null;

/*
mSelectStem(image_props)
Places a div over the good part of an image representing a ribozyme.
Adapted from a function written by Julien Gervais-Bird (Use autorised by author).
*/
function mSelectStem(image_props) {
    //alert("coucou");
    if (image_props != '') {
        imageRibo = mGetImage();
        if (imageRibo.style.display != 'none') {
            //alert("hello");
            selectionDiv = mGetSelDiv();
            x = imageRibo.posX();
            y = imageRibo.posY();
            
            tab = image_props.split(';');
            selectionDiv.style.left = (x+parseInt(tab[0])) + "px";
            selectionDiv.style.top = (y+parseInt(tab[1])) + "px";
            selectionDiv.style.width = parseInt(tab[2]) + "px";
            selectionDiv.style.height = parseInt(tab[3]) + "px";
            selectionDiv.style.display = 'block';
        }
    }
}

/*
unSelectStem()
Hides the div previously showed
Adapted from a function written by Julien Gervais-Bird (Use autorised by author).
*/
function mUnselectStem() {
    mGetSelDiv().style.display = 'none';
}

/*
Retrieves the div containing the image we want to select.
Adapted from a function written by Julien Gervais-Bird (Use autorised by author).
*/
function mGetImage() {
    if(!_imageRibo) {
        _imageRibo=new getElement('image_ribo');
    }
    return _imageRibo;
}

/*
Retrives the div that we move around.
Adapted from a function written by Julien Gervais-Bird (Use autorised by author).
*/
function mGetSelDiv() {
    if(!_selDiv) {
        _selDiv=new getElement('selection_div');
    }
    return _selDiv;
}

/* 
getElement(id)
Retrieves an HTML element by id
Adapted from a function written by Peter-Paul Koch (Free of copyright)
Available from http://www.quirksmode.org/
*/
function getElement(id) {
    if (document.getElementById) {
        this.obj = document.getElementById(id);
        this.style = document.getElementById(id).style;
    }
    else if (document.all) {
        this.obj = document.all[id];
        this.style = document.all[id].style;
    }
    else if (document.layers) {
        this.obj = document.layers[id];
        this.style = document.layers[id];
    }
    this.posX = mPosX;
    this.posY = mPosY;
    /* 
    mPosX()
    Finds the x position of the object relative to the document
    */
    function mPosX() {
        return getPosX(this.obj);
    }
    /* 
    mPosY()
    Finds the y position of the object relative to the document
    */
    function mPosY() {
        return getPosY(this.obj);
    }
}

/* 
getPosX()
Finds the x position of the supplied object relative to the div with id "main"
Adapted from a function written by Peter-Paul Koch (Free of copyright)
Available from http://www.quirksmode.org/
*/
function getPosX(obj) {
    var curleft = 0;
    var ie = document.all ? true : false;
    if (obj.offsetParent) {
        while (obj.offsetParent && obj.id != "main")
        {
            if (ie && obj.tagName == "TD")
            {
                curleft += obj.clientLeft;
            }
            curleft += obj.offsetLeft;
            
            obj = obj.offsetParent;
        }
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}

/* 
getPosY()
Finds the y position of the supplied object relative to the div with id "main"
Adapted from a function written by Peter-Paul Koch (Free of copyright)
Available from http://www.quirksmode.org/
*/
function getPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent && obj.id != "main") {
            curtop += obj.offsetTop
            obj = obj.offsetParent;
        }
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}

