var sublist_old_onload = window.onload;
window.onload = function(){
    if (typeof sublist_old_onload == 'function')
    {
         sublist_old_onload();
    }

    window.sublistAppInstance = new sublistApp();
    sublistAppInstance.init();
	
    window.itemInstance = new prodItem();
}

sublistApp = function ()
{
    this.resDate = null;
}

sublistApp.prototype.init = function()
{
    this.listContainer = document.getElementById('sublist');
    if (!this.listContainer)
    {
        return;
    }	
	
    this.resDate = document.getElementById('rdate');
    if (!this.resDate)
    {
        return;
    }

    // attach submit trigger to select onchange
    var script = this;
    this.resDate.onchange = function()
    {
        script.submitFormAsBlock();
    }
	if(this.resDate.value != document.getElementById('currentRdate').value)
	{
		this.resDate.onchange();
	}
}


sublistApp.prototype.submitFormAsBlock = function()
{
    Blocks.submitFormAsBlock(this.resDate.form, 'sublist_block', this.processFormResponse, this);
    return false; // prevent full page form submit
}

sublistApp.prototype.processFormResponse = function(xmlhttp, script)
{
    if (xmlhttp.readyState != 4)
    {
        return;
    }
	script.listContainer.innerHTML = xmlhttp.responseText;
}

/* prod items */
prodItem = function ()
{
    this.listContainer = document.getElementById('similarItems');
    if (!this.listContainer)
    {
        return;
    }

    this.mainPicture = document.getElementById('inBigImage');
    if (!this.mainPicture)
    {
        return;
    }
	
    this.buttonContainer = document.getElementById('submitForm');
    if (!this.buttonContainer)
    {
        return;
    }	
	
    this.resDate = document.getElementById('rdate');
    if (!this.resDate)
    {
        return;
    }
	
  	this.init();

    // attach submit trigger to select onchange
    var script = this;
    this.resDate.onchange = function()
    {
        script.submitFormAsBlock();
    }
//    this.resDate.onchange();	
}


prodItem.prototype.init = function()
{
    var script = this;

	var hrefs = this.listContainer.getElementsByTagName('a');
	for(var i = 0, link; link = hrefs[i]; ++i)
	{
		if(link.parentNode.className == 'naviBtn')
		{
			link.blockUrl = link.href + '&block=item_similar&random=' + Math.random();
			link.onclick = function()
			{
				var el = this;
				loadXmlHttp(el.blockUrl, script.processSimilarResponse, script);
				el = null;
				return false;
			}
		}
		else
		{
			link.blockUrl = link.href + '&block=item_details&random=' + Math.random();
			link.onclick = function()
			{
				var el = this;
				loadXmlHttp(el.blockUrl, script.processDetailsResponse, script);
				el = null;
				return false;
			}
		}
	}
}

prodItem.prototype.processSimilarResponse = function(xmlhttp, script)
{
    if (xmlhttp.readyState != 4)
    {
        return;
    }
    script.listContainer.innerHTML = xmlhttp.responseText;
   	itemInstance.init();
}

prodItem.prototype.processDetailsResponse = function(xmlhttp, script)
{
    if (xmlhttp.readyState != 4)
    {
        return;
    }
    script.mainPicture.innerHTML = xmlhttp.responseText;
}


prodItem.prototype.submitFormAsBlock = function()
{
    Blocks.submitFormAsBlock(this.resDate.form, 'item_block', this.processFormResponse, this);
    return false; // prevent full page form submit
}

prodItem.prototype.processFormResponse = function(xmlhttp, script)
{
    if (xmlhttp.readyState != 4)
    {
        return;
    }
	script.buttonContainer.innerHTML = xmlhttp.responseText;
}