
allFavoritos = new Array();  //all Favoritos on page
activeFavoritos = new Array() //all Favoritos stored by the user
currentFavorito = 0; //where in the list of Favoritos is the user


function loadFavoritos()
	//get Favoritos from a cookie
	{
	favoritosString = null;
	tempArray = document.cookie.split(";");
	x = -1;
		
		for (tA = 0; tA < tempArray.length; tA++)
			{
			if (tempArray[tA].indexOf('favoritos=') > -1) //found the favoritos section
				{
				tPos = tempArray[tA].indexOf("=")+2;

				favoritosString = tempArray[tA].substring(tPos,tempArray[tA].length); //striping out "favoritos=^"
				//alert(favoritosString);
				}
			
			}
			
	if (favoritosString != null)
		{		
		tempArray = favoritosString.split("^");
		if (tempArray.length > 1)
		{
		x=0;
		for (i=0; i < tempArray.length/4; i++)
			{
			//alert(tempArray[x]+"----"+tempArray[x+1]+"---- "+tempArray[x+2]+"---- "+tempArray[x+3])
			activeFavoritos[i] = new Favorito(tempArray[x],tempArray[x+1],tempArray[x+2],tempArray[x+3])
			x=x+4;
			}}
		}
	}

	
function saveFavoritos()
	{
	tempCookie = "favoritos=";
	for(i=0; i < activeFavoritos.length; i++)
		{
		tempCookie=tempCookie+"^"+(activeFavoritos[i].id)+"^"+(activeFavoritos[i].zona)+"^"+(activeFavoritos[i].headline)+"^"+(activeFavoritos[i].read);
		}
		
	
	 	var expire = new Date ();
   		expire.setTime (expire.getTime() + (6 * 24 * 3600000)); //expires in 6 days from users clock
   		expire = expire.toGMTString();
	finalCookie = tempCookie+"; path=/; expires="+expire;  	
  	document.cookie = finalCookie;
  	
	}


function eventCheckForCookies()
	{
	tempCookie = "favoritos=";
	for(i=0; i < activeFavoritos.length; i++)
		{
		tempCookie=tempCookie+"^"+(activeFavoritos[i].id)+"^"+(activeFavoritos[i].zona)+"^"+(activeFavoritos[i].headline)+"^"+(activeFavoritos[i].read);
		}
		
	
	 	var expire = new Date ();
   		expire.setTime (expire.getTime() + (6 * 24 * 3600000)); //expires in 6 days from users clock
   		expire = expire.toGMTString();
  	
	finalCookie = tempCookie+"; path=/; expires="+expire;  	
  	document.cookie = finalCookie;
	
  	checkForCookie = document.cookie.split(";");
  	
  		for (x=0; x < checkForCookie.length; x++)
  			{
  			if (checkForCookie[x].indexOf("favoritos") >= 0) {cookiesOn = true;}
  			}
  		if (cookiesOn == false) alert("Es necesario  activar  los  cookies de tu navegador para poder  usar   los favoritos.");
	
	}

function Favorito(id,zona,headline,read)
	//favorito class
	{
	this.id = id;
		this.zona = zona;
	this.headline = headline;
	this.read = read;
	this.clicked = markRead;
	}

function markRead(id)
	{
	if (!id) id = this.id.substring(5,this.id.length);
	for (i=0; i < activeFavoritos.length; i++)
		{
		if (activeFavoritos[i].id == id) activeFavoritos[i].read = "yes";
		}
	}	

function eventShowNextFavorito()
	//reads next favorito in list
	{
	if (activeFavoritos != null) 
		{
		total = 0;
		for (aC = 0; aC < activeFavoritos.length; aC++)
			{
			if (activeFavoritos[aC].read != "yes") 
				{
				markRead(activeFavoritos[aC].id);
				saveFavoritos();
				
				break;
				}
			}
		}
	}
	
function eventClearReadFavoritos()
	{
	tempFavoritos = new Array()
	x = 0;
	for (i=0; i < activeFavoritos.length; i++)
		{
		if (activeFavoritos[i].read != "yes") {tempFavoritos[x] = activeFavoritos[i]; x++}
		}
	activeFavoritos = tempFavoritos;
	drawFavoritos();
	setFavoritosVisibility();	
	updateFavoritoCounter();
	saveFavoritos();
	}
	
