INITIALIZER.addPriority(
		function() {
			//var ss = new SlideShow.Viewer("ssViewer");
			Menu.createSelector("div", "mainMenu", "Selected");
			Menu.createSelector("li", "subMenuItem", "Selected");			
			Sales.createLocator();
			initNewsExpander();
		}
);

Assets.blankGIF = "../images/blank.gif";

//Debug.setOn(true);

/**********************************
***********************************
*** Sales Force Locator ***********
***********************************
**********************************/

var Sales = {
		locators: new Array(),
		createLocator:function() {
			var loc = new Sales.Locator();
			this.locators.push(loc);
			return loc;
		}	
	};

Sales.Locator = Class.create();
Sales.Locator.prototype = {

	initialize: function() {
		this.areas = {};
		this.visibleIds = new Array();
		
		this.postcodeParam= "";
		this.areaParam = "";
		this.typeParam = " ";
		this._build();
	},

	_build: function() {		
		var list = document.getElementById("salesForceList");
		if (list == null) {
			Debug.print("no salesForceList");
			return;		
		}
		
		this.areas = getSalesAreas();
		Debug.print("Areas list is: " + this.areas.length);

		
		var elemArea = this.getAreaElement();
		elemArea.onchange = this.locatorElementChanged.bindWith(this, [elemArea]);
		
		var elemPostcode = this.getPostcodeElement();
		elemPostcode.onkeydown = this.locatorElementChangedAsync.bindWith(this, [elemPostcode]);

		var elemType = this.getTypeElement();
		elemType.onchange = this.locatorElementChanged.bindWith(this, [elemType]);
		
		
		//document.getElementById("locatorArea").onchange = locatorValueChange;//select	
		//document.getElementById("locatorPostcode").onkeydown = locatorValueChangedAsync;
		//document.getElementById("locatorType").onchange = locatorValueChange;//select

		var areaMap = document.getElementById("salesAreaMap");
		var counter = 1;
		
		for (var i=0; i < areaMap.childNodes.length; i++) {
			var a = areaMap.childNodes[i];
			if (a.nodeName != "AREA") continue;

			var index = parseInt(a.id.charAt(a.id.length-1));
            a.onclick = this.setLocatorArea.bindWith(this, [index]);
		}
		this.update();//clears it initially
		this._checkPeople();
	},

	_checkPeople: function() {
		//so go through the list of id's on the sales div, 
		//and if they don't have an entry in the array
		//add a general entry "-", "$id", " "

		var salesDiv = this.getSalesElement();
		var list = findChildByTag(salesDiv, "UL").childNodes;

		var listOfIds = [];
		
		for (var i=0; i < list.length; i++) {
			var li = list[i];
			if (!(li.nodeName) || li.nodeName != "LI") continue;

			if (li.id == "noMatches" || this._hasEntry(li.id)) continue;
				
			listOfIds.push(li.id);			            
		}

		for (var j = 0; j < listOfIds.length; j++) {
			//Debug.print("add id : " + this.listOfIds[j]);
			this.areas.push(["-", listOfIds[j], " "]);
		}
	},

	_hasEntry: function(id) {
		//go through the sales areas looking for an id that matches
		for (var i=0; i < this.areas.length; i++) {
			if (id == this.areas[i][1]) return true;
		}
		return false;
	},

	getAreaElement: function() {
		return document.getElementById("locatorArea");
	},

	getPostcodeElement: function() {
		return document.getElementById("locatorPostcode");
	},

	getTypeElement: function() {
		return document.getElementById("locatorType");
	},

	getSalesElement: function() {
		return document.getElementById("salesForceList");
	},

/*	setAreaArray: function(array) {
		//init's the list of areas
		this.areas = array;
	},*/

	setLocatorArea: function(index) {
		var areaElement = this.getAreaElement();
		areaElement.value = parseInt(index);
		this.locatorElementChanged(areaElement);
	},	

	//async is for the key down timing, seems onkeydown fires before
	//the value in the control has been updated.
	//this is a little "yield" so the event thread can continue and set it.
	//then we fetch and update the value.
	//barely noticeable.  timer gap could be tuned perhaps.
	locatorElementChangedAsync: function(elemRef) {			
		setTimeout(this.locatorElementChanged.bindWith(this, [elemRef]), 50);
	},

	//directly callable with fake event source
	locatorElementChanged: function(elemRef) {		
		var value = elemRef.value;		
		switch (elemRef.id) {
			case "locatorArea": 
				this.setArea(value);			
			break;
	
			case "locatorPostcode":
				this.setPostcode(value);
			break;
	
			case "locatorType":
				this.setType(value);
			break;		
		}
	},

	setArea: function(a) {
		this.areaParam = a.trim();
		this.update();
	},

	isAreaMatch: function(a) {
		//Debug.print("Matching \"" + this.areaParam + "\" and \"" + a + "\"");		
		if (this.areaParam == "" || this.areaParam == " ") {
			//Debug.print("blank...returning true");
			return true;
		}

		var ch = a.charAt(0);	

		if (ch.search(/[1234567890]/) == -1) {//matches non-numeral		
			//Debug.print("non-numeral");	
			return true;
		}
		var result = ch == this.areaParam;
		//Debug.print("isAreaMatch : " + result);
		return (result);
	},



	setPostcode: function(p) {
		this.postcodeParam = p.trim().toUpperCase();
		this.update();
	},



	isPostcodeMatch: function(p) {
		//Debug.print("Matching \"" + this.postcodeParam + "\" and \"" + p + "\"");	
		var result = true;
		if (this.postcodeParam != "" && this.postcodeParam != " ") {

		//now then, can assume that p is 4 chars, some of which may be '-'.	

			//Debug.print(Math.min(4, this.postcodeParam.length));

			var end = Math.min(4, this.postcodeParam.length);
			for (var i=0; i < end; i++) {			
				var m = p.charAt(i);


				if (m == "-") {
					//Debug.print("end of mask");
					break;//run out of mask, so matches
				}


				var pv = this.postcodeParam.charAt(i);
				if (m != pv) {
					//Debug.print("mismatch: " + m + " != " + pv);
					result = false;
					break;
				}

			}
		}
		//Debug.print(result);
		return result;

	},



	setType: function(t) {
		this.typeParam = t.trim();
		//Debug.print("Type is " + t);
		this.update();
	},

	isTypeMatch: function(t) {
		//Debug.print("Matching \"" + this.typeParam + "\" and \"" + t + "\"");
		if (this.typeParam == " ") {
			//Debug.print("blank...returning true");
			return true;
		}

		return (t == this.typeParam);
	},



	update: function() {
		this.visibleIds.length = 0;

		var hasSearchParams = false;
		
		Debug.print("---- update -----------------");
		Debug.print("Area : '" + this.areaParam + "'");
		Debug.print("Type : '" + this.typeParam + "'");
		Debug.print("Postcode : '" + this.postcodeParam + "'");

		if (this.areaParam.trim().length != 0 || this.postcodeParam.trim().length != 0 || this.typeParam.trim().length != 0) {

			hasSearchParams = true;		

			for (var j=0; j < this.areas.length; j++) {
				var arr = this.areas[j];
				/*Debug.print("-------------------------------------");
				Debug.print("Area : " + arr[1]);
				Debug.print("Type : " + arr[2]);
				Debug.print("Postcode : " + arr[0]);*/
				var visible =  this.isAreaMatch(arr[1]) && this.isTypeMatch(arr[2]) && this.isPostcodeMatch(arr[0]);
				//duplicate check here - much quicker than not doing it - despite all the array walking
				if (visible && this.visibleIds.getIndexOfValue(arr[1]) == -1) {
					//Debug.print("adding " + arr[1] + " as visible");
					this.visibleIds.push(arr[1]);
				}
			}
		} else {
			Debug.print("No params");
		}

		var salesDiv = this.getSalesElement();

		salesDiv.style.visibility = "hidden";

		//make them all invisible, then selectively show them

		var list = findChildByTag(salesDiv, "UL").childNodes;	

		for (var i=0; i < list.length; i++) {
			if (list[i].style) {
				list[i].style.display = "none";
			}
		}

		Debug.print("visible list is : " + this.visibleIds.length);
		if (this.visibleIds.length == 0) {
			if (hasSearchParams) {
				document.getElementById("noMatches").style.display = "block";
			}
		} else {

			for (i=0; i < this.visibleIds.length; i++) {
				//Debug.print(this.visibleIds[i]);
				var elemRef = document.getElementById(this.visibleIds[i]);

				try {
					elemRef.style.display = "block";
				} catch (e) {
					Debug.print("error showing id: \"" + this.visibleIds[i] + "\"");
					Debug.print(e);
				}
			}
		}

		salesDiv.style.visibility = "visible";

		//alert("update");

	}
}

