/* copyright: generalrobots 2010 all rights reserved */


if(typeof(grobots) != "object") var grobots = {};


grobots.RotatingImage = function(imgName, parentID, width, height,
                                 imgPrefix, imgExt, numImgs,
                                 progressScript, readyScript)
{
    this.imgElm       = document.images[imgName];
    this.initWidth    = width;
    this.initHeight   = height;
    this.left         = 0;
    this.top          = 0;
    this.parentElm    = document.getElementById(parentID);

    this.numImgs      = numImgs;
    this.imgsCache    = [];
    this.imgsComplete = false;

    this.imgIdx       = 0;
    this.zoomPercent  = 100;
    this.paused       = false;
    this.delay        = null;
    this.direction    = null;
    this.interval     = null;


    var that       = this;
    var imgsLoaded = 0;

    for(var i=0; i < numImgs; i++){
        this.imgsCache[i] = new Image();
        this.imgsCache[i].onload = (
            function(theImage)
            {
                return function()
                {
                    imgsLoaded += 1;
                    if(imgsLoaded == 1){
                        // an hack if we can't set both width and height in css:
                        that.initHeight = that.imgsCache[0].height / that.imgsCache[0].width * that.initWidth;
                        that.parentElm.style.height = that.initHeight + "px";
                    }
                    if(progressScript) progressScript(imgsLoaded);
                    if(imgsLoaded == numImgs){
                        that.imgsComplete = true;
                        if(readyScript) readyScript();
                    }
                }
            }
        )(this.imgsCache[i]);
        this.imgsCache[i].onerror = function (){this.onload();};
        this.imgsCache[i].src = imgPrefix + (i+1) + imgExt;
    }
}


grobots.RotatingImage.prototype.enablePanning = function(start_cb)
{
    var that = this;

    this.imgElm.style.cursor = "move";
    this.parentElm.style.cursor = "move";
    this.parentElm.onmousedown = mousedownHandler;

    function mousedownHandler(evt)
    {
        evt = evt || window.event;
        if(evt.button > 1 || evt.which > 1) return true;

        if(start_cb) start_cb();

        var mousePos1 = grobots.getAbsolutePositionFromEvent(evt);
        var idx = that.imgIdx;

        that.imgElm.style.cursor = "pointer";
        that.parentElm.style.cursor = "pointer";
        grobots.addEventhandler("mousemove", document, mousemoveHandler, true);
        grobots.addEventhandler("mouseup", document, mouseupHandler, true);

        function mousemoveHandler(evt)
        {
            evt = evt || window.event;

            var mousePos2 = grobots.getAbsolutePositionFromEvent(evt);
            var diff = that.initWidth / (mousePos1.x - mousePos2.x);
            mousePos1 = mousePos2;
            idx += that.numImgs / diff;
            if(idx - 0.5 < 0) idx += that.numImgs - 1;
            else if(idx + 0.5 >= that.numImgs) idx -= that.numImgs;
            that.imgIdx = Math.round(idx);
            that.imgElm.src = that.imgsCache[that.imgIdx].src;

            return false;
        }

        function mouseupHandler()
        {
            grobots.delEventhandler("mousemove", document, mousemoveHandler, true);
            grobots.delEventhandler("mouseup", document, mouseupHandler, true);
            that.imgElm.style.cursor = "move";
            that.parentElm.style.cursor = "move";

            return false;
        }

        return false;
    }
};


grobots.RotatingImage.prototype.enableDragging = function(start_cb)
{
    var that = this;

    this.imgElm.style.cursor = "move";
    this.parentElm.style.cursor = "move";
    this.parentElm.onmousedown = mousedownHandler;

    function mousedownHandler(evt)
    {
        evt = evt || window.event;
        if(evt.button > 1 || evt.which > 1) return true;

        if(start_cb) start_cb();

        var mousePos1 = grobots.getAbsolutePositionFromEvent(evt);
        var width = that.imgElm.width;
        var height = that.imgElm.height;

        if(!that.imgElm.style.position) that.imgElm.style.position = "relative";
        that.imgElm.style.cursor = "pointer";
        that.parentElm.style.cursor = "pointer";
        grobots.addEventhandler("mousemove", document, mousemoveHandler, true);
        grobots.addEventhandler("mouseup", document, mouseupHandler, true);

        function mousemoveHandler(evt)
        {
            evt = evt || window.event;

            var mousePos2 = grobots.getAbsolutePositionFromEvent(evt);
            that.left += mousePos2.x - mousePos1.x;
            that.top  += mousePos2.y - mousePos1.y;
            mousePos1 = mousePos2;
            if(that.left > width) that.left = width;
            else if(that.left < -1 * width) that.left = -1 * width;
            if(that.top >= height) that.top = height;
            else if(that.top < -1 * height) that.top = -1 * height;
            that.imgElm.style.left = Math.round(that.left) + "px";
            that.imgElm.style.top  = Math.round(that.top) + "px";

            return false;
        }

        function mouseupHandler()
        {
            grobots.delEventhandler("mousemove", document, mousemoveHandler, true);
            grobots.delEventhandler("mouseup", document, mouseupHandler, true);
            that.imgElm.style.cursor = "move";
            that.parentElm.style.cursor = "move";

            return false;
        }

        return false;
    }
};


grobots.RotatingImage.prototype.is_running = function()
{
    return this.interval != null && !this.paused;
}


grobots.RotatingImage.prototype.start = function(delay, direction)
{
    this.direction = direction;
    if(!this.interval || delay != this.delay){
        this.stop();
        this.delay = delay;

        var that = this;
        this.interval = setInterval(
            function()
            {
                if(that.imgsComplete && !that.paused){
                    if(that.direction){
                        that.imgIdx++;
                        if(that.imgIdx >= that.numImgs) that.imgIdx = 0;
                    }
                    else{
                        that.imgIdx--;
                        if(that.imgIdx < 0) that.imgIdx = that.numImgs - 1;
                    }
                    that.imgElm.src = that.imgsCache[that.imgIdx].src;
                }
            },
            delay
        );
    }
    this.paused = false;
};


grobots.RotatingImage.prototype.stop = function()
{
    if(this.interval){
        clearInterval(this.interval);
        this.interval = null;
        this.paused = false;
        return true;
    }
    this.paused = false;
    return false;
};


grobots.RotatingImage.prototype.pause = function()
{
    this.paused = !this.paused;
    return this.paused;
}


grobots.RotatingImage.prototype.toggle = function(delay, direction)
{
    if(!this.paused && this.interval && this.delay == delay && this.direction == direction){
        this.stop();
    }
    else{
        this.start(delay, direction);
    }
};


grobots.RotatingImage.prototype.zoomTo = function(percent)
{
    this.zoomPercent = percent;

    var last_width = this.imgElm.width;
    var last_height = this.imgElm.height;

    this.imgElm.style.height = Math.round(this.initHeight * (percent / 100)) + "px";
    this.imgElm.style.width  = Math.round(this.initWidth * (percent / 100)) + "px";

    if(!this.imgElm.style.position) this.imgElm.style.position = "relative";

    if(!this.left) this.left = 0;
    if(!this.top) this.top   = 0;
    this.left += (last_width - this.imgElm.width)   / 2.0;
    this.top  += (last_height - this.imgElm.height) / 2.0;
    this.imgElm.style.top  = Math.round(this.top)  + "px";
    this.imgElm.style.left = Math.round(this.left) + "px";
};

