var variationOldOnload = window.onload;
var firstRun = true;
window.onload = function()
{
    if (typeof variationOldOnload == 'function')
    {
         variationOldOnload();
    }

    window.imageInstance = new imageItem();
   	imageInstance.init();
    firstRun = false;
}


imageItem = function ()
{
     this.listContainer = null;
     this.thumbsContainer = null;
}

imageItem.prototype.init = function()
{

    this.listContainer = document.getElementById('steps');

    this.imageContainer = document.getElementById('bigImageContainer');
    if (!this.imageContainer)
    {
        return;
    }



	var script = this;

    if (this.listContainer != null)
    {
        var hrefs = this.listContainer.getElementsByTagName('a');
        for(var i = 0, link; link = hrefs[i]; ++i)
        {
            link.blockUrl = link.href + '&block=image&random=' + Math.random();
            link.onclick = function()
            {
                var el = this;
                loadXmlHttp(el.blockUrl, script.processImageResponse, script);
                el = null;
                return false;
            }
        }
    }

    this.prevLink = document.getElementById('prevLink');
    this.nextLink = document.getElementById('nextLink');

    if (this.prevLink != null)
    {
        this.prevLink.onclick = function()
        {
            var url = script.prevLink.href + '&block=image&random=' + Math.random();
            loadXmlHttp(url, script.processImageResponse, script);
            return false;
        }
    }

    if (this.nextLink != null)
    {
        this.nextLink.onclick = function()
        {
            var url = script.nextLink.href + '&block=image&random=' + Math.random();
            loadXmlHttp(url, script.processImageResponse, script);
            return false;
        }
    }

    if(firstRun)
    {
        imageInstance.bla();
    }


}

imageItem.prototype.bla = function()
{
    this.thumbsContainer = document.getElementById('realLifePictures');

    var script = this;

    if (script.thumbsContainer != null)
    {
        var hrefs = script.thumbsContainer.getElementsByTagName('a');
        for(var i = 0, link; link = hrefs[i]; ++i)
        {
            if(link.parentNode.className == 'naviBtn')
            {
                link.blockUrl = link.href + '&block=reallife&random=' + Math.random();
                link.onclick = function()
                {
                    var el = this;
                    loadXmlHttp(el.blockUrl, script.processThumbsResponse, script);
                    el = null;
                    return false;
                }
            }
            else if(!firstRun)
            {
                // attach onclick event handler to all thumbs
                leafBox.attachEventHandler( link, 'click', leafBox.linkClicked );
            }
        }
    }
}

imageItem.prototype.processImageResponse = function(xmlhttp, script)
{
    if (xmlhttp.readyState != 4)
    {
        return;
    }
    script.imageContainer.innerHTML = xmlhttp.responseText;
    imageInstance.init();
}

imageItem.prototype.processThumbsResponse = function(xmlhttp, script)
{
    if (xmlhttp.readyState != 4)
    {
        return;
    }
    script.thumbsContainer.innerHTML = xmlhttp.responseText;
    imageInstance.bla();
}
