// Manages and displays the last X pages visited.
// Pages you've already visited won't be added again until they fall of the list
// Requires Mootools Element, Hash, Hash-Cookie

var SKIP_LAST_PAGE_FEATURE = false;

window.addEvent( 'domready', function( e ) {
	if ( $$('ul.pages_last_visited').length > 0 ) {

		if ( $('LastPagesVisited') ) {
			// JS is running so make this feature visible!
			$('LastPagesVisited').setStyle( "display", "block" );
		}

		LastPagesVisited.Init();
	}
});

var LastPagesVisited = {
	// How many pages should we remember (don't go silly eh?)
	MAX_LAST_PAGES_VISITED : 8,

	GetShortenedCookieUrl : function() {
		// Try to save a bit of space in the cookie by trimming hrefs where we can (ie dynamic ones)
		var theurl = location.pathname.indexOf( "/q." ) > -1 ? location.pathname.substring( location.pathname.indexOf( "/q." ) ).replace( /\.?\.?q.html/, "" ) : location.pathname;

		// Bin the in page anchors
		if ( theurl.indexOf( "#" ) > -1 ) theurl = theurl.substring( 0, theurl.indexOf( "#" ) );
		return theurl;
	},

	Init : function() {

		// Have we had this page before or have we been told to skip this page?
		if ( !SKIP_LAST_PAGE_FEATURE && this.MyCookie.hasKey( this.GetShortenedCookieUrl() ) == false ) {

			// Save this page to the last visited list
			var page_details = {};
			page_details.querystring = ""; //location.search; // No longer storing the query string

			// Sanitise the querystring as best we can
			if ( !location.search.match( /[^0-9A-Z&_]/ig ) ) {

				// Get the title and clean it up a bit
				var title = document.title;
				title = title.replace( "Olympic Holidays - ", "" );
				title = title.replace( "Olympic Holidays", "" );
				title = title.replace( "Holidays to ", "" );
				title = title.replace( "Holidays in ", "" );
				title = title.indexOf( "-" ) > -1 ? title.substring( 0, title.indexOf( "-" ) ) : title;
				// Trim off anything we don't need
				title = title.length > 50 ? title.substring( 0, 50 ) : title;

				page_details.title = title;

				// Use the url as the key
				this.MyCookie.set( this.GetShortenedCookieUrl(), page_details );
			}
		}

		// Limit the number of pages we keep
		var urls = this.MyCookie.keys();

		while( urls.length > this.MAX_LAST_PAGES_VISITED ) {
			this.MyCookie.dispose( urls[0] );
			urls.remove( urls[0] );
		}

		this.MyCookie.save();

		this.Render();
	},

	MyCookie : new Hash.Cookie( 'last_pages_visited', { autoSave: true, duration: 365, path: '/' } ), //, domain: location.host.replace( /^[^.]+?\./, "" ) } ),

	RemoveAll : function() {
		if ( confirm( "Are you sure you want to clear out this list?" ) ) {
			this.MyCookie.empty();
			this.MyCookie.save();
		}
		this.Render();
	},

	Render : function() {
//		console.group();

		var lists = $$('ul.pages_last_visited');

		lists.each( function( ele ) {

			ele = $defined( ele ) ? ele : $('pages_last_visited');

			var pages = this.MyCookie;
			var urls = pages.keys();

			ele.getChildren().each( function( li ) {
				li.remove();
			});

			while( urls.length > 0 ) {

				var url = urls.getLast();
				var props = pages.get( url );

				// Add a LI tag
				var plv_li = new Element( 'li' );

				// Create a link tag for this page
				var plv_link = new Element( 'a', {
					'href' : "http://www.olympicholidays.com" + url + ( url.indexOf( props.querystring ) == -1 ? escape( props.querystring ) : "" ),	// escape this to prevent nasty XSS attacks?
					'title' : 'Return to one of your previously visited pages, ' + props.title
				});

				plv_link.setText( props.title.length >= 25 ? props.title + "..." : props.title );

				// Add the link to the LI
				plv_li.adopt( plv_link );

				ele.adopt( plv_li );

				urls.remove( urls.getLast() );
			}

			if ( !$defined( ele.getNext() ) ) {
				var remove_link = new Element( 'a', {
					'href' 		: '#RemoveAllPageHistory',
					'title' 	: 'Remove all the pages in this list'
				});

				remove_link.setText( "Remove all links" );
				remove_link.injectAfter( ele );

				remove_link.addEvent( "click", function( e ) {
					LastPagesVisited.RemoveAll();
					return false;
				});
			}
		}, this );
	}
};



function AssertJSLoadedOk() {
	return true;
}