var theJson;
var attachmentImage = new Image(); 

function showThreads(threadListId)
{

	// set up the tabs; these values map onto the 4 tabs in the UI HTML
	var threadList = new Array('hot-topics', 'most-recent', 'most-viewed', 'highest-rated');
	
	// first make sure all the other tabs are turned off and the tab is not bold
	for (i=0;i<threadList.length;i++) {
		
		// get the node of the thread list
		threadSet = document.getElementById('threads-'+threadList[i]);
		
		// make sure the node exists
		if (threadSet) {
			
			// turn it off
			threadSet.style.display = 'none';
			
		}
		
		// get the node of the thread's tab
		threadTab = document.getElementById('tab-'+threadList[i]);
		
		// make sure the node exists
		if (threadTab) {
			
			// turn off bold
			threadTab.style.fontWeight = 'normal';
			
		}
		
	}
	
	// turn on the one in question: get the node of the thread list
	selectedThreadSet = document.getElementById('threads-'+threadListId);
	
	// make sure the node exists
	if (selectedThreadSet) {
		
		// turn it on
		selectedThreadSet.style.display = 'block';
		
	}
	
	// get the node of the thread's tab
	selectedThreadTab = document.getElementById('tab-'+threadListId);
	
	// make sure it exists
	if (selectedThreadTab) {
		
		// turn it on
		selectedThreadTab.style.fontWeight = 'bold';
		
	}
	
}


function showThread(threadId)
{
	var threadExcerpt = document.getElementById(threadId+'Excerpt');
	var threadFull = document.getElementById(threadId+'Full');
	
	var threadIdCaps = threadId.replace('thread', 'Thread');
	
	var threadRatingCondensed = document.getElementById(threadIdCaps+'-rating-condensed');
	var threadRatingFull = document.getElementById(threadIdCaps+'-rating-full');
	
	if (threadExcerpt) {
		
		var hideExcerpt = new Effect.SlideUp(threadExcerpt, {queue:'front', duration: 0.75});
		var showThread = new Effect.SlideDown(threadFull, {queue:'end', duration: 1});	
		
		/*if (threadRatingFull.innerHTML.indexOf('href') != -1) {
		
			threadRatingCondensed.style.display = 'none';
			threadRatingFull.style.display = 'block';
		
		}*/
		
	} else {
	
		alert('The requested thread cannot be found!');
	
	}
	
}

function hideThread(threadId)
{
	
	var threadExcerpt = document.getElementById(threadId+'Excerpt');
	var threadFull = document.getElementById(threadId+'Full');
	
	var threadIdCaps = threadId.replace('thread', 'Thread');
	
	var threadRatingCondensed = document.getElementById(threadIdCaps+'-rating-condensed');
	var threadRatingFull = document.getElementById(threadIdCaps+'-rating-full');
	
	if (threadExcerpt) {

		var hideThread = new Effect.SlideUp(threadFull, {queue:'front', duration: 0.75});		
		var showExcerpt = new Effect.SlideDown(threadExcerpt, {queue:'end', duration: 0.5});

		/*if (threadRatingFull.innerHTML.indexOf('href') != -1) {

			threadRatingFull.style.display = 'none';
			threadRatingCondensed.style.display = 'block';
		
		}*/
		
	} else {
	
		alert('The requested thread cannot be found!');
	
	}
	
}


function submitRatingVote(handle, rating, forumId, threadId, messageId, containerId, maxRating, threadView)
{
	// hide the tooltip
	eval(handle+'Tooltip.hideToolTip()');
	
	new Ajax.Request('/jsonobj-vote.jspa?vote='+rating+'&forumID='+forumId+'&threadID='+threadId+'&messageID='+messageId, {
			method:'get',
				onSuccess: function(transport){
				// map the result object to a local var
				var json = eval('('+transport.responseText+')');
				// set up a blank string to be filled by starts
				var newRating = '';
				// loop through rating and give a start each time
				for (i = 1 ; i <= json.rating ; i++) {
					newRating += '<img src="images/fullstar.gif">';
				}
				for (; i <= maxRating; i++) {
					newRating += '<img src="images/greystar.gif">';
				}
				// set up a node handle name for collapsed version
				var ratingNodeHandleCondensed = threadView+'message'+messageId+'RatingCondensed';
				// apply the new rating
				document.getElementById(ratingNodeHandleCondensed).innerHTML = newRating;
				// set up a node handle name for the full view
				var ratingNodeHandleFull = threadView+'message'+messageId+'RatingFull';
				// apply the new rating
				document.getElementById(ratingNodeHandleFull).innerHTML = newRating;
			}
	});
}

