// -------------------------------------------------------------------------------------------------------------------------------------------------------------------
//	(C) 2008 Andrew Murphy All Rights Reserved
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------

var cablechip = {} ;

//
cablechip.VERSION		= "cablechip.js : 1.0 (2009-06-30)";

// -------------------------------------------------------------------------------------------------------------------------------------------------------------------
// toggle
// --------------------------------------------------------------------------------------------------------------------------------------------------------------------

//
cablechip.toggle	= function ( element_name )
	{
	// -----------------------------------------------------------------------------------------------------
	// toggle element name's class between class=show and class=hide
	// e.g. used to show/hide comments on this weeks walks
	// -----------------------------------------------------------------------------------------------------
	
	var element_id = document.getElementById( element_name );
	
	if ( element_id.className == "show")
		{
		element_id.className = "hide";
		} 
	else
		{
		element_id.className = "show";
		} 
	} ;
	
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------
// tabs
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------

//
cablechip.tabs	= function ( tab_name, active_tab ) 
	{
	// --------------------------------------------------------------------------------------------------
	// e.g. have 5 tabs across the page, switch content of 1 one, and the others off
	// ids are: menu_1, menu_content_1, menu_2, menu_content_2, ...
	// title class : 'tabs_active' or 'tabs_inactive' and body class: 'show' or 'hide'
	// used: this_weeks_walks include, recent comments page
	//
	// note 
	// - [element].childNodes is NOT the same in IE and firefox, - IE works as you expect, firefox counts space as an empty text node, so has far more of them
	// - cant use getElementsByTagName as works recursively, gets all <div>s not just the top level one, so need to use names!
	// ---------------------------------------------------------------------------------------------------

	// how many tabs are there?
	if ( ! document.getElementById( tab_name + '_headers' ) )
		{
		document.write ("ERR: no such element : " + tab_name + '_headers');
		return ;
		}
		
	//
	var headers = document.getElementById( tab_name + '_headers' ).getElementsByTagName( 'div' )  ;
	
	//
	cablechip.debug( "EVENT: cablechip.tabs - id: " + tab_name + ", active: " + active_tab + ", headers: " + headers.length  );
	
	//
	for ( var loop in headers )
		{
		//
		var content =  document.getElementById( tab_name + '_' + loop ) ;
		
		//
		if ( ! headers[ loop ]  || ! content )
			{
			continue;
			}
		
		//
		if ( loop == active_tab )
			{
			headers[ loop ].className = "tabs_active"; 
			content.className = "show";
			} 
		else 
			{
			headers[ loop ].className = "tabs_inactive"; 
			content.className = "hide";
			}
		} 

	} ;

// -------------------------------------------------------------------------------------------------------------------------------------------------------------------
// CGI params
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------

//
cablechip.get_cgi_params = function () 
	{
	// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
	// read CGI params into cablechip.cgi_params
	//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
	
	if ( cablechip.cgi_params )
		{
		// already got them
		return;
		}
	
	var query_string	= window.location.search.substring(1); // ?photo=67 (skip the question mark)
	var pairs			= query_string.split(/&/) ;
	
	cablechip.cgi_params		= {} ;
	
	for ( var pair_no in  pairs )
		{
		var key_value = pairs[ pair_no ].split( "=", 2 );
		cablechip.cgi_params[ key_value[0] ] = decodeURI( key_value[1] );
		cablechip.debug( '.. cgi param : ' + key_value[0] + ' = ' + decodeURI( key_value[1] )  );
		}
	} ;

// ----------------------------------------------------------------------------------------------------
//HTMLdog drop down tables
// ----------------------------------------------------------------------------------------------------

cablechip.drop_down_windows_init = function ()
	{
	//
	if ( window.attachEvent ) 	window.attachEvent( "onload", cablechip.sfHover );
	} ;
	
//
cablechip.sfHover = function () 
	{
	// --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
	// Drop-Down Menu Code for IE - (C) www.HTMLDog.com
	// used in top_menu.html for the drop-downs
	//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

	var sfEls = document.getElementById("header").getElementsByTagName("li");
	
	for ( var i in sfEls ) 
		{
		sfEls[i].onmouseover	= function() { this.className += " sfhover"; } ;
		
		sfEls[i].onmouseout		= function() { this.className = this.className.replace(new RegExp(" sfhover\\b"), ""); } ;
		}
	};
	
