// these two variables track which side menu options have been selected

var type = null;
var issue = null;



// on page load

$(document).ready( function()
{
	// add rollover effect to side menu items

	$(".side_menu_option").hover( 

		// on mouse over

		function()
		{
			if( false == $(this).hasClass('side_menu_on_mouse_over') )
			{
				// use gray bubble background and a gray color box mask

				$(this).addClass('side_menu_on_mouse_over')
					.find('.mask')
					.attr('src','images/mask_gray.png');
			}
		},

		// on mouse out

		function()
		{
			if( true == $(this).hasClass('side_menu_on_mouse_over') )
			{
				// use transparent background and a gray or white color box mask

				var color = $(this).hasClass('selected_side_menu_option') ? 'gray' : 'white' ;

				$(this).removeClass('side_menu_on_mouse_over')
					.find('.mask')
					.attr('src','images/mask_' + color + '.png');
			}
		}
		);
	
	// add corners to main div

	$("#main_area, .item").corner();
}
);



// vote on an item

function vote( id, direction )
{
	// remove voting links

	$('#item_' + id + ' .vote').remove();


	// send ajax request

	$.post('/index.php/item/vote/id/' + id + '/direction/' + direction, {}, vote_response);
}



// display vote response

function vote_response( response )
{
	// get target and text

	response = response.split(',');


	// append text to target

	$('#item_' + response[0] + ' .item_description').append( response[1] );
}




// sets the issue of items to be displayed in the main area and fetches new items

function set_issue( new_issue )
{
	// replace background color and color box mask

	$('#left_side_div .selected_side_menu_option')
		.removeClass('selected_side_menu_option')
		.find('.mask')
		.attr('src','images/mask_white.png');


	// set new issue

	issue = new_issue;


	// set background color and color box mask

	$('#link_' + issue)
		.addClass('selected_side_menu_option')
		.find('.mask')
		.attr('src','images/mask_gray.png');


	// load items

	load_to_main();
}



// sets the type of item to be displayed in the main area, and fetches new items

function set_type( new_type )
{
	// replace background color and color box mask

	$('#right_side_div .selected_side_menu_option')
		.removeClass('selected_side_menu_option')
		.find('.mask')
		.attr('src','images/mask_white.png');


	// set new type

	type = new_type;


	// set background color and color box mask

	$('#link-' + type)
		.addClass('selected_side_menu_option')
		.find('.mask')
		.attr('src','images/mask_gray.png');


	// load items

	load_to_main();
}



// notify user that new content is coming

function notify_loading()
{
	$('#page_content').prepend('<div style="padding: 0px 0px 10px 10px;">Loading sources...</div>');
}



// fetches new items based on current type and issue

function load_to_main()
{
	// notify user that new content is coming

	notify_loading();


	// build query url

	var page = 'index.php/item/view';


	// if a resource type is set, add the id as a parameter

	if( type )
	{
		page += '/type/' + type;
	}


	// if a resource issue is set, add the id as a parameter

	if( issue )
	{
		page += '/issue/' + issue;
	}


	// pass a limit on the number of items to return

	if( null != type || null != issue )
	{
		var count = (null == type ? 0 : 1) + (null == issue ? 0 : 1);
		page += '/limit/' + (2 == count ? 50 : 25);
	}


	// send request

	$.post(page, {}, update_main);
}



// fetches items that have relationships with the given item and loads them to the main area

function load_related( item_id )
{
	clear_side_menu('both');


	// notify that sources are coming

	notify_loading();


	// send request

	var page = 'index.php/item/view/id/' + item_id;

	$.post(page, {}, update_main);
}



// clears the selected issue and type variables

function clear_side_menu( menu )
{
	// replace background color and mask on left side

	if( 'issue' == menu || 'both' == menu )
	{
		$('#left_side_div .side_menu_option')
			.removeClass('selected_side_menu_option')
			.find('.mask')
			.attr('src','images/mask_white.png');

		issue = '';
	}


	// replace background color and mask on right side

	if( 'type' == menu || 'both' == menu )
	{
		$('#right_side_div .side_menu_option')
			.removeClass('selected_side_menu_option')
			.find('.mask')
			.attr('src', 'images/mask_white.png');

		type = '';
	}
}



