// poor man's name space
var the_website_daily = {
	ajaxCall: function (entryPoint, args, handler, raw) {
		var xmlHttp;
		try {
			// Firefox, Opera 8.0+, Safari
			xmlHttp = new XMLHttpRequest();
		} catch (e) {
			// Internet Explorer
			try {
				xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				} catch (e) {
					alert("Your browser does not support AJAX!");
					xmlHttp =  null;
				}
			}
		}

		if (xmlHttp != null) {
			xmlHttp.onreadystatechange =
				function() {
					if(xmlHttp.readyState==4) {
						var response;
						if (raw) response = xmlHttp.responseText;
						else response = eval("("+xmlHttp.responseText+")");
						handler(response);
					}
				};
			xmlHttp.open("POST", entryPoint, true);

			//Send the proper header information along with the request
			xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlHttp.setRequestHeader("Content-length", args.length);
			xmlHttp.setRequestHeader("Connection", "close");
			xmlHttp.send(args);
		}
	},

	preEncode: function (value) {
		// Note: special handling needed for '&' and '+'
		// replace these with substrings that end users are unlikely to enter
		return value.replace(/&/g, "<ampersand>").replace(/\+/g, "<plus>");
	},

	submitTip: function (url) {
		form = document.forms['tipform'];
		if (form.tip_content.value.length == 0) {
			alert('Please enter a tip');
		} else {
			args = "email="+encodeURI(this.preEncode(form['tip_email'].value))+"&content="+encodeURI(this.preEncode(form.tip_content.value));
			this.ajaxCall(
				url,
				args,
				function(response) {
					if (response.error != "") {
						alert("Can't submit question/tip now. Try again later.");
					}
				},
				false
			);
			hideTipDialog();
		}
	},

	// popup support
	showTipDialog: function () {
		this.popup.curPosition = 'left_down';
		this.popup.showDelayedPopup('hot-tip');
		this.popup.curDesc = null;
		document.getElementById('tip_content').focus();
	},

	hideTipDialog: function () {
		tip = document.getElementById('hot-tip'); 
		tip.style.display='none';
		tip.style.top = '-1000px';
		tip.style.left = '-1000px';
	},

	popup: {
		timer: null,
		curDesc: null,
		curPosition: null,
		mousePos: {x: 0, y:0},
		startTimer: function (event, msDelay) {
			this.timer = setTimeout(event, msDelay);
		},

		stopTimer: function () {
			clearTimeout(this.timer);
			this.timer = null;
		},

		showDelayedPopup: function(descID, msDuration) {
			this.stopTimer();
			var desc = document.getElementById(descID);
			if (desc != null) {
				xOffset = 16;
				yOffset = 16;
				pos = this.mousePos;
				desc.style.display='inline';
				if (this.curPosition == 'right_up') {
					yOffset -= desc.offsetHeight;
				} else if (this.curPosition == 'left_down') {
					xOffset -= desc.offsetWidth;
				} else if (this.curPosition == 'left_up') {
					yOffset -= desc.offsetHeight;
					xOffset -= desc.offsetWidth;
				}
				desc.style.top = (pos.y+yOffset)+'px';
				desc.style.left = (pos.x+xOffset)+'px';
				this.curDesc = descID;
				if (msDuration > 0) this.startTimer("the_website_daily.popup.hide('timeout')", msDuration);
			}
		},
		
		// Main public function
		// descID: id of element to show
		// position: 'left_up', 'left_down', 'right_up', 'right_down' relative to mouse position
		// msDelay: millisecond delay before opening popup (0 means open immediately)
		// msDuration: close popup after msDuration milliseconds (0 means don't close)
		show: function(descID, position, msDelay, msDuration) {
			if (this.curDesc == null) {
				this.hide('start');
				this.curPosition = position;
				if (msDelay > 0)
					this.startTimer("the_website_daily.popup.showDelayedPopup('"+descID+"', "+msDuration+")", msDelay);
				else this.showDelayedPopup(descID, msDuration);
			}
		},

		mouseInsideElement: function(id, mouse) {
			if (id == null) return false;
			var element = document.getElementById(id);
			if (element == null) return false;
			var left = element.style.left.replace('px', '');
			var top = element.style.top.replace('px', '');
			var right = left+element.offsetWidth;
			var bottom = top+element.offsetHeight;
			return (left <= mouse.x && mouse.x < right &&
					top <= mouse.y && mouse.y < bottom);
		},

		hide: function(source) {
			if (source=='mouseout' && this.mouseInsideElement(this.curDesc, this.mousePos)) return;
			this.stopTimer();
			if (this.curDesc != null) {
				var desc = document.getElementById(this.curDesc);
				if (desc != null) {
					desc.style.display='none';
					desc.style.top = '-1000px';
					desc.style.left = '-1000px';
				}
				this.curDesc = null;
				this.curPosition = null;
			}
		}
	},

	first_element: function(e) {
		/* Avoids browser quirks with firstChild and whitespace in HTML */
		if (e.nodeType != 1) do e = e.nextSibling; while (e && e.nodeType != 1);			
		return e;
	},
	
	showBio: function (id) {
		document.getElementById('staff_link').innerHTML = "<a href=\""+bio_links[id]+"\">Stories</a>";
		if (bio_imgs[id]) document.getElementById('staff_img').innerHTML = bio_imgs[id];
		if (bio_content[id]) document.getElementById('staff_bio').innerHTML = bio_content[id];
		
		// rebuild add to any subscribe button
		var addtoany_container = document.getElementById('staff_subscribe'),
		addtoany_button = this.first_element(addtoany_container.firstChild),
		new_addtoany_button = document.createElement('a');
	
		/* Replicate current AddToAny button using DOM */
		new_addtoany_button.className = "a2a_dd addtoany_subscribe"; /* Required for drop-down */
		new_addtoany_button.href = "http://www.addtoany.com/subscribe?linkname="+bio_subscribe_title[id]+"&linkurl="+bio_subscribe_url[id];
		new_addtoany_button.innerHTML = addtoany_button.innerHTML;
		
		/* Insert new AddToAny button, and remove previous instance */
		addtoany_container.insertBefore(new_addtoany_button, addtoany_button);
		addtoany_button.parentNode.removeChild(addtoany_button);
		
		/* Set AddToAny standard variables again */
		a2a_linkname = bio_subscribe_title[id];
		a2a_linkurl = bio_subscribe_url[id];
		
		/* Create new drop-down menu instance without having to request page.js script */
		a2a_init('feed');
	}

}

if (document.all)
	document.onmousemove = function(e) {
		the_website_daily.popup.mousePos.x = Math.max(event.clientX + document.body.scrollLeft,0);
		the_website_daily.popup.mousePos.y = Math.max(event.clientY + document.body.scrollTop,0);
		return true;
	}
else {
	document.captureEvents(Event.MOUSEMOVE);
	document.onmousemove = function(e) {
		the_website_daily.popup.mousePos.x = Math.max(e.pageX,0);
		the_website_daily.popup.mousePos.y = Math.max(e.pageY,0);
		return true;
	}
}