//http://creative.gettyimages.com/source/classes/FrameSet.aspx?s=ImagesSearchState|3|1|0|15|2|1|0|0|1|60|2ed3.d7c5.03ff.e000.002f.76b0.00e0|1|0|cityscape||1|0&pk=4




/*var LOCATOR;

function initLocator() {
	var list = document.getElementById("salesForceList");
	if (list) {
		LOCATOR = new Sales.Locator();
		loadSalesAreas();
		LOCATOR.update();//clears it initially
	}
}

//for image map
function setLocatorArea(area) {
	var elemRef = document.getElementById("locatorArea");
	elemRef.value = area;
	locatorElementChanged(elemRef);
}

//async is for the key down timing, seems onkeydown fires before
//the value in the control has been updated.
//this is a little "yield" so the event thread can continue and set it.
//then we fetch and update the value.
//barely noticeable.  timer gap could be tuned perhaps.
var _evtSrc = null;
function locatorValueChangedAsync(evt) {	
	_evtSrc = EventUtil.getEventSrc(evt);
	setTimeout(locatorValueChangedTimer, 50);
}

function locatorValueChangedTimer() {	
	if (_evtSrc == null) return;

	locatorElementChanged(_evtSrc);
	_evtSrc = null;
}




//event handler
function locatorValueChange(evt) {	
	var evtSrc = EventUtil.getEventSrc(evt);
	locatorElementChanged(evtSrc);
}
	
//directly callable with fake event source
function locatorElementChanged(evtSrc) {		
	var value = evtSrc.value;
	Debug.print(value);
	switch (evtSrc.id) {
		case "locatorArea": 
			LOCATOR.setArea(value);			
		break;

		case "locatorPostcode":
			LOCATOR.setPostcode(value);
		break;

		case "locatorType":
			LOCATOR.setType(value);
		break;		
	}
}

INITIALIZER.add(initLocator);*/

