var secs
var timerID = null
var timerRunning = false
var timerIDM = null
var timerIDUO = null
var timerRunningM = false
var timerRunningUO = false
var delay = 1000
var delayM = 1000
var delayUO = 1000
var tipus = 0
var tipusM = 10
var tipusUO = 0
var secsM = 0
var secsUO = 0
var param = '';
var id1 = -1;
var id2 = -1;
var provincia = 'Lleida'
var comunitat = 0

function pC()
{
secs=10000;
}
function InitializeTimerUsuarisOnline()
{
    // Set the length of the timer, in seconds
	tipusUO = 0
    secsUO = 60
  StopTheClockUO()
    StartTheTimerUO()
}

function InitializeTimerUsuarisOnlineRuta1()
{
    // Set the length of the timer, in seconds
	tipusUO = 1
    secsUO = 60
  	StopTheClockUO()
    StartTheTimerUO()
}


function InitializeTimerMissatgesPetit()
{
    // Set the length of the timer, in seconds
	tipusM = 10
    secsM = 30
    StopTheClockM()
    StartTheTimerM()
}

function InitializeTimerMissatgesPetitPaginaConversa(id1T,id2T)
{
    // Set the length of the timer, in seconds
	tipusM = 14
    secsM = 20
	id1 = id1T
	id2 = id2T
    StopTheClockM()
    StartTheTimerM()
}


function InitializeTimerMissatgesPetitRuta1()
{
    // Set the length of the timer, in seconds
	tipusM = 11
    secsM = 30
    StopTheClockM()
    StartTheTimerM()
}

function InitializeTimerMissatgesPetitRuta2()
{
    // Set the length of the timer, in seconds
	tipusM = 12
    secsM = 30
    StopTheClockM()
    StartTheTimerM()
}

function InitializeTimerMissatgesPetitRuta3()
{
    // Set the length of the timer, in seconds
	tipusM = 13
    secsM = 30
    StopTheClockM()
    StartTheTimerM()
}

function InitializeTimerIndexGeneral()
{
    // Set the length of the timer, in seconds
	tipus = 0
    secs = 60
    StopTheClock()
    StartTheTimer()
}

function InitializeTimerIndexProvincia(tProvincia)
{
    // Set the length of the timer, in seconds
    tipus = 5
    secs = 60
    provincia = tProvincia
    StopTheClock()
    StartTheTimer()
}

function InitializeTimerIndexComunitat(iComunitat)
{
    // Set the length of the timer, in seconds
    tipus = 6
    secs = 60
    comunitat = iComunitat
    StopTheClock()
    StartTheTimer()
}

function InitializeTimerIndexGeneralTemp()
{
    // Set the length of the timer, in seconds
	tipus = -1
    secs = 60
    StopTheClock()
    StartTheTimer()
}

function InitializeTimerMiGrupo()
{
    // Set the length of the timer, in seconds
	tipus = 1
    secs = 60
    StopTheClock()
    StartTheTimer()
}

function InitializeTimerKhacesInicial(id)
{
	param = id
	InitializeTimerKhaces()
}

function InitializeTimerKhacesInicial2(id)
{
	param = id
	InitializeTimerKhaces2()
}

function InitializeTimerKhaces()
{
//	window.alert(param)
    // Set the length of the timer, in seconds
	tipus = 2
    secs = 30
    StopTheClock()
    StartTheTimer()
}

function InitializeTimerKhaces2()
{
//	window.alert(param)
    // Set the length of the timer, in seconds
    tipus = 3
    secs = 30
    StopTheClock()
    StartTheTimer()
}

function StopTheClock()
{
    if(timerRunning)
        clearTimeout(timerID)
    timerRunning = false
}

function StopTheClockM()
{
    if(timerRunningM)
        clearTimeout(timerIDM)
    timerRunningM = false
}

function StopTheClockUO()
{
    if(timerRunningUO)
        clearTimeout(timerIDUO)
    timerRunningUO = false
}