function eventClearAllFavoritos()
	{
	activeFavoritos = new Array();
	drawFavoritos();
	setFavoritosVisibility();	
	updateFavoritoCounter();
	saveFavoritos();
	}
	
function drawFavoritos()
	//creates the HTML for the favoritos menu when a favorito is added
	//removed create element and used innerHTML instead
	{
	newHTML = "";
	
	if (activeFavoritos.length < 1) 
		{
		//draw this text if there are no clippigns
		
		newHTML = "<span class='favoritoItem'>En este momento no tienes ningún restaurante dentro de la sección “Favoritos”. Para agregar tus restaurantes favoritos a esta sección,  has click en la imagen ";
		newHTML += "<img src='/imagenes/favorito.gif'>";
				newHTML += ", la cual se localiza en la parte inferior de cada restaurante.</span>";
		
		

		}
		
		
	for (i=0; i < activeFavoritos.length; i++)
		{
		
		favorito = activeFavoritos[i]
		//a bug in IE5 on mac exists which causes the a not work, will need to find a work around
		
		if (favorito.read == "yes") tClass = "favoritoItemRead";
		else tClass = "favoritoItem";
				
		newHTML += "<a href=menu.asp?id_r="+favorito.id+"&zona="+favorito.zona+" onmousedown='markRead("+favorito.id+")' class='"+tClass+"' id='cLink"+favorito.id+"'>";
		newHTML += favorito.headline+"</a>";
		
		}
		//parentObj.innerHTML = newHTML;
		
	obj = document.getElementById("favoritosContainer");
	obj.innerHTML = newHTML;
	
	//make sure that the favoritos container does not go off screen
	favoritosSetContainerHeight()

	}


function favoritosSetContainerHeight()
	{
	obj = document.getElementById("favoritosContainer");
	obj.style.height= "200px";
	}


clipAnim = 0;
clipOriginX = clipOriginY = 0; //the starting x,y position of the favorito
clipNavX = clipNavY = 0;

function animateFavorito()
	// creates a flying icon to the favorito the bar
	{

	if (clipAnim < 5)
		{
		for(x = 0; x < 4; x++)
			{
			obj = document.getElementById("favoritoSprite"+x)
			obj.style.visibility = "visible"
			tX  = clipOriginX+(clipAnim*((clipNavX-clipOriginX)/(7-x)))
			tY  = clipOriginY+(clipAnim*((clipNavY-clipOriginY)/(7-x)))
			obj.style.left = tX// clipOriginX+(clipAnim*(clipNavX-clipOriginX))
			obj.style.top = tY//clipOriginY+clipAnim*(clipNavY-clipOriginY)
			}
		clipAnim++;
		}
	else 
		{
		for(x = 0; x < 4; x++)
			{
			obj = document.getElementById("favoritoSprite"+x)
			obj.style.visibility = "hidden"
			}
		clipAnim = 0;
		clearInterval(clipTimer)
		}
	}

function checkForDuplicates()
	//make sure there is no other record of that favorito in use
	{
	for (i=0; i < activeFavoritos.length; i++)
		{
		if (newFavorito == activeFavoritos[i].id) {i = allFavoritos.length; duplicate = true;}
		}
	}

function createFavoritoSprites()
	//creates the HTML for the favoritos sprite which is animated when the user adds a favorito to the queue
	{
	for (x=0; x < 4; x++)
	{
	if (document.getElementById("favoritoSprite") == null)
		{
		favoritoSprite = document.createElement("img");
		favoritoSprite.src = "/imagenes/favoritofanta.gif";
		favoritoSprite.setAttribute(classFix,"favoritoSprite");
		favoritoSprite.setAttribute("id","favoritoSprite"+x)
		parentObj = document.getElementById("bodyNode");
		parentObj.appendChild(favoritoSprite);
		}
		}
	}

var favoritosDivArray = new Array();

function createPageFavoritosArray()
	{

		d = document.getElementsByTagName("DIV")
		for (j=0; j < d.length; j++) if (d[j].id.indexOf("clp") > -1) 
			{
			favoritosDivArray[favoritosDivArray.length] = d[j];
			}
	 }


