addDOMLoadEvent(setupCategoryMenu);
addDOMLoadEvent(fixProducts);
addDOMLoadEvent(setupEnquiryForm);
addDOMLoadEvent(fixHomeBoxes);

function setupEnquiryForm()
{
    if ($('formHandling'))
    {
        if($('javaField')) {
            contactMethod = $('javaField').getValue();
            updateFields(contactMethod);
        }
        var y = $$('#formHandling tr.contactMethod input');
        for(var n=0; n < y.length; n++)
        {
            Event.observe($(y[n]), 'click', function(e)
            {
                contactMethod = this.getValue();
                updateFields(contactMethod);
            });
        }
    }
}

function updateFields(contactMethod)
{
                if (contactMethod=="email")
                {
                    $('formHandling').down('.emailField').show();
                    $('formHandling').down('.phoneField').hide();
                    $('formHandling').down('.addressField').hide();
                    $('formHandling').down('.signupField').show();
                    $('formHandling').down('.paraField').show();
                    $('formHandling').down('.pricelistField').show();
                } else if (contactMethod=="phone")
                {
                    $('formHandling').down('.emailField').hide();
                    $('formHandling').down('.phoneField').show();
                    $('formHandling').down('.addressField').hide();
                    $('formHandling').down('.signupField').hide();
                    $('formHandling').down('.paraField').hide();
                    $('formHandling').down('.pricelistField').hide();
                } else if (contactMethod=="post")
                {
                    $('formHandling').down('.emailField').hide();
                    $('formHandling').down('.phoneField').hide();
                    $('formHandling').down('.addressField').show();
                    $('formHandling').down('.signupField').hide();
                    $('formHandling').down('.paraField').hide();
                    $('formHandling').down('.pricelistField').show();
                }
}

function fixProducts()
{
    screenWidth = document.viewport.getWidth();
    if (screenWidth > 1950) maxCol = 6;
    else if (screenWidth > 1630) maxCol = 5;
    else if (screenWidth > 1320) maxCol = 4;
    else if (screenWidth > 990) maxCol = 3;
    else maxCol = 2;

    counterRow = 1;
    counterCol = 1;
    maxHeight = 0;
    var y = $$('.product');
    for(var n=0; n < y.length; n++)
    {
        $(y[n]).removeClassName("first");
        if (counterCol == 1) $(y[n]).addClassName("first");
        $(y[n]).addClassName("row" + counterRow);
        maxHeight = Math.max(maxHeight, $(y[n]).down(".padFix").getHeight());
        if (counterCol == maxCol || n==y.length-1) {
            counterCol = 1;
            setHeight("row" + counterRow, maxHeight);
            maxHeight = 0;
            counterRow++;
        } else {
            counterCol++;
        }
    }
}

function fixHomeBoxes()
{
    screenWidth = document.viewport.getWidth();
    if (screenWidth > 1950) maxCol = 6;
    else if (screenWidth > 1630) maxCol = 5;
    else if (screenWidth > 1320) maxCol = 4;
    else if (screenWidth > 990) maxCol = 3;
    else maxCol = 2;

    counterRow = 1;
    counterCol = 1;
    maxHeight = 0;
    var y = $$('.homebox');
    for(var n=0; n < y.length; n++)
    {
        $(y[n]).removeClassName("first");
        if (counterCol == 1) $(y[n]).addClassName("first");
        $(y[n]).addClassName("row" + counterRow);
        maxHeight = Math.max(maxHeight, $(y[n]).down(".padFix").getHeight());
        if (counterCol == maxCol || n==y.length-1) {
            counterCol = 1;
            setHeight("row" + counterRow, maxHeight);
            maxHeight = 0;
            counterRow++;
        } else {
            counterCol++;
        }
    }
}

function setHeight(boxClass, maxHeight)
{
    var y = $$('.' + boxClass);
    for(var n=0; n < y.length; n++)
    {
        $(y[n]).down(".padFix").setStyle({height: maxHeight + 'px'});
    }
}

function setupCategoryMenu()
{
    var y = $$('#categories li.level0');
    for(var n=0; n < y.length; n++)
    {
        $(y[n]).insert({top:'<img src="images/blank.png">'});
    }

    var x = $$('#categories ul ul');
    for(var i=0; i < x.length; i++)
    {
        if ($(x[i]).up("li").hasClassName("active")) showRegion($(x[i]));
        else hideRegion($(x[i]));
    }
}

function hideRegion(p)
{
	p.up("li").removeClassName("active");
        p.up("li").addClassName("inactive");
        p.up("li").down('img').src = "images/plus-grey.png";
	p.up("li").down('img').alt = "Show sub-categories";

	Event.stopObserving(p.up("li").down('img'), 'click');
	Event.observe(p.up("li").down('img'), 'click', function(e)
	{
            showRegion(this.up("li").down("ul"));
	});
}

function showRegion(p)
{
        p.up("li").removeClassName("inactive");
        p.up("li").addClassName("active");
        p.up("li").down('img').src = "images/minus-grey.png";
	p.up("li").down('img').alt = "Hide sub-categories";
	Event.stopObserving(p.up("li").down('img'), 'click');
	Event.observe(p.up("li").down('img'), 'click', function(e)
	{
		hideRegion(this.up("li").down("ul"));
	});
}
