function getCookieValue(keyName)
{
	var cookieValues	= document.cookie;
	if(cookieValues == false)cookieValues = '';
	
	var cookiePos		= cookieValues.indexOf(keyName + '=');
	if(cookiePos == -1)return false;
	
	cookieValues		= cookieValues.substring(cookiePos,cookieValues.length);
	cookieValues		= cookieValues.substring(0,cookieValues.indexOf('; ') != -1 ? cookieValues.indexOf('; ') : cookieValues.length);	
	cookieValues		= cookieValues.split('=');
	
	return cookieValues[1] != null ? cookieValues[1] : '';
}

function setCookieValue(keyName, keyValue)
{
	if(document.cookie == false)return false;
	document.cookie = keyName + '=' + keyValue + '; path=/';
	return true;
}


function setBandTester(elementSize, sourceVideo)
{
	var bandwidth			= getCookieValue('bandwidth');
	var attempts			= getCookieValue('num_attempts');
	var bodyElement		= $('tag-tvuol');
	
	if(bodyElement.connectionType != null)
	{
		var bandwidth = bodyElement.connectionType == 'lan' ? 256 : 56;
		setSpeedMode(bandwidth, 3);	
		return;
	}	

	if(isNaN(attempts))attempts = 0;	
  if(bandwidth != '' && bandwidth != false && attempts > 2){setSpeedMode(bandwidth,attempts);return 'play_video';}
  	
	var startTime			= (new Date()).getTime();
	var targetElement		= document.createElement('img');
	targetElement.setAttribute('id','bandTesterImage');
	targetElement.onload	= function(){DownKbps(startTime, elementSize, attempts, sourceVideo);}
	targetElement.onerror	= function(){ShowErrorMessage('video', sourceVideo);}
	targetElement.onabort	= function(){ShowErrorMessage('video', sourceVideo);}
	targetElement.setAttribute('src',imageURL + 'band_test_01.jpg?startTime='+ startTime);
	targetElement.style.display = 'none';	
	var getBody				= document.getElementsByTagName('body')[0];
	getBody.appendChild(targetElement);
	return 'stop_video';
}

function DownKbps(startTime,size, attempts, targetVideo) 
{
	var prevMeasure		= getCookieValue('bandwidth');
	attempts			= attempts == '' || attempts == false ? 0 : parseInt(attempts);
	
	var endTime			= (new Date()).getTime();
	var bandwidth		= (Math.floor((((size * 8) / ((endTime - startTime) / 1000)) / 1024) * 10) / 10);
	
	if(attempts > 0 && prevMeasure != '')bandwidth = (parseInt(prevMeasure) + parseInt(bandwidth)) / (parseInt(attempts) + 1);
	setSpeedMode(bandwidth, ++attempts);
	if(targetVideo != null)insertInfo(targetVideo,false);
}

function setButtonMode(source, mode)
{
	if(mode == null)mode = 'over';
	var replacer 	= mode == 'over' ? 'out' : 'over';
	var imageSource	= source.getAttribute('src');
	source.setAttribute('src',imageSource.replace(replacer,mode));
}

function ShowErrorMessage(targetID, targetVideo)
{
	var videoElement			= $(targetID);
	var gradeElement			= $('grade');
	var lineElement				= $('line_videos');
	var listElement				= $('current_videos');
	var textElement				= $('text');
	
	var imagePath				= imageURL;
	setScreenSize('default');
	
	if(videoElement == null)return;
	window.targetVideo			= targetVideo;
	
	if(listElement != null){listElement.innerHTML = '';listElement.style.display = 'none';}
	if(lineElement != null)lineElement.style.display = 'none';
	
	
	var avisoElement	= '<div class="aviso">';
	avisoElement		+='<img src="' + imagePath + 'img_atencao.gif" class="img-aviso-icone" alt="Atenção" />';
	avisoElement		+='<div class="div-aviso-titulo">Atenção</div>';
	avisoElement		+='<div class="div-aviso-mensagem">Não foi possível identificar em seu computador conexão suficiente para exibição do vídeo em banda larga.</div>';
	avisoElement		+='<img src="' + imagePath + 'bt_assista_out.gif" onmouseover="setButtonMode(this,\'over\')" onmouseout="setButtonMode(this,\'out\')" class="img-aviso-ver56" alt="Ver em 56k" onclick="setSpeedMode(5,2,window.targetVideo)" />';
	avisoElement		+='<img src="' + imagePath + 'bt_assine_out.gif" onmouseover="setButtonMode(this,\'over\')" onmouseout="setButtonMode(this,\'out\')" class="img-aviso-assine" alt="Assine o UOL Banda Larga" />';
	avisoElement		+='</div>';
	
	videoElement.innerHTML	= avisoElement;
	resetBarHeight();
}

function setSpeedMode(speed, attempts, targetVideo)
{	
	attempts			= attempts == '' || attempts == false ? 0 : parseInt(attempts);
	
	setCookieValue('bandwidth',speed);
	setCookieValue('num_attempts',attempts);
	
	if(speed >= 450)window.speedMode = '500';
	else if(speed > 56)window.speedMode = '256';
	else window.speedMode = '56';
	
	if(targetVideo != null)insertInfo(targetVideo, false);
}