// ----------------------------------------------------------------------------------------------------
// unobtrusive sortable tables
// ----------------------------------------------------------------------------------------------------

cablechip.sortable_table_initiated = function ()
	{
	cablechip.debug( "table: sort initiated callback");
	
	cablechip.status( "Sorting ...", 'status_working');
	};
	
//
cablechip.sortable_table_complete = function ( )
	{
	// -------------------------------------------------------------------------------------------------
	// loop thru the table, 
	// - if the region name is the same as the line above, change the cells class name to blank it our 
	//- table id must be sortable_table
	// ------------------------------------------------------------------------------------------------
	
	cablechip.debug('table: sort complete callback');
	
	var this_table = document.getElementById( 'sortable_table' ) ; 
	
	var rows = this_table.tBodies[0].rows ;

	var last_row = [ 'xxx', 'xxx', 'xxx', 'xxx' ] ;
	
	cablechip.debug('.. rows: ' + rows.length );
	
	//
	for (var i = 0; i < rows.length; i++)
		{
		var always_show_row = 0 ;
		
		for (var j = 0 ; j < 3; j++ )
			{
			var cell_data 	= rows[ i ].cells[ j ].innerHTML ;
		
			if ( ! cell_data.match( /<span/i )  )
				{
				continue;
				}
			
			//cablechip.debug('.. row: ' + i + ' = ' + region + ' from ' + rows[ i ].cells[ 0 ].className );
			
			//
			var cell_class	= rows[ i ].cells[ j ].className ;
			
			if ( ! always_show_row && cell_data == last_row[j] )
				{
				//hide
				if ( ! cell_class.match( / sp_col_hide/ ) )
					{
					rows[ i ].cells[ j ].className 	+= ' sp_col_hide';
					}
				}
			else
				{
				// show
				always_show_row = 1 ;
				
				if ( cell_class.match( / sp_col_hide/ ) )
					{
					rows[ i ].cells[ j ].className	= cell_class.replace( / sp_col_hide/g , '' );
					}
				}
			
			//cablechip.debug('..     = ' + rows[ i ].cells[ 0 ].className );
		
			last_row[j] = cell_data ;
			}
		}
		
	//
	cablechip.status( "<i>To sort by more than 1 column, press shift, then click the column titles in order</i>", 'status_ok' );
	}
	
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------
// reverse a table
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------

//
cablechip.reverse_table = function ( params )
	{
	// -------------------------------------------------------------------------------------------------
	// reverse a table (on the 'this weeks walks' page - as blogs are in reverse chronological order)
	// ------------------------------------------------------------------------------------------------
	
	var this_table = document.getElementById( params.id ) ; 
	
	var rows = this_table.tBodies[0].rows ;
	
	for ( var i = 1; i < this_table.rows.length; i++ )
		{
		this_table.tBodies[0].insertBefore( rows[i], rows[0] );
		}
	};

// -------------------------------------------------------------------------------------------------------------------------------------------------------------------
// train times
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------

//
cablechip.train_times_url = function ( from_id, to_id )
	{
	// -------------------------------------------------------------------------------------------------
	// link to national rail
	// originally used on the "walk index.shtml" pages, but replaced by sean's stuff
	// ------------------------------------------------------------------------------------------------

	var url = 'http://ojp.nationalrail.co.uk/en/pj/jp?from.searchTerm=' + from_id + '&to.searchTerm=' + to_id + '&planjourney=SEARCH' ;
		
	document.write( '<a href="' + url + '">');
	};
	
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------
// new script node
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------

//
cablechip.new_script_node = function ( args )
	{
	// Synchronous !
	// Get JSON or a js script file by creating a <script source="file.js"> node 
	
	cablechip.debug ( " .. making script node : " + args.url) ;
	
	var script = document.createElement('script');
	
	script.setAttribute('src', args.url );
	script.setAttribute('id', args.id );
	script.setAttribute('type', 'text/javascript');
		
	document.documentElement.firstChild.appendChild( script );
	};

