var oCookie;
Array.prototype.IndexOf=function(elem)
{
	for(var x=0; x<this.length; x++)
	{
		if(this[x]==elem)
		{
			return x;
		}
	}
	return -1;
}

var imoList= new Array();
var nodeList= new Array();
//var lng ="PT";

function addImo(clid)
{
//	alert("pushing :"+clid);
	imoList.push(parseInt(clid));
}

function remImo(clid)
{
	if(imoList.IndexOf(clid)!=-1)
	{
		imoList.splice(imoList.IndexOf(clid),1);
	}
}


function initFavourites()
{
	//lng=lang;

		if(cookiesEnabled())
		{
			var list=readCookie('favourites');
			if(list!="")
			{
				imoList=list.split(',');
			//	alert('getting from cookie');
			}
		}	
		
	nodeList=getElements();
	refreshNodes();
}

function refreshNodes()
{
	for(var g=0;g<nodeList.length;g++)
	{              
		var node=nodeList[g];
		if(imoList.IndexOf(node.getAttribute("favourite"))==-1)
		{				
			node.ondoubleclick=function(){}
			paint(node,'add');
			
			node.onclick=function()
			{
				addImo(this.getAttribute("favourite"));
				refreshNodes();
								
				if(this.getAttribute("_onClick"))
					eval(this.getAttribute("_onClick"));
			}
		}
		else
		{		
			node.ondoubleclick=function(){}
			paint(node,'rem');
			if(node.getAttribute('action') && node.getAttribute('action')=='insertonly')
			{
				node.onclick=null;
				node.onmouseover=null;
				node.onmouseout=null;
			}
			else
			{
				node.onclick=function()
				{
					remImo(this.getAttribute("favourite"));
					refreshNodes();
						
					if(this.getAttribute("_onClick"))
						eval(this.getAttribute("_onClick"));
				}
			}
		}		
	}//end for
}//end function

function paint(obj,_mode)
{
	switch(obj.tagName)
	{
		case 'SPAN':
			obj.style.cursor="hand";	
			obj.innerHTML=obj.getAttribute(_mode+'Alt');
		break;
		
		case 'INPUT':
			if(obj.type.toLowerCase()=="button")	
			{
				obj.style.cursor="hand";	
				obj.value=obj.getAttribute(_mode+'Alt');					
			}
		break;
		
		case 'IMG':
			obj.style.cursor="hand";	
			obj.src=obj.getAttribute(_mode+'Image');
			obj.alt=obj.getAttribute(_mode+'Alt');
		break;
	}
	
	if(obj.getAttribute('hasrollover'))
	{
		obj.onmouseover=function(){this.src=this.getAttribute(_mode+'ImageOver')}	
		obj.onmouseout=function(){this.src=this.getAttribute(_mode+'Image')}
	}
	
	/*
	if(node.getAttribute("favStyle") && node.getAttribute("favStyle")=='true')
	{
		setCssClass(_mode+"Favourite");	
	}
	*/
}

////////////////////////Interface functions /////////////////////////
function listFavourites(url)
{
	if(!url)
		url="listagem.htm";
		
	var aux=imoList.join(',');
	aux=aux.charAt(0)==","?aux.substring(1):aux;
	aux=aux==""?0:aux;
	var href="?RIL="+aux;
	window.location.href=url+href;
}

function getFavourites()
{
		var aux=imoList.join(',');
		aux=aux.charAt(0)==","?aux.substring(1):aux;
		aux=aux==""?0:aux;
		return "RIL="+aux;
}

function clearCookies(_refresh)
{
	imoList= new Array();
	saveCookie();	
	if(_refresh)
		refreshNodes();
}

function saveCookie()
{
	var Realestatelist=imoList.join(',');
	Realestatelist = Realestatelist.charAt(0) == "," ? Realestatelist.substring(1) : Realestatelist;
	var dat = new Date();
	var nextYear = dat.getTime()+ (365 * 24 * 60 * 60 * 1000);
	dat.setTime(nextYear);
	document.cookie='favourites='+Realestatelist+';expires='+dat.toGMTString()+';path=/';
}

////////////////////////guardar cookie /////////////////////////

window.onbeforeunload=function(){
	saveCookie();
}

/*** verifica se os cookies estão bloqueados ***/
function cookiesEnabled()
{
	var _date = new Date();
	_date.setTime(_date.getTime()+9000);
	document.cookie='test=true;expires='+_date.toGMTString();
	var aux=readCookie('test');

	if(aux != "")
	{
		return true;	
	}
	
	return false;
}

/*** Percorre todos os elementos do body e devolde um array com a referência dos que manipulam os favoritos  ***/
function getElements()
{
	var temp= new Array(document.body);
	var fav= new Array();
	//var temp= document.body.childNodes;
	
	for(var i=0; i<temp.length; i++)
	{
		if(temp[i].hasChildNodes)
		{
			for(var x=0; x<temp[i].childNodes.length;x++)
			{
				var node=temp[i].childNodes[x];
				if(node.nodeType==1)
				{
					temp.push(node);
					if(node.getAttribute('favourite'))
						fav.push(node);
				}
			}
		}
	}
	return fav
}



/////////////////////////////  Generic functions  ///////////////////////

/*** Devolve o valor de uma determinada chave do cookie ***/
function readCookie(val)
{
	var scrambles=new Array();
	scrambles=document.cookie.split(';');
	for(var x=0; x<scrambles.length;x++)
	{
		var keys= new Array();
		keys= scrambles[x].split('=');
		
		var aux=keys[0].toLowerCase();
		if(aux.trim()==val.toLowerCase() && keys[1] && keys[1]!="")
		{	
			return keys[1];
		}
	}
	return "";
}


String.prototype.lTrim=function()
{
	var index=0;
	while(this.charCodeAt(index)==32 && index<this.length)
	{
		index++; 
	}
	
	if(index==this.length)
		return "";
	else
		return this.substring(index);
}

String.prototype.rTrim=function()
{
	var index=this.length-1;
	
	while(this.charCodeAt(index)==32 && index>0)
	{
		index--;	
	}
	
	if(index==0 && this.charAt(index)==32)
		return "";
	else
		return this.substring(0, index+1);
}

String.prototype.trim=function()
{
	var aux=this.rTrim();
	return aux.lTrim();
}


