/** 
 * @projectDescription	Various tasks to perform when the page loads.
 * @author 	Matt Hobbs matt.hobbs@harvestdigital.com
 * @version 	0.2
 */

/** @id codeFocus 
* @classDescription Focuses on the postcode input and selects the value text. 
*/
var postcodeFocus = {
	grab:function(){
		if(!document.getElementById){return;};
		if(!document.getElementById('postcodeInput')){return;};
		var theInput = document.getElementById('postcodeInput');
		theInput.focus();
		theInput.select();
	}
}
/** @id tableSelected 
* @classDescription Changes the class to 'selected' on A to Z table row.  
*/
var tableSelected = {
	grabLetter:function(){
		if(!document.getElementById){return;};
		if(!document.getElementById('atoz')){return;};
		var alphaList = document.getElementById('browseAlphabet').getElementsByTagName('a');
		for (var i=0;i<alphaList.length;i++){
			alphaList[i].onclick = function() {
				var alphaListOnly = document.getElementById('browseAlphabet');
				var listItems = alphaListOnly.getElementsByTagName('li');
				var rowList = document.getElementsByTagName('tr');
				//Loop through top links and remove selected
				for (var h=0;h<listItems.length;h++){
					listItems[h].className = "";
				}
				//Loop through rows and remove selected
				for (var j=0;j<rowList.length;j++){
					rowList[j].className = "";
				}
				this.parentNode.className = "selected";
				var selectedRowID = this.href.substr(this.href.indexOf('#') + 1);
				var selectedRow = document.getElementById(selectedRowID);
				selectedRow.className = "selected";
			}
		}
	}
}
/** @id standardCompliantLinks 
* @classDescription Looks for rel=external, opens selected link in new window.
*/
function doStandardsCompliantExternalLinks() {
	if (!document.getElementsByTagName) {return;}
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		if (anchors[i].getAttribute("href") && anchors[i].getAttribute("rel") == "external") {// for example: title="Download Adobe Reader" rel="external"
			anchors[i].target = "_blank";
			anchors[i].title += " - Opens in new window";
		}
	}
}
/** @id mapInit
* @classDescription Init depends on what page is loaded
*/
function mapInit(){
	if(document.getElementById("atoz")){
		return;
	} else if(document.getElementById("home")){
		return;
	} else if(document.body.className === ("mCarsupermarkets")){
		initCarSupermarket();
	} else if(document.body.className === ("mGroup")){
		initCarSupermarkets();
	} else if(document.body.className === ("mNearest")){
		initNearest();
	} else if(document.body.className === ("mNearest2")){
		initNearest();
	} else if(document.body.className === ("mRegional")){
		initCarSupermarkets();
	}
};
/** @id codeOnloadEvent 
* @classDescription Queue up functions to load on page load 
*/
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}
addLoadEvent(postcodeFocus.grab);
addLoadEvent(doStandardsCompliantExternalLinks);
addLoadEvent(tableSelected.grabLetter);
addLoadEvent(mapInit);

function openBox()
{
	document.getElementById("AdvantagesText").style.display = "block";
}

function closeBox()
{
	document.getElementById("AdvantagesText").style.display = "none";
}
