function openProfile(target, w, h) {
	var width = w || 680;
	var height = h || 700;
//	var brochure = window.open(target, "brochure", "width="+width+",height="+height+",menubar=no,toolbar=no,status=no,links=no,directories=no,personalbar=no,scrollbars=yes,resizable=yes");
	var brochure = window.open(target, "brochure");
	brochure.focus();
	return false;
}

function toProfile(direction) {
	document.changeProfile.referrer.value = window.location.href;
	document.changeProfile.direction.value = direction;
	document.changeProfile.submit();
}

function AJAXInteraction(pURL, pCallbackFunction, pAsText, source_block_id) {
	var vRequest = init();
	vRequest.onreadystatechange = processRequest;

	function init() {
		if(window.XMLHttpRequest) return new XMLHttpRequest();
		else if(window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
	}

	function processRequest () {
		if(vRequest.readyState == 4) {
			if(vRequest.status == 200) {
				if(pCallbackFunction) {
					if(pAsText) pCallbackFunction(vRequest.responseText,source_block_id);
					else pCallbackFunction(vRequest.responseXML,source_block_id);
				}
			}
		}
	}

	this.doGet = function() {
		vRequest.open("GET", pURL, true);
		vRequest.send(null);
	}

	this.doPost = function(pBody) {
		vRequest.open("POST", pURL, true);
		vRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		vRequest.send(pBody);
	}
}

function feedback_update(input, block_id){
	block_id = 'feedback-form-' + block_id;
	document.getElementById(block_id).innerHTML = input;
}

function feedback_submit(form){
	var answer;

	if(form.answer.length > 0){
		for (i=form.answer.length-1; i > -1; i--) {
			if (form.answer[i].checked) {
				answer = form.answer[i].value;
			}
		}

	} else {
		answer = form.answer.value;
	}

	if(answer != "" && answer != undefined){
	req = new AJAXInteraction(form.action + '?qid=' + form.qid.value + '&answer=' + encodeURIComponent(answer),feedback_update,true,form.qid.value);
	req.doGet();
	}
	return false;
}

/**
 * Displays a random banner from an array of banners injected into the guide page template.
 */
function showRandomBanner(parent) {
	if (banners.length > 0) {
		var randIndex = Math.floor(Math.random() * banners.length);
		var rand = banners[randIndex];
		parent.innerHTML = rand;
		parent.className = 'sidebar';
	}
}

Ext.onReady(
	function()
	{
		var fx = {duration: .25, useDisplay: true, block:true };
		var popFx = {duration: .25, useDisplay: true, concurrent: false};
		
		Ext.select('#nearbyGuides').on
		(
			'mouseenter',
			function(ev) 
			{
				if (this.delayedTask != null){
					this.delayedTask.cancel();
					this.delayedTask = null;
				}
				var p = Ext.get('nearbyGuides').down('.popover');
				if (p && p.isDisplayed() != true){
					ev.preventDefault();
					showPopover('nearbyGuides');
				}
			}
		);
		
		// closable popover!
		Ext.select('#nearbyGuides').on
		(
			'mouseleave',
			function(e)
			{
				var p = Ext.select('#nearbyGuides .popover');
				if (p && this.delayedTask == null){
					this.delayedTask = new Ext.util.DelayedTask(
						function(){
							p.fadeOut(popFx);
							this.delayedTask = null;
						}, this);
					this.delayedTask.delay(250);
				}
			}
		);
			
		Ext.select('#searchTerm').on({
			focus: {
				fn: function(ev) {
					var searchField = Ext.get('searchTerm');
					if (searchField.getValue() == 'Search Guide'){
						searchField.set({value: ''});
					}
				},
				scope: this
			}
		});
		
		function showPopover(id)
		{
			var c = Ext.get(id),
				p = Ext.select("#" + id + " .popover");
			if (p)
			{
				var y = c.getY();
				var x = c.getX();
				var height = c.getHeight();
				var width = c.getWidth();
				var pTop = ((y + height) + 12) + "px";
				var pLeft = ((x + (width/2)) - 80) + "px";
				p.setLeftTop(pLeft,pTop).fadeIn(fx);
			}
		}
		
	}
);
