/*
 * Author: Jae'da Berry
 *
 * Date: 12.1.2010
 *
 * main-site-lib.js
 *    Contains majority of global JavaScript functions all pages have access to.
 *
 * Requires MooTools
 */
 
var TanimeGlobal = new Object({
	BASE_URL: '',
	BASE_END_INDEX: 2, // 
	BASE_URL_APPEND: '/' // 
 });
 
// Sets the page title text
TanimeGlobal.SetTitle = function( title ) {
	if ( !$('title-text') )
		return;
	$('title-text').innerHTML = title;
	document.title            = title + ' | Tenshi Animetion';
 };
  
TanimeGlobal.UpdatePermanentURL = function( link ) {
	if ( !$('permanent-url') )
		return;
	
	var baseURL = top.window.location.protocol + '//' + top.window.location.host;
	$('permanent-url').innerHTML = 'Permanent URL to this page: <a class="href-page-url" href="' + 
									baseURL + '/' + link + '" title="Permanent URL">' + 
									baseURL + '/' + link + '</a>';
 };

TanimeGlobal.UpdateCommentSection = function( link ) {
	if ( !$('page-comments') )
		return;
		
	//var req = new Request.HTML( { url: '/forums/page-comment.php?url=' + link, update: $('page-comments') } ).send();

	$('page-comments').innerHTML   = 'Comment Section coming soon';
	//$('page-comments').innerHTML = '/forums/page-comment.php?url=' + link;
 };
// Dynamically updates the Navigation bar
TanimeGlobal.UpdateNavigation = function( link, fullURL ) {
	if ( ( !$defined( ChapterLinks ) ) && ( !$('navigation-section') ) )
		return;
		
	var url        = ( $defined( link ) ) ? link.split( '/' ) : new URI(window.location.href ).get('directory').split( '/' );
	var chapNum    = 0;
	var sectionNum = 0;
	var htmlStr    = '';

	TanimeGlobal.UpdatePermanentURL( fullURL );
	TanimeGlobal.UpdateCommentSection( fullURL );
	
	// Get base...
	ChapterLinks.each( function( item, index ) {
			if ( item.id == url[0] )
			{
				htmlStr += '<a href="' + TanimeGlobal.BASE_URL_APPEND + 
							item.id + '" title="' + item.description + '">' + 
								item.title + '</a>';
				chapNum  = index;
			}
		});
	url.shift(); // Remove first element
	if ( url.length > 0 )
	{
		SiteNavLinks[chapNum].each( function( item, index ) {
				if ( item.id == url[0] )
				{
					htmlStr += ' || <a href="' + TanimeGlobal.BASE_URL_APPEND +
							ChapterLinks[chapNum].id + '/' + item.id + '" ' +
							'title="' + item.description + '">' + 
							item.title + '</a>';
					sectionNum = index;
				}
			});
	}
	url.shift(); // Remove first element
	if ( url.length > 0 )
	{
		SiteNavLinks[chapNum][sectionNum].subsections.each( function( item ) {
				if ( item.id == url[0] )
					htmlStr += ' || <a href="' + TanimeGlobal.BASE_URL_APPEND +
							ChapterLinks[chapNum].id + '/' + SiteNavLinks[chapNum][sectionNum].id +
								'/' + item.id + '" ' +
							'title="' + item.description + '">' + 
							item.title + '</a>';
			});
	}
	url.shift(); 
	$('navigation-section').innerHTML = htmlStr;
 }