// load the welcome screen to the main div

function load_welcome()
{
	$.post('index.php/index/welcome/',{}, update_main);

	clear_side_menu('both');
}



// load the issues view

function load_issues()
{
	$.post('index.php/index/issues/',{}, update_main);

	clear_side_menu('both');
}



// load the types view

function load_types()
{
	$.post('index.php/index/types/',{}, update_main);

	clear_side_menu('both');
}



// call back function to insert ajax responses into the main area

function update_main( response )
{
	// for an empty response, create a message

	if( '' == response )
	{
		var issue_name =  ( issue ) ? $('#link_' + issue + ' > div:eq(1)').html().toLowerCase() : '';
		var type_name = ( type ) ? $('#link-' + type + ' div:eq(2)').html().toLowerCase() : 'resource';
		
		response = '<div style="padding:10px;margin:auto;">Sorry, no ' + issue_name + ' ' + type_name + ' was found, but you can add one <a href="/index.php/item/add/?height=200&amp;width=500&amp;issue=' + issue + '&amp;type=' + type + '" class="thickbox" title="Submit an Item" style="text-decoration:underline">here</a>.</div>';
	}


	// update page

	$('#page_content').html(response);

	$('.item').corner();


	//thickbox functionality

	tb_init('#main_area .thickbox');
}



// wrapper function for escaping URLs

function uri_encode( string )
{
	return escape( string );
}



// approve an item in the moderation queue 

function approve_item( id )
{
	// encode the name of the queued item

	name = $('[name="name' + id + '"]').serialize();
	name = name.split('=')[1];
	name = uri_encode(name);


	// encode the description of the queued item

	description = $('[name="description' + id + '"]').serialize();
	description = description.split('=')[1];
	description = uri_encode(description);


	// build a url for the approval request

	var url = '/index.php/item/approve/id/' + id + '/name/' + name + '/description/' + description;


	// send request

	$.get(url, {}, update_form);
}



// approve a revision in the moderation queue 

function approve_revision( id )
{
	// get name

	name = $('[name="name' + id + '"]').serialize();
	name = name.split('=')[1];
	name = uri_encode(name);


	// get description

	description = $('[name="description' + id + '"]').serialize();
	description = description.split('=')[1];
	description = uri_encode(description);


	// build url

	var url = '/index.php/revision/approve/id/' + id + '/name/' + name + '/description/' + description;



	// send request

	$.get(url, {}, update_form);
}



// reject an item that is currently in the moderation queue

function reject_item( id )
{
	var url = '/index.php/item/reject/id/' + id;

	$.post(url, {}, update_form);
}



// reject an revision that is currently in the moderation queue

function reject_revision( id )
{
	var url = '/index.php/revision/reject/id/' + id;

	$.post(url, {}, update_form);
}



// update moderation form after approving or rejecting an item

function update_form( response )
{
	response = response.split(',');

	$('#mod_form_' + response[0]).html(response[1]);
}



// approve a relationship between two items

function approve_relationship( id )
{
	var url = '/index.php/relationship/approve/id/' + id;

	$.get(url, {}, update_form);
}



// reject a relationship between two items

function reject_relationship( id )
{
	var url = '/index.php/relationship/reject/id/' + id;

	$.post(url, {}, update_form);
}



// load the newest view

function load_newest()
{
	// notify user that new content is coming

	notify_loading();

	$.post('index.php/item/newest/',{}, update_main);

	clear_side_menu('both');
}


// load the newest view

function load_tool( type )
{
	// notify user that new content is coming

	notify_loading();

	$.post('index.php/item/' + type + '/',{}, update_main);

	clear_side_menu('both');
}



// delete an item

function delete_item( id )
{
	if( ! confirm( 'Really delete?' ) )
	{
		return;
	}


	// remove delete link

	$('#item_' + id + ' .delete').remove();


	// send ajax request

	$.post('/index.php/item/delete/id/' + id, {}, vote_response);
}


