addDOMLoadEvent(findActiveLinks);
addDOMLoadEvent(productDetailsView);
addDOMLoadEvent(setupOrderSummaryTable);
addDOMLoadEvent(setupClubShowHide);
addDOMLoadEvent(setupLinkDeleteButtons);

function setupOrderSummaryTable()
{
    if ($('orderSummaryTable'))
    {
        $('orderSummaryTable').insert({after: "<p><strong>Click on the order number to view details of that order.</strong></p>"});
        var x = $$('#orderSummaryTable .orderIdCell');
        for(var i=0; i < x.length; i++)
        {
            targetId = ($(x[i]).down('a').identify());
            new FancyZoom(targetId);
        }
    }
}

function productDetailsView()
{
    var x = $$('.product');
    for(var i=0; i < x.length; i++)
    {
        if ($(x[i]).next('.productpopup') && $(x[i]).down('.description')) {
            targetDiv = $(x[i]).next('.productpopup').identify();
            $(x[i]).down('.description').insert({bottom: '<br><a class="findoutmore" href="#' + targetDiv + '">find out more...</a>'});
            new FancyZoom($(x[i]).down('.findoutmore').identify());
        }
    }
}

function findActiveLinks()
{
    // this bit searches through all the menu links and changes active colour of active link
    var x = $$('#nav a');
    for(var i=0; i < x.length; i++)
    {
        var currentPath = window.location.pathname;
	var currentPage = currentPath.substring(currentPath.lastIndexOf('/') + 1);
	var linkPath = $(x[i]).href;
	var linkPage = linkPath.substring(linkPath.lastIndexOf('/') + 1);
	if (currentPage == linkPage || (linkPage == "index.php" && currentPage == ''))
	{
            $(x[i]).up("li").addClassName('active');
	}
    }
}

function setupClubShowHide()
{
    var x = $$('.showhide');
    if ($('clubs'))
    {
        $('clubs').addClassName("showhidewrapper");
        $(x[0]).insert({before:'<p>Select a club night from the list below to view more details:</p>'});
    }

    if ($('events'))
    {
        $('events').addClassName("showhidewrapper");
        $(x[0]).insert({before:'<p>Select an event from the list below to view more details:</p>'});
    }
    
    for(var i=0; i < x.length; i++)
    {
        $(x[i]).insert({top:'<img src="images/plus.gif" alt="Show more information">'});
        hideRegion($(x[i]));
    }
}

function hideRegion(p)
{
	p.next(".showhideme").style.display = "none";
        p.down('img').src = "images/plus.gif";
	p.down('img').alt = "Show more information";

	Event.stopObserving(p, 'click');
	Event.observe(p, 'click', function(e)
	{
            showRegion(this);
	});
}

function showRegion(p)
{
        p.next('.showhideme').style.display = "block";
        p.down('img').src = "images/minus.gif";
	p.down('img').alt = "Hide information";
	Event.stopObserving(p, 'click');
	Event.observe(p, 'click', function(e)
	{
		hideRegion(this);
	});
}