// ----------------------------------------------------------------------------------------------------
// status, debug, and message
// ----------------------------------------------------------------------------------------------------
// the set/append inner_html functions
//	cablechip.status ( text )				set inner html of id=status with the test, if text=blank, remove the background image
//	cablechip.debug ( text )				append text to inner html of id=debug, limit to 200 debug messages
// 	cablechip.message ( params )			used by above 2, append/replace inner_html of a id
// -----------------------------------------------------------------------------------------------------

// global data
cablechip.message_ids = {
	'debug':  { 'content':'', 'line_count': 0, 'line_max': 400  },
	'status': { 'content': '' }
	};

// -----------------------------------------------------------------------------------------------------
cablechip.version = function ( text )
	{
	cablechip.debug( 'Loaded OK: ' + text );
	}
	
// -----------------------------------------------------------------------------------------------------
cablechip.status 		= function ( text, class_name )
	{
	// class_name's
	//	status_ok		(default if text is blank
	//	status_error
	//	status_working	(default if some text) 

	//
	var dom_id			= document.getElementById( 'status' ) ;
	
	var debug_text		= text ;
	
	//
	if ( text )
		{
		if ( ! class_name )
			{
			class_name = 'status_working';
			}
		}
	else
		{
		debug_text = "ok" ;
		
		if ( ! class_name )
			{
			class_name = 'status_ok';
			}
		}
		
	//
	cablechip.debug( ".. Status: " + debug_text + ' ( class name: ' + class_name + ' )' ) ;
	
	//
	if ( ! dom_id )
		{
		cablechip.debug ( ".. INFO: cablechip.status() id 'status' does not exist." );
		return;
		}
		
	//
	dom_id.className	= class_name; 
	
	//
	var status_text		= text.replace( /^[\.\s]+/g , '') ; // remove any .. .. at the start
	
	//
	cablechip.message( 
		{
		id:"status",
		text: status_text,
		type:"replace"
		} );
	};
	
// -----------------------------------------------------------------------------------------------------
cablechip.debug 		= function ( text )
	{	
	//
	if ( cablechip.message_ids.debug.stopped )
		{
		return;
		}
		
	//
	cablechip.message_ids.debug.line_count++ ;
		
	//
	if (		cablechip.message_ids.debug.line_max 
			&&	cablechip.message_ids.debug.line_count > cablechip.message_ids.debug.line_max  )
		{
		cablechip.message_ids.debug.stopped = 1 ;
	
		text	+= '<p>max no of messages exceeded</p>\n';
		}
	
	//
	text = text.replace( /[\n\r\t]+/g, ' ');
	text = text.replace( /\s+/g, ' ');
	
	//
	if ( window.console && window.console.log )
		{
		console.log( text );
		}
		
	//
	text = text.replace( /</ig, '&lt;'); 
	text = text.replace( />/ig, '&gt;'); 
	
	//	
	cablechip.message( 
		{
		id:"debug",
		text:text,
		type:"append"
		} );
	};

// -----------------------------------------------------------------------------------------------------
// params: id, text, type, status
// config: line_count, line_max, content
cablechip.message		= function ( params )
	{
	// params
	if ( ! params || ! params.id)
		{
		alert('cablechip.messsage not passed an id');
		return;
		}
			
	//
	var dom_id			= document.getElementById( params.id ) ;
	
	var dom_id_exists	= dom_id ? 1 : 0 ;
	
	//
	if ( ! cablechip.message_ids[ params.id ] )
		{
		cablechip.message_ids[ params.id ] = { line_count: 0, content:'' } ;
		}
				
	//
	var type = 'append';
	
	if ( params.type && params.type == 'replace')
		{
		type = 'replace';
		}
		
	//
	var text	= params.text;
	
	//
	if ( text === undefined )
		{
		text = '[undefined]';
		}
		
	//
	if ( type == 'append')
		{
		cablechip.message_ids[ params.id ].content 		+= text + '<br>' ;
		}
	else
		{
		/* type is replace */
		cablechip.message_ids[ params.id ].content 		= text ;
		}
		
	//
	if ( dom_id_exists )
		{
		dom_id.innerHTML	= cablechip.message_ids[ params.id ].content ;
		}
		
	//
	return ;
	};

// ---------------------------------------------------------------------------------------------------------------------------------------------------------------

//
cablechip.version ( cablechip.VERSION ) ;

