addDOMLoadEvent(setupTextResizer);
addDOMLoadEvent(checkQuickLinksDisplay);
addDOMLoadEvent(setupLinkDeleteButtons);
addDOMLoadEvent(fixSideBarBorder);
addDOMLoadEvent(loadMap);
addDOMLoadEvent(setupReadspeaker);

function setupReadspeaker()
{
    if ($('readspeaker'))
    {
        $('readspeaker').insert({top: '<a href="http://app.readspeaker.com/proreader/proreader.php?cid=4619&amp;lang=en_uk&amp;url=' + location.href + '" target="prwin" onclick=\'window.open(this.href+"&amp;selhtml="+escape(selectedString), this.target, "width=190, height=120, resizable=1, scrollbars=1, screenX=0, screenY=0, left=0, top=0"); return false;\'>Listen to this page</a> <a href="http://app.readspeaker.com/proreader/proreader.php?cid=4619&amp;lang=en_uk&amp;url=' + location.href + '" target="prwin" onclick=\'window.open(this.href+"&amp;selhtml="+escape(selectedString), this.target, "width=190, height=120, resizable=1, scrollbars=1, screenX=0, screenY=0, left=0, top=0"); return false;\'><img src="images/readspeaker.gif" alt="Listen to this page"></a>'});
        $('content').insert({top: '<div id="listen"><a href="http://app.readspeaker.com/proreader/proreader.php?cid=4619&amp;lang=en_uk&amp;url=' + location.href + '" target="prwin" onclick=\'window.open(this.href+"&amp;selhtml="+escape(selectedString), this.target, "width=190, height=120, resizable=1, scrollbars=1, screenX=0, screenY=0, left=0, top=0"); return false;\'><img src="images/readspeaker.gif" alt="Listen to this page"></a></div>'})
    }
}

function fixSideBarBorder()
{
	if ($('side-bar')) {
		var x = $$('#side-bar .featured');
		$(x[0]).setStyle({borderTop: "none"});
	}

}

function hideQuickLinks()
{
	if ($('quick-links'))
	{
		$('quick-links').down('ul').hide();
		if ($('hide-quick-links')) $('hide-quick-links').remove();
		$('quick-links').down('h2').removeClassName('show');
		$('quick-links').down('h2').addClassName('hide');
		$('quick-links').setStyle({paddingBottom: '4px'});
		$('quick-links').insert({bottom: '<a href="#" title="Maximise Quick Links" id="show-quick-links"><img src="images/plus.png" alt="Maximise Quick Links"></a>'});
		Event.observe($('show-quick-links'), 'click', function(e)     {
			showQuickLinks();
			Event.stop(e);
		}, false);	
		createCookie("quickLinks", "hide", 365);
	}
}

function showQuickLinks()
{
	if ($('quick-links'))
	{
		$('quick-links').down('ul').show();		
		if ($('show-quick-links')) $('show-quick-links').remove();
		$('quick-links').down('h2').removeClassName('hide');
		$('quick-links').down('h2').addClassName('show');		
		$('quick-links').setStyle({paddingBottom: '10px'});
		$('quick-links').insert({bottom: '<a href="#" title="Minimise Quick Links" id="hide-quick-links"><img src="images/minus.png" alt="Minimise Quick Links"></a>'});
		Event.observe($('hide-quick-links'), 'click', function(e)     {
			hideQuickLinks();
			Event.stop(e);
		}, false);	
		createCookie("quickLinks", "show", 365);
	}
}

function setupTextResizer()
{
	if ($('fontControls')) 
		$('fontControls').insert({top: 'Change text size: '});
}

function checkQuickLinksDisplay()
{
	var quickLinksValue = readCookie("quickLinks");
	if (quickLinksValue == "hide")
		hideQuickLinks();
	else
		showQuickLinks();
}

function setupBoardShowHide()
{
	var x = $$('.showhide');
	for(var i=0; i < x.length; i++)
    {   
		if (i==0) {
		    $(x[i]).insert({before:'<p class="lead">To view more information and a photo for each Board member, click on the <img src="images/plus-grey.png" alt="plus symbol"> symbol by their name'});
		}
		$(x[i]).insert({top:'<img src="images/minus-grey.png" alt="Show more information">'});
		hideRegion($(x[i]));
    }
}

function setupPerformanceShowHide()
{
	var x = $$('#performanceResults .showhide');
	for(var i=0; i < x.length; i++)
    {
		if (i==0) {
		    $(x[i]).insert({before:'<p class="lead">To view the results, click on the <img src="images/plus-grey.png" alt="plus symbol"> symbol by each heading.'});
		}
		$(x[i]).insert({top:'<img src="images/minus-grey.png" alt="Show more information">'});
		hideRegion($(x[i]));
    }
	
	var y = $$('#whatwedid .showhide');
	for(var n=0; n < y.length; n++)
    {
		if (n==0) {
		    $(y[n]).insert({before:'<p class="lead">To view our response, click on the <img src="images/plus-grey.png" alt="plus symbol"> symbol by each heading.'});
		}
		$(y[n]).insert({top:'<img src="images/minus-grey.png" alt="Show more information">'});
		hideRegion($(y[n]));
    }

        if (location.hash!="")
        {
            selectedOption = location.hash.replace("#","");
            var y = $('content').select('a');
            for(var i=0; i < y.length; i++)
            {
                if ($(y[i]).readAttribute('name')==selectedOption)
                {
                    showRegion($(y[i]).next(".showhide"));
                    $(y[i]).scrollTo();
                }
            }

        }
}

