// Various Javscript stuff needed for the site
// (C) Steph Reed 2009

function getHTTPObject() { 
	if (typeof XMLHttpRequest != 'undefined') { 
		return new XMLHttpRequest(); 
		} 
	try { 
		return new ActiveXObject("Msxml2.XMLHTTP"); 
		} catch (e) { 
			try { 
				return new ActiveXObject("Microsoft.XMLHTTP"); 
			} catch (e) {} 
		} 
		return false;
	}

function expand( name, other ) {
	// Expand or shrink a section
	var d = document.getElementById(name);
	if( !d )
		return;

	// Show / hide the requested DIV
	if( $("#" + name).is(':visible') )
		$("#" + name).hide();
	else
		$("#" + name).show();

	// Hide all the others
	var selector = "ul ul:not(#" + name + ")";
	$(selector).hide();	

   }

function changePage( page ) {
	// Change the main section to the specified page with a fade
	var url = "index_ajax.php?page=" + page;

	// Show the loading animation
	$("#loader").show();

	// Reset the video player status
	video_ready = false;

	if( httpD )
		delete httpD;

	var httpD = getHTTPObject();
 	httpD.onreadystatechange = function () {
		if (httpD.readyState == 4) {
			contentReady( httpD.responseText );
			}
		}
 	httpD.open("GET", url, true);
 	httpD.send(null);
 
}

$(document).ready(function() {
	// $("#mainNew").fadeOut(0);

	// Show a message to IE6 users
	if($.browser.msie && $.browser.version=="6.0") 
		$("#browser").show();

	// Load the facebook button (if earlier it prevents the JS from working until loaded)
	var facebook = "<script type=\"text/javascript\">FB.init(\"13db7cccc89e0feed6e340b5d2af5880\");</script>";
	facebook += "<fb:fan profile_id=\"89975229415\" stream=\"\" connections=\"\" width=\"190\"></fb:fan>";

	$("#facebook").html( facebook );

	} );

function contentReady( result ) {
 // Called when AJAX content is ready to be shown

	// Extract the new page title
	var pageTitle = "";
	var start = result.indexOf("<!-- Title:");
	var end = result.indexOf( "-->", start );

	if( start != -1 && end != -1 )
		pageTitle = result.substring( start + 12, end );

	document.title = pageTitle;

	// HIde the new DIV (Needed for first time it's used)
	$("#mainNew").hide();

	// Set the content of the new DIV
	$("#mainNew").html("<div class=\"box\" id=\"boxNew\">" + result + "</div>\n");

	// Hide the loading animation
	$("#loader").hide();
	
	// Fade out the old div
	$("#main").fadeOut(2000);

	// Fade in the new DIV
	$("#mainNew").fadeIn(2000, function () {  
		// Called when fade is finished

		// Set the old div's content to the same as the new one
		$("#main").html( $("#mainNew").html() );

		// Show the old one, hide the new one
		$("#main").fadeIn(0);
		$("#mainNew").fadeOut( 0 );
		} );

}

function debug( message, indent ) {
 // Adds a debug message to the debug div if it exists
 var tabs = ""; 

 if( indent == 0 )
	tabs = "<br />";

 for( var i=0 ; i<indent ; i++ )
	tabs += "&nbsp;&nbsp;&nbsp;&nbsp;"

 if ( $("#debug").length > 0 ) {
 	$("#debug").html( $("#debug").html() + "<br \>" +tabs + message );
	$("#debug").attr({ scrollTop: $("#debug").attr("scrollHeight") });
	}

 }

