var addListener = function() {
	if ( window.addEventListener ) {
		return function(el, type, fn) {
			el.addEventListener(type, fn, false);
		};
	} else if ( window.attachEvent ) {
		return function(el, type, fn) {
			var f = function() {
				fn.call(el, window.event);
			};
			el.attachEvent('on'+type, f);
		};
	} else {
		return function(el, type, fn) {
			el['on'+type] = fn;
		};
	}
}();

var removeListener = function() {
	if ( window.addEventListener ) {
		return function(el, type, fn) {
			el.removeEventListener(type, fn, false);
		};
	} else if ( window.attachEvent ) {
		return function(el, type, fn) {
			var f = function() {
				fn.call(el, window.event);
			};
			el.detachEvent('on'+type, f);
		};
	} else {
		return function(el, type, fn) {
			el['on'+type] = null;
		};
	}
}();

var dispatchListener = function() {
	if (document.createEvent) {
		return function(el, type, module) {
			var evObj = document.createEvent((module)? module : "Events");
			evObj.initEvent(type, true, true);
			el.dispatchEvent(evObj);
		};
	} else if (document.createEventObject) {
		return function(el, type, module) {
			el.fireEvent("on" + type);
		};
	} else {
		return function(el, type, module) {
			eval("el.on" + type + "();");
		};
	}
}();

function killDefault( e ) {
	if (e && e.preventDefault) {
		e.preventDefault();
	} else {
		e = e || window.event;
		e.returnValue = false;
	}
}

function killPropagation( e ) {
	if (e && e.stopPropagation) {
		e.stopPropagation();
	} else {
		e = e || window.event;
		e.cancelBubble = true;
	}
}

function swapImgSrc( obj, newSrc, srcStrg, srcObj ) {
	if (obj && obj.getAttribute) {
		if (!(srcObj && srcObj.getAttribute)) {
			srcObj = obj;
		}
		var newImg = srcObj.getAttribute(newSrc);
		var currImg = obj.getAttribute("src");
		if (currImg && newImg) {
			if (srcStrg) {
				var oldImg = obj.getAttribute(srcStrg);
				if (!oldImg) {
					obj.setAttribute(srcStrg, currImg);
				}
			}
			obj.setAttribute("src", newImg);
		}
	}
}

(function() {
	/*function swapPhil( e ) {
		swapImgSrc(this, "altSrc", "origSrc");
	}
	
	function unSwapPhil( e ) {
		swapImgSrc(this, "origSrc", "altSrc");
	}
	
	var philPic = document.getElementById("phil");
	if (philPic && philPic.getAttribute("altSrc")) {
		addListener(philPic, "mouseover", swapPhil);
		addListener(philPic, "mouseout", unSwapPhil);
	}*/
	
	var vidPopLinks = document.getElementsByTagName("a");
	if (!!vidPopLinks) {
		for (var i = 0, j = vidPopLinks.length; i < j; i++) {
			if (vidPopLinks[i].className && /^|\bvidPopLink\b|$/.test(vidPopLinks[i].className)) {
				addListener(vidPopLinks[i], "click", function(ev) {
					killDefault(ev);
					window.open(this.href,'elocallinktv','scrollbars=yes,resizable=yes,width=716,height=440,left=0,top=0,ScreenX=0,ScreenY=0');
				});
			}
		}
	}
	
	var tourPic = document.getElementById("tourImg");
	var tourLinksBox = document.getElementById("imgLinks");
	var tourLinks = (tourLinksBox)? tourLinksBox.getElementsByTagName("a") : null;
	
	function updateTour( e ) {
		swapImgSrc(tourPic, "href", "", this);
		var count = this.getAttribute("id").match(/^tl\-(\d+)$/);
		if (count && count[1] && tourPic) {
			count = count[1];
			tourPic.setAttribute("currImg", count);
		}
		killDefault(e);
	}
	
	function updateTourPrev( e ) {
		var count = 0;
		if (tourPic && tourLinks) {
			count = parseInt(tourPic.getAttribute("currImg"));
			count = (0 < count)? count : tourLinks.length;
			var nextIm = document.getElementById("tl-" + --count);
			swapImgSrc(tourPic, "href", "", nextIm);
			tourPic.setAttribute("currImg", count.toString());
		}
		killDefault(e);
	}
	
	function updateTourNext( e ) {
		var count = 0;
		if (tourPic && tourLinks) {
			count = parseInt(tourPic.getAttribute("currImg"));
			count = (tourLinks.length > ++count)? count : 0;
			var nextIm = document.getElementById("tl-" + count);
			swapImgSrc(tourPic, "href", "", nextIm);
			tourPic.setAttribute("currImg", count.toString());
		}
		killDefault(e);
	}
	
	function addPrevNext () {
		if (tourPic) {
			tourPic.setAttribute("currImg", "0");
			var ns = tourPic.nextSibling;
			var pn = tourPic.parentNode;
			if (pn) {
				var holster = document.createElement("div");
				var pLink = document.createElement("a");
				var nLink = document.createElement("a");
				var pImg = document.createElement("img");
				var nImg = document.createElement("img");
				holster.className = "pnLinks";
				pLink.className = "pLink";
				nLink.className = "nLink";
				pLink.setAttribute("href", "#");
				nLink.setAttribute("href", "#");
				pImg.setAttribute("src", "lib/prevBttn.jpg");
				nImg.setAttribute("src", "lib/nextBttn.jpg");
				pLink.appendChild(pImg);
				nLink.appendChild(nImg);
				holster.appendChild(pLink);
				holster.appendChild(nLink);
				addListener(pLink, "click", updateTourPrev);
				addListener(nLink, "click", updateTourNext);
				if (ns) {
					pn.insertBefore(holster, ns);
				} else {
					pn.appendChild(holster);
				}
			}
		}
	}
	
	if (tourPic && tourLinks) {
		var preLoadedImgs = [];
		preLoadedImgs[0] = new Image();
		preLoadedImgs[0].src = "lib/prevBttn.jpg";
		preLoadedImgs[1] = new Image();
		preLoadedImgs[1].src = "lib/nextBttn.jpg";
		for (var i = 0, j = tourLinks.length; i < j; i++) {
			var c = preLoadedImgs.length;
			preLoadedImgs[c] = new Image();
			preLoadedImgs[c].src = tourLinks[i].getAttribute("href");
			tourLinks[i].setAttribute("id", ("tl-" + i));
			addListener(tourLinks[i], "click", updateTour);
		}
		addPrevNext(tourPic);
	}
})();

