/******************************
* CUSTOM JAVASCRIPT FUNCTIONS *
******************************/

function ltrim(str)
{ 
	for(var k = 0; k < str.length && whiteSpace(str.charAt(k)); k++);
	
	return str.substring(k, str.length);
}
function rtrim(str)
{
	for(var j = str.length - 1; j >= 0 && whiteSpace(str.charAt(j)); j--);
	
	return str.substring(0, j + 1);
}
function trim(str)
{
	return ltrim(rtrim(str));
}
function whiteSpace(chr)
{
	var whiteChars = ' \t\n\r\f';
	
	return (whiteChars.indexOf(chr) >= 0);
}


/*****************************/


// Checks all the checkboxes
// @params	mixed		The element that if checked, checks all the checkboxes in the field
// @params	string
function checkall(element, field)
{
	if (typeof element == 'string')
	{
		element = document.getElementById(element);
	}
	
	if (element.checked)
	{
		for (i = 0; i < field.length; i++)
		{
			field[i].checked = true;
		}
	}
	else
	{
		for (i = 0; i < field.length; i++)
		{
			field[i].checked = false;
		}
	}
}

// Unchecks the 'checked all' checkbox if any other checkbox is unchecked
// @params	mixed		The element, if un-checked, will un-check the next object
// @params	mixed		The element to un-check
function uncheck(element, elementb)
{
	if (typeof element == 'string')
	{
		element = document.getElementById(element);
	}
	if (typeof elementb == 'string')
	{
		elementb = document.getElementById(elementb);
	}
	
	if (element.checked == false)
	{
		elementb.checked = false;
	}
}

// Posts a comment
function post_comment()
{
	var query_str =
		   'comment[comment]=' + document.getElementById('comment-comment').value
		+ '&comment[wxohash]=' + document.getElementById('comment-wxohash').value
		+ '&comment[javascript]=true'
	;
	
	$.post(BASE_URL + 'media/comment' + EXTENSION, query_str, post_comment_done);
}
function post_comment_done(data)
{
	document.getElementById('comment-comment').value = '';
	document.getElementById('comment-message').innerHTML = data;
}

// Creates a bookmark for the desided URL
// @params	string		The title of the bookmark
// @params	string		The URL in which to bookmark
// @returns	bool		Should the bookmark be initialized
function media_bookmark(title, url)
{
	if (window.sidebar) // Mozilla Firefox Bookmark
	{
		window.sidebar.addPanel(title, url, '');
		return true;
	}
	else if (window.external) // IE Favorite
	{
		window.external.AddFavorite(url, title);
		return true;
	}
	
	return false;
}

// AJAX - Reports a media for checking (media probably broken)
// @params	int			The ID of the media to report
// @return	bool		Has this media been reported
function media_report(id)
{
	if (confirm('Are you sure you want to report this media?'))
	{
		jQuery.get(SITE_URL + 'media/report/' + id + EXTENSION, media_report_done);
		
		return true;
	}
	
	return false;
}
function media_report_done(data) { $('#action-report').hide(); }

// AJAX - Add a rating vote to a media
// @param	string		Encoded hash containing the necessities to rate the media
function media_rate(hash)
{
	$.get(SITE_URL + 'media/rate/' + hash + '--js' + EXTENSION, media_rate_done);
}
function media_rate_done(data) { if (data == 'true') { $('#media-ratings').hide(); } }


function deleteCheck(item, name)
{
	return confirm('Are you sure you want to delete the ' + item + ':\n' + name + '?');
}

function divSwitch(id)
{
	$('#' + id).slideToggle(500);
}

/*********************
* CALENDAR FUNCTIONS *
*********************/

// Calendar 'Jump To' Selector
function calendar_jump()
{
	var elm1 = document.getElementById('calendar-jump');
	
	var vtmp = elm1.options[elm1.selectedIndex].value.split(',');
	
	location.href = SITE_URL + 'calendar/' + vtmp[0] + '/' + vtmp[1] + EXTENSION;
}

// Calendar selected dates unselector
// @params	int			The number of total release dates to unselect (one of them potentially is selected)
function calendar_unselect(total)
{
	for (i = 1; i <= total; i++)
	{
		$('#cal' + i).removeClass('selected');
	}
}

// Calendar episode listing
// @params	int			The year of the listing date
// @params	int			The month of the listing date
// @params	int			The day of the listing date
// @params	int			The desired page for the listings
function calendar_list(year, month, day, page)
{
	page = (page > 0) ? '?page=' + page : '';
	
	document.getElementById('calendar-release-list-loading').style.display = 'block';
	
	$.get(SITE_URL + 'calendar/list/' + year + '/' + month + '/' + day + EXTENSION + page, calendar_list_done);
}
function calendar_list_done(data)
{
	document.getElementById('calendar-release-list-loading').style.display = 'none';
	document.getElementById('calendar-release-list-content').innerHTML = data;
}

/********************/