	
	function results(category, subcategory) {
		// If we have both category and subcategory, update the results:
		if (category && subcategory) { 
			$j.post("/ajax/list_ideas.php", {category: category, subcategory: subcategory, format: 'html'},
				function(responseXML) {
					var status = $j('status', responseXML).text();
					var message = $j('message', responseXML).text();
					var html = $j('html', responseXML).text();							
					if (status) {
						$j("#results").html(html);
					}
					else {
						$j("#results").html("ERROR: "+message);
					}		
				}
			);
		}
		else if (!subcategory) {
			// Otherwise, f we've called this by clicking on a primary category
			// [and therefore lack a subcategory] update the subcategory list
			$j.post("/ajax/subcategories.php", {category: category},
				function(responseXML) {
					var status = $j('status', responseXML).text();
					var message = $j('message', responseXML).text();
					var html = $j('html', responseXML).text();							
					if (status) {
						$j("#submenu").html(html);
					}
					else {
						$j("#submenu").html("ERROR: "+message);
					}		
				}
			);
		}
		
	}
	function idea(id) {
		// $j("#idea").load("/idea.php?id="+id+" #content", {limit: 25},
		// We no longer need to extract the #content node, as idea.php no longer contains any other HTML tags
		// Note that we also flag to idea.php that this is a 'click', for logging purposes
		$j("#idea").load("/idea.php", {id: id, click: 1},
		  function() {
		  	textReplace();
		  	dimScreen(500, 0.7, function() {
			    $j('#idea').fadeIn('fast');
				});
		  }
		);
	}
	
	function saveToMyIdeas(id) {
		$j.post("/ajax/save_to_my_ideas.php", {id: id},
			function(responseXML) {
				var status = $j('status', responseXML).text();
				var message = $j('message', responseXML).text();
				var html = $j('html', responseXML).text();							
				if (status) {
					alert("Idea saved");
				}
				else {
					alert("ERROR: "+message);
				}		
			}
		);
		return false;
	}

	
	function closeIdea() {
		var current_url = String(document.location);
		var ideas_folder = /\/ideas\//;
		if (current_url.search(ideas_folder) != -1) {
			// We're in the /ideas/ virtual folder, which means we've accessed this Idea via direct URL
			
			// For consistency, we want to return to the root of the site when we close the overlay:
			document.location = '/';
		} else {
			$j('#idea').fadeOut();
			dimScreenStop();
			window.setTimeout('thisMovie("carousel").carouselRestart()',5000);
		}
		return false;
	}
	
	function dimScreenStop() {
		var x = jQuery('#__dimScreen');
    var opacity = x.attr('fade_opacity');
    var speed = x.attr('speed');
    x.fadeOut(speed, function() {
        x.remove();
    });
	}
	
	function dimScreen(speed, opacity, callback) {
    if(jQuery('#__dimScreen').size() > 0) return;
    
    if(typeof speed == 'function') {
        callback = speed;
        speed = null;
    }

    if(typeof opacity == 'function') {
        callback = opacity;
        opacity = null;
    }

    if(speed < 1) {
        var placeholder = opacity;
        opacity = speed;
        speed = placeholder;
    }
    
    if(opacity >= 1) {
        var placeholder = speed;
        speed = opacity;
        opacity = placeholder;
    }
    speed = (speed > 0) ? speed : 500;
    opacity = (opacity > 0) ? opacity : 0.5;
    return jQuery('<div></div>').attr({
            id: '__dimScreen'
            ,fade_opacity: opacity
            ,speed: speed
        }).css({
        background: '#000'
        ,height: $j(document).height() + 'px'
        ,left: '0px'
        ,opacity: 0
        ,position: 'absolute'
        ,top: '0px'
        ,width: $j(document).width() + 'px'
        ,zIndex: 500
    }).appendTo(document.body).fadeTo(speed, opacity, callback);	
	}

	function tflog(e, target, id) {
		$j.post("/ajax/tflog.php", {event: e, target: target, id: id},
			function(responseXML) {
				var status = $j('status', responseXML).text();
				var message = $j('message', responseXML).text();
				var html = $j('html', responseXML).text();							
				if (!status) {
					alert("ERROR");
				}		
			}
		);	
	}


function miniSurvey(id) {		

	var like = $j('input:radio[@name=like]:checked').val();
	var simple = $j('input:radio[@name=simple]:checked').val();
	var mytry = $j('input:radio[@name=try]:checked').val();
	
	if (!like) {
		alert("Please tell us whether you liked this idea");
	}
	else if (!simple) {
		alert("Please tell us whether you thought this idea was simple");
	}
	else if (!mytry) {
		alert("Please tell us whether you would try this idea");
	}
	else {
		$j.post("/ajax/mini_survey.php", {id: id, like: like, simple: simple, mytry: mytry},
			function(responseXML) {
				var status = $j('status', responseXML).text();
				var message = $j('message', responseXML).text();
				var html = $j('html', responseXML).text();							
				if (status) {
					$j('#form').replaceWith('<div class="thanks">'+html+'</div>');
				}
				else {
					$j('#form').replaceWith('<div class="thanks"><p>Error</p></div>');
				}		
			}
		);
		return false;
	
	}

	return false;
}
function myPrint(idea_name) {
	window.print();
	tflog('idea','print',idea_name);
	return false;
}

function clickCarousel() {
	// This function is called from Flash when the user rotates the Carousel manually, and it logs the action:
	tflog('click','carousel',1);
}