function favoritoInstanceVisibility(id,state)
	//finds duplicate favoritos on a page
	{
	t = document.getElementsByName(id);
	if(t.length > 0)
		{
		for (j=0; j < t.length; j++) 
			{
			t[j].style.visibility = state;
			t[j].onclick = addFavorito;
			}
		}
	else { //this is a hack, and possibly slow bu IE5mac and NS6 do not return a length on Object collections
	 	d = favoritosDivArray;
  		{
		for (j=0; j < d.length; j++) if (d[j].id == id) 
			{
			d[j].style.visibility = state;
			d[j].onclick = addFavorito;
			}
		}
		}		
	}
	
function setFavoritosVisibility()
	{
	//sets the visibility and events for an object;
	
	for (i=0; i < allFavoritos.length; i++)
		{
		vis = "visible";
		
		//find if this favorito is already selected
		for (x=0; x <activeFavoritos.length; x++) if (allFavoritos[i].id == activeFavoritos[x].id) vis = "hidden";
	
	
			obj = "clp"+allFavoritos[i].id;
			favoritoInstanceVisibility(obj,vis)
			}
			
		}

clipTimer = false;

function addFavorito()
	{
	//this event is triggered when a favorito is clicked 
	
	newFavorito = this.id.substring(3,this.id.length)
	duplicate = false;
	for (i=0; i < allFavoritos.length; i++) //find the position the allFavoritos array of the selected favorito
		{
		if (newFavorito == allFavoritos[i].id) {pos = i; i = allFavoritos.length}
		}
		
	if (activeFavoritos.length > 0)	checkForDuplicates() //make sure favorito is not already seletected
	
	
	if (!duplicate)
	{	
		
		
		eventCheckForCookies();
		if (cookiesOn == true)
			{
			activeFavoritos[activeFavoritos.length] = new Favorito(allFavoritos[pos].id,allFavoritos[pos].zona,allFavoritos[pos].headline);
			
	 		favoritoInstanceVisibility(this.id,'hidden') //find all instances of the favorito in article and hides it
	 		
	 		//get the starting position of the favorito by finding the mouse position
	 		clipOriginX = mouseX;
	 		clipOriginY = mouseY+atumesaven.scrollTop();
	 		
	 		//find where on screen the button is for the favoritos in the nav
	 		obj = document.getElementById("navTop")
	 		clipNavX = 500;
	 		clipNavY = parseInt(obj.style.top)-10//offsetTop+atumesaven.scrollTop()+28;
	 		
	 		drawFavoritos();	
	 		
	 		event.cancleBubble = true;
	 		clearInterval(clipTimer);
			clipTimer = setInterval("animateFavorito()",15);
			updateFavoritoCounter();
			saveFavoritos();
		 	}
	 }
	 
	
	
	
	}

function updateFavoritoCounter()
	//positions the numbers in the favorito counter 
	//based on the number of UNREAD favoritos
	{
	t2 = 0;
	t1 = 0;
	if (activeFavoritos != null) 
		{
		total = 0;
		for (aC = 0; aC < activeFavoritos.length; aC++)
			{
			if (activeFavoritos[aC].read != "yes") total++
			}
		t2 = total;
		
	if (total > 9) 
		{
		total = total+""; //make it a string;
		t1 = total.substring(0,1);
		t2 = total.substring(1,2);
		}
		}
	document.getElementById("cln0").style.top = t1*-9;
	document.getElementById("cln1").style.top = t2*-9;
	
	t2 = 0;
	t1 = 0;
	if (currentFavorito != null) 
		{
		total = currentFavorito;
		t2 = total;
		
	if (total > 9) 
		{
		total = total+""; //make it a string;
		t1 = total.substring(0,1);
		t2 = total.substring(1,2);
		}
		}
	}	


		
	
var cookiesOn = false;

function initFavoritos()
	//reads in the favoritos on page from an array;
	{
	createPageFavoritosArray()
	createFavoritoSprites();
	loadFavoritos();
	setFavoritosVisibility();		
	updateFavoritoCounter();
	drawFavoritos();
	}	