function setupSurveyCardsShowHide()
{
	var x = $$('.showhide');
	for(var i=0; i < x.length; i++)
        {
		if (i==0) {
		    $(x[i]).insert({before:'<p class="lead">To complete a survey card, click on the <img src="images/plus-grey.png" alt="plus symbol"> symbol by each heading.'});
		}
		$(x[i]).insert({top:'<img src="images/minus-grey.png" alt="Show more information">'});
		hideRegion($(x[i]));
        }

        if (location.hash!="")
        {
            selectedOption = location.hash.replace("#","");
            var y = $('content').select('a');
            for(var i=0; i < y.length; i++)
            {
                if ($(y[i]).readAttribute('name')==selectedOption)
                {
                    showRegion($(y[i]).next(".showhide"));
                    $(y[i]).scrollTo();
                }
            }

        }
}

function setupInvolvementShowHide()
{
	var x = $$('.showhide');
	for(var i=0; i < x.length; i++)
        {
		if (i==0) {
		    $(x[i]).insert({before:'<p class="lead">To view more information, click on the <img src="images/plus-grey.png" alt="plus symbol"> symbol by each heading.'});
		}
		$(x[i]).insert({top:'<img src="images/minus-grey.png" alt="Show more information">'});
		hideRegion($(x[i]));
        }

        if (location.hash!="")
        {
            selectedOption = location.hash.replace("#","");
            var y = $('content').select('a');
            for(var i=0; i < y.length; i++)
            {
                if ($(y[i]).readAttribute('name')==selectedOption)
                {
                    showRegion($(y[i]).next(".showhide"));
                    $(y[i]).scrollTo();
                }
            }
        }
}

function setupHousingSchemesShowHide()
{
	var x = $$('.showhide');
	for(var i=0; i < x.length; i++)
        {
		if (i==0) {
		    $(x[i]).insert({before:'<p class="lead">To view more information on each scheme, click on the <img src="images/plus-grey.png" alt="plus symbol"> symbol by each heading.'});
		}
		$(x[i]).insert({top:'<img src="images/minus-grey.png" alt="Show more information">'});
		hideRegion($(x[i]));
        }

        if (location.hash!="")
        {
            selectedOption = location.hash.replace("#","");
            var y = $('content').select('a');
            for(var i=0; i < y.length; i++)
            {
                if ($(y[i]).readAttribute('name')==selectedOption)
                {
                    showRegion($(y[i]).next(".showhide"));
                    $(y[i]).scrollTo();
                }
            }
        }
}

function hideRegion(p)
{
	p.down('img').src = "images/plus-grey.png";
	p.down('img').alt = "Show more information";

	p.next(".showhide_me").style.display = "none";
	
	Event.stopObserving(p.down("img"), 'click');
	Event.observe(p.down("img"), 'click', function(e)
	{
		showRegion(this.up('.showhide'));
	});
}

function showRegion(p)
{
	if ($('surveyCards') || $('performanceResults')) {
            var x = $$('.showhide');
            for(var i=0; i < x.length; i++)
                hideRegion($(x[i]));
        }

        if ($('performanceResults')) {
            if (p.next('.showhide_me').down('.ajaxRegion'))
            {
                surveyId = p.next('.showhide_me').down('.ajaxRegion').identify();
                new Ajax.Updater(p.next('.showhide_me').down('.ajaxRegion'), 'ajax/displaySurveyResults.php', { method: 'post', parameters: {id: surveyId}  });
            }
        }
        
    p.next('.showhide_me').style.display = "block";
	p.down('img').src = "images/minus-grey.png";
	p.down('img').alt = "Hide extra information";
	Event.stopObserving(p.down("img"), 'click');
	Event.observe(p.down("img"), 'click', function(e)
	{
		hideRegion(this.up('.showhide'));
	});
}

function loadMap () 
{
    if ($('map')) {  
		if (GBrowserIsCompatible()) 
		{
			mapDisplay = new GMap2($('map'));
			mapDisplay.setCenter(new GLatLng(51.374950, -0.096709), 13);
			// add map control
			mapDisplay.addControl(new GSmallMapControl());
			mapDisplay.addControl(new GMapTypeControl());
			mapDisplay.addOverlay(new GMarker(new GLatLng(51.374950, -0.096709)));
		}		  
    }
}