var favourite = Class.create();

favourite.prototype = {
	initialize: function(params){
		this.params=params;
		var val_cookie=readCookie("favorite");
		if(val_cookie!=null){
			this.favorite=eval(val_cookie);
			if(this.params.geonameid && !eval("/\\b"+this.params.geonameid+"\\b/.test(val_cookie);")){
				this.afiseaza_optiunea_curenta();
			}
			
			this.show_favorite();
		}
		else{
			this.favorite=[];
			if(this.params.geonameid)
			this.afiseaza_optiunea_curenta();
		}
	},

	afiseaza_optiunea_curenta: function(){		
		var div_curent_location=new Element("div").setStyle({
														marginLeft:"8px"
													});

		var img_plus=new Element("img",{"src":"/images/plus.gif","width":"10px","height":"10px"});
			img_plus.setStyle({
								"paddingRight":"5px"
							});
			div_curent_location.appendChild(img_plus);
		var text=document.createTextNode("adauga "+unescape(escape(this.params.name))+(this.params.country_code!=''?" ("+this.params.country_code+")":"")+" la locatii favorite");
			div_curent_location.appendChild(text);
			Event.observe(
							$(div_curent_location),
							"click",
							this.add_favorite.bindAsEventListener(this)
						);
														
			$(div_curent_location).style.cursor="pointer";
			$(div_curent_location).style.cursor="hand";

		$('curent_locatie').appendChild(div_curent_location);
	},

	add_favorite: function(e){
		var el=e.nodeType==1?e:Event.element(e);
		this.favorite[this.favorite.length]={
												"geonameid":this.params.geonameid,
												"name":escape(this.params.name),
												"country_code":this.params.country_code,
												"url":this.params.url
											};

		createCookie("favorite",Object.toJSON(this.favorite),"100");
		$('curent_locatie').innerHTML="";
		this.draw_favorite({
												"geonameid":this.params.geonameid,
												"name":escape(this.params.name),
												"country_code":this.params.country_code,
												"url":this.params.url
											});
	},

	show_favorite:function(){
		for(var i=0;i<this.favorite.length;i++){
			this.draw_favorite(this.favorite[i]);
		}
	},

	draw_favorite: function(detalii){
		var div_fav=new Element("div").setStyle({
													paddingLeft:"15px",
													marginLeft:"5px"
											});

		
			div_fav.className="div_fav";

		var img_minus=new Element("img",{"src":"/images/minus.gif","width":"10px","height":"10px"});
			img_minus.setStyle({
								"paddingLeft":"5px"
							});
						
			$(img_minus).style.cursor="pointer";
			$(img_minus).style.cursor="hand";

			Event.observe(
							img_minus,
							"click",
							function(e){
								this.del_favorite(detalii,e);
							}.bindAsEventListener(this)
						);
		var text=new Element("a",{"href":detalii.url}).update(unescape(detalii.name)+(detalii.country_code!=''?" ("+detalii.country_code+")":""));

			div_fav.appendChild(text);
			div_fav.appendChild(img_minus);
			
		$('locatii').appendChild(div_fav);
	},

	del_favorite: function(detalii,e){
	
		var el=e.nodeType==1?e:Event.element(e);
		el.up("div").parentNode.removeChild(el.up("div"));
		var new_favorite=[];

		for(var i=0;i<this.favorite.length;i++){
			if(this.favorite[i].geonameid!=detalii.geonameid){
				new_favorite[new_favorite.length]=this.favorite[i];
			}
		}
		this.favorite=eval(new_favorite);
		createCookie("favorite",Object.toJSON(new_favorite),"100");
	}

}