// -------------------------------------------------------------------------------------------------------------------------------------------------------------------
//	(C) 2008 Andrew Murphy All Rights Reserved
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------
// - called by a <script> tag in top_menu.html, so all these can be used from any page
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Where is javascript used?
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------
// [all pages] 					drop-down menu at top of page
// [all pages]					amazon ads at bottom of page
// [all pages]					toggle debug div
//
// [swc - this weeks walks]		reverse "this weeks walks" table, 3 tabs of walk numbers
// [swc - recent comments]		get RSS feeds, and display
// [swc - book 3 admin]			get JSON, book 3 walk 27 => walk title
// [swc - complete list walks]	sortable table
//
// [1/2/3 - walks - map]		display streetmap/multimap and OS Explorer links, display GPX files (routes and/or markers)
// [1/2/3 - walks - photos]			photos and thumbnails
// [1/2/3 - walks - index]			sean's lib
//
// [hills - map]					get json, display OS map of hill, set titles, set map bounds
// [maps - OS explorer map]			get json, display OS map, set titles, set map bounds
// [maps - rail]					get XML, display google map
// [maps - attractions				get XML, display attractions
//
// sortable tables					book index, hill tables, complete list of walks, list of os explorer maps
//
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------
// How are the .js scripts organised
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------
//
// The plan ..
//
// swc.js							- specific to SWC pages 
// swc/recent_comments.js			- the recent comments page
// swc/hills.js						- hills
// swc/maps.js						- maps
//
// cablechip.js						- generic stuff, eg drop down menus
// cablechip/maps.js				- map api
// cablechip/maps/google.js	
// cablechip/maps/multimap.js
// cablechip/maps/OS.js
//
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------
// TO DO
// -------------------------------------------------------------------------------------------------------------------------------------------------------------------
//
// ---------------------------------------------------------------------------------------------------------------------
// SWC specific methods
// ---------------------------------------------------------------------------------------------------------------------
//	walk photos			- for the /book_nn/walk_nn thumbnail gallery and photo pages
//	amazon ads 			- on the bottom of every page
// ---------------------------------------------------------------------------------------------------------------------

var swc 		= {} ;

swc.VERSION		= 'swc.js : 3.0 (2009-06-30)';

// ---------------------------------------------------------------------------------------------------------------------
// WALK PHOTOS
// ----------------------------------------------------------------------------------------------------------------------

//
swc.photo		= function( photo ) 
	{
	// --------------------------------------------------------------------------------------------------
	// used: /book_xx/walk_xx/walk_photos.shtml
	// get photo numbers from 1) cgi params at start of page, or 2) clicking on a ph0to calling this function 
	// sets page title, photo heading, <image src=, etc
	// called with an object WRONG - should have used a photo number
	// ---------------------------------------------------------------------------------------------------
	
	document.getElementById( "big_title" ).innerHTML	= photo.title ;
	document.getElementById( "big_author" ).innerHTML	= photo.by + '<br>' + photo.date ;
	document.getElementById( "big_photo" ).src			= photo.src_big ;
	document.getElementById( "big_url" ).href			= photo.url ;
	document.getElementById( "big_url" ).alt			= photo.title  ;
	};

//
swc.photo_init	= function() 
	{
	// --------------------------------------------------------------------------------------------------
	// used: /book_xx/walk_xx/walk_photos.shtml
	// get the "photo number" cgi param, and call swc.photo
	// photos[ number] refers to a list of photo objects inline html'ed into the photo page
	// ---------------------------------------------------------------------------------------------------
	
	cablechip.get_cgi_params();
	
	var photo_no = 0 ;
	
	//
	if ( cablechip.cgi_params.photo && cablechip.cgi_params.photo > 0 )
		{
		photo_no		= cablechip.cgi_params.photo  ;
		}
	
	//
	swc.photo ( photos[ photo_no ] ) ;
	};
	

// ----------------------------------------------------------------------------------------------------
// swc.data functions
// these use the data in swc.data (if its there)
// ----------------------------------------------------------------------------------------------------

//
swc.walk_name = function ( params )
	{
	//
	var book_no		= params.book_no ;
	var walk_no		= params.walk_no ;
	var walk_nn		= walk_no;
	
	//
	var book_name	= "TOCW " + book_no + ",";
	
	//
	if ( book_no == 3 )
		{
		book_name	= "SWC (Free)";
		}
	
	//
	if ( walk_no.length == 1 )
		{
		// javascript has no sprintf
		walk_nn = "0" + walk_no ;
		}
		
	var title = "<a href='http://www.walkingclub.org.uk/book_" + book_no + "/walk_" + walk_nn + "/updates.shtml'>" + book_name  + " Walk " + walk_no + "</a>" ;

	if ( swc.data.books && swc.data.books[ book_no ] && swc.data.books[ book_no][ walk_no - 1 ] && swc.data.books[ book_no ][ walk_no - 1 ].walk_name )
		{
		title = title + ' : ' + swc.data.books[ book_no ][ walk_no - 1 ].walk_name;
		}
	else
		{ 
		cablechip.debug ("ERROR swc.walk_name() : error getting walk title for book no: " + book_no + ", walk_nn: " + walk_nn ); 
		}
	
	return title ;
	}

	
