/*
function:	targetLinks()

If placed in an onload function, this will loop through all of the A tags in a
document and hunt for the attribute of REL = EXTERNAL. If found it will open a
new window for the link URL. This is to get around the W3C XHTML discontinuation
of the TARGET = _BLANK attribute in links.
*/

function targetLinks() {
	var lnks = document.getElementsByTagName('a');
    for (var i = 0; i < lnks.length; i++) {
		var lnk = lnks[i];
		if (lnk.getAttribute("href") && lnk.getAttribute("rel") == "external") lnk.target = "_blank"; 
   	}
}

/*
function:	swapDefault(input, string)
author:		Miroslav Wiesner
date:		May 25, 2006

Determines whether the value of the input is the provided string. If yes onFocus,
the value is blank. If the value is blank onBlur, the value becomes the string.

*/

function swapDefault(obj, str) {
	if (obj.value == str) obj.value = "";
	Event.observe(obj, 'blur', 
			function(e) {
				if (Event.element(e).value == "") obj.value = str;
			}, false);
}

// function to add a selected class to an object

function makeSelected(obj, state) {
	if(state == true) obj.className += " selected";
	else if(state == false) obj.className = obj.className.substring(0, obj.className.indexOf(" selected"));
}

// Prerequisites: Tabs must be made from a UL with child LI and A tags and 
// must be the FIRST element to appear inside the TARGET.

function toggleTabs(obj, target) {
	var i;
	var target	= $(target);
	Element.cleanWhitespace(target);
	var ul		= target.getElementsByTagName("ul")[0];
	var tabs	= ul.getElementsByTagName("a");
	var divs	= target.childNodes;
	for(i = 0; i < tabs.length; i++) {
		var j		= i + 1;
		if(tabs[i] == obj) {
			selectTab(tabs[i]);
			Element.show(divs[j]);
		} else Element.hide(divs[j]);
	}
}

function selectTab(obj) {
	var i;
	var anx	= obj.parentNode.parentNode.getElementsByTagName("a");
	for(i = 0; i < anx.length; i++) {
		if(anx[i] == obj) {
			obj.className += " selected";
		} else {
			anx[i].className = anx[i].className.substring(0, anx[i].className.indexOf(" selected"));
		}
	}
}
	
//	-------------------------------------
//	Favorites
//	-------------------------------------
	
function favorite()
{
	$("ul#favorites_list li.save a").click( function(event) {
		var id			= $(this);
		var itemid		= $(this).attr("href");
		var title		= $(this).attr("title");
		var del			= "";
		var type		= "entry_id";
		var save_url	= "/ajax/save/";
		var count_url	= "/ajax/favorites_count/";
		var count_id	= "favorites_count";
		
		if ( $(this).attr("class") == "unsave" )
		{
			del	= "/delete";
		}
		
		if ( $(this).attr("alt") == "member" )
		{
			type	= "member_id";
		}
		
		if ( type == "member_id" )
		{
			save_url	= "/ajax/save_member/";
			count_url	= "/ajax/member_count/";
			count_id	= "member_count";
		}
		
		$(id).attr("class","loading");
		
		$.post( save_url + itemid + del, {}, function(data) {
			if ( data.substr(0,1) == "!" )
			{
				alert(data.substr(1));
				return;
			}
			else
			{
				alert(data);
				
				$("#" + count_id).load( count_url + itemid, {}, function() {
					if ( del == "" )
					{
						$(id).attr("class","unsave");
						$(id).text("Remove " + title + " from my Favorites");
					}
					else
					{
						$(id).attr("class","save");
						$(id).text("Save " + title + " to my Favorites");
					}
					
					$("#" + count_id).append( " for " + title );
				});
			}
		});
		
		return false;
	});
}
	
//	-------------------------------------
//	Ratings
//	-------------------------------------

function ratings()
{
	// Capture rating mouseover
	$('a.star').mouseover(function(event)
	{
		rate(event, this);
	});
	
	// Capture rating mouseout
	$('a.star').mouseout(function(event)
	{
		rate(event, this);
	});
	
	// Capture rating star click and kill it
	$('a.star').click(function(event)
	{
		rating = $('img', this).attr('alt');
		
		event.preventDefault();
		return false;
	});
	
	// Capture rating submit button click
	$('a.r_submit').click(function(event)
	{
		if (rating != 0) {
			$('input[@name=rating]', this.parent).val(rating);

			$(this).parents('form').submit();

			rating = 0;
		} else {
			alert('You must click on the stars before you can save the rating.');
		}
		
		event.preventDefault();
		return false;
	});
}
	
function rate(event, el)
{
	var id 		= (event.type == 'mouseout') ? rating : $('img', el).attr('alt');
	var stars 	= $(el).siblings().add(el);
	
	$.each(stars, function(i, n) {
		$('img', n).attr("src","http://spotlight.macfound.org/themes/rating_themes/kn/images/star-0.gif").removeClass('on');
		
		if ($('img', n).attr('alt') <= id) {
			$('img', n).attr("src","http://spotlight.macfound.org/themes/rating_themes/kn/images/star-100.gif").addClass('on');
		}
	});
}

function results_tabs()
{
	$("ul#related_sort_tabs li a").click( function(event)
	{
		var selected	= $(this).attr("href");
		
		$("ul#related_sort_tabs li").removeClass("selected");
		
		$.each( ['people', 'discussions', 'events'], function(i,n)
		{
			$("div#" + n ).hide();
		});
		
		$("div#" + selected ).show();
		
		$(this).parent("li").addClass("selected");
		
		return false;
	});
}

function sort_tabs()
{
	$("ul#sort_tabs li a").click( function(event) {
		$(this).parents("li").siblings().removeClass("selected");
		$(this).parents("li").addClass("selected");
		$("ul#sort_tabs li a").each( function(i,n) {
			$("div#" + $(n).attr("href")).hide();
		});
		$("div#" + $(this).attr("href")).show();
		
		return false;
	});
}
	
//	-------------------------------------
//	Top searches
//	-------------------------------------

function top_searches ()
{
	$("form#top_searches a").click( function(event) {
		$("form#top_searches input#top_searches_keywords").attr( "value", $(this).attr("href") );
		$("form#top_searches")[0].submit();
		
		event.preventDefault();
		return false;
	});
}
	
//	-------------------------------------
//	Check all
//	-------------------------------------

function checkall ( which )
{
	if ( which == 'categories' )
	{
		$("#category_checks input#checkall").click( function(event) {
			var check	= ( $(this).attr( "checked" ) == true ) ? true: false;
			
			$("#category_checks input.checkbox").attr( "checked", check );
		});
	}
}
	
//	-------------------------------------
//	Page load trigger
//	-------------------------------------

var rating = 0;

jQuery(function($)
{
	favorite();
	ratings();
	results_tabs();
	sort_tabs();
	checkall( 'categories' );
	top_searches();
});