

var G6_RedirectionManager = function(settingsPolicy)
{
	if(typeof(settingsPolicy)=="undefined")
	{
		this._settingsPolicy = settingsPolicy;
	}
	else
	{
		this._settingsPolicy = _g6SettingsPolicy;
	}
	
    if(typeof(_g6CoName) == "undefined")
    {
        this._g6CoName = "RooMediaVideoSettings";
    } 
    else
    {
        this._g6CoName = _g6CoName;
    }
    //this.detectedBandwidth='';
}

G6_RedirectionManager.prototype.redirect = function(qs_format,qs_bitrate,target)
{
	//	Get the defaults

	var format	= this._settingsPolicy.getDefaultFormat();
	var bitrate = (this._settingsPolicy.isEnabled())?_g6CurrBitrate:'';							
	
	//	If cookie is set then use the cookie values over default values
	var _g6CoVal = this.getSettingsCookie();
	if(_g6CoVal != null){
		var valSettingsArray = _g6CoVal.split(";");
		format = valSettingsArray[0];
		bitrate = valSettingsArray[1];		
	}
		
	//	Override qs takes precedence over cookie
	if(qs_format){format = qs_format};
	if(qs_bitrate){bitrate = qs_bitrate};

	//	Construct the querystring
	var parray = this.getQueryVariableArray();
	var qs = "?";
	for(var i=0;i < parray.length; i++){
		
		//	Remove the following elements from qs
		switch(parray[i][0]){
			case 'format':
			case 'bitrate':
			case 'bt':
			case 'bp':
			case 'bst':
			case 'biec':	
				break;								
			default:
				qs += (qs.length > 1)?'&':'';
				qs += parray[i][0] + '=' + parray[i][1];
				break;
		}
	}
	
	var locstr = window.location.protocol + "//" + window.location.hostname + window.location.pathname;
	if (typeof(target) != 'undefined'){
		locstr = target;
	}
	
	//	Add the new values
	qs += (qs.length > 1)?'&':'';
	qs += 'bt=' + _g6Browser.bt;
	qs += '&bp=' + _g6Browser.bp;
	qs += '&bst=' + _g6Browser.bst;
	qs += '&biec=' + _g6Browser.biec;
	qs += (format != '')?'&format=' + format:'';
	qs += (bitrate != '')?'&bitrate=' + bitrate:'';
	
	//alert(qs);
	//	Redirect with replaced elements
	window.location = locstr + qs;
}

G6_RedirectionManager.prototype.getSettingsCookie = function()
{
	return G6_GetCookie(this._g6CoName);		//_g6CoName: global variable
}

G6_RedirectionManager.prototype.getQueryVariable = function(variable)
{
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for(var i=0;i<vars.length;i++) 
	{
		var pair = vars[i].split("=");
		if(pair[0] == variable)
		{
			return pair[1];
		}
	}
	return '';
}

G6_RedirectionManager.prototype.getQueryVariableArray = function()
{
	var pairsarray = [];
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for(var i=0;i<vars.length;i++) 
	{
		if(vars[i].indexOf('=')!=-1)
		{
			var pair = vars[i].split("=");
			pairsarray.push(pair);
		}
	}
	return pairsarray;
}

G6_RedirectionManager.prototype.changeMediaFormat = function(format,bitrate,target)
{
	if(this._settingsPolicy.isValidSetting(format,bitrate))
	{
		if(typeof(format)!='undefined' && format!='null')
		{
			_g6CurrFormat = format;
		}
		if(typeof(bitrate)!='undefined' && bitrate!='null')
		{
			_g6CurrBitrate = bitrate;
		}
		
		this.setPreferenceCookie();	
		
		if(typeof(target) != 'undefined')
		{
			this.redirect(target);
		}
		else
		{
			this.redirect();
		}
	}
	else
	{
		alert('Your platform does not support changing to this media format.');
	}
}

G6_RedirectionManager.prototype.setPreferenceCookie = function() 
{
	var _g6CoVal;
	var _g6CoExp = G6_AddDays(new Date(),365);
	_g6CoVal = _g6CurrFormat + ";" + _g6CurrBitrate;	// concatenate value
	
	//NN Bug fix 2290 - removed fourth parameter '/'
	//G6_SetCookie(this._g6CoName,_g6CoVal,_g6CoExp,'/');			// actually set cookie (_g6CoName is global)
	G6_SetCookie(this._g6CoName,_g6CoVal,_g6CoExp);			// actually set cookie (_g6CoName is global)
}			

G6_RedirectionManager.prototype.checkForRedirect = function()
{
		
	//	Get Querystring for validation
	var qs_format = this.getQueryVariable("format");
	var qs_bitrate = this.getQueryVariable("bitrate");

	//	Test against invalid Querystring
	if(!this._settingsPolicy.isValidSetting(qs_format,qs_bitrate))
	{
		this.redirect();
	}
	else
	{
		// Handle IE / WIN differently / Flash redirect off - only redirect if cookie is set
		if(_g6Browser.bt == 'IE' && _g6Browser.bst == 'IE' && _g6Browser.bp == "WIN" || !this._settingsPolicy.isEnabled())
		{
			// if theres something in the cookie then try a redirect
			if(this.getSettingsCookie() != null)
			{	
				if(this.getQueryVariable("bt") != _g6Browser.bt) // have we checked this page before? 
				{				
					this.redirect(qs_format,qs_bitrate);
				}
			}
			else
			{
				// have we checked this page before and is it not IE Compatible? If not then redirect .
				if((this.getQueryVariable("bt") != _g6Browser.bt) && !_g6Browser.biec)
				{				
					this.redirect(qs_format,qs_bitrate);
				}
				//Otherwise page will load with default g6client settings.
				//this.setDetectedBandwidth();
			}		
		}
		else
		{
			// have we checked this page before? If not then redirect.
			if(this.getQueryVariable("bt") != _g6Browser.bt)
			{
				this.redirect(qs_format,qs_bitrate);
			}
		}
		///Bandwidth Sniffer
		//	If cookie is not set then use the sniffed bandwidth 
			//this.setDetectedBandwidth();
	}
}

