
/** This javascript file should be included in the form
  * <script src='/js/redirect.js' page='somepagename'> to
  * check if the file is in the form '...index.html?name'
  * and if not redirect to '/index.html?somepagename'.
  *
  * Note the the value of 'page' should be a key in the
  * siteStructure or the redirect will fail (and end up
  * on the main page).
  */

// If window.siteStructure was loaded already, no redirect
// will be necessary
if( !window.siteStructure && location.search.indexOf('standalone')<0 ) {
  
  var attr = document.getElementsByTagName('script')[0].attributes['page'];
  if( attr ) {
    var page = attr.value;
    var me;
    var content = location.href.indexOf( '/content/' );
    
    if( content > 0 ) {
      me = location.href.substring( 0, content ) + '/index.php/';
    } else {
      me = location.href.indexOf( '/index.' );
      if( me < 0 ) {
        me ='/index.php/';
      } else {
        me = location.href.indexOf( '/', me+2 );
        me = location.href.substring( 0, me+1 ).replace( /index.html?/, 'index.php/' );
      }
    }
    //if( location.href.match( /.*\.php/?[^/]*$/i ) ) {
      //relocate( 'http://www.tiobe.com/index.html?' + page );
      //alert('relocate to: '+me+page);
      relocate( me + page );
      // full url required for jacobe.com and clocksharp.com
    //}
  }
}

/** This function is used to redirect to a different url without
  * adding the redirect to the history, so the back button keeps
  * working as expected.
  *
  * url   Relative or absolute url. (Opera only works for absolute url?)
  */
function relocate(url) {
  
  // Older browsers don't have location.replace().
  if( location.replace ) {
    // replace url so 'back' will skip this redirect page
    location.replace( url );
  } else {
    // in older browsers this trick will not work.
    location.href = url;
  }
  
}
