/******************************************************************************

 REDIRECT.JS

 - written by Tyler Waters
 - writes a common redirect template with a fancy count-down.
 - location.replace()s when link clicked or timer runs out




*******************************************************************************
Copyright © 2008, Province of British Columbia, all rights reserved. This 
material is owned by the Ministry of Forests and Range and the Government of 
British Columbia and is protected by copyright law. It may not be reproduced 
or redistributed without the prior written permission of the Province of 
British Columbia.
*******************************************************************************



*******************************************************************************
 USAGE:

 - make a reference to 'redirect.js'

 - determine your URL. if this is relative (e.g. in test) ensure relative:true 
 this prepends "protocal // server" to the URL

 - add a reference to "c" variable to append form to a child ( please note, 
 this should run on-load to work properly )
 
 - don't reference "c". this will document.write the contents ( if run on-load, 
 this will kill the existing page... which isn't so bad, it's only a redirect)

*******************************************************************************

EXAMPLE

<script src="/mof/portal/scripts/redirect.js"></script>
<script type="text/javascript">
new Redirect({ c:null, url:'/index.htm', relative:true, interval:10 });
</script>

*******************************************************************************
ADDITIONAL OPTIONS

 - you can change any text of the form, however the layout can't be changed: 

<h1>[moveTitle]</h1>
<hr><h2>[moveHead]:<br><a href="[URL]">[URL]</a></h2>
<p>[moveText].</p>
<p>[redirtext] <span id="mycount">[interval]</span> seconds
 ...<a href="stop()">(stop the count)</a></p>

defaults:
 - movetitle = "Attention – This page has moved!"
 - movehead = "This page has moved to:"
 - movetext = "Please update your bookmarks/favourites."
 - redirtext = "You will be redirected automatically in "


*******************************************************************************
LIMITATIONS

There is no NOSCRIPT support (naturally) so either place the URL in a NOSCRIPT
tag or set an HTTP redirect in the META section of the HEAD, e.g.:

<head>
<META http-equiv="refresh" content="[i];URL=[url]"> 
</head>

where 	[i] - is the number of seconds to wait
	[url] - is the url to redirect to (this can be relative )

of course, you could just do that and never use this script...
 but what's the fun in that?

******************************************************************************/



function Redirect( args ){
	if(! args ) { this.help(); return; }
	if( args.help == true ) { this.help(); return; }
	
	if( typeof this.show == "undefined" ) this.show = true;
	
	for( var myarg in args ) { this[myarg] = args[myarg]; }

	if( (typeof this.c).toLowerCase() == "string" ) { this.c = document.getElementById(this.c); }
	if( !this.url ) { if( confirm("url must be passed, show help?") ) this.help(); }
	if( typeof this.movetitle == "undefined" ) {this.movetitle = "Attention \u2013 This page has moved!";}
	if( typeof this.movehead == "undefined" ) {this.movehead = "This page has moved to:";}
	if( typeof this.movetext == "undefined"  ) {this.movetext = "Please update your bookmarks/favourites.";}
	if( typeof this.redirtext == "undefined"  ) {this.redirtext = "You will be redirected automatically in ";}
	if( typeof this.relative == "undefined" ) {this.relative = true;}
	if( typeof this.interval == "undefined" ) {this.interval = 5;}
	if( this.relative ) { 
		var myUrl = document.location.protocol+"//"+document.location.hostname;
		var firstChar = this.url.substr(0,1);
		if( this.url.indexOf( "../" ) > 0 ) { alert("error, '../' not supported"); return false; }
		if( firstChar == "/" ) {this.url = myUrl + this.url}
		else {
			var mypath = document.location.pathname;
			this.url = myUrl + mypath.substring(0,mypath.lastIndexOf("/")+1) + this.url
		} 
	}
	this.toid = 0
	this.guid = "a_" + Math.random().toString().substr(2); window[this.guid] = this;
	this.count = document.createElement("SPAN")
	this.count.innerHTML = this.interval
	this.count.id = "mycount";
	
	//if( !this.show ) { this.show = confirm("this may not work, continue?") }
	if( this.show ) { this.create( this.c ); }
	
	this.time();
}

Redirect.prototype = {

	help : function() {
		var s=''
		s += "Redirect Help\n"
		s += "This script is used to show consistent redirect notice\n\n"
		s += "Accepted Parameters:\n"
		s += "\nurl (required) - absolute or relative url"
		s += "\nrelative - if true, assumes url starts with /"
		s += "\nc - container, string or DOM. If null, document.write(s)"
		s += "\nmovetitle - title of the page ('Attention \u2013 This page has moved!')"
		s += "\nmovehead - text before url ('This page has moved to:')"
		s += "\nmovetext - some instructions for user ('Please update your bookmarks/favourites.')"
		s += "\nredirtext - appears before the countdown ('You will be redirected automatically in')"
		s += "\ninterval - the number of seconds to wait until redirecting (default: 5)"
		s += "\n\nTo grab an example, press CTRL-C"

		var ex = "new Redirect( {c:null, url:'/index.htm', relative:true});"
		this.show = false;
		prompt(s, ex);
	}

	, create:function( con ){
		var h1 = document.createElement("H1")
			,h2 = document.createElement("H2")
			,anc = document.createElement("A")
			,red = document.createElement("P")
			,count = document.createElement("P")
			,stop = document.createElement("A")
	
		anc.href = this.url
		anc.id = "myanchor"
		stop.href = "javascript:"+this.guid+".stop()";
		
		h1.appendChild(document.createTextNode( this.movetitle ));
		h2.appendChild(document.createTextNode( this.movehead ));
		h2.appendChild( document.createElement("BR") )
		h2.appendChild( anc );
		red.appendChild(document.createTextNode( this.movetext ));
		anc.appendChild(document.createTextNode( this.url ));
		stop.appendChild( document.createTextNode( "(stop the count)") )
		count.appendChild( document.createTextNode( this.redirtext ) )
		count.appendChild( this.count )
		count.appendChild( document.createTextNode( " seconds...") );
		count.appendChild( stop )
		
		if(! con ) con = document.createElement("DIV");
		con.appendChild(h1);
		con.appendChild(document.createElement( "HR" ))
		con.appendChild(h2);
		con.appendChild(red);
		con.appendChild(count);
		if( !this.c ) { 
			document.write( con.innerHTML ); 
			this.count = document.getElementById('mycount'); 
			anc = document.getElementById('myanchor')
		} 
		anc.onclick = function(){document.location.replace(this.href);return false;}
		
		this.time();
	}

	,stop:function(){clearTimeout(this.toid);}
	,time:function(){
		if(this.interval == 0 ){
			clearTimeout(this.toid);
			location.replace(this.url);
		}
		else{
			this.count.innerHTML = this.interval--; var t = this;
			this.toid = setTimeout( this.guid +".time()", 1000)
		}
	}
}