/*G6_RedirectionManager.prototype.getDetectedBandwidth = function(){
	return this.detectedBandwidth;
}*/

G6_RedirectionManager.prototype.setDetectedBandwidth = function(){
	//Bandwidth Sniffer.Sets the cookie if it doesn't exist, otherwise don't do anything.
	var g6Cookie = this.getSettingsCookie();
	//alert('set:'+_g6DetectedBandwidth);
	if(typeof(g6Cookie) == 'undefined' || g6Cookie == null){
		if(typeof(_g6DetectedBandwidth)!='undefined' && _g6DetectedBandwidth!=0)
		{
			_g6CurrBitrate = (this._settingsPolicy.isValidSetting(_g6CurrFormat,_g6DetectedBandwidth)?_g6DetectedBandwidth:_g6CurrBitrate);
			//alert('setting cookie');
			this.setPreferenceCookie();	
			this.changeMediaFormat(_g6CurrFormat,_g6CurrBitrate);
		}
	}
}
////////////////////////////////////////////////////////////////////////
//	OLD UNENCAPSULATED CODE - TO DELETE AFTER TESTING
////////////////////////////////////////////////////////////////////////
/*
function G6_GetQueryVariable(variable){
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for(var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if(pair[0] == variable){
			return pair[1];
		}
	}
	return '';
}

function G6_GetQueryVariableArray(){
	var pairsarray = [];
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for(var i=0;i<vars.length;i++) {
		if(vars[i].indexOf('=')!=-1){
			var pair = vars[i].split("=");
			pairsarray.push(pair);
		}
	}
	return pairsarray;
}
*/
/*
	Should probably be refactored to be included within the settings object? RY 7-7-
 *//*
function G6_CheckForRedirect(){
	//	Only redirect if _g6SettingsPolicy is enabled

	//	Get Querystring for validation
	var qs_format = G6_GetQueryVariable("format");
	var qs_bitrate = G6_GetQueryVariable("bitrate");

	//	Test against invalid Querystring
	if(!_g6SettingsPolicy.isValidSetting(qs_format,qs_bitrate))
	{
		G6_DoPageRedirect();
	}
	else
	{
		// Handle IE / WIN differently / Flash redirect off - only redirect if cookie is set
		if(_g6Browser.bt == 'IE' && _g6Browser.bst == 'IE' && _g6Browser.bp == "WIN" || !_g6SettingsPolicy.isEnabled())
		{
			
			if(G6_GetSettingsCookie() != null)
			{	
				// have we checked this page before? If not then redirect.
				if(G6_GetQueryVariable("bt") != _g6Browser.bt)
				{				
					G6_DoPageRedirect();
				}
			}
			else
			{
				// Page will load with default g6client settings.
			}		
		}
		else
		{
			// have we checked this page before? If not then redirect.
			if(G6_GetQueryVariable("bt") != _g6Browser.bt)
			{
				G6_DoPageRedirect();
			}
		}
	}
}

function G6_GetSettingsCookie()
{
	return G6_GetCookie(_g6CoName);		//_g6CoName: global variable
}

function G6_DoPageRedirect(target)
{

	//	Get the defaults
	var format	= _g6SettingsPolicy.getDefaultFormat();
	var bitrate = _g6CurrBitrate;							//(ALL PLATFORMS DEFAULT TO G6CLIENT BITRATE)
	
	//	If cookie is set then use the cookie values over default values
	var _g6CoVal = G6_GetSettingsCookie();
	if(_g6CoVal != null){
		var valSettingsArray = _g6CoVal.split(";");
		format = valSettingsArray[0];
		bitrate = valSettingsArray[1];		
	}

	//	Construct the querystring
	var parray = G6_GetQueryVariableArray();
	var qs = "?";
	
	for(var i=0;i < parray.length; i++){
		qs += (qs.length > 1)?'&':'';
		
		//	Remove the following elements from qs
		switch(parray[i][0]){
			case 'format':
			case 'bitrate':
			case 'bt':
			case 'bp':
			case 'bst':
			case 'biec':	
				break;								
			default:
				qs += parray[i][0] + '=' + parray[i][1];
				break;
		}
	}

	var locstr = window.location.protocol + "//" + window.location.hostname + window.location.pathname;
	if (typeof(target) != 'undefined'){
		locstr = target;
	}
	
	//	Redirect with replaced elements
	window.location = locstr + qs + 'bt='+_g6Browser.bt+'&bp='+_g6Browser.bp+'&bst='+_g6Browser.bst+'&biec='+_g6Browser.biec + '&format=' + format + '&bitrate=' + bitrate; 
}

*/
