jQuery( document ).ready( tInit );

function tInit()
{
	tInitSearch();
	tInitWarnings(); // All the little "don't do this" type stuff, goes in here
	tInitSignup();
}

function tInitSignup()
{
	tDebug( 'Yo!' );
	jQuery( 'input[name=checkboxUsername]' ).remove();
	jQuery( 'span:contains("Same as email")' ).remove();
	jQuery( 'p:contains("John Smith")' ).after( '<p>Please use your Telstra LAN login, i.e. your "c" or "d" number.</p>' ).remove();
}

function tInitWarnings()
{
	tDebug( "Warnings" );
	jQuery( '#viewAttachmentsDiv form' ).before( '<p class="t-warning-minor"><strong>WARNING: DO NOT</strong> upload sensitive files onto the Wiki. All pages on the Wiki are available to all staff and contractors within Telstra.</p>' );
}

function tInitSearch()
{
	// Swap the search button
	jQuery( '#search input.search-button' ).remove();
	var search_button = '<input class="search-button searchButton" src="/custom-resources/images/search-button.gif" type="image" value="Search"/>';
	jQuery( '#search .search-query' ).after( search_button );
	jQuery( '#search form.confluence-searchbox' ).submit( tSearchSubmit );
}

function tSearchSubmit()
{
	// Normalise query
	var query = jQuery( this ).find( '.search-query' ).val();
//	query = SearchQueryParser.normalise( query );
	jQuery( this ).find( '.search-query' ).val( query );
}

function tPageHasLabel( label_text )
{
	// Iterate the labels, and see if any match the required label_text
	var labels = jQuery( '#labelsList a.label' );
	for ( var i = 0; i < labels.length; i++ ) {
		var label = jQuery( '#labelsList a.label:eq('+i+')' );
		if ( jQuery.trim( label.text() ) == label_text ) return true;
	}
	tDebug( 'Label does not exist on this page.' );
	return false;
}

function tMatchesIncomingLink( href, url_part )
{
	tDebug( '------------------' );
	tDebug( url_part );
	tDebug( href );
	// Replaces spaces with "+"
	var to_match = url_part.replace( /\s/g, '+' );
	tDebug( to_match );
	tDebug( href.indexOf( to_match ) !== -1 );
	tDebug( href.indexOf( to_match ) );
	tDebug( '------------------' );
	return ( href.indexOf( to_match ) !== -1 );
}

function tDebug( msg, force )
{
	if ( typeof window.loadFirebugConsole == "undefined" ) {
		if ( force ) alert( msg );
		return;
	}
	console.debug( msg );
}