// ----------------------------------------------------------------------------------------------------
// Add Hyperlinks
// ----------------------------------------------------------------------------------------------------

swc.add_hyper_links = function ( text )
	{
	//
	var html = text + '' ;
	
	//
	try {
		if ( ! text.match( /href/i ) )
			{
			if ( text.match( /http:/i ) )
				{
				cablechip.debug("match http");
				html = text.replace( /(http:\/\/[a-z\-\.\d]*(\/[\.\w\?\/\-\_\=\%]*){0,1})/i , "<a href='$1'>$1</a>" ) ;
				}
			else
				{
				html = text.replace( /(www\.[a-z\-\.]*[\w\?\/\-\_\=\%]*)/i , "<a href='http://$1'>$1</a>" ) ;
				}
			}
		}
	catch (err)
		{
		cablechip.debug("regex failed : " + text ) ;
		}
	
	return html
	}
	
// ----------------------------------------------------------------------------------------------------
// AMAZON ADS
// ----------------------------------------------------------------------------------------------------

swc.amazon_publishers = function ( params )
	{
	//
	var publishers = params.publishers ;
	
	//
	var html = "<ul>\n";
	
	//
	for ( var i in publishers )
		{
		//
		html 	+= '<li> ' +
					swc.amazon_search_url( { 
						keywords:	publishers[i].keywords,
						link:		publishers[i].link ,
						travel: 	publishers[i].travel || 0 
						} ); 
		
		//
		if ( publishers[i].text )
			{
			html += ' (' + publishers[i].text + ")\n" ;
			}
		}
	
	//
	html += "</ul>\n";
	
	//
	document.write ( html );
	}

//
swc.amazon_searches = function ( params )
	{
	//
	var searches = params.searches ;
	
	//
	var html	= "";
	
	//
	for (var s in searches)
		{
		//
		if ( searches[s].title )
			{
			html	+= "<h3>" + searches[s].title + "</h3>\n" ;
			}
		
		//
		html	+= "<ul><li>\n";
		
		//
		var travel	= searches[s].travel || 0 ;
			
		//
		for ( var k in searches[s].keywords )
			{
			html 	+= swc.amazon_search_url( { 
						keywords:	searches[s].keywords[k],
						link:		searches[s].keywords[k],
						travel: 	travel
						} );
			
			html	+= ", \n" ;
			}
		
		//
		html	+= "</ul>\n";
		}
		
	//
	document.write ( html );
	}

//
swc.amazon_search_url = function ( params )
	{
	var keywords	= params.keywords ;
	
	var link		= params.link || keywords;
	
	var search_uri	= encodeURIComponent ( keywords );
	
	var travel		= params.travel || 0 ;
			
	var html		= '' ;
	
	//
	if ( travel )
		{
		html =  '<a href="http://www.amazon.co.uk/gp/redirect.html?ie=UTF8&' +
			'location=http%3A%2F%2Fwww.amazon.co.uk%2Fs%3Fie%3DUTF8%26rs%3D83%26sort%3Dsalesrank%26ref%255F%3Dsr%255Fst%26keywords%3D' +
			search_uri +
			'%26bbn%3D83%26qid%3D1240849468%26rh%3Dn%253A266239%252Cn%253A%25211025612%252Ck%253A' +
			search_uri + 
			'%252Cn%253A83%26page%3D1&tag=satuwalkclub-21&linkCode=ur2&camp=1634&creative=19450">' +
			link + "</a>" ;
		}
	else
		{
		html =  '<a href="http://www.amazon.co.uk/gp/redirect.html?' +
			'ie=UTF8&location=http%3A%2F%2Fwww.amazon.co.uk%2Fs%3Fie%3DUTF8%26rs%3D266239%26sort%3Dsalesrank%26ref%255F%3Dsr%255Fst%26keywords%3D' +
			search_uri +
			'%26bbn%3D266239%26qid%3D1238854347%26rh%3Dn%253A266239%252Cn%253A%25211025612%252Ck%253A' +
			search_uri +
			'%26page%3D1&tag=satuwalkclub-21&linkCode=ur2&camp=1634&creative=19450">' +
			link + "</a>" ;
		}
		
	return html ;
	}
	
// ----------------------------------------------------------------------------------------------------
// THE END
// ----------------------------------------------------------------------------------------------------

cablechip.version ( swc.VERSION );

		

