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

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


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

imageItem.prototype.init = function()
{
    this.initBigImageChange();
    this.initThumbNavigation()


    /*


    */

}

imageItem.prototype.initBigImageChange = function()
{
    this.imageContainer = document.getElementById('bigImageContainer');
    this.listContainer  = document.getElementById('thumbsList');

    if (
        (!this.imageContainer)
        ||
        (!this.listContainer)
    )
    {
        return;
    }

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

    var script = this;

    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;
        }
    }


    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;
        }
    }
}

imageItem.prototype.initThumbNavigation = function()
{
    this.thumbsContainer = document.getElementById('thumbsContainer');
    if (!this.thumbsContainer)
    {
        return;
    }
    var script = this;

    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=thumbs&random=' + Math.random();
            link.onclick = function()
            {
                var el = this;
                loadXmlHttp(el.blockUrl, script.processThumbsResponse, script);
                el = null;
                return false;
            }
        }
    }
}


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

    script.switchSliceIfNecessary();
}

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

imageItem.prototype.switchSliceIfNecessary = function()
{
    var sliceLink = document.getElementById('sliceLink');
    this.thumbsContainer = document.getElementById('thumbsContainer');
    if (
        (!sliceLink)
        ||
        (!this.thumbsContainer)
    )
    {
        return;
    }

    var hrefs = this.thumbsContainer.getElementsByTagName('a');
    for(var i = 0, link; link = hrefs[i]; ++i)
    {
        if (link.parentNode.className == 'naviBtn')
        {
            if (link.href == sliceLink.href)
            {
                link.onclick();
            }

        }
    }





}
