var homepage = Class.create();

homepage.prototype = {
	initialize: function(params){
		this.params=params;
		var val_cookie=readCookie("my_homepage");
		if(val_cookie!=null){
			this.my_homepage=eval(val_cookie);
			
			if(this.params.geonameid && !eval("/"+this.params.geonameid+"/.test(val_cookie);")){
				this.afiseaza_optiunea_curenta();
			}
			this.show_homepage();
		}
		else{
			this.my_homepage=[];
			if(this.params.geonameid)
			this.afiseaza_optiunea_curenta();
		}
	},

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

		var img_plus=new Element("img",{"src":"/images/add_homepage.gif","width":"10px","height":"10px"});
			img_plus.setStyle({
								"paddingRight":"5px"
							});
			div_curent_location.appendChild(img_plus);
			if(this.my_homepage[0]){
				var text=document.createTextNode("inlocuieste "+unescape(this.my_homepage[0].name)+(this.my_homepage[0].country_code!=''?" ("+this.my_homepage[0].country_code+")":"")+" cu "+unescape(escape(this.params.name))+(this.params.country_code!=''?" ("+this.params.country_code+")":""));
			}
			else{
				var text=document.createTextNode("seteaza "+unescape(escape(this.params.name))+(this.params.country_code!=''?" ("+this.params.country_code+")":"")+" ca homepage");
			}
		
			div_curent_location.appendChild(text);
			Event.observe(
							$(div_curent_location),
							"click",
							this.add_homepage.bindAsEventListener(this)
						);
			
			$(div_curent_location).style.cursor="pointer";
			$(div_curent_location).style.cursor="hand";

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

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

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

	show_homepage:function(){
		this.draw_my_homepage(this.my_homepage[0]);
	},

	draw_my_homepage: function(detalii){
		$('my_homepage').innerHTML="";
		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);
			
		$('my_homepage').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"));
		this.my_homepage=[];
		this.afiseaza_optiunea_curenta();
		eraseCookie("my_homepage");
	}

}