/**********************************
***********************************
***  News item shower/hider *******
***  must OO-ify  *****************
***********************************
**********************************/

function initNewsExpander() {
	var feed = document.getElementById("newsFeed");
	if (feed == null) {		
		return;
	}

	for (var i=0; i < feed.childNodes.length; i++) {
		var child = feed.childNodes[i];
		try {
			if (!child.tagName || child.tagName != "DIV") continue;
		} catch (e) {
			continue;
		}

        var header = findChildByClass(child, "articleHeader");
		if (header == null) {
			//alert("header not found");
			continue;
		}
        
        var title = findChildByTag(header, "H2");
		if (title) {
			title.onmouseover = onNewsTitleOver;
			title.onmouseout = onNewsTitleOut;
			title.onclick = onNewsTitleClick;
			if (Browser.isIE()) {
				title.unselectable = "on";	
			}
		} else {
			alert("title not found");
		}

		
		Util.setClassTo(child, "Hidden");
		
	}
}    


function onNewsTitleOver(evt) {
	var elemRef = EventUtil.getEventSrc(evt);
	Util.setClassTo(elemRef, "Over");
	
}

function onNewsTitleOut(evt) {
	var elemRef = EventUtil.getEventSrc(evt);
	Util.setClassFrom(elemRef, "Over");
}

function onNewsTitleClick(evt) {
	var elemRef = EventUtil.getEventSrc(evt);
	if (elemRef) {
	
		var newsItem = elemRef.parentNode.parentNode;		
		
		if (newsItem.className.indexOf("Hidden") == -1) {			
			Util.setClassTo(newsItem, "Hidden");			
		} else {			
			Util.setClassFrom(newsItem, "Hidden");
		}		
	}
}