function StartTheTimer()
{
    if (secs==0)
    {
        StopTheClock()
        // Here's where you put something useful that's
        // supposed to happen after the allotted time.
        // For example, you could display a message:
		
        //alert("Acaben de passar 10 segons nene");
		// Crida a ajax		
		if (tipus < 10)
		{
			recarregarContinguts('contingut');
			if (tipus == 0)
			{
				InitializeTimerIndexGeneral();
			}
			else if (tipus == -1)
			{
				InitializeTimerIndexGeneralTemp();
			}		
			else if (tipus == 1)
			{
				InitializeTimerMiGrupo();
			}
			else if ((tipus == 2))
			{
				InitializeTimerKhaces();
			}		
			else if ((tipus == 3))
			{
				InitializeTimerKhaces2();
			}	
			else if ((tipus == 5))
			{
				InitializeTimerIndexProvincia(provincia);
			}
			else if ((tipus == 6))
			{
				InitializeTimerIndexComunitat(comunitat);
			}		
		}
    }
    else
    {
     //   self.status = secs
        secs = secs - 1
        timerRunning = true
        timerID = self.setTimeout("StartTheTimer()", delay)
    }
}

function StartTheTimerUO()
{
    if (secsUO==0)
    {
        StopTheClockUO()
//		window.alert('recarrego');
		recarregarContingutsUO('usuaris');
		if (tipusUO == 0)
		{
			InitializeTimerUsuarisOnline();
		}
		else if (tipusUO ==1)
		{
			InitializeTimerUsuarisOnlineRuta1();
	    }
	}
    else
    {
     //   self.status = secs
        secsUO = secsUO - 1
        timerRunningUO = true
        timerIDUO = self.setTimeout("StartTheTimerUO()", delayUO)
    }
}

function StartTheTimerM()
{
    if (secsM==0)
    {
        StopTheClockM()
        // Here's where you put something useful that's
        // supposed to happen after the allotted time.
        // For example, you could display a message:
		
//        alert("Acaben de passar 30 segons nene");
		// Crida a ajax		
		recarregarContingutsM('missatge');
		if ((tipusM == 10))
		{
			InitializeTimerMissatgesPetit();
		}	
		if ((tipusM == 11))
		{
			InitializeTimerMissatgesPetitRuta1();
		}	
		if ((tipusM == 12))
		{
			InitializeTimerMissatgesPetitRuta2();
		}		
		if ((tipusM == 13))
		{
			InitializeTimerMissatgesPetitRuta3();
		}	
		if ((tipusM == 14))
		{
			InitializeTimerMissatgesPetitPaginaConversa(id1,id2);
		}			
    }
    else
    {
     //   self.status = secs
        secsM = secsM - 1
        timerRunningM = true
        timerIDM = self.setTimeout("StartTheTimerM()", delayM)
    }
}

function recarregarContinguts (type)
{
	var url = '';
//	alert(tipus);
	if (tipus == 0)
	{
		url = 'autoindexV2.php';	
	}
	else if (tipus==-1)
	{
		url = 'autoindextemp.php';
	}	
	else if (tipus==1)
	{
		url = 'autogrupoV2.php';
	}
	else if (tipus==2)
	{
		url = 'autokhacesultimcomentariV2.php?val=' + param + '&lastCom=' + lastCom;
		//window.alert(url);
	}	
	else if (tipus==3)
	{
		url = '../../../autokhacesultimcomentariV2.php?val=' + param + '&lastCom=' + lastCom + '&ruta=' + '3';
//		window.alert(url);
	}	
	else if (tipus==5)
	{
		url = '../autoindexProvinciaV2.php?pro=' + provincia;
	//	window.alert(url);
	}
	else if (tipus==6)
	{
		url = '../autoindexComunitat.php?com=' + comunitat;
		//window.alert(url);
	}	

	var temporal;
	//url = 'grabAmic.php?val=' +  field;
	//window.alert(url);
	$.get(url, {}, 
		 function(html) {
			if (html == 'NOIDENTIFICAT') {
				return;
			} else {
				//window.alert(html);
				temporal = unescape(html);
				//temporal=temporal.replace(/\+/gi," ");
				//alert(temporal);
				
				if (tipus==2)
				{

					if (temporal == "OK")
					{
//																window.alert('Sin cambios');	
					}
					else
					{
						$('#'+type).html(temporal);
		//				window.alert(numCom + '-' +  numComNous);
						if (numCom != numComNous)
						{
//							window.alert('canvio de numero');
							var cadena='';
							if (numComNous == 1)
							{
								cadena = '<img border=0 width=16 src="imatges/icono_comentario.gif"></img>&nbsp;<b>' + '1' + ' comentario</b>';
							}
							else
							{
								cadena = '<img border=0 width=16 src="imatges/icono_comentario.gif"></img>&nbsp;<b>' + numComNous + ' comentarios</b>';								
							}
							numCom = numComNous;
//window.alert(cadena);
							$('#numComments').html(cadena);
						}
					}

				}
				else if (tipus==3)
				{

					if (temporal == "OK")
					{
	//					window.alert('Sin cambios');
					}
					else
					{
						$('#'+type).html(temporal);
//window.alert(temporal);
//						window.alert(numCom + '-' +  numComNous);
						if (numCom != numComNous)
						{
//							window.alert('canvio de numero');
							var cadena='';
							if (numComNous == 1)
							{
								cadena = '<img border=0 width=16 src="/imatges/icono_comentario.gif"></img>&nbsp;<b>' + '1' + ' comentario</b>';
							}
							else
							{
								cadena = '<img border=0 width=16 src="/imatges/icono_comentario.gif"></img>&nbsp;<b>' + numComNous + ' comentarios</b>';								
							}
							numCom = numComNous;
//window.alert(cadena);
							$('#numComments').html(cadena);
						}
					}

				}		
				
				else
				{
					$('#'+type).html(temporal);
				}
				return;
			}
		//	reportAjaxStats('/check_field');
		}
	);
//	window.alert(html);
	return false;
} 


