

window.document.onmousedown = gravarScrollPos;
recuperarscrollPos();


	//Pega valor do cookie atraves do nome do parametro
	function valorCookie(arrCookie, strParametro)
	{
		var arrValores;
		var i;
					
		for(i=0; i < arrCookie.length; i++) 
		{
			arrValores = arrCookie[i].split('=');
			
			if(Trim(arrValores[0]) == strParametro) 
			{							 
				return arrValores[1];
			}	
		}
		
		return '';		  
	}

			
	//Verifica se Parametro e valor existem no cookie
	function testarCookie(strParametro, strValor) 
	{  		
		var strValorCks		
		var cokNome = Trim(document.cookie);       //cookie			
		var arrCookie = cokNome.split(';');
		
		if(verificarNome(arrCookie, strParametro))      //Verifica se parametro existe no cookie
		{   		
			
			strValorCks  = valorCookie(arrCookie, strParametro) //pega valor do coolie					
			if (strValorCks  == strValor)
				 return true;
			else
				return false;
				
		}
		else 
		{
			return false;
		}
	}

	
	//Retira espaco em branco
	function Trim(strValor)
	{
		var strAux ='';
			
		if(strValor == null)
		{
			return '';
		}
				
		for(i=0; i < strValor.length; i++)
		{		
			if(strValor.substring(i, i+1) != ' ')
				strAux += strValor.substring(i, i+1);
		}
			
		return strAux;	
	}

	
	//verificar se parametro existe no cookie
	function verificarNome(arrCookie, strParametro)
	{ 		    
		var arrValores;				
		
		for ( i=0; i< arrCookie.length; i++)
		{
			arrValores = arrCookie[i].split('=');
			
			if (arrValores[0] == strParametro)
				 return true;
		}
		
		return false;
	}

	
	//grava cookie
	function gravarCookie(strParametro, strValor, strExpirar, strDominio)	
	{			
		if(strExpirar != '')
			strExpirar = 'expires=' + strExpirar + ';';
		
		if(strDominio != '')
			strDominio = ''
		
		window.document.cookie = strParametro + '=' + strValor + ';' + strDominio + strExpirar ;					
	}
	
	function recuperarscrollPos() 
	{					
	  
		var strScrollY = new String('');
		//url pg atual
		var strUrlAtual =  window.location.href;
		
				
		
		//URL sem parametros
		indiceParam = strUrlAtual.indexOf('?') < 0?strUrlAtual.length : strUrlAtual.indexOf('?');		
		strUrlAtual = strUrlAtual.replace(strUrlAtual.substring(indiceParam, strUrlAtual.length), '')

		var arrCookie;
		arrCookie = window.document.cookie.split(';');		

		//url Cookie
		strUrlCookie = valorCookie(arrCookie, 'cokUrl');
		
		//URL sem parametros
		indiceParam = strUrlCookie.indexOf('?') < 0?strUrlCookie.length : strUrlCookie.indexOf('?');		
		strUrlCookie = strUrlCookie.replace(strUrlCookie.substring(indiceParam, strUrlCookie.length), '')
						
		if(strUrlCookie != '' && strUrlCookie  != null && strUrlAtual == strUrlCookie )		
			strScrollY = new Number(valorCookie(arrCookie, 'cokscroll')) + 55;		
		else		
			strScrollY = 0;
		
		if (window.innerHeight)	  
		      window.scrollTo(0,strScrollY);
	    else if (document.documentElement)
		     document.documentElement.scrollTop = strScrollY;
	    else if (document.body)	    
		    document.body.scrollTop = strScrollY;		    
		
	}

	
	function gravarScrollPos()
	{	
	    var strScollTop =  new String('0');
	   
	    if (window.innerHeight)
		    strScollTop = window.pageYOffset
	    else if (document.documentElement && document.documentElement.scrollTop)
		    strScollTop = document.documentElement.scrollTop
	    else if (document.body)
		    strScollTop = document.body.scrollTop
	      
		gravarCookie('cokscroll', strScollTop, '', document.location.host);
		gravarCookie('cokUrl', window.location.href, '',document.location.host);	
	}
