
/*
 * Redirect matrix can be viewed in 'redirect matrix.txt' in this directory.
 */

var g_bDebug = false;
var g_aRedirectMatrix = [ [[[[[0,0],[1,0]],[[0,2],[1,2]]],[[[0,0],[1,0]],[[2,2],[1,2]]]],[[[[2,2],[0,2]],[[2,2],[0,2]]],[[[2,2],[1,2]],[[2,2],[0,2]]]]] , [[[[[0,0],[0,0]],[[0,0],[0,0]]],[[[0,0],[0,0]],[[0,0],[0,0]]]],[[[[0,0],[0,0]],[[0,0],[0,0]]],[[[0,0],[0,0]],[[0,0],[0,0]]]]] ];

/**
 * Function to check whether the user should be directed and if so,
 * whether to redirect to the Flash, HTML or mobile site.
 *
 * @param p_ePageType Specifies whether the user is currently at the html or flash site
 * @param p_sPathAndGetVar Combination of the request path and any GET variables
 * @param p_sAddToUrlPath
 * @param p_bForceHtml Whether to force HTML on the user
 * @return boolean Whether the user is being redirected
 */
function f_redirect(p_sPathAndGetVar, p_sAddToUrlPath, p_bForceHtml){
  var w_sHostname   = window.location.hostname;
  var w_bMobileSite = w_sHostname.match(/^(dev\.|test\.)?m\./) || ''.length ? 1 : 0;
  
  // Page is Flash page if pathname starts with /flash
  var w_ePageType             = window.location.pathname.match(/^\/flash\//) || window.location.pathname.match(/^\/flash\/?$/) ? 1 : 0;
  
  // The HTML page is forced when either p_bForceHtml is true or pathname starts with /html
  p_bForceHtml               |= window.location.pathname.match(/^\/html\//) || window.location.pathname.match(/^\/html\/?$/) ? 1 : 0;
  
  var w_sPath = window.location.pathname.substring(1);
  if(w_sPath == 'flash'){
    w_sPath = '';
  }
  if(w_sPath.match(/^flash\//)){
    w_sPath = w_sPath.substring(6);
  }
  if(w_sPath.match(/\/$/)){
    w_sPath = w_sPath.substring(0, w_sPath.length-1);
  }
  
  var w_sAnchor    = window.location.hash.substring(1);
  var w_bHasAnchor = w_sAnchor == '' ? 0 : 1;
  if(g_bDebug && window.console){
    console.info('w_sAnchor: '+w_sAnchor);
  }
  
  var w_bHasPath              = (w_sPath).length ? 1 : 0;
  
  var w_bFlashVersionSuffices = DetectFlashVer(9,0,0) ? 1 : 0;
  
  
  if(g_bDebug && window.console){
    console.info('w_sPath: '+w_sPath);
    console.info(window.location);
    console.info('w_bMobileSite: '+w_bMobileSite);
    console.info('w_ePageType: '+w_ePageType);
    console.info('w_bHasPath: '+w_bHasPath);
    console.info('w_bHasAnchor: '+w_bHasAnchor);
    console.info('w_bFlashVersionSuffices: '+w_bFlashVersionSuffices);
    console.info('p_bForceHtml: '+p_bForceHtml);
  }
  
  var w_eRedirect = g_aRedirectMatrix[w_bMobileSite][w_ePageType][w_bHasPath][w_bHasAnchor][w_bFlashVersionSuffices][p_bForceHtml];
  if(g_bDebug && window.console) console.info('rs='+w_eRedirect);
  if(g_bDebug && window.console) console.info(w_bMobileSite+' | '+w_ePageType+' | ('+w_bMobileSite+') | '+w_bHasPath+' | '+w_bHasAnchor+' | '+w_bFlashVersionSuffices+' | '+p_bForceHtml);
    
  // Convert /flash/a/b/c into /flash/#a/b/c/
  if(w_eRedirect == 0 && window.location.pathname.match(/^\/flash\/[^#]+/)) {
    w_eRedirect = 1;
  }
  if(w_eRedirect == 0 && w_bMobileSite == 1 && w_bFlashVersionSuffices == 1 && p_bForceHtml == 0){
    w_eRedirect = 1;
  }
  
  switch(w_eRedirect){
    // Redirect to Flash site
    case 1:{
      var t_aMatches = /^(dev|test)\./.exec(w_sHostname);
      var w_sEnvironment;
      if(t_aMatches && t_aMatches.length == 2){
        w_sEnvironment = t_aMatches[1];
      }
      else{
        w_sEnvironment = 'www';
      }
      f_executeRedirect('http://'+w_sEnvironment+'.' + w_sHostname.replace(/^(dev\.|test\.)?((m|www)\.)?/, '') + '/flash/' + (w_bHasAnchor ? '#' + w_sAnchor : ((w_sPath).length ? '#' + w_sPath : '')));
      return true;
    }
    // Redirect to HTML site (root /)
    case 2:{
      //f_executeRedirect('http://' + w_sHostname + '/html/' + (p_bForceHtml ? p_sAddToUrlPath : '') + (w_bHasAnchor ? w_sAnchor : ''));
      f_executeRedirect('http://' + w_sHostname + '/' + (p_bForceHtml ? p_sAddToUrlPath : '') + (w_sPath ? w_sPath + '/' : '') + (w_bHasAnchor ? w_sAnchor : ''));
      return true;
    }
    // Redirect to mobile site
    case 3:{
      var t_aDomainMatches = w_sHostname.match(/([^\.]+\.[^\.]+)$/);
      var t_sDomain        = (t_aDomainMatches ? t_aDomainMatches[1] : 'sequential.nl');
      f_executeRedirect('http://m.' + t_sDomain + '/');
      return true;
    }
    // Do nothing (0) or illegal redirect code
    default:{
      if(g_bDebug && window.console){
        console.info('Not redirecting...');
      }
      return false;
    }
  }
}

function f_executeRedirect(p_sUrl){
  if(!g_bDebug){
    window.location = p_sUrl;
  }
  else if(window.console){
    console.info('Redirecting to: ' + p_sUrl);
  }
}

f_redirect('', '', 0);

/*var g_iRedirectState;
var g_aRedirectMatrix;
var g_bRequiredVersion;
var g_bPath;
var g_sPath;
var g_bAnker;
var g_sAnker;
var g_ePageType;
var g_iPageTypeID;*/

/**
 * Function to check whether the user should be directed to a Flash- or HTML-page.
 * @return r_bRedirected Whether the users is being redirected
 */
/*function f_redirect(p_bRedirect){
  var w_bDebugMode = true;
  var w_bRedirect = (p_bRedirect === false ? false : !w_bDebugMode);
	g_ePageType = 'flash';
	g_iPageTypeID;
	switch(g_ePageType){
		case 'html':{
			g_iPageTypeID = 0;
			break;
		}
		case 'flash':{
			g_iPageTypeID = 1;
			break;
		}
	}
	// get location href to read anker
	var t_sLocationHref = window.location.href;
	
	// get Anker
	g_sAnker = t_sLocationHref.substr(t_sLocationHref.indexOf("#")+1);
	if(g_sAnker == t_sLocationHref){
		g_sAnker = '';
	}
	g_bAnker = ((g_sAnker == '') ? 0 : 1);
	
	// set sPath
	g_sPath = '';
	g_bPath = (g_sPath.length ? 1 : 0);
	
	// check if flash has a correct version
	g_bRequiredVersion = DetectFlashVer(9, 0, 0);
	
	// define matrix
	g_aRedirectMatrix = [ [[[[[0,0],[1,0]],[[2,2],[1,2]]],[[[0,0],[1,0]],[[2,2],[1,2]]]],[[[[2,2],[0,2]],[[2,2],[0,2]]],[[[2,2],[1,2]],[[2,2],[0,2]]]]] , [[[[[0,0],[0,0]],[[0,0],[0,0]]],[[[0,0],[0,0]],[[0,0],[0,0]]]],[[[[0,0],[0,0]],[[0,0],[0,0]]],[[[0,0],[0,0]],[[0,0],[0,0]]]]] ];
	// determine the redirect state
	g_iRedirectState = g_aRedirectMatrix[0][g_iPageTypeID][g_bPath][g_bAnker][((g_bRequiredVersion) ? 1 : 0)][0];
	
	switch(g_iRedirectState){
		case 0:{
			break;
		}
		case 1:{
			if(w_bRedirect){
				window.location = 'http://www.sequential.nl/flash/' + ((g_bAnker) ? '#' + g_sAnker : (g_sPath.length ? '#' + g_sPath : ''));
			}
      if(w_bDebugMode && window.console){
        console.log('redirect to: http://www.sequential.nl/flash/' + ((g_bAnker) ? '#' + g_sAnker : (g_sPath.length ? '#' + g_sPath : '')));
      }
			break;
		}
		case 2:{
			if(w_bRedirect){
				window.location = 'http://www.sequential.nl/html/' + (0 ? '' : '') + ((g_bAnker) ? g_sAnker : '');
			}
      if(w_bDebugMode && window.console){
        console.log('redirect to: http://www.sequential.nl/' + (0 ? '' : '') + ((g_bAnker) ? g_sAnker : ''));
      }
			break;
		}
	}
    
    // Return true when the script redirected, false when it doesn't.
    var r_bRedirected = (g_iRedirectState > 0);
    
    return r_bRedirected;
  
}  

//f_redirect();*/