function recarregarContingutsM (type)
{
	var url = '';
//	alert(tipus);
	if (tipusM==10)
	{
		url = 'autoMissatgesPetit.php';
	}	
	else if (tipusM==11)
	{
		url = '../autoMissatgesPetit.php';
	}	
	else if (tipusM==12)
	{
		url = '../../autoMissatgesPetit.php';
	}	
	else if (tipusM==13)
	{
		url = '../../../autoMissatgesPetit.php';
	}
	else if (tipusM==14)
	{
		url = 'autoMissatgesPetit.php';
	}
	

	var temporal;
	//url = 'grabAmic.php?val=' +  field;
	//window.alert(url);
	$.get(url, {}, 
		 function(html) {
			if (html == 'NOIDENTIFICAT') {
				return;
			} else {
				temporal = unescape(html);
				//if (tipusM==10)
				//{
					if (temporal == "OK")
					{

					}
					else 
					//if (temporal == "BART")
					{
						$('#numMissatge').html(temporal);
						if (tipusM == 14)
						{
							url = 'autoPrivatsV2.php?id1=' + id1 + "&id2=" + id2;
						//	window.alert(url);
							$.get(url, {}, 
								function(html) {
								if (html == 'NOIDENTIFICAT') 
								{
									return;
								} 
								else 
								{
									temporal = unescape(html);
								}
								if (temporal == "OK")
								{
	
								}
								else
								{
								//	window.alert(temporal);
									$('#mastemes').html(temporal);
            document.getElementById('nuevosmensajes').style.display = "visible";
									//window.alert(temporal.substr(60));									
								}
								return;
								}
							);
						}
					}
					/*
					else
					{
						//$('#numMissatge').html(temporal);
					}
					*/
				//}				
				//else
				//{
				//	$('#'+typeM).html(temporal);
				//}
				return;
			}
		//	reportAjaxStats('/check_field');
		}
	);
//	window.alert(html);
	return false;
} 



function recarregarContingutsUO (type)
{
	var url = '';
	if (tipusUO==0)
	{
		url = 'autousuariosOnline.php';
	}	
	else if (tipusUO==1)
	{
		url = '../autousuariosOnline.php?ruta=1';
		//window.alert(url);
	}
	var temporal;
	//alert(url);
	$.get(url, {}, 
		 function(html) {
			if (html == 'NOIDENTIFICAT') {
				return;
			} else {
				//window.alert(html);
				temporal = unescape(html);
					if (temporal == "OK")
					{

					}
					else 
					{
						$('#usuariosOnline').html(temporal );
					}
				return;
			}
		}
	);
	//
	return false;
} 