function submitMustReadVote(handle, rating, forumId, threadId, messageId, containerId, maxRating, threadView)
{	
	new Ajax.Request('/jsonobj-vote.jspa?vote='+rating+'&forumID='+forumId+'&threadID='+threadId+'&messageID='+messageId, {
			method:'get',
				onSuccess: function(transport){
				var json = eval('('+transport.responseText+')');
				jQuery('#'+messageId + '-marriott-voteCount').addClass('voted');
				jQuery('#'+messageId + '-good-read-vote-label').text('Good Read ');
				if (json.count == 0 || json.count > 1) {
				document.getElementById(messageId +'-voteCount').innerHTML = json.count;
				document.getElementById(messageId +'-voteCount-label').innerHTML = "&nbsp;votes";
				} else {
				document.getElementById(messageId +'-voteCount').innerHTML = json.count;
				document.getElementById(messageId +'-voteCount-label').innerHTML = "&nbsp;vote";
				}
			}
	});
}



function toggleRating(toHide,toShow) 
{
	var toHide = document.getElementById(toHide);
	var toShow = document.getElementById(toShow);
	if(toShow.style.display == "none" || toShow.style.display == "") {
		toShow.style.display = "block";
		toHide.style.display = "none";
	}
}



// this is here as each select field calls in onChange. Override as necessary in page views.
function selectHandler(fieldName, value)
{

}


/**
* On the advanced search form, duplicate input from the title field into the tags field
*/
function setSearchTagsField(source, target)
{
	var tagsField = null;
	var textField = null;
	if(source == null || target == null || source == '' || target == '') {
		tagsField = document.getElementById('props0');
		textField = document.getElementById('q01');
	}
	else {
		textField = document.getElementById(source);
		tagsField = document.getElementById(target);
	}

	if (tagsField != null && textField != null) {
	
		if (tagsField.value == '' && textField.value != '') {
			
			tagsField.value = textField.value;
			
		}

	}

	return true;
	
}


/**
* Display a post's attachment; this is a wrapper to the prototype window library.
* Note that the variable attachmentImage has been declared at the top of this file and thus has global scope.
* This is critical for being able to recursively loop in checkImageLoaded()!!!
*
* @src	string	path to attachment to display
*/
function showAttachment(src)
{
	
	// reset the attachmentImage variable; if this is not done and multiple attachments of different aspect ratios are viewed,
	// if the new image hasn't loaded yet, the previous one's dimensions get used.
	attachmentImage = new Image();
	
	// set the source
	attachmentImage.src = src;
	
	// check to see if the image has loaded and show it if so
	checkImageLoaded();
	
}


/**
* Check to see if an image has loaded, if so show it, if not call iteself every 1/10th a second until the image is loaded.
*/
function checkImageLoaded()
{
	
	// if the image has loaded, it will have size attributes
	if (attachmentImage.width > 0 && attachmentImage.height > 0) {

		// check to see if this is IE 6, if so, need to hide all form controls on the page, since these
		// render on top of CSS z-index layers.
		if (navigator.appName == 'Microsoft Internet Explorer' && navigator.appVersion.indexOf('MSIE 6.0') > -1) {
			
			var selects = document.getElementsByTagName('select');
	
			for (i=0 ; i<selects.length; i++) {
			
				selects[i].style.visibility = 'hidden';
				
			}
			
		}
		
		// fire the actual effect wrapper to fade the modal window and pop up in.
		attachmentEffect();
	
	// image hasn't finished loading
	} else {
		
		// recurse back into the function in 1/10th of a second to check again.
		setTimeout("checkImageLoaded()", 100);
	}
	
}


/**
* The actual call to the prototype window library. Note the distinct sizing parameters passed in; IE had some issues unless these were explicitly set.
*/
function attachmentEffect()
{
	
	// the content to put float above the modal window
	var html = '<div onClick="Dialog.closeInfo();" style="cursor: pointer;"><img src="'+attachmentImage.src+'"></div><div id="attachment-caption">click on image to close</div>';
	
	// cover the page with a modal window and fade both the modal and attachment in. There's some nice UI by default in the CSS that's been 
	// turned off until it can be approved; see /local/includes/show-attachment.css for commented out bg images.
    Dialog.info(html,{className:'alphacube', width:(attachmentImage.width + 18), height:(attachmentImage.height + 28)});
}